dousongxuan7507 2014-12-08 16:48
浏览 61
已采纳

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

I've been trying all kinds of tutorials and solutions on here and I can not get this to work. Please help!


** UPDATE #1 **
This is the error that I receive when running my code: "Error occured in the insert: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1"


** UPDATE #2 **
Here is the page that shows everything in the table: 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();

`

This is the page that shows up when you click the edit button above: 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>

`

And this is the file that runs when you click the "Update My Record" button above: update.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();

'

  • 写回答

4条回答 默认 最新

  • dony113407 2014-12-08 16:53
    关注

    You have a syntactic problem in your sql:

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

    In addition you need to verify if $id has the correct value. Because if it's empty or null your query will fail.

    If you want to insert data you can use a query like this:

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

    Try this in your edit.php view:

    <?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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错
  • ¥20 @microsoft/fetch-event-source 流式响应问题