0

I'm trying to sanitize a string going into my database. But with the code below, I don't get the update to my db.

First page posts this in an input form:

$note="Here is some example text";

Receiving page:

$note = $_POST['note'];
$note = mysql_real_escape_string($note);
$sql="UPDATE some_table SET notes='$note' WHERE id='$some_id'";
$result=mysql_query($sql);

When I take out the mysql_real_escape_string line it works, but not with it in there. What am I missing?

Thanks!

2
  • 4
    Please do not use the mysql library as it is deprecated in favor of mysqli or PDO. Commented Jun 10, 2013 at 23:24
  • Was there an error when trying to execute the query? Commented Jun 10, 2013 at 23:35

1 Answer 1

1

I strongly recommend using Prepared Statement, mysql_real_escape_string() won't full protect you from SQL Injection.

Example for your update:

<?php
// connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);

// query
$sql = "UPDATE some_table 
        SET notes=? 
        WHERE id=?";
$q = $conn->prepare($sql);
$q->execute(array($$_POST['note'], $some_id));
?>

More details: http://www.php.net/manual/en/intro.pdo.php

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.