0

I'm at a loss here. I'm trying to insert a row into a table with PDO, but it simply doesn't work. I always do it this way with other tables and it never gave any problems! Maybe it has something to do with the table... Anwyway, my script looks like this:

<?php

// Connect to DB.
$hostname = "localhost";
$database = "website";
$username = "leon";
$password = "B6T8WGfs";

try
{
    $connect = new PDO("mysql:host=$hostname; dbname=$database; charset=utf8", $username, $password) or die("Kan geen verbinding maken met database!");
}
catch (Exception $e)
{
    throw new Exception( 'Error connecting to database: ', 0, $e);
}

#### Prepare variables for PDO.
$user_id=28; // will change this later
$university=$_POST['university'];
$name=$_POST['name'];
$email=$_POST['email'];
$message=$_POST['message'];
$order_items=$_POST['message']; // will change this later
$sent='false';

#### Add order to database.
try {
    $add_order=$connect->prepare("INSERT INTO orders(user_id, university, name, email, message, order_items, sent)VALUES(:user_id, :university, :name, :email, :message, :order_items, :sent)");
    $add_order->execute(array(':user_id'=>$user_id,
                                    ':university'=>$university,
                                    ':name'=>$name,
                                    ':email'=>$email,
                                    ':message'=>$message,                               
                                    ':order_items'=>$order_items,                               
                                    ':sent'=>$sent));
    echo "Success";
}
catch(PDOException $ex) {
    echo "An Error occured!"; //user friendly message
    echo ($ex->getMessage());
}

$connect = null;
?>

It seems to work, because the output on the page says "Success". But in fact, the row is not added to the table. My MYSQL 'orders' table looks like this:

PHPMyAdmin Table orders

NOTE: In the Browse (Verkennen) tab there's one row, that I entered manually to test whether it was possible to enter something in the table at all.

Any help would be much appreciated!

TheLeonKing
  • 3,501
  • 7
  • 32
  • 44

2 Answers2

0

I tested your code and it worked like a charm. The only thing I can think of that could be at play here is, that your form elements may not be named.

I.e.: <input type="text" name="university">

If your form doesn't resemble what I have below and are not named, then that is the problem.

I duplicated your table schema/info to a T with success.

<form action="db_insert.php" method="post">
University: 
<input type="text" name="university">
<br>
Name: 
<input type="text" name="name">
<br>
Email: 
<input type="text" name="email">
<br>
Message: 
<input type="text" name="message">
<br>
Orders time: 
<input type="text" name="orders_items">
<br>
<input type="submit" name="submit" value="Submit">
</form>

Footnotes:

I changed the following in my test, just so you know, in order to differentiate from both form elements:

$order_items=$_POST['message']; // will change this later

to:

$order_items=$_POST['orders_items'];

to go with:

<input type="text" name="orders_items">
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

your script will always echo 'success' since it is within the try{} brackets. move it there:

try {
    $add_order=$connect->prepare("INSERT INTO orders(user_id, university, name, email, message, order_items, sent)VALUES(:user_id, :university, :name, :email, :message, :order_items, :sent)");
    $add_order->execute(array(':user_id'=>$user_id,
                                    ':university'=>$university,
                                    ':name'=>$name,
                                    ':email'=>$email,
                                    ':message'=>$message,                               
                                    ':order_items'=>$order_items,                               
                                    ':sent'=>$sent));

}
catch(PDOException $ex) {
    echo "An Error occured!"; //user friendly message
    echo ($ex->getMessage());
    exit; // so that your script will stop if there's an error.
}
echo "Success"; // here

there. if catch does something, success will not echo, else, your query worked