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", ":[email protected]" => "[email protected]")

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

$sql ="SELECT userFirstName FROM users WHERE userLogin = :victor AND userEmail = :[email protected]";

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?

1

3 Answers 3

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" => "[email protected]")

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);
Sign up to request clarification or add additional context in comments.

4 Comments

and how after that im inserting array keys in sql string? ` [:0] => [email protected] [:1] => tyugf` thanks for advice
@Viktorino I don't understand the question in your comment. What are you trying to achieve?
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
@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'] : '');
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.

Comments

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();

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.