0

当我尝试更新数据时,我得到了相同的数据但没有更新。我找不到哪里出错了。更新没有显示任何错误,但是当我查看代码时没有更新。所以更新不起作用。当我尝试更新数据时,我得到了相同的数据但没有更新。我找不到哪里出错了。更新没有显示任何错误,但是当我查看代码时没有更新。所以更新不起作用。

// Start the session
  session_start();
   $dbc = mysqli_connect('localhost', 'root', '', 'onlinetest');
  $sumid = $_SESSION['sumid'];
   if (!isset($_POST['submit']))
   {
      $_SESSION['sumid'] = $_GET['sumid'];


      $query = "SELECT question, answer FROM sum WHERE sumkey = '$sumid' ";

       $data = mysqli_query($dbc, $query);
       if (mysqli_num_rows($data) == 1) {

          $row = mysqli_fetch_array($data);
          $question = $row['question'];
          $answer = $row['answer'];

          echo $question . $sumid;

        }
        mysqli_close($dbc); 

   }


 if (isset($_POST['submit']))
 {
     //$Test = $_SESSION['testid'];
     $question1 = $_POST['question'];
     $answer1 = $_POST['answer'];
     $dbc = mysqli_connect('localhost', 'root', '', 'onlinetest');

    // $dbc = mysqli_connect('localhost', 'root', '', 'onlinetest');
     $query = "UPDATE sum SET question = $question1,answer = $answer1 WHERE sumkey = $sumid";

     mysqli_query($dbc, $query);

     echo 'New test name added';
     echo ''.$question1;
     mysqli_close($dbc);
     //exit();
 }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<div>Add sum</div>
<form method="post" action ="<?php echo $_SERVER['PHP_SELF']; ?>">
  <p>
    <label for="textarea"></label>
  <label>Question</label></p>
  <p>
      <textarea name="question" id="question" value="" cols="100" rows="9"><?php echo $question  ?></textarea>
  </p>
  <p><label>Answer</label>&nbsp;</p>
  <p>
    <label for="textarea2"></label>
    <textarea name="answer" cols="100" rows="20" id="answer" value=""><?php echo $answer  ?></textarea>
  </p>
  <p>
    <input type ="submit" value="ADD" name="submit"/>
  </p>

</form>
</body>
</html>
4

3 回答 3

0

更新 [原文如此] 没有显示任何错误

不足为奇 - 您的代码不会检查错误。尝试在每次 mysqli_ 操作后检查 mysqli_error()。

顺便说一句,您的代码容易受到 SQL 注入的影响,并且在会话中存储与事务相关的数据是非常糟糕的主意。

于 2013-11-11T17:32:00.233 回答
0

1-从我所看到的,您在代码中混淆了这两件事。

http://us1.php.net/mysqli_close

http://php.net/manual/en/mysqli-result.free.php

http://us1.php.net/mysqli_query

2-正如 simcbean 告诉你的那样,你需要检查 SQL INJECTION (大写,所以你知道它非常重要。)

在Little Bobby TABLES决定尝试您的网站之前快点。

3- 个人推荐:

学习PDOprepared statement,习惯了就容易上手,安全多了。 http://php.net/manual/en/pdo.prepared-statements.php

有用的代码:

if ($result = mysqli_query($link, $query)) {
    // Stuff

}else{
   // See what's wrong with the query
}

编辑:如果您的数据不是整数,则需要在 UPDATE 语句中引用您的值

$query = "UPDATE sum SET question = $question1,answer = $answer1 WHERE sumkey = $sumid";
$query = "UPDATE sum SET question = '$question1',answer = '$answer1' WHERE sumkey = '$sumid'";
于 2013-11-11T18:21:29.730 回答
0

根据您给定的脚本,首先您要获取记录然后更新数据,因此可能是因为它没有显示更新的记录。只需反转条件意味着将 update if 条件置于获取查询之上。然后首先它会更新记录,然后获取更新的记录。

脚本易受 SQL 注入攻击,防止使用mysqli_real_escape_string()方法。

// Start the session
  session_start();
  $sumid = $_SESSION['sumid'];
  if (isset($_POST['submit']))
 {
     //$Test = $_SESSION['testid'];
     $question1 = $_POST['question'];
     $answer1 = $_POST['answer'];
     $dbc = mysqli_connect('localhost', 'root', '', 'onlinetest');

    // $dbc = mysqli_connect('localhost', 'root', '', 'onlinetest');
     $query = "UPDATE `sum` SET `question` = '$question1', `answer` = '$answer1' WHERE `sumkey` ='$sumid'";

     mysqli_query($dbc, $query);

     echo 'New test name added';
     echo ''.$question1;
     mysqli_close($dbc);
     //exit();
 }



   $dbc = mysqli_connect('localhost', 'root', '', 'onlinetest');

   if (!isset($_POST['submit']))
   {
      $_SESSION['sumid'] = $_GET['sumid'];    

      $query = "SELECT `question`, `answer` FROM `sum` WHERE `sumkey` = '$sumid' ";

       $data = mysqli_query($dbc, $query);
       if (mysqli_num_rows($data) == 1) {

          $row = mysqli_fetch_array($data);
          $question = $row['question'];
          $answer = $row['answer'];

          echo $question . $sumid;

        }
        mysqli_close($dbc); 

   }



?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<div>Add sum</div>
<form method="post" action ="<?php echo $_SERVER['PHP_SELF']; ?>">
  <p>
    <label for="textarea"></label>
  <label>Question</label></p>
  <p>
      <textarea name="question" id="question" value="" cols="100" rows="9"><?php echo $question  ?></textarea>
  </p>
  <p><label>Answer</label>&nbsp;</p>
  <p>
    <label for="textarea2"></label>
    <textarea name="answer" cols="100" rows="20" id="answer" value=""><?php echo $answer  ?></textarea>
  </p>
  <p>
    <input type ="submit" value="ADD" name="submit"/>
  </p>

</form>
</body>
</html>
于 2013-11-11T17:57:24.657 回答