Can't make INSERT into MySQL work...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hnmcc
    New Member
    • Aug 2010
    • 2

    Can't make INSERT into MySQL work...

    I don't know whether this belongs here or in the MySQL section.

    I'm trying to write a couple of values from a search page to a database: but no matter how I tweak the code, I always end up with "Error, insert query failed"...

    The code is:

    Code:
    <h3><?php echo $_SESSION['SESS_MEMBER_ID']," ",$query; ?></h3>
    <?php
    	$link = mysql_connect('localhost','root','EC4SPvRn');
    	if(!$link) {
    		die('Unable to connect to server: ' . mysql_error());
    	}
    	
    	//Select database
    	$db = mysql_select_db('search_trial');
    	if(!$db) {
    		die("Unable to select database");
    	}
    
    $sql = "INSERT INTO 'search_records' ('query','user') VALUES ('".$query."','".$_SESSION['SESS_MEMBER_ID']."')";
    
    mysql_query($sql) or die ('Error, insert query failed');
    
    ?>
    I've included the code shown as line 1 above because that always works as expected: the variables are known to the script. The original code held all the connection information in another file, but I brought it back in to simplify debugging (lol).

    The table name is correct, as are the field names. There is an auto-incrementing ID as the first field in the table.

    I assume there is some glaringly simple syntax error here, but I've been staring at it and tweaking it for so long that I just can't see it.
  • TheServant
    Recognized Expert Top Contributor
    • Feb 2008
    • 1168

    #2
    Try change line 14 to:
    Code:
    $sql = "INSERT INTO search_records ('query','user') VALUES ('".$query."','".$_SESSION['SESS_MEMBER_ID']."')";
    All I think it is is your table name being surrounded by ''s.

    Comment

    • Dormilich
      Recognized Expert Expert
      • Aug 2008
      • 8694

      #3
      if you want to put quotes around the table or field names, use the backtick (`), that’s he appropriate character for that.

      Comment

      • hnmcc
        New Member
        • Aug 2010
        • 2

        #4
        Thanks guys

        Thanks guys!

        Quoting table and field names with back-ticks (I'd never even noticed such a thing existed!) did the trick.

        I had already tried unquoting the table name, which didn't work; but just now I unquoted table name and field names - which I hadn't tried yesterday - and that worked too.

        Thanks again.
        Last edited by hnmcc; Aug 24 '10, 07:47 AM. Reason: The header doesn't appear

        Comment

        Working...