0

sorry for my English,I try explain my self correctly. i'm trying to select data from db with array of parameters that i got from POST and the array im generating automatic looks like this :

 foreach ($values as $key => $value) {

   $postArray[":" . $value] = $value;

}

output looks like this:

$postArray = array(":victor" => "victor", ":victor@xample.com" => "victor@xample.com")

the problem is in sql syntax after AND with email because of point :

$sql ="SELECT userFirstName FROM users WHERE userLogin = :victor AND userEmail = :victor@xample.com";

I tried to do this like this victor@xample\.com and it is still not working

any one have any tips or solutions about this?

Victorino
  • 1,623
  • 11
  • 22
  • possible duplicate of [PDO valid characters for placeholders](http://stackoverflow.com/questions/5809951/pdo-valid-characters-for-placeholders) (the `.` and the `@` are not in there). – Wrikken Mar 04 '14 at 21:50

3 Answers3

1

You don't need your prepared statement placeholders to have the same name as their bound values.

Simply change your code to:

  foreach ($values as $key => $value) {
     $postArray[":" . $key] = $value;
  }

Just make sure to bind the correct key/value pair.

Jonathan Spiller
  • 1,885
  • 16
  • 25
1

You don't want to use the values as the names of the parameters - instead use fixed parameter names and the values from your post:

foreach ($values as $key => $value) {
    $postArray[":" . $key] = $value;
}

to produce output like this:

$postArray = array(":login" => "victor", ":email" => "victor@xample.com")

Then your SQL will be:

$sql ="SELECT userFirstName FROM users WHERE userLogin = :login AND userEmail = :email";

You'll need to ensure that your POST elements are login and email - note that you shouldn't rely simply on the fact that your form has them defined - this can be easily spoofed. It would be much better to have your code as:

$sql ="SELECT userFirstName FROM users WHERE userLogin = :login AND userEmail = :email";
$dbs = $dbh->prepare($sql);
$params = array(':login' => isset($_POST['login']) ? $_POST['login'] : '',
                ':email' => isset($_POST['email']) ? $_POST['email'] : '');
$dbs->execute($params);
Aleks G
  • 56,435
  • 29
  • 168
  • 265
  • and how after that im inserting array keys in sql string? ` [:0] => 876jghj@xample.com [:1] => tyugf` thanks for advice – Victorino Mar 04 '14 at 21:58
  • @Viktorino I don't understand the question in your comment. What are you trying to achieve? – Aleks G Mar 04 '14 at 22:00
  • i got two key [:0] and [:1] in array and i need to bind them to query string like this `SELECT userFirstName FROM users WHERE userLogin = :0 AND userEmail = :1` it is work if im writing them and i want to bind them automatic – Victorino Mar 04 '14 at 22:03
  • @Viktorino In this case, when you create the array, use keys `':0'` and `':1'` - `$params = array(':0' => isset($_POST['login']) ? $_POST['login'] : '', ':1' => isset($_POST['email']) ? $_POST['email'] : '');` – Aleks G Mar 04 '14 at 22:27
1

I think you probably want:

$postArray[":" . $key] = $value; // Note: don't do this either (see below)

to bind the "name" to the "value". You don't use the value as a named parameter, but instead what it is. So for the username, don't make the named parameter, "Viktorino" but instead make it "username".

However with all that said, even though you are using prepared statements, doing this will leave you completely open to SQL injection because you are directly inserting $_POST values (or names) into your query. That's a bad very thing because anyone can easily modify your query. Instead, you need to validate the data you receive hard code what values you expect to receive into the query. There is really no other way around it in this case without making your script vulnerable.

// *VERY* basic validation. Adjust accordingly
if (empty($_POST['login'])) {
    die("You need to enter your login");
}
if (empty($_POST['email'])) {
    die("You need to enter your email");
}
$stmt = $dbh->prepare("SELECT userFirstName FROM users WHERE userLogin = :login AND userEmail = :email");
$stmt->bindValue("login", $_POST['login']);
$stmt->bindValue("email", $_POST['email']);
$stmt->execute();
Mike
  • 23,542
  • 14
  • 76
  • 87