SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2005
    Posts
    54
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Syntax Error I cant Figure Out

    Ive done similar scripts like this all the time and ive never had a problem like this. Everything works fine until i fill in the form and the stuff isnt added to the database and i get an error like this :

    error adding news: 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 = 'descript', content = 'blahblah', date = CURdate(), pic

    i have no idea whats wrong. ive looked over the syntax numerous times looking for mistakes.

    heres the page: http://frozin.myfxh.com/fn/admin/adnews.php?addnews=1

    heres the code:
    Code:
    <html>
    <link rel=stylesheet href=admin.css type=text/css>
    <?php 
    include 'nav.php' 
    ?>
    <?php if (isset($_GET['addnews'])): // If the user wants to add a news
    ?>
    
    <form action="<?php echo $_SERVER['PHP_SELF'];
    ?>" method=post>
    <div id="form" align="center">
    Author: <input type=text name="poster">
    Title: <input type=text name="title">
    Pic URL: <input type=text name="pic">
    Description: <input type=text name="desc">
    Body: <input type=text name="content">
    <br>
    <input type=submit value=submit>
    </div>
    </form>
    
    
    <?php else:
    
    
        $dbcnx = @mysql_connect('localhost', 'frozin', 'SgripFoO');
    if (!$dbcnx) {
        exit('<p><font face=verdana size=-2 color=333333>unabljke to connect</font></p>');
    } 
    
    if (!@mysql_select_db('frozin_phpb1')) {
        exit('<p><font face=verdana size=-2 color=333333>unable to connect</font></p>');
    } 
    // if a news is added, add it to db
    if (isset($_POST['poster'])) {
        $poster = $_POST['poster'];
        $title = $_POST['title'];
        $desc = $_POST['desc'];
        $content = $_POST['content'];
        $pic = $_POST['pic'];
        $sql = "INSERT INTO news SET
    poster = '$poster', 
    title = '$title', 
    desc = '$desc',
    content = '$content', 
    date = CURdate(),
    pic = '$pic'";
    
    if (@mysql_query($sql)) {
    echo 'news has been added';
    } else {
    echo '<div id="error">';
    echo 'error adding news: ' .
    mysql_error() . '';
    echo '</div>';
    }
    }
    
    
    //if news has been deleted, delete it from databse
    if (isset($_GET['delnews'])) {
    $id = $_GET['delnews'];
    $sql = "DELETE FROM news
    WHERE id=$id";
    if (@mysql_query($sql)) {
    echo '<p>News has been deleted</p>';
    } else {
    echo '<p>error deleting news: ' .
    mysql_error() . '</p>';
    }
    }
    
    
    echo '<div id="tbl">' ;  	  
    echo '<table><tr><td>#</td><td>poster</td><td>title</td><td>desc</td>';
    echo '<td>content</td><td>pic</td><td>DEL</td>';
    
    
    $result = @mysql_query('SELECT id, poster, title, desc, content FROM news');
    
    
    while ($row = @mysql_fetch_array($result)) {
    $id = $row[id];
    $user = $row[poster];   
    $pass = $row[title];
    $b1a = $row[desc];
    $b1b = $row[content];
    $b2a = $row[pic];
    echo '<tr><td>', $id;
    echo '</td><td>', $poster;
    echo '</td><td>', $title;
    echo '</td><td>', $desc;
    echo '</td><td>', $content;
    echo '</td><td><a href=" ' . $_SERVER['PHP_SELF'] .
    '?delstud=' . $id . ' ">delete</a>' ;
    
    echo '</td></tr><br>';
    }
    echo '</div>';
    endif;
    
    ?>
    
    </html>

  2. #2
    SitePoint Zealot
    Join Date
    May 2005
    Location
    Birmingham UK
    Posts
    122
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always insert like this:

    PHP Code:
    $sql "INSERT INTO news (poster, title, desc, content, date, pic) values ('$poster','$title','$desc','$content','CURdate()','$pic'); 

  3. #3
    SitePoint Member
    Join Date
    May 2005
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    MySQl can be annoying when it comes to reserved words, they are listed here:
    http://dev.mysql.com/doc/mysql/en/reserved-words.html

    'desc' is a reserved word so I'd guess that your column 'desc' is causing the problem. Try putting backticks around it like so:
    `desc` = '$desc'

    Regards,
    Jon

  4. #4
    SitePoint Wizard Dylan B's Avatar
    Join Date
    Jul 2004
    Location
    NYC
    Posts
    1,150
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You also run an is_numeric around the id in the URL.


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
  •