473,835 Members | 1,887 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't make INSERT into MySQL work...

2 New Member
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.     }
  8.     //Select database
  9.     $db = mysql_select_db('search_trial');
  10.     if(!$db) {
  11.         die("Unable to select database");
  12.     }
  14. $sql = "INSERT INTO 'search_records' ('query','user') VALUES ('".$query."','".$_SESSION['SESS_MEMBER_ID']."')";
  16. mysql_query($sql) or die ('Error, insert query failed');
  18. ?>
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
3 1740
1,168 Recognized Expert Top Contributor
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
8,658 Recognized Expert Moderator Expert
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
2 New Member
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

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 Solaris Server with php5 mysql 4.1.6 ? That is what happens...I'm suspicious of the accent characters in the code but I am not sure if there's any truth to my suspicion and how I'd even test or fix it(?) Here's the snip I think causes the...
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
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 the background. I've tried it on multiple HTML pages. I've even tried taking the BG image out and seeing if just setting a black BG color works - it doesn't. If I add in the BACKGROUND="" to the BODY tag manually, it works fine. Everything...
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 to 4.1, but, still can not get it work. Here is what I did: # rpm -Uvh MySQL-server-standard-4.1.14-0.rhel4.i386.rpm warning: MySQL-server-standard-4.1.14-0.rhel4.i386.rpm: V3 DSA signature: NOKEY, key ID 5072e1f5
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 because I get the error, that there is no corresponding value in the referenced field. But when I query the referenced table with the value, I get a result. Both fields are of the same type and length (varchar(10)).
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 connect from php code, it told me that "can not connect to local mysql through /tmp/mysql.sock. /tmp/mysql.sock is my sock file to connect and permission is set good. when I add phpinfo() to my php code, it show me that mysql lib php uses is 4.1.X,...
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 connect from php code, it told me that "can not connect to local mysql through /tmp/mysql.sock. /tmp/mysql.sock is my sock file to connect and permission is set good. when I add phpinfo() to my php code, it show me that mysql lib php uses is 4.1.X,...
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 { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // set the connection for database path String Database_Path = "E:/Segi/Project/Coding/Car_Park_Storage.mdb";
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 problem with my php setup but i could not find it... here is the code; ---------------------------------------------------------------------------------------- $db = new PDO("sqlite:bar.db");
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.