-1

The code in question is like this:

if (isset($_SESSION['logged_in'])) {
    if (isset($_POST['title'], $_POST['content'], $_POST['id'])) {
        $title = $_POST['title'];
        $content = $_POST['content'];
        $id = $_POST['id'];
    }
    if (empty($title) or empty($content) or empty($id)) {
        $error = 'All fields are required!';
    } else {

        try {

            $query = $pdo->prepare("UPDATE articles SET article_title = ?, article_content = ? WHERE article_id = ? ");


            $query->bindValue(1, 'title');
            $query->bindValue(2, 'content');
            $query->bindValue(3, 'id');

            $query->execute();

            header('Location: index.php');
        } catch (PDOException $e) {
            print_r($e->errorInfo);
            die();
        }
    }
}

It gives me no error whatsoever and the table does not update.

P.S. I am quite new to PHP in general so please bear with me if my errors are somewhat trivial, I just don't have anyone else to ask.

4

2 回答 2

2
$query->bindValue(1, 'title'); 
$query->bindValue(2, 'content'); 
$query->bindValue(3, 'id'); 

bindValue 的第二个值应该是,而不是值的名称,例如;

$query->bindValue(1, $title); 
$query->bindValue(2, $content); 
$query->bindValue(3, $id, PDO::PARAM_INT); 
于 2013-03-24T13:09:33.770 回答
2

如果您是 PHP 新手,那么我建议您尝试另一种使用占位符 (?) 执行查询的方法,因为它更简单。

首先设置您的连接。

try {
  # First let us connect to our database 
  $db = new \PDO("mysql:host=localhost;dbname=xx;charset=utf8", "xx", "xx", []); 
 } catch(\PDOException $e){
   echo "Error connecting to mysql: ". $e->getMessage();
 }
 $db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

现在调用准备/执行方法,例如:

$stmt = $db->prepare("
        UPDATE articles 
        SET article_title = ?, article_content = ? 
        WHERE article_id = ?
 ");

 $stmt->execute(array($article_title, $article_content,$article_id));

 if($stmt->rowCount()) {
   echo 'success';
 } else {
   echo 'update failed';
 }
于 2013-03-24T13:12:11.703 回答