首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >我正在尝试使用mysqli和php将产品添加到数据库,但没有添加任何内容

我正在尝试使用mysqli和php将产品添加到数据库,但没有添加任何内容
EN

Stack Overflow用户
提问于 2018-07-21 10:23:01
回答 2查看 220关注 0票数 1

我创建了一个页面,使用mysqli和php将库存添加到数据库中。当我填写信息并点击submit按钮时,没有任何反应。这些字段就会变成空白。我检查了数据库,没有放入任何内容。

有人能帮我找出错误在哪里吗?

下面是我的代码:

代码语言:javascript
复制
 <?php
error_reporting(E_ALL);
ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);

include_once("../storescripts/connect_to_mysql.php");
$con = mysqli_connect("$db_host","$db_username","$db_pass","$db_name");

if(isset($_POST['button'])) {

$item_number = mysqli_real_escape_string($con, $_POST['item_number']);
$price = mysqli_real_escape_string($con, $_POST['price']);
$category = mysqli_real_escape_string($con, $_POST['category']);
$desc = mysqli_real_escape_string($con, $_POST['description']);
$qty = mysqli_real_escape_string($con, $_POST['qty']);

// See if that product name is an identical match to another product in the system
    $sql = ("SELECT id FROM products WHERE item_number='$item_number' LIMIT 1");
    $result = mysqli_query($con, $sql);
    $productMatch = mysqli_num_rows($result); // count the output amount
    if ($productMatch > 0) {
        echo 'Sorry you tried to place a duplicate "item_number" into the system, <a href="inventory_list.php">click here</a>';
        exit();
    }
    //Add to inventory now

$sql = "INSERT INTO products ('item_number', 'price', 'category', 'description', 'qty', 'date_added')
    VALUES (?,?,?,?,?,?)";
$stmt = mysqli_stmt_init($con);
if(!mysqli_stmt_prepare($stmt, $sql)) {
    echo "sql error";
} else {
    mysqli_stmt_bind_param($stmt, "ssssss", $item_number, $desc, $price, $catagory, $qty, now());

    mysqli_stmt_execute($stmt);
    echo mysqli_stmt_error($stmt);
    $pid = mysqli_insert_id();
    // Place image in the folder 
    $newname = "$pid.png";
    move_uploaded_file( $_FILES['fileField']['tmp_name'], "../pictures/inventory/$newname");
}
}
?>
<?php
$product_list = '';
include_once("../storescripts/connect_to_mysql.php");
    $con = mysqli_connect("$db_host","$db_username","$db_pass","$db_name");
$sql = "SELECT * FROM products ORDER BY date_added DESC";
$result = mysqli_query($con, $sql);
$product_count = mysqli_num_rows( $result);
if ($product_count > 0) {
    while($row = mysqli_fetch_array($sql)){ 
             $id = $row["id"];
             $item_number = $row["item_number"];
             $price = $row["price"];
             $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
             $product_list .= "Product ID: $id - <strong>$product_name</strong> - $$price - <em>Added $date_added</em> &nbsp; &nbsp; &nbsp; <a href='inventory_edit.php?pid=$id'>edit</a> &bull; <a href='inventory_list.php?deleteid=$id'>delete</a><br />";
             $qty = $row["qty"];
    } 
} else {
        $product_list = "You Have No Items In Inventory";
    }
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="author" content="samuel jaycox">
<title>Inventory List</title>
<link rel="stylesheet" href="../style.css" type="text/css" media="screen" />
<link rel="shortcut icon" type="image/png" href="pictures/pinky.png">
</head>
<body>
<div align="center" id="mainWrapper">
  <div id="pageContent"><br />
    <div align="right" style="margin-right:32px;"><a href="inventory_list.php#inventoryForm">+ Add New Inventory Item </a></div>
