php - 将输入的信息发送到数据库中的 MySQL 表的 HTML 表单

标签 php html mysql database

我已经尝试了这个网站上给出的几个不同的答案,但我似乎仍然无法让一切正常工作。我将发布我目前拥有的内容。

我的数据库名称是“artStore”
我的表名称是“inventory”

我的表单 HTML 代码是:

<form action="sendToTable.php" method="post">

        <h3>Product:</h3>
            <input type="text" name="$row[product]">

        <h3>Category:</h3>
            <input type="text" name="$row[category]">

        <h3>Seller:</h3>
            <input type="text" name="$row[seller]">

        <input type="submit">
</form>

我将信息发送到数据库的 PHP 代码:

 $sql = "INSERT INTO inventory (id, product, category, seller) VALUES ('$row[id]', '$row[product]', '$row[category]', '$row[seller]')";
  //Run the sql statement
  if ($connection->query($sql) === true) {
    echo "The Insert Worked";
  } else {
    echo "Error occured in the insert: " . $connection->error;
  }


   $connection->close();

我用于显示表格的 PHP 代码:

$sql = "SELECT * FROM inventory";
  $result = $connection->query($sql);

  if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
      echo "
      id: $row[id] <br>
      product: $row[product] <br>
      category: $row[category] <br>
      seller: $row[seller] <br>
      <hr>
      ";
    }
  } else {
    echo "No Results";
  }
  $connection->close();

非常感谢任何帮助!我是 Web 编程的新手。

编辑 1:

我更新了我的文件,我得到的错误是:

"Error occured in the insert: Column count doesn't match value count at row 1"

它说错误发生在包含用于将信息发送到数据库的 PHP 代码的文件中,但这是我的所有文件。

表单的 HTML 代码:

<form action="sendToTable.php" method="post">

        <h3>Product:</h3>
            <input type="text" name="product">

        <h3>Category:</h3>
            <input type="text" name="category">

        <h3>Seller:</h3>
            <input type="text" name="seller">

        <input type="submit">
</form>

向数据库发送信息的 PHP 代码:

//Create the SQL Statement 
  $product = $_POST['product'];
  $category = $_POST['category'];
  $seller = $_POST['seller'];

  $sql = "INSERT INTO inventory (id, product, category, seller) VALUES ('$product', '$category', '$seller')";
  //Run the sql statement
  if ($connection->query($sql) === true) {
    echo "The Insert Worked";
  } else {
    echo "Error occured in the insert: " . $connection->error;
  }


   $connection->close();

显示表格的 PHP 代码:

$sql = "SELECT * FROM inventory";
  $result = $connection->query($sql);

  if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
      echo "
      id: $row[id] <br>
      product: $row[product] <br>
      category: $row[category] <br>
      seller: $row[seller] <br>
      <hr>
      ";
    }
  } else {
    echo "No Results";
  }
  $connection->close();

最佳答案

您不应该为“$row[product]”等调用 HTML 输入字段,而应只调用“product”。

您用于插入数据库的 PHP 代码是正确的,但未设置变量。 您需要先从 HTML 表单中检索字段:

$product = $_POST['product'];//'product' 是 HTML 输入字段的名称 $category = $_POST['category']; //等等...

您还应该清理所有用户输入以避免 SQL 注入(inject)等。 See this answer for a good explanation .

同时更改 $sql 字符串变量中变量的名称,因为当前的不存在。

关于php - 将输入的信息发送到数据库中的 MySQL 表的 HTML 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27348829/

相关文章:

PHP 使用错误的字符数序列化 protected 类变量?

php - JSON.parse 错误 #1132 : Invalid JSON parse input (Flex/Actionscript/PHP)

javascript - 使用 JQuery/JS 将 div 从一个列表移动到下一个列表

mysql - 计算MySQL中其他平均值的平均值

mysql - 过滤电话号码 REGEX

java - 如何在 Eclipse RCP 应用程序中使用单独的数据库进行生产和测试

php - 在 PHP 中对数组进行排序与​​从 SQL 中获取数据

使用 JDBC 的 Java MySQL 查询

html - 无法居中菜单

javascript - jQuery:根据任意数量的复选框隐藏/显示 div