473,388 Members | 1,375 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,388 software developers and data experts.

INSERT MySQL query always causing an error.

25
I am trying to create a registering script, but everytime I try to insert the data into my database i get an error.

Expand|Select|Wrap|Line Numbers
  1. $query = "INSERT INTO members (username, pass, email, first_name, last_name) VALUES ($username, $password, $email, $f_name , $l_name);";
  2. $q = mysqli_real_query($dbc, $query);
  3. if($q) {
  4.     // My code goes here
  5. } else {
  6.     // Display an error
  7. }
  8.  
Everytime, $q returns false and goes to the else clause which displays the error. I am sure the database is set up properly, but I can't figure out why it is always returning false.
May 18 '10 #1

✓ answered by Atli

What is the error?

I would guess it has to do with the fact that the string values in your VALUES clause are not quoted. All strings inside MySQL queries need to be enclosed in single-quotes, or they will be parsed as SQL commands rather than string values.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // INCORRECT
  3. $sql = "INSERT ... VALUES($name, $password)";
  4.  
  5. // Correct
  6. $sql = "INSERT ... VALUES('$name', '$password')";
  7. ?>

2 1626
Atli
5,058 Expert 4TB
What is the error?

I would guess it has to do with the fact that the string values in your VALUES clause are not quoted. All strings inside MySQL queries need to be enclosed in single-quotes, or they will be parsed as SQL commands rather than string values.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // INCORRECT
  3. $sql = "INSERT ... VALUES($name, $password)";
  4.  
  5. // Correct
  6. $sql = "INSERT ... VALUES('$name', '$password')";
  7. ?>
May 19 '10 #2
dgourd
25
Thank you very much. I was putting the single quotes on the column names instead of the values.
May 19 '10 #3

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

Similar topics

3
by: joemyre | last post by:
Hi everyone, What I'm trying to do is take php variables i got from user input, and pass them as the MySQL query terms. $query = "select * from ident where ".$searchtype1."=".$searchterm1."";...
6
by: Brian Richmond | last post by:
I'm trying to use a regular expression to match a hidden html tag and replace it with the results of a mysql query. The query is based off a part of the hidden tag. For example: The article...
4
by: yogesh | last post by:
mysql in c++ initialize error occurs a simple program is executed in redhat9.0 , using gcc 3.2.2 compiler version ... #include <stdio.h> #include <mysql.h> #include <string.h> int main() {
2
by: Flic | last post by:
Hi, I have a basic db that I access with MySQL query browser. Everything seems fine to me but I am using this db as part of a php shopping basket and when I try to add an item I get: Notice:...
2
by: stmfc | last post by:
i have a problem with user creation for mysql db i think the problem stems from string style. GRANT ALL ON newDB.* TO 'test'@'localhost' IDENTIFIED BY 'test'; i am...
7
by: ajos | last post by:
hi friends, i have a problem in mysql query..when i click on submit the values get enterred but i get a ever since i did some changes in my code(earlier i was getting a null pointer...
10
by: Lloyd Harold | last post by:
I'm very new to PHP and attempting to put together a simple script for retrieving MySQL data of personal records. The MySQL table I'm using consists of: 0: id 1: name 2: location (an integer...
4
by: TechnoAtif | last post by:
Hi ALL I have entered some array values using checkboxes into mysql database through a form. Next iam creating a searchpage where all those cateogories inserted through checkboxes has to be...
1
by: pretzelboy | last post by:
Hi, I last wrote software 13years ago in the pascal, dbase, clipper days. I have recently built a Ubuntu Box and with C++ (and help from the web) setup a serial barcode reader program using Mysql...
5
by: lisles | last post by:
i have a page funtion.php which hs the function to connect to the db /* Mysql Connection */ function connect(){ global $db_server,$db_user,$db_pass,$db;//Global Values from the config.php...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.