473,382 Members | 1,786 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Can't make INSERT into MySQL work...

2
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:

Expand|Select|Wrap|Line Numbers
  1. <h3><?php echo $_SESSION['SESS_MEMBER_ID']," ",$query; ?></h3>
  2. <?php
  3.     $link = mysql_connect('localhost','root','EC4SPvRn');
  4.     if(!$link) {
  5.         die('Unable to connect to server: ' . mysql_error());
  6.     }
  7.  
  8.     //Select database
  9.     $db = mysql_select_db('search_trial');
  10.     if(!$db) {
  11.         die("Unable to select database");
  12.     }
  13.  
  14. $sql = "INSERT INTO 'search_records' ('query','user') VALUES ('".$query."','".$_SESSION['SESS_MEMBER_ID']."')";
  15.  
  16. mysql_query($sql) or die ('Error, insert query failed');
  17.  
  18. ?>
  19.  
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.
Aug 23 '10 #1

✓ answered by Dormilich

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

3 1712
TheServant
1,168 Expert 1GB
Try change line 14 to:
Expand|Select|Wrap|Line Numbers
  1. $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.
Aug 23 '10 #2
Dormilich
8,658 Expert Mod 8TB
if you want to put quotes around the table or field names, use the backtick (`), that’s he appropriate character for that.
Aug 24 '10 #3
hnmcc
2
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.
Aug 24 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Hal Halloway | last post by:
can mysql work in Windows but not Solaris? Is there any reason you can see why the fulltext php/mysql code below works OK on my win2k php4.3.6. mysql 4.1.3 pc but does *not* work when put on a...
2
by: erik | last post by:
Hi I'm just started to look at MySQL today and what confuses me is that it seems to be a program for a server ?? I'm using WindowsXP-pro. -- Erik Reinert Nielsen www.nogn.dk
2
by: Display Name | last post by:
I can't insert an image using the "add a new stylesheet" function- the function, on dreamweaver MX (i'm on Windows 2k Pro) seems to work as it generates the code... but the image never shows up in...
3
by: Nick | last post by:
hi, all I have a red hat 9 and looks like it had mysql 3.2.54 installed before, but, actually not, i can not find the install folder and I can not uninstall it. So I am trying to install/upgrade...
3
by: Björn Platzen | last post by:
Hello list, I'm a postgres-newbie with a maybe silly question. I'm working on a SuSE-Linux 9.1 with PostgreSQL 7.4.6 with PostGIS 0.9. My problem is, that I can't insert data into a table...
2
by: xiuchuanli | last post by:
I install Fedora 4 with web server, after that I installed mysql 5.0.15. Now I can connect to mysql from command line and mysql-administrator and mysql-query-browser. But when I try to test to...
0
by: xiuchuanli | last post by:
I install Fedora 4 with web server, after that I installed mysql 5.0.15. Now I can connect to mysql from command line and mysql-administrator and mysql-query-browser. But when I try to test to...
2
by: yeap | last post by:
Hi All, I can't insert java variable into ms access database. I'm using odbc connection to ms access. Below are my coding. try { ...
8
by: Betikci Boris | last post by:
Can not insert data into SQLite3 database through browser however i can easily insert data into my db from konsole, in both attmpts i used php 5.2.6 on 2.6.25.* linux kernel i think there is a...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.