SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict
    Join Date
    Mar 2003
    Location
    Greenville, SC
    Posts
    388
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php error in syntax?

    Hello, I am getting the following error message and can't for the life of me see where the problem is...:


    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 ' nsid=1, newsdate=CURDATE()' at line 4


    PHP Code:
    //Insert news into database 
    if (isset($_POST['submit'])) {
        
    $newstitle =mysql_real_escape_string$_POST['newstitle']);
        
    $newstext mysql_real_escape_string($_POST['newstext']);
        
    $news_catid = (int) $_POST['news_id'];
        
    $newssec_id = (int) $_POST['newssec_id'];
        
        
        
    $sql "INSERT INTO news SET
            newstitle='
    $newstitle',
            newstext='
    $newstext',
            ncid=
    $newscat_id,
            nsid=
    $newssec_id,
            newsdate=CURDATE()"

    can anybody see what it is?

  2. #2
    SitePoint Wizard jimbo_dk's Avatar
    Join Date
    May 2005
    Location
    Singapore
    Posts
    1,261
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On first glance $news_catid and $newscat_id (used in SQL) are different. This is probably the reason.
    Winners Respond. Losers React.
    Singapore Web Designer

  3. #3
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    PHP Code:
    /Insert news into database 
    if (isset($_POST['submit'])) {
        
    $newstitle =mysql_real_escape_string$_POST['newstitle']);
        
    $newstext mysql_real_escape_string($_POST['newstext']);
        
    $news_catid = (int) $_POST['news_id'];
        
    $newssec_id = (int) $_POST['newssec_id'];
        
        
        
    $sql "INSERT INTO news SET
            newstitle='
    $newstitle',
            newstext='
    $newstext',
            ncid='
    $news_catid',
            nsid='
    $newssec_id',
            newsdate=CURDATE()"


  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think nsid field is of varchar or something else rather than INT type. So it might be wanting single quotes. Otherwise sql query seems to be fine.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2007
    Location
    Regina, SK, Canada
    Posts
    129
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jive View Post
    Hello, I am getting the following error message and can't for the life of me see where the problem is...:


    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 ' nsid=1, newsdate=CURDATE()' at line 4


    PHP Code:
    //Insert news into database 
    if (isset($_POST['submit'])) {
        
    $newstitle =mysql_real_escape_string$_POST['newstitle']);
        
    $newstext mysql_real_escape_string($_POST['newstext']);
        
    $news_catid = (int) $_POST['news_id'];
        
    $newssec_id = (int) $_POST['newssec_id'];
        
        
        
    $sql "INSERT INTO news SET
            newstitle='
    $newstitle',
            newstext='
    $newstext',
            ncid=
    $newscat_id,
            nsid=
    $newssec_id,
            newsdate=CURDATE()"

    can anybody see what it is?
    It looks to me like youre trying to do a MySQL UPDATE statement but mixing it with an INSERT statement.

    Try the following.
    Code PHP:
    $sql = "UPDATE news SET 
            newstitle='$newstitle',
            newstext='$newstext',
            ncid=$newscat_id,
            nsid=$newssec_id,
            newsdate=CURDATE()";

  6. #6
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jboesch View Post
    It looks to me like youre trying to do a MySQL UPDATE statement but mixing it with an INSERT statement.
    Try the following.
    Code PHP:
    $sql = "UPDATE news SET 
            newstitle='$newstitle',
            newstext='$newstext',
            ncid=$newscat_id,
            nsid=$newssec_id,
            newsdate=CURDATE()";
    It can be inserted the data like he is doing. I mean it is possible to insert new record into the table like update.
    Code PHP:
    $sql = "INSERT INTO news SET 
            newstitle='$newstitle',
            newstext='$newstext',
            ncid=$newscat_id,
            nsid=$newssec_id,
            newsdate=CURDATE()";

    And this is not the error I think.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  7. #7
    SitePoint Guru mmarif4u's Avatar
    Join Date
    Dec 2006
    Location
    /dev/swat
    Posts
    619
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    look at here the error is coming from here.

    PHP Code:
        $news_catid = (int) $_POST['news_id']; 
    But here in insert statement he specify it like this:
    PHP Code:
            ncid='$newscat_id'
    and i did not see any $newscat_id in the code above.
    I change the code to his varaible one.

    Yes rajug is right, also can use insert like update in that way.


Bookmarks

Posting Permissions

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