0

I'm at best a hobbyist who is just now bringing to use PDO and i'm have a heck of a time with insert statements. I have a function which is meant to update several columns in a table but my data does not seem to be binding correctly? I've looked over google, php.net, http://wiki.hashphp.org, stack overflow itself, and the sql site, but i've not found a solid example i can draw an apples to apples comparison too on how to do this. I've seen lots of examples where they update a single column, but none showing how to update a couple columns and i'm just not able to figure it out.

hopefully someone out there can help me and my flu soaked brain figure this out.

MY GITHUB: https://github.com/monkeework/WrDK My SCRIPT: https://github.com/monkeework/WrDK/blob/master/library/timeline.php

MY FUNCTION:

function timelineRevise(){
#TimelineID & EntryID passed as hidden post values from timelineEdit() function
/*
     <input type="hidden" name="EntryID" value="' . dbOut($row['TimelineID']) . '" />
    <input type="hidden" name="EntryID" value="' . dbOut($row['EntryID']) . '" />
*/
$TimelineID     = strip_tags($_POST['TimelineID']);    #int - primaryKey
$EntryID            = strip_tags($_POST['EntryID']);           #int
$EntryTitle     = strip_tags($_POST['EntryTitle']);        #str
$EntryDate          = strip_tags($_POST['EntryDate']);         #str - entered by user
$EntryDescription   = strip_tags($_POST['EntryDescription']); #str
$CharTag            = strip_tags($_POST['CharTag']);          #str of comma sep numbers


$db = pdo(); # pdo() creates and returns a PDO object

//dumpDie($FirstName);

$sql = "
    UPDATE ma_Timeline

    SET
    TimelineID              = :timeline_id, #PrimeKey
    EntryID                 = :entry_id,
    EntryTitle              = :entry_title,
    EntryDate               = :entry_date,
    EntryDescription        = :entry_desc,
    CharTag                 = :char_tag

    WHERE EntryID           = :entry_id";

$stmt = $db->prepare($sql);

//The Primary Key of the row that we want to update.
$stmt->bindValue(':timeline_id',  $TimelineID,              PDO::PARAM_STR); #INT - PrimeKey
$stmt->bindValue(':EntryID',      $EntryID,                   PDO::PARAM_INT);

$stmt->bindValue(':entry_title',  $EntryTitle,              PDO::PARAM_STR);
$stmt->bindValue(':entry_date',   $EntryDate,                   PDO::PARAM_STR);
$stmt->bindValue(':entry_desc',   $EntryDescription,            PDO::PARAM_STR);
$stmt->bindValue(':char_tag',     $CharTag,                 PDO::PARAM_STR);
//INTEGER EXAMPLE $stmt->bindValue(1, $id, PDO::PARAM_INT);


try {$stmt->execute();} catch(PDOException $ex) {trigger_error($ex->getMessage(), E_USER_ERROR);}
#feedback success or failure of update

if ($stmt->rowCount() > 0)
{//success!  provide feedback, chance to change another!
    feedback("Event Revised Successfully!","success");
}else{//Problem!  Provide feedback!
    feedback("Event NOT REVISED!","warning");
}

myRedirect(THIS_PAGE);

}

Chezshire
  • 713
  • 5
  • 13
  • 32
  • 2
    Please, take a look at [How to squeeze error message out of PDO?](http://stackoverflow.com/questions/3726505/how-to-squeeze-error-message-out-of-pdo) – FirstOne Jul 17 '16 at 00:29
  • I attempted to use that, and i got a lot of errors. I'm still working procedurally. I'm just getting into classes and PDO. First trying to learn PDO, then will be doing classes. So I think the syntax of what your suggested causes lots of errors for me that don't help unfortunately :( – Chezshire Jul 17 '16 at 00:46
  • 1
    have you looked at the sql query log to see the query statement 'as received' and processed by mysql ? you never know, the underlying error might be more obvious than without. – YvesLeBorg Jul 17 '16 at 01:12
  • 2
    Threre are two fields EntryID and entry_id binding with same parameter.Pls Clearly specify what u wan – Pradeep Jul 17 '16 at 01:16
  • see you [query log](http://stackoverflow.com/a/38394479) – Drew Jul 17 '16 at 01:18
  • 1
    just a wag, maybe PDO has issue with the embedded comment `#PrimeKey` – YvesLeBorg Jul 17 '16 at 01:20
  • 1
    ya Your Query : $sql ="UPDATE ma_Timeline SET TimelineID = :timeline_id, EntryTitle = :entry_title, EntryDate = :entry_date, EntryDescription = :entry_desc, CharTag = :char_tag WHERE EntryID = :EntryID"; – Pradeep Jul 17 '16 at 01:22
  • The issue happened before i used any comments to track what i was doing (such as '#PrimeKey' which is a not to me as to what it is). And I added the Entry_ID in again as i thought maybe i needed to so i could inject the data into the entry that matches EntryID 22. – Chezshire Jul 17 '16 at 01:23
  • 1
    add this line : $stmt->bindValue(':EntryID', $EntryID, PDO::PARAM_INT); – Pradeep Jul 17 '16 at 01:25
  • It took me a few minutes to find the LOG (Didn't know i had one honestly). The error on last test lists as/shows as '[Jul-16-2016 18:37:18] SQLSTATE[HY093]: Invalid parameter number in /Applications/AMPPS/www/WrDK/library/timeline.php on line 690' – Chezshire Jul 17 '16 at 01:38
  • 1
    you just want to update the table based on entry_id or something else pls specify – Pradeep Jul 17 '16 at 01:44
  • I've updated my function to reflect how the code now looks AND pushed the file to my github repository as well which is public. ---- My goal is to Update EntryTitle, EntryDescription, CharTag WHERE the EntryID = MyEntryID -- i've no idea how to hand that into the PDO statement what so every. in mySqli i would just do '....Where EntryID = $myID;' – Chezshire Jul 17 '16 at 01:51
  • You can see a semi-working example of how it renders here: http://marvel-adventures.com/library/timeline.php *you'd have to be logged in to be able edit it. but i want to let someone who is logged in update an entry in the time line IF they make a typo or something. – Chezshire Jul 17 '16 at 01:54
  • 1
    remove this #PrimeKey too – Pradeep Jul 17 '16 at 02:01
  • I removed the #PrimeKey notation as asked; Error reads after testing: Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined' in /Applications/AMPPS/www/WrDK/library/timeline.php on line 681. --- Line #681 ' $stmt->bindValue(':EntryID', $EntryID, PDO::PARAM_INT);' -- not sure what to do or what this means. – Chezshire Jul 17 '16 at 02:13
  • updated my answer copy and map them with your array variable and try : Same name is for avoiding mapping of variable put all as the name of the table field so no confliction no worries to map them – Pradeep Jul 17 '16 at 02:33

0 Answers0