从PHP站点更新MySQL数据库的信息

时间:2014-12-08 16:48:31

标签: php mysql database forms

我一直在这里尝试各种教程和解决方案,但我无法让它发挥作用。请帮忙!


** 更新#1 **
这是我在运行代码时收到的错误:" 插入时出错:您的SQL语法出错;查看与您的MySQL服务器版本相对应的手册,以获得在''附近使用的正确语法。第1行 "


** 更新#2 **
这是显示表格中所有内容的页面: editBrowse.php

`

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

  if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
      echo "
      <img src=$row[image] id='productImage'> <br>
      product: $row[product] <br>
      category: $row[category] <br>
      seller: $row[seller] <br>
      <a href='edit.php?id=$row[id]'>Edit</a>
      <hr>
      ";
    }
  } else {
    echo "No Results";
  }
  $connection->close();

`

这是您单击上面的编辑按钮时显示的页面:edit.php

`

  <?php
  // Connection code..etc..


  $sql = "SELECT * FROM inventory WHERE id=".$_REQUEST['id'];
  $result = $connection->query($sql);

  if ($result->num_rows > 0) {
    $row = $result->fetch_assoc();
  } else {
    echo "No Results";
  }
  $connection->close();

?>



<div class="row" id="mainSection">

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

    <input type="hidden" name="id" value="<?=$id?>">

    <h3>Image:</h3>
        <input type="text" name="image" value="<?=$row['image']?>">

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

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

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

    <br>
    <br>

    <input type="submit" value="Update My Record">

  </form>
  <br>
  <a href="admin.php" id="backButton">Back</a>

</div>

`

这是当您单击&#34;更新我的记录&#34;时运行的文件。上面的按钮:update.php

&#39;

$id= $_POST['id'];
$image = $_POST['image'];
$product = $_POST['product'];
$category = $_POST['category'];
$seller = $_POST['seller'];



$sql = "UPDATE inventory set image='$image', product='$product', category='$category', seller='$seller' WHERE id = $id";


if ($connection->query($sql) === true) {
  echo "Inserted Successfully";
} else {
  echo "Error occured in the insert: " . $connection->error;
}

 $connection->close();

&#39;

4 个答案:

答案 0 :(得分:3)

你的sql中存在句法问题:

$sql = "UPDATE inventory set image='$image', product='$product', category='$category', seller='$seller' WHERE id = $id";

此外,您需要验证$ id是否具有正确的值。因为如果它为空或空,则查询将失败。

如果要插入数据,可以使用如下查询:

$sql = "INSERT INTO inventory (image, product, category, seller) VALUES ('$image', '$product', '$category', '$seller')";

在edit.php视图中尝试此操作:

<?php
    $sql = "SELECT * FROM inventory WHERE id=".$_REQUEST['id];
      $result = $connection->query($sql);

      if ($result->num_rows > 0) {
        $row = $result->fetch_assoc();
      } else {
        echo "No Results";
      }
      $connection->close();
?>
<div class="row" id="mainSection">
  <form action="update.php" method="post">
    <input type="hidden" name="id" value="value="<?php echo $row['id']?>"">
    <h3>Image:</h3>
        <input type="text" name="image" value="<?php echo $row['image']?>">
    <h3>Product:</h3>
        <input type="text" name="product" value="<?php echo $row['product']?>">
    <h3>Category:</h3>
        <input type="text" name="category" value="<?php echo $row['category']?>">
    <h3>Seller:</h3>
        <input type="text" name="seller" value="<?php echo $row['seller']?>">
    <br><br>
    <input type="submit" value="Update My Record">
  </form>
  <br>
  <a href="admin.php" id="backButton">Back</a>    
</div>

答案 1 :(得分:1)

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

  if ($result->num_rows > 0) {
    while ($row = $result->fetch_assoc()) {
      echo "
      <img src=$row['image'] id='productImage'> <br>
      product: $row['product'];<br>
      category: $row['category']; <br>
      seller: $row['seller'] ;<br>
      <a href="edit.php?id=$row['id']">Edit</a>
      <hr>
      ";
    }
  } else {
    echo "No Results";
  }
  $connection->close();

<强> EDIT.PHP

<div class="row" id="mainSection">
  <?php $id = $_REQUEST['id']; ?>
  <form action="update.php" method="post">

    <input type="hidden" name="id" value="<?=$id?>">

    <h3>Image:</h3>
        <input type="text" name="image">

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

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

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

    <br>
    <br>

    <input type="submit" value="Update My Record">

  </form>
  <br>
  <a href="admin.php" id="backButton">Back</a>

</div>

<强> UPDATE.PHP

<?php
$id= $_POST['id'];
$image = $_POST['image'];
$product = $_POST['product'];
$category = $_POST['category'];
$seller = $_POST['seller'];

$sql = "UPDATE inventory set image='".$image."', product='".$product."', category='".$category."', seller='".$seller."' WHERE id = ".$id." ";

if ($connection->query($sql) === true) {
  echo "Inserted Successfully";
} else {
  echo "Error occured in the insert: " . $connection->error;
}

 $connection->close();

答案 2 :(得分:0)

您需要为id设置一个值,然后在查询中使用$_POST['id']

答案 3 :(得分:0)

首先,更新查询的sql语法是错误的。其次,表单中的id没有设置值。因此,当您发布表单时,该值将为空。