<div align="left" style="margin-left:24px;">
      <h2>Inventory list</h2>
      <?php echo $product_list; ?>
    </div>
    <hr />
    <a name="inventoryForm" id="inventoryForm"></a>
    <h3>
    &darr; Add New Inventory Item Form &darr;
    </h3>
    <form action="inventory_list.php" enctype="multipart/form-data" name="myForm" id="myForm" method="POST">
    <table width="90%" border="0" cellspacing="0" cellpadding="6">
      <tr>
        <td width="20%" align="right">Item Number</td>
        <td width="80%"><label>
          <input name="item_number" type="text" id="item_number" size="64" />
        </label></td>
      </tr>
      <tr>
        <td align="right">Product Price</td>
        <td><label>
          $
          <input name="price" type="text" id="price" size="12" />
        </label></td>
      </tr>
      <tr>
        <td align="right">Category</td>
        <td><label>
          <select name="category" id="category">
          <option value="Bracelets">Bracelet</option>
          <option value="Necklace">Necklace</option>
          <option value="Earring">Earring</option>
          <option value="Childrens">Childrens</option>
          <option value="Sets">Sets</option>
          <option value="Rosary">Rosary</option>
          <option value="Accessories">Accessories</option>
          </select>
        </label>
      </tr>
      <tr>
        <td align="right">Description</td>
        <td><label>
          <textarea name="description" id="description" cols="64" rows="20"></textarea>
        </label></td>
      </tr>
         <tr>
        <td width="20%" align="right">Qty</td>
        <td width="80%"><label>
          <input name="qty" type="text" id="qty" size="9" />
        </label></td>
      </tr>
      <tr>
        <td align="right">Product Image</td>
        <td><label>
          <input type="file" name="fileField" id="fileField" />
        </label></td>
      </tr>      
      <tr>
        <td>&nbsp;</td>
        <td><label>
          <input type="submit" name="button" id="button" value="Submit"/>
        </label></td>
      </tr>
    </table>
    </form>
    <br />
  <br />
  </div>
</div>
</body>
</html>

我是mysqli的新手,只是不知道从哪里开始寻找。任何帮助都将不胜感激。谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-21 10:42:15

将以下代码放在php代码的顶部:

代码语言:javascript
复制
error_reporting(E_ALL);
ini_set('display_startup_errors', 1); // add this too
ini_set('display_errors', 1);

它将显示所有对您有帮助的错误,并在您的mysql_stmt_execute()后面添加以下代码:

代码语言:javascript
复制
echo mysqli_stmt_error($stmt);

此代码将返回mysql错误消息,这可能会有所帮助。不要忘记将错误消息粘贴到您的问题中。

编辑:关于你的代码,

如果我是正确的,那么您正在尝试使用准备好的语句。它应该是

代码语言:javascript
复制
//add ? marker, see official php page about prepared statements
$sql = "INSERT INTO products ('item_number', 'description', 'price', 
'catagory', 'qty', 'date_added')
    VALUES (?, ?, ?, ?, ?, ?)";
$stmt = mysqli_stmt_init($con);
if(!mysqli_stmt_prepare($stmt, $sql)) {
    echo "sql error";
} else {
    //parameters (variable) are binded here. s for string, see official php page for more information. 
    mysqli_stmt_bind_param($stmt, "ssssss", $item_number, $desc, $price, $catagory, $qty, now());

    mysqli_stmt_execute($stmt);
    $pid = mysqli_insert_id();
    // Place image in the folder 
    $newname = "$pid.png";
    move_uploaded_file( $_FILES['fileField']['tmp_name'], 
"../pictures/inventory/$newname");
}
    header("location: inventory_list.php?item addes successfully");
}

请参阅mysqli bind param

编辑2:尝试在$con = mysqli_connect("$db_host","$db_username","$db_pass","$db_name");之后添加此代码以检查您的连接。

代码语言:javascript
复制
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

并从$sql = "INSERT INTO products ('item_number', 'price', 'category', 'description', 'qty', 'date_added') VALUES (?,?,?,?,?,?)";中删除‘(撇号)

编辑3: php中没有now()函数,您可以将其更改为date("Y-m-d H:i:s"),或者将您的预准备语句代码更改为以下代码。

代码语言:javascript
复制
//now() function is mysql function, so it should be called in sql query.
$sql = "INSERT INTO products ('item_number', 'description', 'price', 
'catagory', 'qty', 'date_added')
    VALUES (?, ?, ?, ?, ?, NOW())";
$stmt = mysqli_stmt_init($con);
if(!mysqli_stmt_prepare($stmt, $sql)) {
    echo "sql error";
} else {
    //remove now() function.
    mysqli_stmt_bind_param($stmt, "sssss", $item_number, $desc, $price, $catagory, $qty);

    mysqli_stmt_execute($stmt);
    $pid = mysqli_insert_id();
    // Place image in the folder 
    $newname = "$pid.png";
    move_uploaded_file( $_FILES['fileField']['tmp_name'], 
"../pictures/inventory/$newname");
}
票数 0
EN

Stack Overflow用户

发布于 2018-07-21 10:56:10

查询中的值部分是错误的。应该是值(‘value1’,依此类推),在你的例子中,它只是值(value1,这不是正确的方式。

您可以随时回显您的Sql语句,并在HeidiSQL或MySQL的CLI等Sql客户端中运行查询。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51452285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档