SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem with PDO array insert

    I am trying to make an insert into my DB and checking if the $_POST global isset but do not seem to have any luck get an insert to work.
    It will work if there is only one field and checking one field, but I want more than one.

    Can you see what is wrong with my code please.
    PHP Code:
    if (isset($_POST['name'])&& ($_POST['email']) && ($_POST['message'])){
        try
        {
            
    $pdo = new PDO('mysql:host=localhost;dbname=chairkit'$user,$pass);
            
    $pdo->setAttribute(PDO::ATTR_ERRMODEPDO::ERRMODE_EXCEPTION);
            
    $pdo->exec('SET NAMES "utf8"');
        }
        catch (
    PDOException $e)
        {
            
    $output 'unable to connect to server'$e->getMessage();
            include 
    './connect/output.html.php';
            exit();
        }

        
    // get connection to DB
        
    try {

            
    $sql "INSERT INTO comments (name,email,comment) VALUES (:name,:email,:comment)";
            
    $s $pdo->prepare($sql);
            
    $s->execute(array(':name'=>$_POST['name'],
                
    ':email'=>$_POST['email'],
                
    ':comment'=>$_POST['comment']));
        } catch (
    PDOException $e)
        {
            
    $error 'Error inserting comments ' $e->getMessage();
            include 
    './listcomments/error.html.php';
        }
        
    header('Location: thankyou.html');
        exit();


    Last edited by SpacePhoenix; Sep 29, 2013 at 15:58. Reason: placed php tags around php code

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,077
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Change this line:
    PHP Code:
    if (isset($_POST['name'])&& ($_POST['email']) && ($_POST['message'])){ 
    to:

    PHP Code:
    if ((isset($_POST['name'])) && (isset($_POST['email'])) && (isset($_POST['message']))) { 
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    SitePoint Enthusiast
    Join Date
    Jun 2008
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks I was thinking that when I was in bed...

    Quote Originally Posted by SpacePhoenix View Post
    Change this line:
    PHP Code:
    if (isset($_POST['name'])&& ($_POST['email']) && ($_POST['message'])){ 
    to:

    PHP Code:
    if ((isset($_POST['name'])) && (isset($_POST['email'])) && (isset($_POST['message']))) { 


Tags for this Thread

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
  •