未运行MYSQLI PHP的Prepared语句


Prepared statement not running MYSQLI PHP

我一直试图切换到准备好的语句,但我不明白为什么我的新代码不再起作用。我刚开始使用这些,还在学习,但我知道这是安全方面的最佳实践。任何帮助都将不胜感激。非常感谢。

<?php
$servername = "11.11.11.11";
$username = "root";
$password = "root";
$dbname = "sit";
$conn = new mysqli($servername, $username, $password,$dbname);

if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$result = mysqli_query($conn, "SELECT * FROM `ourstory` ");
$values = mysqli_fetch_array($result);

if(isset($_POST['ourstory_title'])){
$ourstory_title = $_POST['ourstory_title'];
$ourstory_testimonial = $_POST['ourstory_testimonial'];
$ourstory_content = $_POST['ourstory_content'];
$ourstory->execute();
$ourstory = $conn->prepare("UPDATE ourstory SET
    ourstory_title='$ourstory_title' ,
    ourstory_content='$ourstory_content' ,
    ourstory_testimonial='$ourstory_testimonial' 
    WHERE  ourstory_id='1'");
$ourstory->bind_param("sss", $ourstory_title, $ourstory_content, $ourstory_testimonial);   


if (mysqli_query($conn, $ourstory)) {
    echo "Record updated successfully";
} else {
    echo "Error updating record: " . mysqli_error($conn);
}   
$ourstory->close();
$conn->close();
}
?>
<form id="comment_form" method="post" 
      action="<?php echo $ourstory?>" 
      onsubmit="setTimeout(function () { 
             window.location.reload(); 
      }, 10), location.reload(true);">
<table width="100%" border="0" cellspacing="1" cellpadding="2">

<tr>
<td width="85%">About Us Title</td>
</tr>
<tr>
<td>
   <input class="commentarea" 
          name="ourstory_title" type="text" 
          id="ourstory_title" value="<?php echo $values['ourstory_title']?>">
</td>
</tr>
<tr>
<td width="85%" >Testimonial</td>
</tr>
<tr>
<td>
   <pre>
     <textarea class="commentarea" 
      name="ourstory_testimonial" type="text" 
      id="ourstory_testimonial" rows= "10" ><?php echo $values['ourstory_testimonial']?>
     </textarea>
   </pre>
</td>
</tr>
<tr>
<td width="85%" >About Us Content</td>
</tr>
<tr>
<td>
  <pre>
    <textarea class="commentarea" name="ourstory_content" 
        type="text" id="ourstory_content"  
         rows= "10" ><?php echo $values['ourstory_content']?>
    </textarea>
  </pre>
 </td>
</tr>

<tr>
<td>
<input type="submit" value="Update">
</td>
</tr>
</table>
</form>

结合Mark的回答,我提交了以下作为补充回答,并使用了我在OP问题下留下的一些评论。

首先,<textarea>没有类型。type="text"删除所有这些。

然后,$ourstory->execute();放错地方了,一旦你使用了Mark的答案,并使用了答案和手册中所述的占位符,它就需要放在$ourstory->bind_param("sss",...之后http://www.php.net/manual/en/mysqli.quickstart.prepared-statements.php

你不应该有if (mysqli_query($conn, $ourstory)) {你想用的是affected_rowshttp://php.net/manual/en/mysqli.affected-rows.php在条件语句中检查查询是否确实成功。


根据您的编辑:https://stackoverflow.com/revisions/31003865/4

printf("Affected rows (UPDATE): %d'n", $ourstory->affected_rows);
$ourstory->execute();

这需要在执行之后进行:

$ourstory->execute();
printf("Affected rows (UPDATE): %d'n", $ourstory->affected_rows);

但我会使用条件if来表示,它应该是连接的变量,即来自手册的:

int $mysqli->affected_rows;

也是如此

printf("Affected rows (UPDATE): %d'n", $conn->affected_rows);
  • http://php.net/manual/en/mysqli.affected-rows.php

手册示例:

<?php
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s'n", mysqli_connect_error());
    exit();
}
/* Insert rows */
$mysqli->query("CREATE TABLE Language SELECT * from CountryLanguage");
printf("Affected rows (INSERT): %d'n", $mysqli->affected_rows);
$ourstory = $conn->prepare("UPDATE ourstory SET ourstory_title='$ourstory_title' ,ourstory_content='$ourstory_content' ,ourstory_testimonial='$ourstory_testimonial' WHERE  ourstory_id='1'");
$ourstory->bind_param("sss", $ourstory_title, $ourstory_content, $ourstory_testimonial);   

如果要将值绑定到准备好的语句,则需要在该查询中设置占位符。。。。不注入值本身,然后尝试绑定它们

$ourstory = $conn->prepare("UPDATE ourstory SET ourstory_title=? ,ourstory_content=? ,ourstory_testimonial=? WHERE  ourstory_id='1'");
$ourstory->bind_param("sss", $ourstory_title, $ourstory_content, $ourstory_testimonial);   

您还可以绑定ourstory_id和的值