I’m using a simple cms as backend to my website where I’m able to update news and such. I want to be safe from SQL-injections, so I’m wondering if this code is considered to be safe or if there’s something I can do to make it safer:
if($_POST) {
if(isset($_POST['title']) and (isset($_POST['content']) and ($_POST['added']))) {
$title = "'".mysql_real_escape_string($_POST['title'])."'";
$content = "'".mysql_real_escape_string($_POST['content'])."'";
$added = "'".mysql_real_escape_string($_POST['added'])."'";
if(isset($_POST['id']) && $_POST['id']!=''){
$result = mysql_query("UPDATE news SET title = ".$title.", added =".$added.", content = ".$content." WHERE id = ".$_POST['id']);
$msg = "News Updated Successfully";
}else{
$result = mysql_query("INSERT INTO news (title, content, added) values($title, $content, $added)") or die("err0r");
$msg = "News Added Successfully";
}
}
Thanks and have a great day!
You are not sanitizing
$_POST['id'].Do an
intval()on it, or (better) refuse processing altogether if the ID is not an integer (assuming ID is anintfield).