Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Insert query syntax help

    When you have been away from coding it is struggle. My thanks to those who have helped. Obviously, since I get an error, I have one too many or too few single or double quotes. Please help.
    Code:
    $sql = "INSERT INTO " . WP_CALENDAR . " SET event_title='" . mysql_escape_string($title)  . "', event_desc='" . mysql_escape_string($desc) . "', event_begin='" . mysql_escape_string($start_date) 
                 . "', event_end='" . mysql_escape_string($end_date) . "', event_time='" . mysql_escape_string($game_time) . "', event_recur='" . mysql_escape_string($recur) . "', event_repeats='" . mysql_escape_string($repeat) . "', event_author='" mysql_escape_string($author) . "', event_category=".mysql_escape_string($category).", event_link='".mysql_escape_string($link)."'";
    Thanks

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    First of all, read this page:
    http://www.php.net/manual/en/functio...ape-string.php

    See the big "DEPRECATED" there?

    But beyond that: Why would you want to treat dates and times ($start_date, $game_time) as *TEXT*? SURELY those values are DATETIME or TIMESTAMP fields in your database, no? So treat them as such. And are the fields such as event_recur and event_repeat really *text* fields in the table? Or are they numbers? If numbers, treat them as numbers.

    As an aside, it strikes me as bad practice to have multiple tables with the same data structure, as is implied by the fact that the table name is a variable in your code. Or at least it seems that way. How come WP_CALENDAR isn't a PHP variable? That is, $WP_CALENDAR or similar? Where is it coming from ?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,901
    Thanks
    79
    Thanked 4,423 Times in 4,388 Posts
    Quite honestly, I don't see the error.

    Unless it's the missing $ in front of WP_CALENDAR????

    I think I might have coded it thus, though, for clarity:
    Code:
    $title = mysql_real_escape_string($title);
    $desc = mysql_real_escape_string($desc);
    $start_date = date("Y-m-d",$start_date);
    $end_date = date("Y-m-d",$end_date);
    $game_time = date("h:i:s",$game_time); /* PURE GUESS ON THIS ONE! */
    $recur = mysql_real_escape_string($recur);
    $repeat = mysql_real_escape_string($repeat);
    $author = mysql_real_escape_string($author);
    $category = 1 * $category; /* force to be a number! */
    $link = mysql_real_escape_string($link);
    
    $sql = "INSERT INTO " . WP_CALENDAR 
         . "(event_title, event_desc, event_begin, event_end, "
         .  "event_time, event_recur, event_repeats, event_author, "
         .  "event_category, event_link) " 
         . " VALUES('$title$','$desc','$start_date','$end_date', "
         .         "'$game_time','$recur','$repeat','$aughor', "
         .         "$category,'$link')";
    
    echo "<hr>DEBUG SQL: " . $sql . "<hr>\/n";
    ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Senior Coder
    Join Date
    Apr 2010
    Posts
    1,453
    Thanks
    71
    Thanked 102 Times in 101 Posts
    Please post the error its giving you. That will tell exactly what the problem is. I have a hunch it's a variable problem.


  •  

    Posting Permissions

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