无法使用PHP在数据库中添加值

时间:2016-02-22 07:32:18

标签: php mysql

我目前正在做一个使用数据库添加值的项目,但我似乎遇到了问题。我确信我的查询是正确的,因为我尝试在mysql中手动添加它。只有一些字段似乎能够得到我输入的内容。我收到了错误

  

"错误:INSERT INTO库存(itemCode,dateReceived,typeOfFabric,details,unitOfMeasurement,amount,assignedOrderUse,section,row)VALUES('','' ,''怀特''',' 5','',' C',' C')"

    <?php

$host = "localhost";
$user = "root";
$pass = "";
$db = "gracydb";

if (isset($_POST['addInventory']))
{
    if(isset($_POST['itemCode'])){ $itemcode = $_POST['itemCode']; } 
    if(isset($_POST['dateReceived'])){ $inventoryDateReceived = $_POST['dateReceived']; } 
    if(isset($_POST['typeOfFabric'])){ $fabric = $_POST['typeOfFabric']; }
    if(isset($_POST['details'])){ $details = $_POST['details']; } 
    if(isset($_POST['unitOfMeasurement'])){ $measurement = $_POST['unitOfMeasurement']; }
    if(isset($_POST['amount'])){ $amount = $_POST['amount']; } 
    if(isset($_POST['assignedOrderUse'])){ $order = $_POST['assignedOrderUse']; } 
    if(isset($_POST['section'])){ $section = $_POST['section']; }
    if(isset($_POST['row'])){ $row = $_POST['row']; }

    $conn = mysql_connect($host, $user, $pass);
    $db_selected = mysql_select_db($db, $conn);

    $sql = "INSERT INTO inventory (itemCode, dateReceived, typeOfFabric, details, unitOfMeasurement, amount, assignedOrderUse, section, row)
    VALUES ('$itemcode', '$datereceived', '$fabric', '$details', '$measurement', '$amount', '$order', '$section', '$row')";

    if (mysql_query($sql)) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . mysql_error($conn);
    }

    mysql_close($conn);
    //header ('Location: .php');
}

?>

<form action = "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method = "POST">
Item Code: <input type = "text" name = "itemcode"><br>
Date Received: <input type = "date" name = "inventoryDateReceived"><br>
Type of Fabric: <input type = "text" name = "fabric"><br>
Unit of Measurement: 
<select name = "measurement">
<option value = "Grams">Grams</option>
<option value = "Kilograms">Kilograms</option>
</select><br>
Amount: <input type = "number" name = "amount"><br>
Assigned Order/Use: <input type = "text" name = "order"><br>
Section: <input type = "text" name = "section"><br>
Row: <input type = "text" name = "row"><br>
<input type = "submit" value = "submit" name = "addInventory">
</form>

2 个答案:

答案 0 :(得分:1)

这些索引与您的输入表单名称不匹配:

$_POST['itemCode']
$_POST['dateReceived']
$_POST['typeOfFabric']

这些应该是:

$_POST['itemcode']
$_POST['inventoryDateReceived']
$_POST['fabric']

检查表单输入:

<input type = "text" name = "itemcode">
<input type = "date" name = "inventoryDateReceived">
<input type = "text" name = "fabric">

答案 1 :(得分:1)

我在这部分代码中没有任何意义:

if(isset($_POST['itemCode'])){ $itemcode = $_POST['itemCode']; } 
if(isset($_POST['dateReceived'])){ $inventoryDateReceived = $_POST['dateReceived']; } 
if(isset($_POST['typeOfFabric'])){ $fabric = $_POST['typeOfFabric']; }
if(isset($_POST['details'])){ $details = $_POST['details']; } 
if(isset($_POST['unitOfMeasurement'])){ $measurement = $_POST['unitOfMeasurement']; }
if(isset($_POST['amount'])){ $amount = $_POST['amount']; } 
if(isset($_POST['assignedOrderUse'])){ $order = $_POST['assignedOrderUse']; } 
if(isset($_POST['section'])){ $section = $_POST['section']; }
if(isset($_POST['row'])){ $row = $_POST['row']; }

您只是为新变量设置值(如果是isset) - 但如果它们不存在,您仍将使用未定义的变量。此外,没有逃避以防止sql注入和验证给定值!

我认为由于缺少变量,您将收到此错误。