1

My Query:

$query = 'INSERT INTO ptd_users (username,contact,email,longitude,latitude,state,city,address) values (?,?,?,?,?,?,?,?)';
$q = $conn->prepare($query);
$q->execute($_POST['user']);

Result of print_r($_POST[user]) :

Array ( [name] => marc [contact] => 123456789 [email] => [email protected] [longitude] => 12.3786085 [latitude] => 96.6126145 [state_select] => Arizona [city_select] => sussex [address] => address details ) 

I'm getting the following error while executing the query:

PDOException: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined

6
  • 2
    Try $q->execute(array($_POST['user']['name'], $_POST['user']['contact'], ..., $_POST['user']['address'])); Replace this ... with the rest of your $_POST :) Commented Feb 27, 2015 at 11:13
  • 2
    Thats the same array as $_POST['user'] Commented Feb 27, 2015 at 11:14
  • Look over a tutorial for mysqli Commented Feb 27, 2015 at 11:16
  • @lmarcelocc: wouldn't that be a lot painful in case one has a large amount of post vars? Commented Feb 27, 2015 at 11:27
  • 2
    @popla, yes it is! But you can conctruct an array and then sending it in the $q->execute($myArray); method. And, if your expected some other types then just strings, its a good pratice to bind them like $conn->bindParam(2, $_POST['user']['contact'],PDO::PARAM_INT); But @Rizier123 example gives you a better perspective :) Commented Feb 27, 2015 at 11:41

1 Answer 1

2

You only bind 1 parameter to 8 placeholders, so that is not going to work. Now you have 3 ways to solve this:

1. Access the single array elements and bind them like this:

$q->execute($_POST['user']['name'], $_POST['user']['contact'], $_POST['user']['email'], $_POST['user']['longitude'], $_POST['user']['latitude'], $_POST['user']['state_select'], $_POST['user']['city_select'], $_POST['user']['address'] );

2. Use placeholders with names like this:

$query = "INSERT INTO ptd_users (username,contact,email,longitude,latitude,state,city,address) values (:name, :contact, :email, :longitude, :latitude, :state_select, :city_select, :address)";

And then you can use your associative array like this:

$q->execute($_POST['user']);

3. Use Positional placeholders like this:

$query = 'INSERT INTO ptd_users (username,contact,email,longitude,latitude,state,city,address) values (?,?,?,?,?,?,?,?)';

And then you can change your associative array to numeric array like this:

$q->execute(array_values($_POST['user']));
Sign up to request clarification or add additional context in comments.

7 Comments

What's wrong with un-named placeholders like "(?,?,?,?,?,?,?,?)" ?
@popla Nothing, but If you use a associative array how should it know which array element should be binded with which placeholder?! That's why you have to use every element as single parameter or use placeholder with names, so that it can use the indexes of the array to bind the values
Yes that sounds reasonable. But then there's a glitch too, I mean then under which circumstances the un-named placeholders are going to be used? I'm pretty new to PDO, clarifying this would be very much helpful for me. Thanks.
I have given a try to the second method. But that's inserting ':name', ':contact', ':email', ':longitude', ':latitude', ':state_select', ':city_select', ':address' in my table as is.
@popla That's a good question :D If you use positional placeholders (?) Then the order how you bind the parameters matters! so first value which you bind will be replaced with the first question mark and so on... If you use named placeholders(:name) Then the order doesn't matter you just have to make sure that you have a associative array with the named placeholders as keys
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.