doutao1171 2014-04-09 20:46
浏览 44

使用php和mysql更新UPDATE的麻烦

I am having difficulty using the UPDATE command on a functioning database.

What I am attempting to do:

On an existing database I want to update a specific field (sdate) using a date picker and have the value be saved into the database.

What is happening:

I am able to access the database (the echo of one of the elements (row[0]) works) but I am not able to get the date picker's value to get saved into the database.

Can someone point me in the right direction please?

Here is the main html code:

<?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'; ?>

Here is 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);
?>

Any help is much appreciated!

  • 写回答

1条回答 默认 最新

  • doudaiyao0934 2014-04-09 20:54
    关注

    As for as i understand your problem you want to update each row of a table by clicking update button. for this you have to create multiple form not a single form. Your logic is not correct. use the below code a hopes that will solve your problem.

    <?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'; ?>
    

    Here is you another file.

     // 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);
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP