-1

Why is this doing what it's doing?

Code:

<?php
    if (isset($_POST['sourceInsert'])) {
        $url = $db_con3->real_escape_string($_POST['url']);
        $desc = $db_con3->real_escape_string($_POST['desc']);
        echo '$urlbefore is ' . $url . '<br />'; ///for troubleshooting
        $result = $db_con3->query("INSERT INTO gdrive_links (evalid, userid, url, desc) VALUES ('$evalid', '$id', '$url', '$desc')");

        echo '$urlafter is ' . $url . '<br />'; ///For troubleshooting
        echo $db_con3->error; ///For troubleshooting
    }
?>

HTML output:

$urlbefore is https://docs.google.com/file/d/0B0tcjQ3FxlB6dWlMTkNQVjBwVDA/edit?usp=sharing
$urlafter is https://docs.google.com/file/d/0B0tcjQ3FxlB6dWlMTkNQVjBwVDA/edit?usp=sharing
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'desc) VALUES ('1284017', '1', 'https://docs.google.com/file/d/0B0tcjQ3FxlB6dWlMT' at line 1

So the string is fine before and after the query string, but in the query it's being cut off at the 49th character. Am I missing something dumb? It looks like my query syntax is correct...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rich701
  • 311
  • 1
  • 4
  • 10

1 Answers1

4

The problem is because, you have a reserved keyword, unescaped.

$result = $db_con3->query("INSERT INTO gdrive_links (`evalid`, `userid`, `url`, `desc`) VALUES ('$evalid', '$id', '$url', '$desc')");

You need to escape them using backticks, this way. desc is a reserved keyword in MySQL. Escape them like above.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252