wright67uk Posted October 21, 2012 Share Posted October 21, 2012 Would you say that this code is safe from SQL injection? If not, then why? <?php if ($_SERVER['REQUEST_METHOD']=='POST'){ $con = mysql_connect( "###","###","###" ); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("###", $con); $n = mysql_real_escape_string($_POST['name']); $e = mysql_real_escape_string($_POST['email']); $sql="INSERT INTO waiting (name, email) VALUES('$n','$e')"; if (!mysql_query($sql,$con)){ die('Error: ' . mysql_error()); } echo "Thankyou! We will be in touch soon."; mysql_close($con); } ?> <?php if ($_SERVER['REQUEST_METHOD']<>'POST'){ ?> <p> This website is under construction!<br /> Enter your details and we will get back to you as soon as we are all done.<br /> <br /> <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post"> Name: <input type="text" name="name" /><br /><br /> Email: <input type="email" name="email" /> <br /><br /> <input type="submit" value="Submit" /> </form></p> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/269745-sql-injection-protection/ Share on other sites More sharing options...
silkfire Posted October 21, 2012 Share Posted October 21, 2012 I would recommend you to use PDO with prepared statements, then you're 100% safe from injections of any sort. Quote Link to comment https://forums.phpfreaks.com/topic/269745-sql-injection-protection/#findComment-1386807 Share on other sites More sharing options...
Christian F. Posted October 22, 2012 Share Posted October 22, 2012 Yes, your code is safe against SQL injections. There are only two variables used in the query, and you've correctly escaped them with mysql_real_escape_string (). What your code isn't protected against, however, is HTML injections (XSS etc). To protect yourself against that, please see this article. You'll also want to have a look at rawurlescape () and htmlspecialchars (). Quote Link to comment https://forums.phpfreaks.com/topic/269745-sql-injection-protection/#findComment-1387018 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.