MySQL query gone wrong? - php

another query gone wrong!
I am trying to INSERT INTO anretOrders (order, orderNumber) VALUES ('test', 15)
Now, the anretOrders table has 3 columns, but one is the id so i am leaving that out since it auto-increments. The other is "order" which is a text column, and orderNumber which is an int.
I cannot, for the life of me, figure out what is wrong here? what am i missing?

escape the field names since order is a keyword in mysql
INSERT INTO anretOrders (`order`, `orderNumber`) VALUES ('test', 15)

order is a Reserved Words
So try like this
create table anretOrders (`order` varchar(20),orderNumber int);
INSERT INTO anretOrders (`order`, `orderNumber`) VALUES ('test', 15)
Sql FIDDLE

Related

Insert into mysql specific situation

I have a strange situation in mysql, So I have table witch have multiple columns, one of it is specific. This columns is specific INT(10) UNSIGNED NOT NULL.
So If I write in sql :
INSERT INTO infos_game (id_game,specific) VALUES (0, 12) ---- doesn't work
If I write :
INSERT INTO infos_game (id_game,``specific``) VALUES (0, 12)----- it works, so if specific is between `` works fine. What is the problem ? Help me please. Thx in advance.
If is a reserved word, how to insert in database from php, I tried :
$o_infos_game_user_registered = new \Entity\Cluster\InfosGame(array(
'id_game' => $game->id_game,
'specific' => $game->specific,
),
);
$o_infos_game_user_registered->save();
But I get the error
It seems that specific is a reserved word. To use a reserved word you need to use back ticks (``) otherwise the query will fail.
Reading Material
MySQL Keywords https://dev.mysql.com/doc/refman/5.5/en/keywords.html
Hit CTRL + F then type specific and it will highlight it.
Executing queries with reserved words can be done in 3 different ways
1) Rename columns into non reserved word
ALTER TABLE "table_name" RENAME COLUMN "column 1" TO "column 2";
2) Use back ticks in queries (``)
INSERT INTO infos_game (`id_game`,`specific`) VALUES (0,12)
3. Use inserts without specifying columns (when u insert data which contains all columns )
//old query
INSERT INTO infos_game (id_game,specific) VALUES (0, 12)
//new query
INSERT INTO infos_game VALUES (0, 12)

INSERT query not working in mysqli

I have a table named user_data which contains 5 rows-id(primary key),name,address,phone,sex.When I try to insert values into the table via this query
mysqli_query($con,"INSERT INTO user_data VALUES ('Peter_malik', 'Griffin door',35897,'male')");
it doesnt work.But When I tried this one,it works.
mysqli_query($con,"INSERT INTO user_data (name,address,phone,sex) VALUES ('Peter_Gregory', 'Griffin door',35897,'male')");
I didnt understand what is the real issue behind this.I am using PHP 5.4.7 and XAMPP 1.8.1.
instead of this:
mysqli_query($con,"INSERT INTO user_data VALUES ('Peter_malik', 'Griffin door',35897,'male')");
Use this when the field is set as NOT NULL
mysqli_query($con,"INSERT INTO user_data VALUES (NULL, 'Peter_malik', 'Griffin door',35897,'male')");
Or use this when the field is set as NULL
mysqli_query($con,"INSERT INTO user_data VALUES (0, 'Peter_malik', 'Griffin door',35897,'male')")
See the mysql manual: http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
It does not insert in the first query since it assumes the first value i.e. 'Peter_malik' is for your primary key and it fails.
When you specify the column names in the query it knows which value is for which column.
I addition in the first case i.e.
'Peter_malik', 'Griffin door',35897,'male'
will refer to
id(pk),name,address,phone
So u need to pass the first value as NULL so that id gets auto incremented. So the first query should be as
VALUES (NULL, 'Peter_malik', 'Griffin door',35897,'male')
If your table has 5 columns id, name, address, phone and sex your staement has to provide 5 values, one for each column. Since your stament provides only 4 values, you need a column list to tell MySQL which values you do provide.
If your id column is a auto_increment column, you can provide null in your values clause.
So you have to say:
INSERT INTO user_data VALUES (NULL, 'Peter_malik', 'Griffin door',35897,'male');
OR
INSERT INTO user_data (name,address,phone,sex) VALUES
('Peter_malik', 'Griffin door',35897,'male')
You need to include the id column on your insert statement:
mysqli_query($con,"INSERT INTO user_data VALUES (null, 'Peter_malik', 'Griffin door',35897,'male')");
, provided that your id field is set to autoincrement! ;)
If you provide the ID also in your first query, it will work without any problem.
Assuming that id=1
mysqli_query($con,"INSERT INTO user_data VALUES (1,'Peter_malik', 'Griffin door',35897,'male')");
As others said when you have not explicitly mentioned your column names in a query, you have to provide all the values.
when you are trying the following query then...
mysqli_query($con,"INSERT INTO user_data VALUES ('Peter_malik', 'Griffin door',35897,'male')");
In this query you only inserting 4 values in the table but the table has 5 field so it is causing problem because in the above query the value is inserting form first field & the sequence or datatype mismatching in the database because of this this query causing problem .
If u want insert wuthout specifing field then you may use the following query....
mysqli_query($con,"INSERT INTO user_data VALUES (0,'Peter_malik', 'Griffin door',35897,'male')");<br><br>
It Will work properly.
and in the your second query you also specifying the field name & corresponding their values so that's query not causing any problem.

single Insert query to insert multiple rows in one column

I am trying to make a query that will insert multiple values from my form into a single row in my table.
$q2="INSERT INTO tbl_Answer('Answer')VALUES ('$A1'),('$A2'),('$A3'),('$A4'),('$A5')";
Everything I have found tells me this should work using PHP with a MySQl database. Any ideas if I have done something wrong with the syntax or where my issue is? Thanks
INSERT INTO tbl_Answer (Answer) VALUES ('$A1'),('$A2'),('$A3'),('$A4'),('$A5')
Is the same as saying:
INSERT INTO tbl_Answer (Answer) VALUES ('$A1')
INSERT INTO tbl_Answer (Answer) VALUES ('$A2')
INSERT INTO tbl_Answer (Answer) VALUES ('$A3')
INSERT INTO tbl_Answer (Answer) VALUES ('$A4')
INSERT INTO tbl_Answer (Answer) VALUES ('$A5')
What exactly are you looking for?
Here is a demo of your original query working: http://sqlfiddle.com/#!2/e20fc/1
Looks like the value of $A1-$A5 is throwing the query off. Are you receiving any error?
We might need more information however a couple of things:
If you are using mysql in unix based system tables are case sensitive.
Second, should you use ` instead of ' on the name of your columns?(and i would say they are both innecesary) like:
INSERT INTO tbl_Answer(`Answer`) or even better INSERT INTO tbl_Answer(Answer)
You need more fields in your table. You should have a field for each of your variables.
You have a minor problem with your query syntax.
$q2 = "INSERT INTO tbl_Answer
('A1', 'A2', 'A3', 'A4', 'A5')
VALUES
('$A1', '$A2', '$A3', '$A4', '$A5');";

MySQL insert into with subquery (on a junction table)

Basically, I'd like to insert into a junction table, getting values from another one.
Like this:
$sql= "INSERT INTO cars_owners (car_id, owner_id ) VALUES ($id, SELECT owners.owner_id FROM owners WHERE owners.owner_name='$name'))";
However, this one does not work, anyone know why?
You're mixing the syntax for inserting a set of values with the syntax for inserting from a SELECT statement. There is no VALUES keyword when inserting from a SELECT.
INSERT INTO cars_owners
(car_id, owner_id )
SELECT $id, owners.owner_id
FROM owners
WHERE owners.owner_name='$name'

PHP MYSQL - Insert into without using column names but with autoincrement field

I need to insert a long row with 32 fields into a MySQL table.
I'd like to do something like this:
$sql="insert into tblname values (... 32 fields ...)";
Obviously, it works fine if the fields are in the same order as the MySQL table fields. But, my table has an auto-increment id as it's first field.
What I want is to fill in all table names but the first (id) one.
Suggestions?
Just use NULL as your first value, the autoincrement field will still work as expected:
INSERT INTO tblname VALUES (NULL, ... 32 Fields ... )
Insert NULL into the auto-increment field.
I recommend that unless this is a hack script, you use field names. The rationale is that your code will break if you ever add a field to the table or change their order.
Instead, be explicit with field names, and it will go much better in the future.
Use NULL or 0 to insert an auto-incremented value as shown below:
-- Here
INSERT INTO tblname VALUES (NULL, ... 32 Fields ... )
-- Here
INSERT INTO tblname VALUES (0, ... 32 Fields ... )
We should omit any column values when we try without column name in insert query,
Advise if above information is wrong.
Here's a simple shortcut that I've used:
$fieldlist=$vallist='';
foreach ($_POST as $key => $value) {
$fieldlist.=$key.',';
$vallist.='\''.urlencode($value).'\','; }
$fieldlist=substr($fieldlist, 0, -1);
$vallist=substr($vallist, 0, -1);
$sql='INSERT INTO customer_info ('.$fieldlist.') VALUES ('.$vallist.')';
Please note that this code would be vulnerable to SQL Injection and should be modified to accommodate PDO's, but I felt this simplified script would more directly answer the question with regards to the originally posted code.

Categories