简体   繁体   中英

MySQL syntax error 1064 with PDO UPDATE statement (with named or unnamed placeholders) returning WSOD

I'm well aware the fact that receiving the #1064 error points to invalid MySQL syntax, but the situation I find myself in hasn't been resolved by incessant trips to Manual-land, or the perusal of both SO (the PDO wiki in particular as well as the massive raft of related questions) and Google for the last five days. The problem being this PDO UPDATE statement;
$stm = $pdo->prepare( "UPDATE articles SET title=?, article_text=?, category=? WHERE article_id=? AND user_id=?" );

When I run this statement (just the UPDATE part) on my PMA workbench on my live server, it returns the 1064 - which is only successfully resolved by single-quoting ( ' ) the unnamed placeholders ( COMPLETELY WRONG! ). As well, whether I bindParam/bindValue or lazily bind by passing parameters to execute , I always am returned the WSOD . Even if I try to execute with the unnamed placeholders surrounded by ' , there is no error reported whatsoever (changed to named placeholders, but still same result).

I have error checking enabled for both PDO (in my db.php file - included at the top of the query page) - PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION (in the $options array) AND PHP -

`ini_set('display_startup_errors',1);
 ini_set('display_errors',1);
 error_reporting(-1);` 

The reason this is so vexing is that it's modeled on a currently-used query (works each time run) living on the same live server hosting my domain. I'm keeping the query as simple as possible, but I just can't seem to get beyond non-update of the db by the query as well as the continual blank output to the browser. I'm tempted at this point to use ON DUPLICATE KEY UPDATE , but why should I go crazy like that when there is no conceivable reason why this simple PDO update won't add my edit info? Here's what I tried to get up to the WSOD ;

1st TRY (Original Query Model)

 if($action == 'save changes') {  
 $sql = "UPDATE articles SET title=?, article_text=?, category=? WHERE article_id=?";                   
 $q = $pdo->prepare($sql);    
 $q->execute(array($title,$article_text,$category,$article_id));  
 //If successful, redirect  
 if($sql){  
 header('Location: pending.php');exit;    
 } else {  
 echo '<a href="javascript: history.go(-1)"><font color="#0000CC" face="arial black">No record added. Return to form, please!</font></a>';  
           }}

RESULT? WSOD

2nd Try (with bindParam)

 if($action == 'save changes') {  
 $stmt = $pdo->prepare('UPDATE articles SET title = :title, article_text = :article_text, `category` = :category, WHERE article_id = :article_id');                                      
  $stmt->bindParam(':title', $title, PDO_PARAM_STR);  
  $stmt->bindParam(':article_text', $article_text, PDO_PARAM_STR);  
  $stmt->bindParam(':category', $category, PDO_PARAM_STR);  
  $stmt->bindParam(':article_id', $article_id, PDO_PARAM_INT);  
  return $stmt->execute();`  
  }

RESULT? WSOD

For anyone wondering, all variables used were declared directly before there usage (eliminating all potential Undefined Index errors). I made a few more tweaks in a few more successive attempts, but I haven't been able to move beyond this sticking point. I'm still kind of aghast that the PMA let me run the query with the single-quoted unnamed placeholders. Anyway, I'm still with the syntax error which is said by the error info to start after the equal to sign ( UPDATE articles SET title`= ). What am I doing wrong or missing? Thanks to any enlightenment anyone can provide by way of links or theory that'll help me understand this situation to bring resolution to this strange problem. Any other information deemed to be relevant to this question will be provided if needed.

After countless tweakage runs, I finally was able to obtain my desired goal;

    $article_id = (isset($_POST['article_id'])) ? $_POST['article_id'] : '';
    $title = (isset($_POST['title'])) ? $_POST['title'] : '';
    $article_text = (isset($_POST['article_text'])) ? $_POST['article_text'] : '';
    $category = (isset($_POST['category'])) ? $_POST['category'] : '';  

    $sql = 'UPDATE `articles` SET `title`=?, `article_text`=?, `category`=? WHERE `article_id`=?';              
    $q = $pdo->prepare($sql);
    $q->execute(array($title,$article_text,$category,$article_id));
            //If successful redirect to display database update details
            if($sql){
    header('Location: view_article.php?id='.intval($_POST['article_id']));
    exit;
    }                 
    else{
    echo '<a href="javascript: history.go(-1)"><font color="#0000CC" face="arial black">No record added. Return to form, please!</font></a>';
          } 
    break;

SO happy to report that while posting this answer, tried it 3 consecutive times with the desired results. That the member can now be taken directly to the view of their content update. I knew it couldn't be THAT difficult. Just had to rearrange some of the code as far as the execution flow. Am actually beginning to enjoy working with PDO.

IMPORTANT EDIT The crux of my problem had to do with how I was having the $action variable operate - in my edit form, it was actually relegated as a hidden input. Once I decided to actually move the variable attribute to the submit button;
<input type="submit" name="action" value="Save Changes" />
THEN everything dropped into place and allowed the database to update effortlessly. Once again, a prime example of how Human Error contributes to unwanted and unexpected code outcomes (lol!).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM