Results 1 to 3 of 3

Thread: ' <-- Need help

  1. #1
    Join Date
    Mar 2009
    Location
    Western Australia
    Posts
    386
    Tokens
    0

    Default ' <-- Need help

    Ok so I've got a simple page editor:
    PHP Code:
    $update mysql_query("UPDATE pages SET body='$_POST[content]' WHERE id='$_GET[id]'") or die(mysql_error());
      die(
    "<b>The page has successfully been updated!</b>"); 
    but if I in the text area where you edit I put a " or ' it gives me an error that says this:
    Code:
    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 'll' WHERE id='1'' at line 1
    Obviously it's the ' in the mysql query but how do I prevent?

    BTW: it has ll 'cos for the example I wrote ya'll
    Last edited by Shibby-Shabs; 09-11-2010 at 10:36 AM. Reason: Ya know... I wanted to.

  2. #2
    Join Date
    Feb 2006
    Location
    Scotland
    Posts
    2,087
    Tokens
    138

    Latest Awards:

    Default

    http://php.net/manual/en/function.my...ape-string.php
    http://www.php.net/manual/en/function.addslashes.php
    http://www.php.net/manual/en/function.stripslashes.php

    Have a look through those, it should help

    PHP Code:
    $update mysql_real_escape_string(mysql_query("UPDATE pages SET body='$_POST[content]' WHERE id='$_GET[id]'")) or die(mysql_error());
      die(
    "<b>The page has successfully been updated!</b>"); 
    You really should be sanitising your data input too, hence why this is happening. Another few good things to pick up on are:

    http://php.net/manual/en/function.htmlspecialchars.php
    http://www.php.net/manual/en/functio...ars-decode.php
    http://www.php.net/manual/en/function.strip-tags.php
    Last edited by Johno; 09-11-2010 at 08:54 PM.

  3. #3
    Join Date
    Mar 2008
    Posts
    5,108
    Tokens
    3,780

    Latest Awards:

    Default

    PHP Code:
    <?php 

    $id 
    mysql_real_escape_string($_GET['id']); // gets the ID from the page.  blah.php?id=1

    // This below will check to make sure the page actually exists in the database... //
    $check_id mysql_num_rows(mysql_query("SELECT * FROM `pages` WHERE `id`='$id' LIMIT 0,1"));

    if(!
    $check_id) {
        exit(
    "Page does not exist."); // page doesn't exist.
    }

    $content mysql_real_escape_string($_POST['content']);

    $update mysql_query("UPDATE `pages` SET `body`='$content' WHERE `id`='$id'") or die(mysql_error());

    echo 
    "<strong>The page has been updated successfully!</strong>";

    ?>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •