SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2001
    Location
    University of Louisville
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    error in your SQL syntax near '' at line 1

    I have studied Kevin Yanks', "Build your own Database Driven Website using PHP & MySQL" tutorial and downloaded the source code files, and have learned Oh so very much! It may be "cut and paste programming" for me right now, but it's a great way to really learn something. I am very thankful.

    I have started customizing the php pages in the tutorial (just s.s. includes for the page layout and db connection so far) and most of the pages work perfectly. However, I keep getting a syntax error when I try to use the editcat.php, editauthor.php, or editjoke.php files. They use essentailly the same code.

    In my implementation, (Seen here ) the table "departments" has two fields, named "id" and "Department". I can add new data to the table with no problem. What I'd like to do next is edit the data using a web form, based on which id gets passed. So I use:

    $sql = "UPDATE departments SET " .
    "Department='$dname' " .
    "WHERE id=$id";

    if (mysql_query($sql)) {
    echo("<P>Department details updated.</P>");
    } else {
    echo("<P>Error updating department details: " .
    mysql_error() . "</P>");
    }



    and pass the variable using:

    $dept=mysql_query("SELECT Department " .
    "FROM departments " .
    "WHERE id=$id");
    if (!$dept) {
    echo("<P>Error fetching department details: " .
    mysql_error() . "</P>");
    exit();
    }

    $dept = mysql_fetch_array($dept);

    $dname = $dept["department"];



    from the form that looks like:

    <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=POST>
    <P>Edit the departmentBR>
    Name:
    <INPUT TYPE=TEXT NAME="Department" VALUE="<?php echo($dname); ?>" SIZE=20 MAXLENGTH=100><BR>
    <INPUT TYPE=HIDDEN NAME="id" VALUE="<?php echo($id); ?>">
    <INPUT TYPE=SUBMIT NAME="submit" VALUE="SUBMIT"></P>
    </FORM>



    <?php endif; ?>


    Judging from the error message (Error updating department details: You have an error in your SQL syntax near '' at line 1) the part that fails is the $dept.

    This is basically the exact code from the tutorial download. I have tried changing only the tutorial's db connection and leaving everthing else unchanged and then uploaded it, but still get the same error.

    If anyone has any ideas about what syntax I should be using I would sure appreciate your help.
    I have high hopes for learning php and look forward to builing some wonderous sites.

    Thank you.

  2. #2
    SitePoint Wizard johnn's Avatar
    Join Date
    Mar 2001
    Location
    Southern California, USA
    Posts
    1,181
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For this line
    "WHERE id=$id");

    try

    "WHERE id='$id'");

  3. #3
    SitePoint Zealot Hulkur's Avatar
    Join Date
    Oct 2001
    Location
    Estonia
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $sql = "UPDATE departments SET " .
    "Department='$dname' " .
    "WHERE id=$id";
    you use $dname as department name, but on form
    you use NAME="Department"

    in php you should use $department. (case?)
    (2B) or (not 2B) = FF

  4. #4
    SitePoint Zealot Hulkur's Avatar
    Join Date
    Oct 2001
    Location
    Estonia
    Posts
    141
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a suggestion.

    Don't use separate files for new and edit.

    new is actually edit with id=0

    Then you can avoid problems like this one as your new script
    works correctly.
    (2B) or (not 2B) = FF

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2001
    Location
    University of Louisville
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by johnn
    For this line
    "WHERE id=$id");

    try

    "WHERE id='$id'");
    Yes! Thank you so much.

    I had actually tried that as the first thing that came to mind, but I neglected in changing it in both variables. Plus, I had other problems on the page as well.

    The syntax error has been corrected, and your insight is appreciated.
    --

  6. #6
    SitePoint Enthusiast
    Join Date
    Nov 2001
    Location
    University of Louisville
    Posts
    26
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by Hulkur


    you use $dname as department name, but on form
    you use NAME="Department"

    in php you should use $department. (case?)

    Thanks for spotting this! I guess $dname could not get any data because department isn't the same as Department. It makes sense to me now - "ahhhh!"

    I am getting a sense of the importance of a good naming convention as well.


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
  •