www.webdeveloper.com
Results 1 to 8 of 8

Thread: PDO - Escaping characters on INSERT query

  1. #1
    Join Date
    Jun 2013
    Posts
    6

    PDO - Escaping characters on INSERT query

    Help!

    I am using PDO. When I run my INSERT query everything works great! However, if there is a single quote used in one of the input text fields it will not.

    I am a NOOB, so speaking like an expert will not help me. Think 1st grader. HA.

    Here is the code:

    $query = ("INSERT INTO missionaries
    (firstname, lastname, spouse, image, info, continent, field)
    VALUES ('$miss_fn', '$miss_ln', '$miss_spn', '$image', '$info', '$continent', '$field')");
    if ($db->exec($query)){
    //Success
    header('Location: content.php');
    exit;
    } else {
    //Display error message
    echo '<p> Missionary addition failed.</p>';
    }


    Any help is greatly appreciated.

  2. #2
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    640
    Where you say "display error message" add this:
    PHP Code:
       $pdo_errinfo $db->ErrorInfo();
       
    $err $pdo_errinfo[2];
       echo 
    "Error message is: $err"
    That will tell you about your query error

    Also - you don't need parens around the query statement.

  3. #3
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    640
    Of course - if you have quotes in your data, then you will have a problem. Read up on how to sanitize data before putting it into your db.

  4. #4
    Join Date
    Jun 2013
    Posts
    6
    Thanks for the reply.

    Just before I got your reply a friend pointed me to this video and it worked perfectly: http://www.youtube.com/watch?v=CDa3Z52scW0

    Problem solved.

  5. #5
    Join Date
    Jun 2013
    Posts
    6
    Oh, this is the new code in case interested

    $q = ("INSERT INTO missionaries
    (firstname, lastname, spouse, image, info, continent, field)
    VALUES (:miss_fn, :miss_ln, :miss_spn, :image, :info, :continent, :field)");
    $query = $db->prepare($q);
    $results = $query->execute(array(
    ":miss_fn" => $miss_fn, ":miss_ln" => $miss_ln, ":miss_spn" => $miss_spn,
    ":image" => $image, ":info" => $info, ":continent" => $continent,
    ":field" => $field
    ));


    if ($results) {
    //Success
    header('Location: addMiss.php');
    exit;
    } else {
    //Display error message
    echo '<p> Missionary addition failed.</p>';
    }
    }
    else {echo 'Must enter First and Last Name.' . '<br/>';
    echo 'Click Back button to try again.';
    }

  6. #6
    Join Date
    Jul 2013
    Posts
    31

    PDO - Escaping characters on INSERT query

    If you are using PDO, always make use of prepared statements.
    Your code should be like this :
    PHP Code:
    //assuming $db is the PDO connection variable
    $query $db->prepare("INSERT INTO missionaries
    (firstname, lastname, spouse, image, info, continent, field)
    VALUES (?,?,?,?,?,?,?)"
    );
    if (
    $query->execute(array($miss_fn$miss_ln,$miss_spn$image$info$continent$field)){
    //Success
    header('Location: content.php');
    exit;
    } else {
    //Display error message
    echo '<p> Missionary addition failed.</p>';


  7. #7
    Join Date
    Feb 2013
    Posts
    86
    He's using client-side prepared statements:

    PHP Code:
    $query = ("INSERT INTO 
                   missionaries (firstname, lastname, spouse, image, info, continent, field)
               VALUES 
                   (:miss_fn, :miss_ln, :miss_spn, :image, :info, :continent, :field)"
    );
    $stmt $db->prepare($query);
    $result $stmt->execute(array(
        
    ":miss_fn" => $miss_fn,
        
    ":miss_ln" => $miss_ln,
        
    ":miss_spn" => $miss_spn,
        
    ":image" => $image
        
    ":info" => $info,
        
    ":continent" => $continent,
        
    ":field" => $field
            
    ));


    if (
    $result) {
    //Success
        
    header('Location: addMiss.php');
        exit;
    } else {
    //Display error message
        
    echo '<p> Missionary addition failed.</p>';

    Last edited by Strider64; 07-10-2013 at 10:07 AM.

  8. #8
    Join Date
    Jun 2013
    Posts
    6
    Yes, that seemed to best suit me; but thanks for the other example. One problem solved....now on to the next. I love learning!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles