Jump to content

[SOLVED] Update SQL record containing punctuation....


cweepy

Recommended Posts

Hello, I'm working on a backend for a website.  I am trying to update a book's information using an html form.  Some of the items that need to be updated will contain punctuation such as double or single quotes and semicolons.

 

	$sql = 'UPDATE books SET img = $id, Title = "'.$title.'", Author = "'.$author.'", desc = "'.$desc.'", isbn-10 = "'.$isbn10.'", isbn-13 = "'.$isbn13.'", Publisher = "'.$publisher.'", Cover = "'.$cover.'", pagecnt = $pagecnt, edition = "'.$edition.'", dimensions = "'.$dim.'", Language = "'.$lang.'", WHERE id = $id';

        mysql_query($sql) or die ("Query failed: " . mysql_error());

 

Error:

Query failed: 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 = "", isbn-10 = "316143472", isbn-13 = "978-0316143479", Publisher = "Littl' at line 1

 

 

I believe I get this error because, as the query is executed, the quotes are acting as delimeters in the update line.  I tried adding "addslashes" to the query but that did not help. 

 

How do I avoid this conflict whilst keeping all puncuation open for use?  I'm using MySql 5.0.51a-community.

Link to comment
Share on other sites

I tried to implement what was suggested in that thread to no avail...

 

        $id = $_POST['id'];
        $title = mysql_real_escape_string($_POST['title']);
        $author = mysql_real_escape_string($_POST['author']);
        $desc = mysql_real_escape_string($_POST['desc']);
$isbn10 = mysql_real_escape_string($_POST['isbn10']);
$isbn13 = mysql_real_escape_string($_POST['isbn13']);
        $publisher = mysql_real_escape_string($_POST['publisher']);
$cover = $_POST['cover'];
$pagecnt = $_POST['pagecnt'];
$edition = $_POST['edition'];
$dim = $_POST['dim'];
$lang = $_POST['lang'];

$sql = "UPDATE books SET img = $id, Title = $title, Author = $author, desc = $desc, isbn-10 = $isbn10, isbn-13 = $isbn13, Publisher = $publisher, Cover = $cover, pagecnt = $pagecnt, edition = $edition, dimensions = $dim, Language = $lang WHERE id = $id";

        mysql_query($sql) or die ("Query failed: " . mysql_error());

 

 

Error produced:

Query failed: 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 'When You Are Engulfed in, Author = David Sedaris, desc = , isbn-10 = 316143472, ' at line 1

Link to comment
Share on other sites

When I did not succeed the first time I attempted to remove the quotes to explore the effect.  Currently:

 

        $id = $_POST['id'];
        $title = mysql_real_escape_string($_POST['title']);
        $author = mysql_real_escape_string($_POST['author']);
        $desc = mysql_real_escape_string($_POST['desc']);
$isbn10 = mysql_real_escape_string($_POST['isbn10']);
$isbn13 = mysql_real_escape_string($_POST['isbn13']);
        $publisher = mysql_real_escape_string($_POST['publisher']);
$cover = mysql_real_escape_string($_POST['cover']);
$pagecnt = $_POST['pagecnt'];
$edition = mysql_real_escape_string($_POST['edition']);
$dim = mysql_real_escape_string($_POST['dim']);
$lang = mysql_real_escape_string($_POST['lang']);

$sql = "UPDATE books SET img = $id, Title = '$title', Author = '$author', desc = '$desc', isbn-10 = '$isbn10', isbn-13 = '$isbn13', Publisher = '$publisher', Cover = '$cover', pagecnt = $pagecnt, edition = '$edition', dimensions = '$dim', Language = '$lang' WHERE id = $id";

        mysql_query($sql) or die ("Query failed: " . mysql_error());

 

Error:

Query failed: 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 = '', isbn-10 = '316143472', isbn-13 = '978-0316143479', Publisher = 'Littl' at line 1

Link to comment
Share on other sites

Really appreciate the help, it worked famously after I changed "desc" column in the db to "description" and after eliminating the "-" hyphens from isbn column names as well.

 

Final Code:

        $id = $_POST['id'];
        $title = mysql_real_escape_string($_POST['title']);
        $author = mysql_real_escape_string($_POST['author']);
        $desc = mysql_real_escape_string($_POST['summary']);
$isbn10 = mysql_real_escape_string($_POST['isbn10']);
$isbn13 = mysql_real_escape_string($_POST['isbn13']);
        $publisher = mysql_real_escape_string($_POST['publisher']);
$cover = mysql_real_escape_string($_POST['cover']);
$pagecnt = $_POST['pagecnt'];
$edition = mysql_real_escape_string($_POST['edition']);
$dim = mysql_real_escape_string($_POST['dim']);
$lang = mysql_real_escape_string($_POST['lang']);

$sql = "UPDATE books SET img = $id, Title = '$title', Author = '$author', description = '$desc', isbn10 = '$isbn10', isbn13 = '$isbn13', Publisher = '$publisher', Cover = '$cover', pagecnt = $pagecnt, edition = '$edition', dimensions = '$dim', Language = '$lang' WHERE id = $id";

        mysql_query($sql) or die ("Query failed: " . mysql_error());

 

 

Thanks again!

 

P.S. I'm amazed and pleased at your response times to my posts!  Your help is invaluable.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.