PDO/SQL insert not working

seamus

got the following code. All values are gotten through javascript and then sent through ajax. The var_dump($array) at the end works and display all the correct values. So they are all passed through correctly. The catch error for the try method also never pops up. The values are not being inserted into the sql table. Whats wrong? Thanks in advance.

$name = $_GET['name'];
$category = $_GET['category'];
$subCategory = $_GET['subCategory'];
$date = $_GET['date'];
$address = $_GET['address'];
$city = $_GET['city'];
$state = $_GET['state'];
$host = $_GET['host'];
$imagePath = $_GET['imagePath'];
$info = $_GET['info'];

//turn into array
$array = array();
$array[0]=$name;
$array[1]=$category;
$array[2]=$subCategory;
$array[3]=$date;
$array[4]=$address;
$array[5]=$city;
$array[6]=$state;
$array[7]=$host;
$array[8]='j';//$imagePath;
$array[9]=$info;

try {
    $con = new PDO('mysql:host=localhost;dbname=test');

$insert = $con->prepare(" INSERT INTO create 
(name,category,subCategory,date,address,city,state,host,imagePath,info)
VALUES (?,?,?,?,?,?,?,?,?,?) ");
$insert->execute($array);   
}

catch(PDOException $e) { //try
    echo 'error';
   //echo 'ERROR: ' . $e->getMessage();
}

var_dump($array);
jeroen

create is a reserved word in mysql so you need to quote it in backticks:

INSERT INTO `create` ...

To have PDO throw exceptions, you need to add that after you open your connection:

 $con = new PDO('mysql:host=localhost;dbname=test');
 $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

By the way, I assume that you are logging into your database with a username and a password as well (the second and third parameter of the PDO constructor)...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related