【问题标题】:How do I edit a specific row in SQL using PHP如何使用 PHP 编辑 SQL 中的特定行
【发布时间】:2017-03-08 20:06:39
【问题描述】:

我对 PHP 和 SQL 还很陌生。 我希望能够将任务分配给某个用户,并使用分配给该任务的用户名更新数据库中的该行。 这是我的代码:

<table border="0" width="1100px" style= "font-size: 12px" >
  <thead>
    <tr valign="top" align="left">
    <th height="20"></th>
      <th height="20">Customer</th>
      <th>Vehicle</th>
      <th>Appt Time</th>
      <th>Notes</th>
      <th>Assign</th>
      <th>Action</th>
      <tr><td valign="top" colspan="6"><hr><br></td></tr>
    </tr>
  </thead>
  <tbody>
    <?php
      while( $row = mysql_fetch_assoc( $result ) ){
        echo
        "<tr valign='center'>

          <td width='50'><b>{$row['id']}</td>

            <td width='220' height='70'><b>{$row['firstname']}
          </td>


          <td width='240'><b>{$row['car']}</b> <br>{$row['reg']}</td>
          <td width='170'>Monday<br>24 September<br>17:00</td>

          <td width='240'>{$row['notes']}<br><b>Status:</td>
          <td width='240'>
          <form action='bookings.php' method='post'>
          <select style='width:90px' class='reg' name='assign'required>
          <option value=''></option>
          <option value='User1'>User1</option>
          <option value='User2'>User2</option>
          <option value='User3'>User3</option>
          <option value='User4'>User4</option>
          <option value='User5'>User5</option>
         </select><input type='submit' value='>'  class='assignButton'/></form>
          </td><td>
          <button class='myButton'>Edit</button>
          </td>
          <tr><td colspan='6'><hr class='hrTitle'></td></tr>
        </tr>\n";
      }  
    ?>

  </tbody>
</table>

如您所见,我有许多可供选择的用户,我希望能够将该任务分配给选择列表中的用户。 非常感谢任何帮助。

【问题讨论】:

  • UPDATE table_name SET value1 = $your_value, value2 = $your_value2 WHERE user_id = $user_id
  • 非常粗鲁的猜测:UPDATE ... set assigned=1 where user=$_POST['assign']
  • @nogad yuck,它需要被转义!
  • 我说这很糟糕
  • 您正在使用不安全、未维护且已弃用近 10 年的数据库功能。停下来。 stackoverflow.com/q/12859942/1255289

标签: php html mysql database


【解决方案1】:

尝试以下步骤。

  1. 为您的提交按钮命名。

    <input type='submit' value='>' name='submit' class='assignButton'>
    
  2. 在您的表单中添加一个隐藏的输入字段。给它当前行的 id 的值。给它一个名字。我选择了身份证。 这非常重要,因此您可以知道要编辑哪个客户。请仔细检查您将设置的值。从您的代码中,我看到它是 $row['id']。

    <input type="hidden" name="id" value='$row["id"]''>
    
  3. 在您的 PHP 文件 bookings.php(假设您已连接到数据库)中,处理提交的表单。

    if(isset($_POST["submit"])){ //checks if the form was submitted
        $id = $_POST["id"]; //id of the customer
        $assign = $_POST["assign"]; //your selected value
        $query = "UPDATE table SET columnToModify = '$assign' WHERE id = '$id'";
        $result = $connection->query($query); //run the query
    }
    

希望对你有帮助。

【讨论】:

  • 非常感谢,我已经完成了您所说的一切,但几乎没有任何事情发生。没有错误,但该行未分配给任何人。知道为什么会发生这种情况吗?
  • 显示你的代码。 bookings.php 文件和包含表单的 html。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多