Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php和mysql进行更新时遇到问题_Php - Fatal编程技术网

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

使用php和mysql进行更新时遇到问题,php,Php,在正常运行的数据库上使用UPDATE命令时遇到困难 我试图做的是: 在现有数据库上,我希望使用日期选择器更新特定字段(sdate),并将值保存到数据库中 正在发生的事情: 我可以访问数据库(其中一个元素(行[0])的回音可以工作),但无法获取日期选择器的值以保存到数据库中 有人能给我指一下正确的方向吗 以下是主要的html代码: <?php include '../include/header.php'; ?> <?php include '../include/datepic

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

我试图做的是:

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

正在发生的事情:

我可以访问数据库(其中一个元素(行[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);
?>


非常感谢您的帮助

据我所知,您希望通过单击“更新”按钮来更新表中的每一行。 为此,您必须创建多个表单,而不是单个表单。你的逻辑不正确。使用下面的 代码a希望能解决您的问题

<?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);
    }
//汇编数组
如果($result=$conn->query($comm)){
/*获取对象数组*/
而($row=$result->fetch_row()){
如果($row[5]=“presale”)或($row[4]!=0)){
回声“;
回声“;
回显“$row[0]”;
回声“;
回声“;
回声“;
回声“;
回声“;
}
}
}
如果(isset($_POST[“更新”])){
$entry4=$_POST[“sdate”];
$rec_id=$_POST[“rec_id”];
$cmd=“UPDATE$dbtable SET$col4='$entry4'其中$col1=$rec_id”;
//使用预先准备好的语句来提高安全性
如果($stmt=mysqli\u prepare($conn,$cmd)){
mysqli_stmt_execute($stmt);
}
//关闭语句和连接
mysqli_stmt_close($stmt);
}

Sidenote:如果您在查询字符串中使用变量,您可能会错过prepared语句可以提供的更高安全性。。。您可以尝试查找这些值:)是否有任何错误?日期的格式是否正确以匹配数据库中的列?侧注:由于您处理的是字符串,因此需要在
$col4=$entry4
中使用引号,就像在
$col4='$entry4'
中一样;很有可能是Durbnpoison,-我没有收到任何错误。这是令人惊讶的,因为我一直在寻找错误来调试这个。是“{}”符号真的需要,还是出于说明的目的。我尝试了上面的代码,但仍然没有运气。奇怪的是,我没有收到任何帮助调试的错误消息:(你能不能使用
printf(“错误消息:%s\n”,mysqli\u error($conn))
获取错误并通知我,以便更好地帮助您。我没有看到任何错误。我试图在此处发布代码,但仅限于字符。我只需更改完整答案。请相应地修改您的文件并让我知道它是否有效。问题在于您的逻辑。尝试一下新代码。。