[英]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();
如果放错了位置,则需要使用$ourstory->bind_param("sss",...
使用了Mark的答案并按照答案中所述以及从手册http://www.php.net/使用占位符后, 手动/ EN / mysqli.quickstart.prepared-statements.php
您不应该在有条件的语句中使用if (mysqli_query($conn, $ourstory)) {
您要使用的是affected_rows
http://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);
手册中的示例:
<?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
的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.