[英]Mysql update statement won't execute
Can somebody help me with this? 有人可以帮我吗? When i'm trying to update my table nothing happens and I can't figure out what's wrong.
当我尝试更新我的表时,什么也没有发生,而且我无法弄清楚出了什么问题。 I've tried different query's but not 1 worked.
我试过不同的查询,但没有1工作。
My code: http://pastebin.com/8zDpm0Ah it doesn't work from line 23 我的代码: http : //pastebin.com/8zDpm0Ah从第23行开始不起作用
<?php
foreach($db->query("SELECT * FROM blog WHERE ID LIKE '$ID'") as $row){
echo "Titel: <input type='text' size='50' name='titel' value='$row[titel]'><br>";
echo "Post:<br>";
echo "<textarea name='editblog'>";
echo $row[post];
echo "</textarea>";
echo "<input type='hidden' name='ID' value='";
echo $row[ID];
echo "'>";
}
?>
<input type="submit" value="Edit post" name="postedit">
</form>
<script>
CKEDITOR.replace( 'editblog' );
</script>
<?php
if(isset($_POST['postedit'])){
$titel = $_POST['titel'];
$post = $_POST['editblog'];
$ID = $_POST['ID'];
$STH = $db->prepare("UPDATE blog SET titel='$titel',post='$post' WHERE ID=$ID");
$STH->execute();
echo "<h2>Post edited!</h2>";
}
If you aren't using prepared statements as they were intended (using $db->bind_params()
), then I recommend switching line 29 to 如果您没有按预期使用准备好的语句(使用
$db->bind_params()
),则建议将第29行切换为
$STH = $db->query("UPDATE blog SET titel='$titel',post='$post' WHERE ID=$ID");
and removing line 30. 并删除第30行。
You are not at all using parameterized queries, switching to PDO, does not mean only changing mysql_query()
to $db->prepare()
It is a little bit more than that. 您根本没有使用参数化查询,而是切换到PDO,这并不意味着仅将
mysql_query()
更改为$db->prepare()
这还不止于此。 while using PDO, you can't place every variable in your query, I am talking about this code: 使用PDO时,您不能在查询中放置每个变量,我在谈论以下代码:
$STH = $db->prepare("UPDATE blog SET titel='$titel',post='$post' WHERE ID=$ID");
$STH->execute();
The values, $title, $post and $id
need to be replaced as ?
需要将值
$title, $post and $id
替换为?
in your query, and later can be executed. 在您的查询中,以后可以执行。
So, try the following.: 因此,请尝试以下操作:
if(isset($_POST['postedit'])) {
$title = $_POST['titel'];
$post = $_POST['editblog'];
$id = $_POST['ID'];
try {
$STH = $db->prepare("UPDATE blog SET titel = ?, post = ? WHERE ID = ? ");
$STH->execute(array($title, $post, $id));
} catch(PDOExeption $e){
echo 'The error is: '. $e->getMessage();
}
if($STH->rowCount() > 0){
echo 'data updated successfuly';
}else{
echo 'data updating failed';
}
}
Debugging: 调试:
<?php
$title = 'I am post title';
$post = 'I am POST';
$id = '17';
try {
new PDO("mysql:host=localhost;dbname=addictus_db", "addictus_admin", "dikketetten");
$STH = $db->prepare("UPDATE blog SET titel = ?, post = ? WHERE ID = ? ");
$STH->execute(array($title, $post, $id));
} catch(PDOExeption $e){
echo 'The error is: '. $e->getMessage();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.