我无法UPDATE数据库mysqli PHP

时间:2017-10-30 09:44:18

标签: php jquery html mysql

这是todolist项目。这个项目是关于login-> addtask - >提交并将其存储在数据库中,现在我遇到编辑任务的问题

为什么更新查询不会更新数据库中的数据?

连接数据库并选择正在运行。

这是html主页

  

home.php

    <?php
session_start();
require ('dbms.php');

if(!isset($_SESSION['login'])){
    header('Location: index.php?login=noLOGIN');
    exit();
}

?>

<h2>Hello <?php  echo $_SESSION['username']; ?> </h2>
<h2>Add Todolist</h2>
<form action="conadd.php" method="post">

  <label>header: </label> <br>
   <input type="text"  name="header" > <br>
    <label>detail: </label>  <br>

    <textarea  type="text" name="detail" ></textarea> <br>
    <label>priority:</label><br> 
    <input type="text"   name="type" ><br>

    <input type="date"  name ="time" ><br>

    <button type="submit" name="addtask"> submit</button>
</form>
<hr>

  <?php
$sql_incom = "SELECT * FROM tb_note WHERE user_id = '".$_SESSION['id']."' ";
$result_incom = mysqli_query($conn,$sql_incom);

if($result_incom->num_rows >0 ){

  while($row = mysqli_fetch_assoc($result_incom)){

           echo "<li>";
           echo '<input type="checkbox"><label>'.$row["header"].'</label><input type="text">';
           echo  $row["detail"];
           echo $row["type"];
            echo $row["time"];
            ?>
            <a href="editpage.php?note_id=<?php echo $row["id"]; ?>"> Edit</a>
            <?php
           echo "</li>";
          }
        }
          ?>
    <a href="logout.php">Logout </a>

这是编辑html

  

editpage.php

    <?php
session_start();
require ('dbms.php');

if(!isset($_SESSION['login'])){
    header('Location: index.php?login=noLOGIN');
    exit();
}
?>
<h2>Hello <?php  echo $_SESSION['username']; ?> </h2>
<h2>Edit Todolist</h2>

<?php
$sql_incom = "SELECT * FROM tb_note WHERE user_id = '".$_SESSION['id']."' AND  id = '".$_GET['note_id']."' ";
$result_incom = mysqli_query($conn,$sql_incom);
if($result_incom->num_rows >0 ){
  while($row = mysqli_fetch_assoc($result_incom)){          
var_dump($row);
    echo '<form action="conedit.php" method="post">';
    echo '<label>header: </label> <br>';
    echo  '<input type="text"  name="header" value="'.$row["header"].'"> <br>';
    echo '<label>detail: </label>  <br>';
    echo  '<textarea type="text" name="detail" cols="66" rows="10">'.$row["detail"].'</textarea> <br>' ;
    echo '<label>priority:</label><br>';
    echo '<input type="text"  name="type" value="'.$row["type"].'"><br>';
    echo '<input type="date"  name ="time" value="'.$row["time"].'"><br>';
    echo '<input type="hidden" name="note_id" value="'.$row["id"].'">'; 

    echo '<button type="edit" name="editbtn"> submit</button>';
echo '</form> ';

          }
        }
?>

这是更新sql

  

conedit.php

    <?php
session_start();
include ('dbms.php');

if(isset($_POST['editbtn'])){

    $id = $_POST['note_id'];
    $header =$_POST['header'];
    $detail =$_POST['detail'];
    $time =  $_POST['time'];
    $type =  $_POST['type'];

    if($header =="" || $detail == "" || $type =="" ){
      header('Location: home.php?note=failtime');

            exit();
        }
        //$user_id = $_SESSION['id'];
        $sql_comm = 
        "UPDATE tb_note
         SET header = '$header',
         detail ='$detail',
         time = '$time',
         type = '$type',
          WHERE id = '$id';";

    if ($conn->query($sql_comm) === TRUE) {

        header('location: home.php?add=true');
          exit();
    } 
    else {
        echo "Error ".$sql_comm ;
        }
    }
?>

这是错误代码,但我认为sql是正确的。

Connected successfullyError UPDATE tb_note SET header = 'test3', detail ='go bkk', time = '2017-10-20', type = '1', WHERE id = '8';

2 个答案:

答案 0 :(得分:0)

在我看来,正如错误所述,请移除,where之前的update query

答案 1 :(得分:-1)

您没有在更新请求中连接参数,我认为您应该先更改此参数。您还应该检查&#34;准备&#34;,因为在这里您将自己暴露于sql注入。链接here

    $sql_comm = 
    "UPDATE tb_note
     SET header = :header,
     detail = :detail,
     time = :time,
     type = :type,
      WHERE id = :id;";

    $conn->prepare($sql_comm, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY))
    $conn->execute(
         array(
           ':header' => $header,
           ':detail' => $detail,
           ':time' => $time,
           ':type' => $type,
           ':id' => $id
         )
    );