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

时间:2018-03-22 13:20:45

标签: php sql xampp mariadb

  • 美好的一天。我有这个表格输入多个 复选框并将一行输入到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>

enter image description here

$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);

1 个答案:

答案 0 :(得分:0)

这样可行,但它有点笨拙......

<?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);