dsbx40787736 2015-02-27 15:29
浏览 5

too long

I am trying to update data from a submission form into the MySQL database using PHP. When I click on the button to update the value in the database, it becomes empty.

Additionally, I also receive the following two error messages :

Notice: Undefined variable: id in C:\xampp\htdocs\test1\edit.php on line 64

Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\test1\edit.php on line 66

The following is my PHP Code:

<?php

  $name = '';

  if (isset($_GET['editQ'])) {
    $ok = true;
    $id = $_GET['editQ'];

    if ($ok) {
      // add database code here
      $db = mysqli_connect('localhost', 'root', '', 'test2015');
      $sql = sprintf("UPDATE question SET question_body='%s'
              WHERE question_id=%s",
      mysqli_real_escape_string($db, $name),$id);
      mysqli_query($db, $sql);
      echo '<p>User updated.</p>';
      mysqli_close($db);
    }
    } else {
      $db = mysqli_connect('localhost', 'root', '', 'test2015');
      $sql = sprintf('SELECT * FROM question WHERE question_id=%s', $id);
      $result = mysqli_query($db, $sql);
      foreach ($result as $row) {
        $name = $row['question_body'];

      }
      mysqli_close($db);
    }
  ?>

  <form name="editQ" method="POST" action="edit.php" > 
    <td>Please Edit the Question</td> 
    <input type="text" name="<?php echo ($q)?>" value="<?php
            echo htmlspecialchars($name);?>" /> 
    <input type="submit" name="submit" value="Edit">
  </form>

Any help/advice would be much appreciated. Thanks in advance.

  • 写回答

4条回答 默认 最新

  • dsgsdg206050 2015-02-27 15:33
    关注

    $id is not being set before the SQL statement is being called. It's in the else section of the if statement isset($_GET['editQ']) which defines $id;

    Also instead of foreach use the following with a mysqli result

    while($row = $result->fetch_assoc())
    {
        // STUFF
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示