SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: PDO statement

  1. #1
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PDO statement

    can any one spot the syntax issues here i can not see it
    PHP Code:
    // Insert Details to log
        
    $sth2 $db->prepare ("INSERT INTO logs (id,to,from,message) VALUES (:id,:to,:from,:message)");
        
    $sth2->bindParam(':id'$idPDO::PARAM_INT);
        
    $sth2->bindParam(':to'$toPDO::PARAM_INT);
        
    $sth2->bindParam(':from'$fromPDO::PARAM_INT);
        
    $sth2->bindParam(':message'$messagePDO::PARAM_INT);
        
    $sth2->execute(); 

    error i am getting
    Code:
    Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'to,from,message) VALUES ('1','4477777777','4477777777','test')'

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,827
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    Why would to, from, and message be integers? I think those would be PDO::PARAM_STR

    Second, put it in a try/catch and see what kicks out
    PHP Code:
    try {
    // Insert Details to log 
        
    $sth2 $db->prepare ("INSERT INTO logs (id,to,from,message) VALUES (:id,:to,:from,:message)"); 
        
    $sth2->bindParam(':id'$idPDO::PARAM_INT); 
        
    $sth2->bindParam(':to'$toPDO::PARAM_INT); 
        
    $sth2->bindParam(':from'$fromPDO::PARAM_INT); 
        
    $sth2->bindParam(':message'$messagePDO::PARAM_STR); 
        
    $sth2->execute();  
    } catch (
    PDOException $exception) {
        
    var_dump($exception);

    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  3. #3
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    again it's thrown up errors
    Code:
    Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'to,from,message) VALUES ('1','44773345345','447736545','test')' at line 1' in /var/zpanel/hostdata/zadmin/public_html/login/log.php:71
    Stack trace:
    #0 /var/zpanel/hostdata/zadmin/public_html/login/spoof.php(71): PDOStatement->execute()
    #1 {main}
      thrown in /var/zpanel/hostdata/zadmin/public_html/login/spoof.php on line 71

  4. #4
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,827
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    Post the table design for logs, is id an auto-increment column?
    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  5. #5
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)


    id isnt a auto as it's the id number of the user.

    the table is for logging messages

  6. #6
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    4,827
    Mentioned
    142 Post(s)
    Tagged
    0 Thread(s)
    TO and FROM are keywords in MySQL

    Try this:
    PHP Code:
    try {
    // Insert Details to log 
        
    $sth2 $db->prepare ("INSERT INTO logs (id,`to`,`from`,message) VALUES (:id,:to,:from,:message)"); 
        
    $sth2->bindParam(':id'$idPDO::PARAM_INT); 
        
    $sth2->bindParam(':to'$toPDO::PARAM_INT); 
        
    $sth2->bindParam(':from'$fromPDO::PARAM_INT); 
        
    $sth2->bindParam(':message'$messagePDO::PARAM_STR); 
        
    $sth2->execute();  
    } catch (
    PDOException $exception) {
        
    var_dump($exception);

    Be sure to congratulate xMog on earning April's Member of the Month
    Go ahead and blame me, I still won't lose any sleep over it
    My Blog | My Technical Notes

  7. #7
    SitePoint Zealot txt3rob's Avatar
    Join Date
    Jul 2013
    Location
    Liverpool UK
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    that worked spot on thank you very much for that!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •