I dont get this... (mysql insert)

Discussion in 'PHP' started by killaklown, Dec 20, 2007.

  1. #1
    I have this:

    
       $sql = mysql_query("INSERT INTO games (id, name, category, width, height, type)
           VALUES('NULL', '$name', '$cat', '$w', '$h', '$type')") or die (mysql_error()); 
    
    PHP:
    it works fine, but if i add ,desc to the first line and , '$desc' to the 2nd line, i get this error:


    Anyone know whats wrong?



    I have:
    $desc = $_POST['d'];

    <textarea rows="5" cols="20" name="d"></textarea>
     
    killaklown, Dec 20, 2007 IP
  2. tushardhoot1

    tushardhoot1 Active Member

    Messages:
    3,013
    Likes Received:
    96
    Best Answers:
    0
    Trophy Points:
    90
    #2
    Are you spacing out the comma and desc?

    Ie: ,desc or , desc

    That can make a difference.
     
    tushardhoot1, Dec 20, 2007 IP
  3. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #3
    yep, all spacing is correct. I swear ive looked at this for about an hour yesterday, and another hour today. I have absouletly no idea whats wrong, and its going to end up being something like that :p

    
       $sql = mysql_query("INSERT INTO games (id, name, category, width, height, type, desc)
           VALUES('NULL', '$name', '$cat', '$w', '$h', '$type', '$desc')") or die (mysql_error()); 
    
    PHP:
     
    killaklown, Dec 20, 2007 IP
  4. tushardhoot1

    tushardhoot1 Active Member

    Messages:
    3,013
    Likes Received:
    96
    Best Answers:
    0
    Trophy Points:
    90
    #4
    Can you do this for me to try it?

    <textarea rows="5" cols="20" name="desc"></textarea>


    $desc = $_POST['desc];


    $sql = mysql_query("INSERT INTO games (id, name, category, width, height, type, desc)
    VALUES('NULL', '$name', '$cat', '$w', '$h', '$type', '$desc')") or die (mysql_error());

    Might be something with $_POST['variable'] not letting 1 character in.
     
    tushardhoot1, Dec 20, 2007 IP
    killaklown likes this.
  5. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #5
    nah, same thing.. plus i already have

    
    $name = $_POST['n'];
    $cat = $_POST['c'];
    $w = $_POST['w'];
    $h = $_POST['h'];
    $type = $_POST['t'];
    
    PHP:
     
    killaklown, Dec 20, 2007 IP
  6. accel

    accel Well-Known Member

    Messages:
    142
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    133
    #6
    It might be something to do with the fact that desc is a reserved word, i.e. has a special meaning of descending within an SQL statement. If you renamed desc to description in your code you probably won't have this issue.

    Rgds

    Accel
     
    accel, Dec 20, 2007 IP
    killaklown likes this.
  7. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #7
    ...


    Thanks, lol... I hate when its small things like that, and it takes forever to find. Should have known it.


    +rep to both.
     
    killaklown, Dec 20, 2007 IP
  8. tushardhoot1

    tushardhoot1 Active Member

    Messages:
    3,013
    Likes Received:
    96
    Best Answers:
    0
    Trophy Points:
    90
    #8
    Sorry buddy. More of a PHP guy than an SQL guy.

    Good luck with your site.
     
    tushardhoot1, Dec 20, 2007 IP
  9. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #9
    As stated above, desc is a reserved word. You still can use it by placing it within a `` like:

    
    $sql = mysql_query("INSERT INTO `games` (`id`, `name`, `category`, `width`, `height`, `type`,`desc`)
           VALUES('NULL', '$name', '$cat', '$w', '$h', '$type','$desc')") or die (mysql_error());
    
    PHP:
    But I wouldn't recommend it because when you transfer the database you won't be able to do it via softwares like phpMyAdmin.

    Another note, you should escape the desc and other text fields before adding them to mysql:

    
    $sql = mysql_query("INSERT INTO `games` (`id`, `name`, `category`, `width`, `height`, `type`,`desc`)
           VALUES('NULL', '".mysql_real_escape_string($name)."', '$cat', '$w', '$h', '$type','".mysql_real_escape_string($desc)."')") or die (mysql_error());
    
    PHP:

    Spacing doesn't make a difference, you can have spaces or no spaces at all and it should work.

    Peace,
     
    Barti1987, Dec 20, 2007 IP