1

I've seen so many tutorials with so many different ways to insert using PDO. None of them seem to work for me. Can't seem to get mine to send to the database. I have no issue connecting and retreiving the data using FETCH but can't seem to post this data.

Any help with getting my post to work and redirect using the header or meta refresh would be nice. I am $_POST from an html form. Connecting to the db works just fine but can't get the data in.

   $hostdb = 'myremoteip';
   $namedb = 'cpdemo';
   $userdb = 'root';
   $passdb = 'mypassword';

   $conn = new PDO("mysql:host=$hostdb; dbname=$namedb", $userdb, $passdb);

   if(isset($_POST['fname'])) {

         $fname = $_POST['fname'];
         $lname = $_POST['lname'];
         $title = $_POST['title'];
         $photo = $_POST['photo'];

   $stmt = "INSERT INTO row_users (fname,lname,title,photo) 
         VALUES (:first,:last,:title,:photo)";

   $q = $conn->prepare($stmt);
   $results = $q->execute(array(
        ":first"=>$fname,
        ":last"=>$lname,
        ":title"=>$title,
        ":photo"=>$photo
    ));
    echo 'User Added<br/>';
    }

      header ('Location:../insertUser.html');
    exit();
MizAkita
  • 1,115
  • 5
  • 21
  • 52
  • 1
    Here goes the only tutorial you really need: http://stackoverflow.com/tags/pdo/info – Your Common Sense Apr 10 '14 at 06:10
  • Or, more specific to your problem: http://stackoverflow.com/questions/15990857/reference-frequently-asked-questions-about-pdo#15990858 – Your Common Sense Apr 10 '14 at 06:10
  • Your Common Sense... great tut on PDO. Im completely new to PDO prepared statements so this looks very beneficial. Im going to give it a try now... – MizAkita Apr 10 '14 at 06:26
  • What you need at the moment is error reporting. As your code seems okay - so, there can be some issue with database. Or, even if there is any issue with code - it is WAY simpler to find it, guided by error message – Your Common Sense Apr 10 '14 at 06:28
  • So would you suggest adding: (PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION) to my connection? it is connecting but not inserting. – MizAkita Apr 10 '14 at 06:33
  • i actually did have: catch(PDOException $e) { echo $e->getMessage(); } but there is no error or anything and the header doesnt even execute. – MizAkita Apr 10 '14 at 06:34
  • please follow either tutorial. Yes, you need PDO::ATTR_ERRMODE set. No, you don't need catch. You also have to be able to see PHP errors in general – Your Common Sense Apr 10 '14 at 06:42
  • ok got it. let me give it a try... – MizAkita Apr 10 '14 at 06:42
  • @MizAkita I tested your code "as is", and it worked fine. The only thing I can tell that could be the issue is, that your insert won't happen unless it meets the conditional statement you've set `if(isset($_POST['fname']))` --- Check to see if your HTML form's elements are indeed named? I.e. `` etc. If one of those are not not named or has a typo, then your whole query will fail. Show us the HTML form that you're using in your question in order to rule anything out. – Funk Forty Niner Apr 10 '14 at 14:00
  • @Fred-ii- can you submit your response as an answer. Changing if(isset( to $_POST['first'] did it... my form now sends. – MizAkita Apr 10 '14 at 21:49
  • oh, and changing the POST values to my input names did it as well. what i had listed were my column names. Thanks @Fred-ii- – MizAkita Apr 10 '14 at 21:49
  • @MizAkita I posted it below. Am glad the matter got resolved. – Funk Forty Niner Apr 10 '14 at 23:06

3 Answers3

2

What you have to understand that there is no such thing like "PDO Insert Into DB"

There is INSERT query, irrelevant to PDO but regular to database you are using.
And there is PDO prepared statement, irrelevant to query type. You have to follow exactly the same pattern, no matter if it insert or delete.

So - all you need is just a tutorial on PDO prepared statements. That's all. Preferably one that teach you to enable error reporting in the first place.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
1

As requested by OP, comment leading to an answer (to close the question and marked as solved).

I tested your code "as is", and it worked fine.

The only thing I can tell that could be the issue is, that your insert won't happen unless it meets the conditional statement you've set if(isset($_POST['fname']))

Check to see if your HTML form's elements are indeed named?

I.e. <input type="text" name="fname"> etc. If one of those are not not named or has a typo, then your whole query will fail.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

You can try binding parameter before passing it to execute, like for example in the below code

<?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);

// insert one row
$name = 'one';
$value = 1;
$stmt->execute();

// insert another row with different values
$name = 'two';
$value = 2;
$stmt->execute();
?>
opensource-developer
  • 2,826
  • 4
  • 38
  • 88