Note
I can see you're starting out, and it's great! You've always gotta find a bug to learn new stuff, and you're learning about SQL Injections now. If I could suggest something, you'd be best to start at PHP The Right Way, it'll help you a truckload.
You're PHP script (that inserts this data into a database) is not sanitized correctly.
We can't do much without seeing your associated code. But I take it you're using mysql_*
/mysqli_*
functions? We'll the former one is deprecated and removed as of PHP7!
You should start learning either of the following two prepared statement types:
From what I assume, you want to escape the string:
$data = mysql_real_escape_string($_POST['data']);
Although, there are still ways around the above escape; your database can still be hacked via SQL Injection, which is not what you want.
As noted by Armadan, to back up my statement above, mysql_real_escape_string()
is still by-passable in certain cases, read these:
Taking the code you've supplied, you'd use prepare()
and execute()
:
if(isset($_GET['comment1'])) {
if($mysqli = connect_db()) {
$insertcomment1 = $_GET['comment1'];
$stmt = $mysqli->prepare("UPDATE result SET c1=?");
if ( false===$stmt ) {
die('prepare() failed: ' . htmlspecialchars($mysqli->error));
}
$stmt->bind_param('s', $insertcomment1);
// execute
if(!$stmt->execute()){
die('execute() failed: ' . htmlspecialchars($mysqli->error));
}
// handle the rest here.
}
}
You'd be doing something like the above. You're best to read up on the following in relation to prepared statements using MySQLi: