3

Which of this is right and really safe?
Using prepared statements:

$stmt= $db->prepare("INSERT INTO books (title) VALUES (?)");
$booktitle=$_POST['booktitle'];
$stmt->bind_param('s', $booktitle);   
$stmt->execute();

Or using escape function :

$unsafe_variable = $login;
$safe_variable = mysqli_real_escape_string($unsafe_variable);
$stm=mysqli_query($db,"SELECT post_amount FROM users WHERE login='" . $safe_variable . "'");
$stmone=mysqli_fetch_assoc($stm);
$stmtwo=implode($stmone);
echo($stmtwo);

Please, help to deal with it.

3 Answers3

4

The first option is way, way better. I can't stress this enough. Not only does it ensure you always take care of things automatically, but it also gives you cleaner code. If you don't use prepared statements, all it takes is one miss in your sanitation and you're wide open for attacks. Hell, prepared statements are half the reason mysqli was introduced in the first place.

Joel Hinz
  • 24,719
  • 6
  • 62
  • 75
0

The first option is better and it can give you clean code and take care of all things automatically

varad mayee
  • 619
  • 7
  • 19
0

Technically both are safe.

However using prepared statements force you to escape all your variables. You can't miss an escape_string call using prepared statements.

Also look at PDO, which has a nicer way of dealing with prepared statements imho :)

Sbls
  • 495
  • 1
  • 4
  • 10