-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.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Robert
  • 21
  • 1
  • 3
  • Refer : http://stackoverflow.com/questions/9209677/mysql-update-using-pdo-and-prepared-statement-not-working – Arvind Mar 24 '13 at 13:06
  • What does the error say? – samayo Mar 24 '13 at 13:07
  • What tutorial you are trying to follow to? – Your Common Sense Mar 24 '13 at 13:28
  • It was a CMS tutorial youtube by phpacademy, i tried to change quite a lot of parts, and encountered some errors along the way. – Robert Mar 24 '13 at 13:35
  • 1. Thanks for adding your solution. However I've rolled it back, since was identical to the accepted answer aside from some minor variable changes. In future if you have a solution that is quite different to any of the answers given, I suggest adding it as an answer, to make it easier for future readers of your question. 2. If you're following a tutorial, it's worth hyperlinking it, in case anyone wishes to see where you may have gone wrong. – halfer Mar 24 '13 at 14:04
  • 2
    i don't see how the question is "too localized". this is the closest thing i've been able to find about what i need to do with SQL – oldboy Aug 07 '17 at 04:35
  • Is it a possible duplicate, as indicated in the 1st comment? – SherylHohman Nov 27 '19 at 02:30

2 Answers2

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

The second value to bindValue should be the value, not the name of the value, something like;

$query->bindValue(1, $title); 
$query->bindValue(2, $content); 
$query->bindValue(3, $id, PDO::PARAM_INT); 
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
2

If you are new to PHP then I would suggest you try an alternative way of executing queries with placeholders (?) since it is much simpler.

First set up your connection.

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 );

Now call prepare/execute methods like:

$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';
 }
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
samayo
  • 16,163
  • 12
  • 91
  • 106
  • This gives me these errors: Notice: Use of undefined constant article_title - assumed 'article_title' on all the variables – Robert Mar 24 '13 at 13:23
  • @Robert Very sorry, I forgot `$` before the variables, I have updated the answer now – samayo Mar 24 '13 at 13:26