使用 php 将多个复选框值和输入插入单个 sql 数据库

Posted

技术标签:

【中文标题】使用 php 将多个复选框值和输入插入单个 sql 数据库【英文标题】:inserting multiple checkbox values and inputs into a single sql databse with php 【发布时间】:2018-08-31 22:58:36 【问题描述】: 美好的一天。我有这个表格可以输入多个 复选框并将一行输入到 sql 数据库中。我会得到 一个进入,但不是另一个或相反的方式。你会 请帮助我,我是 php 和 sql 的新手。谢谢。

================================================ ==========================

<form method="post" align="center" action="insertsupplier.php">

<table align="center">
    <td>

                <label>Supplier ID:</label></br>
            <input type="text" id="supplier_id" name="supplier_id" /></br></br>

                <label>Company Name:</label></br>
            <input type="text" id="company_name" name="company_name" /></br></br>


    </td>

    <td>

                <label>BEE Level:</label></br>
            <input type="text" id="bee" name="bee" /></br></br>

                <label>Telephone Number:</label></br>
            <input type="text" id="tel" name="tel" /></br></br>

    </td>

    <td>

                <label>Contact Person:</label></br>
            <input type="text" id="contact" name="contact" /></br></br>

                <label>Cell Number:</label></br>
            <input type="text" id="cell" name="cell" /></br></br>

    </td>

    <td>

                <label>Email Address:</label></br>
            <input type="text" id="email" name="email" /></br></br>

                <label>Based:</label></br>
            <input type="text" id="based" name="based" /></br></br>

    </td>

</table>

                    <label>Address:</label></br>
            <textarea rows="5" cols="50" id="address" name="address"></textarea></br></br>

<table align="center">

        <tr>
                <label>Trade in:</label></br>
                <td>
                <input type="checkbox" name="trade[]" value="Air_conditioners">Air Cons</option></br>
                <input type="checkbox" name="trade[]" value="Building_Maintanance">Building Maintanance</option></br>
                <input type="checkbox" name="trade[]" value="BoreHole">BoreHole</option></br>
                <input type="checkbox" name="trade[]" value="Booms">Booms</option></br></br>
                </td>
                <td>
                <input type="checkbox" name="trade[]" value="Cameras">Cameras</option></br>
                <input type="checkbox" name="trade[]" value="IT">IT</option></br>
                <input type="checkbox" name="trade[]" value="VIF Equipment">VIF Equipment</option></br>
                <input type="checkbox" name="trade[]" value="UPS">UPS</option></br></br>
                </td>
                <td>
                <input type="checkbox" name="trade[]" value="WIMS">WIMS</option></br>
                <input type="checkbox" name="trade[]" value="Loops">Loops</option></br>
                <input type="checkbox" name="trade[]" value="Scale">Scale</option></br>
                <input type="checkbox" name="trade[]" value="Generator">Generator</option></br></br>
                </td>
                <td>
                <input type="checkbox" name="trade[]" value="Roads_and_Fittings">Roads and Fittings</option></br>
                <input type="checkbox" name="trade[]" value="Fire_Equipment">Fire Equipment</option></br>
                <input type="checkbox" name="trade[]" value="Minisub">Minisub</option></br>
                <input type="checkbox" name="trade[]" value="Mimic_Panels">Mimic Panels</option></br></br>
                </td>
                <td>
                <input type="checkbox" name="trade[]" value="Furniture">Fruniture</option></br>
                <input type="checkbox" name="trade[]" value="Pest_Control">Pest Control</option></br>
                <input type="checkbox" name="trade[]" value="Telephone">Telephone</option></br>
                <input type="checkbox" name="trade[]" value="Internet">Internet</option></br></br>
                </td>
                <td>
                <input type="checkbox" name="trade[]" value="Chlorinator">Chlorinator</option></br>
                <input type="checkbox" name="trade[]" value="Lawn_Mowers">Lawn Mowers</option></br>
                <input type="checkbox" name="trade[]" value="Electrical">Electrical</option></br>
                <input type="checkbox" name="trade[]" value="Hygiene">Hygiene</option></br></br>
                </td>
        </tr>
</table>
    <td>

            <label>Added By:</label></br>
        <input value="<?php echo $userRow['userName']; ?>" type="text" id="added_by" name="added_by" /></br></br>       


    </td>
<tr>

                <label>Notes:</label></br>
        <textarea rows="5" cols="100" id="notes" name="notes"></textarea></br></br>

            <input type="submit" value="Submit">

</form>

$url='localhost';
$username = "root";
$password = "";
$dbname = "helpdesk";
$checkbox1 = $_POST['trade'];
$chk=""; 
foreach($checkbox1 as $chk1) 
 
$chk.= $chk1.","; 
 
$conn = mysqli_connect($url, $username, $password, $dbname);
if (!$conn) 
die("Connection failed: " . mysqli_connect_error());

