繁体   English   中英

使用php和mysql更新时遇到问题

[英]Trouble with UPDATE using php and mysql

我在运行正常的数据库上使用UPDATE命令时遇到困难。

我正在尝试做的是:

在现有数据库上,我想使用日期选择器更新特定字段(日期),并将其值保存到数据库中。

怎么了:

我能够访问数据库(元素之一(行[0])的回波有效),但是我无法获取日期选择器的值保存到数据库中。

有人可以指出我正确的方向吗?

这是主要的html代码:

<?php include '../include/header.php'; ?>
<?php include '../include/datepicker.php'; ?>
<?php include '../include/format.php'; ?>
<fieldset>
<legend>Presale Units in Stock</legend>
<table border=1>
<tr>
<th>Id</th>
<th>Ship Date</th>
<th>Button?</th>
</tr>
<tr>
<form>
<?php include '../include/junk.presale.mysql.php'; ?>
</table>
<button type="reset" value="Reset">Reset</button>
</form>
</tr>
</fieldset>
<br>
<?php include '../include/footer.php'; ?>

这是junk.presale.mysql.php:

<?php
// Get database credentials
require 'config.php';
$dbtable = "assembly2";
$col1 = "id";
$col4 = "sdate";
$comm = "SELECT * FROM $dbtable";
/* Create a new mysqli object with database connection parameters */
$conn= new mysqli($dbhost,$dbuser,$dbpass,$dbname);
if(mysqli_connect_errno()) {
    echo "Connection Failed: " . mysqli_connect_errno();
    exit();
}

// Assembly array
if ($result = $conn->query($comm)) {
    /* fetch object array */
    while ($row = $result->fetch_row()) {
        if(($row[5]=="presale")or($row[4]!=0)) {
            echo "<tr>";
            echo "<td>$row[0]</td>";
            echo "<td><input type=\"text\" name=\"sdate\" class=\"datepicker\"></td>";
            echo "<td><input name=\"update\" type=\"submit\" id=\"update\" value=\"Update\"></td>";
            if(isset($_POST["update"])){
                $entry4 = $_POST["sdate"];
                $cmd = "UPDATE $dbtable SET $col4=$entry4 WHERE $col1=$row[0]";

                // use prepared statements to increase security
                if ($stmt = mysqli_prepare($conn,$cmd)){
                    mysqli_stmt_execute($stmt);
                }
                // Close statement and connection
                mysqli_stmt_close($stmt);
            }
            echo "</tr>";
        }
    }
}

/* free result set */
$result->close();

// Close statement and connection
mysqli_close($conn);
?>

任何帮助深表感谢!

至于我了解您的问题,您想通过单击更新按钮来更新表的每一行。 为此,您必须创建多个表单而不是单个表单。 您的逻辑不正确。 使用以下代码希望可以解决您的问题。

<?php include '../include/header.php'; ?>
<?php include '../include/datepicker.php'; ?>
<?php include '../include/format.php'; ?>
<fieldset>
<legend>Presale Units in Stock</legend>
<table border="1">
 <tr>
  <th>Id</th>
  <th>Ship Date</th>
  <th>Button?</th>
 </tr>
 <?php include '../include/junk.presale.mysql.php'; ?>
  </table>
<br>
<?php include '../include/footer.php'; ?>

这是您另一个文件。

 // Assembly array
 if ($result = $conn->query($comm)) {
  /* fetch object array */
   while ($row = $result->fetch_row()) {
        if(($row[5]=="presale")or($row[4]!=0)) {
                echo "<tr>";
                echo "<form id=\"form-$row[0]\" name=\"form-name-$row[0]\" method=\"post\">";
                echo "<td>$row[0]</td>";
                echo "<td><input type=\"text\" name=\"sdate\" class=\"datepicker\"></td>";
                echo "<td><input type=\"hidden\" name=\"rec_id\" value=\"$row[0]\"></td>";
                echo "<td><input name=\"update\" type=\"submit\" id=\"update\" 
                                   value=\"Update\"> </td>";
                echo "</form>";
                echo "</tr>";
        }
    }

}
 if(isset($_POST["update"])){
      $entry4 = $_POST["sdate"];
      $rec_id = $_POST["rec_id"];
      $cmd = "UPDATE $dbtable SET $col4='$entry4' WHERE $col1=$rec_id";

      // use prepared statements to increase security
      if ($stmt = mysqli_prepare($conn,$cmd)){
           mysqli_stmt_execute($stmt);
          }
          // Close statement and connection
          mysqli_stmt_close($stmt);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM