PHP PDO不会更新数据库

时间:2014-01-08 11:26:47

标签: php mysql pdo sql-update

我尝试使用PDO,但我得到1,1,1的结果,但数据库不会更新。我不知道我的代码有什么问题。

try {$db = new PDO("mysql:dbname=$db_name;host=$db_host", $db_user, $db_pass );}
catch(PDOException $e){echo $e->getMessage();}

$title_insert = $db->prepare("UPDATE topics SET topic_title = ? WHERE id = ?");
$title_insert->bindParam(1, $id);
$title_insert->bindParam(2, $topic_title);

$tag_insert = $db->prepare("UPDATE topics SET topic_tags = ? WHERE id = ?");
$tag_insert->bindParam(1, $id);
$tag_insert->bindParam(2, $topic_tags);

$story_insert = $db->prepare("UPDATE topics SET topic_story = ? WHERE id = ?");
$story_insert->bindParam(1, $id);
$story_insert->bindParam(2, $topic_story);


$id = 1;
$topic_title = "title";
$topic_tags = "tags";
$topic_story = "story";

$result_title = $title_insert->execute();
echo $sonuc_title.',';

$sonuc_tag = $tag_insert->execute();
echo $sonuc_tag.',';

$result_story = $story_insert->execute();
echo $result_story;

感谢您的回答......

2 个答案:

答案 0 :(得分:2)

您是否尝试过切换参数号?

$title_insert = $db->prepare("UPDATE topics SET topic_title = ? WHERE id = ?");
$title_insert->bindParam(1, $topic_title);
$title_insert->bindParam(2, $id);
祝你好运!

PDO Bind param

答案 1 :(得分:0)

你应该在bind语句之前影响你的变量:

$id = 1;
$topic_title = "title";
$topic_tags = "tags";
$topic_story = "story";

$title_insert = $db->prepare("UPDATE topics SET topic_title = ? WHERE id = ?");
$title_insert->bindParam(1, $id);
$title_insert->bindParam(2, $topic_title);

$tag_insert = $db->prepare("UPDATE topics SET topic_tags = ? WHERE id = ?");
$tag_insert->bindParam(1, $id);
$tag_insert->bindParam(2, $topic_tags);

$story_insert = $db->prepare("UPDATE topics SET topic_story = ? WHERE id = ?");
$story_insert->bindParam(1, $id);
$story_insert->bindParam(2, $topic_story);


$result_title = $title_insert->execute();
echo $sonuc_title.',';

$sonuc_tag = $tag_insert->execute();
echo $sonuc_tag.',';

$result_story = $story_insert->execute();
echo $result_story;