$sql = "INSERT INTO suppliers (trade)VALUES( '$chk' );";
$sql="INSERT INTO suppliers (company_name, supplier_id, telephone_no, contact_person, cell, email, based, address, bee_level, notes, added_by)
VALUES
('$_POST[company_name]','$_POST[supplier_id]','$_POST[tel]','$_POST[contact]','$_POST[cell]','$_POST[email]','$_POST[based]','$_POST[address]','$_POST[bee]','$_POST[notes]','$_POST[added_by]')";

    ini_set('display_errors',10);

    // Aliases for form selection
    $company_name = $_POST["company_name"];
    $supplier_id = $_POST["supplier_id"];
    $tel = $_POST["tel"];
    $contact = $_POST["contact"];
    $cell = $_POST["cell"];
    $email = $_POST["email"];
    $based = $_POST["based"];
    $address = $_POST["address"];
    $bee = $_POST["bee"];
    $notes = $_POST["notes"];
    $added_by = $_POST["added_by"];


  if (mysqli_query($conn, $sql))
    
        $last_id = mysqli_insert_id($conn);
        echo "New record created successfully. Last inserted ID is: " . $last_id;
        echo "<script>setTimeout(\"location.href = 'addsupplier.php';\",2500);</script>";
        unset($_POST);
     else 
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    ; 


mysqli_close($conn);

【问题讨论】:

你能举一个这句话的例子吗:“我会让一个进入,但不让另一个进入,或者反过来。”在这里稍微详细一点会有所帮助。 您好,感谢您的回复。如果我将插入添加到复选框值中,它会将其添加到数据库中,但如果我将插入添加到输入值中,它将添加输入值而不是复选框。 是的,很明显,在您的 php 代码中,您需要在每个“$sql =”语句之后添加“mysql_query”块。您可以粘贴您的 php 代码,以便我们进行更正。 嗨,谢谢,我解决了。我刚刚添加了 '".$chk." " 在一行中如下 $sql = ("INSERT INTO 供应商 (company_name, supplier_id, phone_no, contact_person, cell, email, based, address, bee_level, notes, added_by, trade)VALUES('$_POST[company_name]',' $_POST[supplier_id]','$_POST[tel]','$_POST[contact]','$_POST[cell]','$_POST[email]','$_POST[based]','$_POST [address]','$_POST[bee]','$_POST[notes]','$_POST[added_by]','".$chk."' )"); o jan 和 () 在末尾和我的 sql 语句的开头。 【参考方案1】:

这会起作用,但它有点做作......

<?php

$url='localhost';
$username = "root";
$password = "";
$dbname = "helpdesk";
$checkbox1 = $_POST['trade'];
$chk=""; 

foreach($checkbox1 as $chk1) 
 
  $chk.= $chk1.","; 


$conn = mysqli_connect($url, $username, $password, $dbname);
if (!$conn) 
  die("Connection failed: " . mysqli_connect_error());


$sql1 = "INSERT INTO suppliers (trade)VALUES( '$chk' );";
$sql2 = "INSERT INTO suppliers (company_name, supplier_id, telephone_no, contact_person, cell, email, based, address, bee_level, notes, added_by) VALUES ('$_POST[company_name]','$_POST[supplier_id]','$_POST[tel]','$_POST[contact]','$_POST[cell]','$_POST[email]','$_POST[based]','$_POST[address]','$_POST[bee]','$_POST[notes]','$_POST[added_by]')";

ini_set('display_errors',10);

// Aliases for form selection
$company_name = $_POST["company_name"];
$supplier_id = $_POST["supplier_id"];
$tel = $_POST["tel"];
$contact = $_POST["contact"];
$cell = $_POST["cell"];
$email = $_POST["email"];
$based = $_POST["based"];
$address = $_POST["address"];
$bee = $_POST["bee"];
$notes = $_POST["notes"];
$added_by = $_POST["added_by"];

// Your first query
if (mysqli_query($conn, $sql1))

  $last_id = mysqli_insert_id($conn);
  echo "New record created successfully. Last inserted ID is: " .      $last_id;
  echo "<script>setTimeout(\"location.href = 'addsupplier.php';\",2500);</script>";
  unset($_POST);
 else 
  echo "Error: " . $sql . "<br>" . mysqli_error($conn);
; 
mysqli_close($conn);

// Your second query.
if (mysqli_query($conn, $sql2))

  $last_id = mysqli_insert_id($conn);
  echo "New record created successfully. Last inserted ID is: " .    $last_id;
  echo "<script>setTimeout(\"location.href = 'addsupplier.php';\",2500);</script>";
  unset($_POST);
 else 
  echo "Error: " . $sql . "<br>" . mysqli_error($conn);
; 
mysqli_close($conn);

【讨论】:

以上是关于使用 php 将多个复选框值和输入插入单个 sql 数据库的主要内容,如果未能解决你的问题,请参考以下文章

使用单个过程将多个 xml 数据动态插入到 sql server 表中

SQL - 将多个行值插入单个列

使用 PHP 将多个 SQL 列组合成 HTML 表的单个列

将多个复选框插入 MySQL (PDO)

使用php pdo在sqlite数据库中插入多个复选框的值

如何将数据从多个输入字段转换为单个 JSON 对象以进一步将其插入单个 mysql 字段