www.webdeveloper.com
Results 1 to 3 of 3

Thread: Error in SQL syntax

  1. #1
    Join Date
    Sep 2011
    Posts
    8

    Error in SQL syntax

    The code is :

    <?
    session_start();
    include ("lib/dbconnect.php");
    $autorID=intval($_GET["autorid"]);
    $autoriDS=safe_query("SELECT autori.* FROM autori WHERE autori.id=".$autorID);
    $autorRow=mysql_fetch_object($autoriDS);
    $detalii=$autorRow->cv;
    $autor=$autorRow->autor;
    include ("header.php");
    ?>

    when running /autor.php?autorid=44988 it's OK ( where XXXXX is a number)

    But when i run /autor.php?autorid=44988blblb i need the page to show
    /autor.php?autorid=44988 . Instead i get

    error nr: 1054
    Unknown column '44988blblb' in 'where clause'
    Interogarea: SELECT autori.* FROM autori WHERE autori.id=44988blblb

    What should i change in the code ?
    Thanks !

  2. #2
    Join Date
    Jun 2008
    Posts
    136
    I don't know why your intval() function is failing there. Try changing
    $autorID=intval($_GET["autorid"]);
    to
    $autorID= (int) $_GET["autorid"];

  3. #3
    Join Date
    Dec 2005
    Posts
    2,984
    You've got two problems, your SQL syntax and the value you are trying to parse from the query string.

    The code should look like this:

    PHP Code:
    session_start();
    include (
    "lib/dbconnect.php");
    $autorID=preg_replace('/[^\d]/g','',($_GET["autorid"])); //replace and single character that's not a digit 
    //OR
    preg_match('/^(\d+)/',$_GET["autorid"],$matches);
    $autorID $matches[1]; //grab all the digits from the start of a string to the point where there are no more digits
    $autoriDS=safe_query("SELECT autori.* FROM autori WHERE autori.id='".$autorID."'"); //encapsulate $autorID in single quotes!!! (your first problem)
    $autorRow=mysql_fetch_object($autoriDS); 
    $detalii=$autorRow->cv;
    $autor=$autorRow->autor;
    include (
    "header.php"); 
    I've switched careers...
    I'm NO LONGER a scientist,
    but now a web developer...
    awesome.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles