Error in executing insertion query - php

Isn't this query correct?
$insert = INSERT INTO geninfo (S.N, Name, Address, DOB) VALUES ('Suresh','Ratnanagar','1989/04/10');
I got following error, please help I am a beginner.
Parse error: syntax error, unexpected 'INTO' (T_STRING) in
C:\xampp\htdocs\google.php on line 9

$insert = "INSERT INTO `geninfo` (`S.N`, `Name`, `Address`, `DOB`) VALUES ('Suresh','Ratnanagar','Missing address here','1989/04/10');";
Note that I have also corrected your MySQL query. S.N refers to the column named N on the table named S, which I'm pretty certain is not what you wanted.
Also I just realised you have four columns, but only three values. Fixed that too.

You have no quotes, it should be like this:
$insert = "INSERT INTO geninfo (S.N, Name, Address, DOB) VALUES ('Suresh','Ratnanagar','1989/04/10')";
upd
It seems you are storing date of birth as a string, not as a timestamp (or similar) which is not a good idea

You need to give a (NULL or '') for the S.N field and quotes should be given before and after each and every value.
$insert = "INSERT INTO geninfo (S.N, Name, Address, DOB) VALUES
('', 'Suresh','Ratnanagar','1989/04/10')";
Moreover the field name S.N could create problems. Let me know if this works.

$insert = "INSERT INTO geninfo (S.N, Name, Address, DOB) VALUES ('Suresh','Ratnanagar','1989/04/10')";

Related

Insert value does not match column list:1136

I am trying to insert a record to a table with 2 column but I get this error.
My error starts in part of the execute. Anyone that can help me out with this ?
I am using PDO.
My code:
global $conn_kl;
$sql = $conn_kl->prepare("INSERT INTO order_producten VALUES (?,?)");
$sql->execute(array($product_id, $bewerking_id));
The issue is here:
INSERT INTO order_producten VALUES (?,?)
here columns are not defined in this query, in this case it is expected that you have to pass the values for all columns in the table. But you want to insert the values for only 2 columns, so please please specify that columns names like:
INSERT INTO order_producten(column_name1, column_name2) VALUES (?,?)
order_producten will have more or less than two columns and you are setting only two values.
Please specify columns after table name. for example,
INSERT INTO order_producten(id, name) VALUES(?, ?)
For example, code something like this were working for me:
global $conn_kl;
$sql = $conn_kl->prepare("INSERT INTO `order_bewerkingen` VALUES (null, ?, ?, ?)");
$sql->execute(array($order_id, $method, $position));

php mysql insert into error

I get this error
Error: You have an error in your SQL syntax; check the
manual that corresponds to your MySQL server version for the right
syntax to use near 'character (name, address, birthplace, age,
birthdate) VALUES ( 'Alex','Villa V' at line 1
here
$sql = "INSERT INTO character (name, address, birthplace, age, birthdate)
VALUES ( '$name','$address','$birthplace', '$age','$birthplace')";
if ($conn -> query($sql) === true){
echo "New record created successfully ";
} else{
echo "Error: ".$sql."<br>".$conn -> error;
}
$conn -> close();
Everything else seems to be working fine i checked the syntax however the
error wont disappear.
character is a reserved word and needs to be escaped with backticks.
INSERT INTO `character` (name, address, birthplace, age, birthdate)
VALUES ( 'Alex','Villa Verde Subd.','July 5 1993', '17','July 5 1993')
BTW you should store the date values in a date column and not as a string. And storing the age is also not a good idea since it needs to be adjusted. It can be calculated from the birthdate.
character is a reserved word in MySQL. you have to escape it with backticks:
$sql = "INSERT INTO `character` (name, address, birthplace, age, birthdate) VALUES ( '$name','$address','$birthplace', '$age','$birthplace')";
You cant use character as table name as its reserve keyword. Use backticks
character is a MySQL reserved word.
In order to do this use backticks, for example:
INSERT INTO `character`
Please see here for further information:
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html

An error in SQL syntax

The SQL statement is as follows:
$sql = "
INSERT INTO usertable
(
userid,,
name,
username,
password,
typeofuser,
dateofaddition,
createdby,
status
)
VALUES (
$userid,
'$empname',
'$username',
'$password',
'$usertype',
'$doa',
'$createdby',
'$radiobt'
)
";
Remove the comma from after userid and correctly include your variables
$sql = "INSERT INTO usertable
(
userid,
name,
username,
password,
typeofuser,
dateofaddition,
createdby,
status
)
VALUES
(
".$userid.",
".$empname.",
".$username.",
".$password.",
".$usertype.",
".$doa.",
".$createdby.",
".$radiobt."
)";
Make sure you prepare this statement before getting it into the DB! (more info: Prepared statement (Wikipedia))
There is a double ,, after userid.
$sql = "INSERT INTO usertable(userid,name,username,password,typeofuser,dateofaddition,createdby,status) VALUES ($userid,'$empname','$username','$password','$usertype','$doa','$createdby','$radiobt')"
Let us take things step by step:
Field names in the query match the field name in the table (they should exactly be the same even the case should match).
What and from where are the values for the varaibles getting generated?
Do the values correspond to the datatype of the fields in the table?
For eg. If there is a variable which expects to receive integer as values and it is made to store strings or characters, mysql is bound to give an error message.
Check for these and let me know.

Auto increment id

This is query where i want to insert new data to database. Before this id is auto by using old system and now i make a new form by php. This is my query and now i want make auto increment id.How to make a auto increment id.Please Help me if anyone know.
$sql="INSERT INTO $tbl_name(id, name, icno, email, applyIntake_id, modeOfStudy_id,
choice1_id, dob, nationalityType, gender_id, race_id, highestEducation_id,
address1, address2, postcode, city, state_id, permanent_address1,
permanent_address2, permanent_postcode, permanent_city, permanent_state,
telephoneNo, mobileNo)
VALUES('$name', '$icno', '$email', '$id', '$applyIntake_id', '$modeOfStudy_id',
'$choice1_id', '$dob', '$nationalityType', '$gender_id', '$race_id',
'$highestEducation_id', '$address1', '$address2', '$postcode', '$city',
'$state_id', '$permanent_address1', '$permanent_address2',
'$permanent_postcode', '$permanent_city', '$permanent_state',
'$telephoneNo', '$mobileNo')";
$result=mysql_query($sql);
Try this for MYSQL DB..
ALTER TABLE
`TABLE_SCHEMA`.`TABLE_NAME`
CHANGE COLUMN
`ID` `ID` INT(6) NOT NULL
AUTO_INCREMENT;
Ok, one thing first: you should escape your parameters! See here.
To solve your problem: you can define a field in the database as auto_increment, what will automatically increment the id if left empty.
Amir you have 3 errors in above insert.
The columns count match but you try to insert into #tbl_name(email) value of $id
Please set the id field as autoincrement the answer is in above posts.
I think when preparing PHP $sql variable you have to use "." as string operator to get proper SQL INSERT statement with data from your variables - currently the below INSERT will insert instead of your name variable $name as string into DB.
Exmaple :
$name = 'Tom';
$email = 'tom#o2.pl'
$sql = "insert into table(name,email) VALUES (".$name.",".$email.");";
Then use following insert but apply the example of string join above - it should work:
$sql="INSERT INTO $tbl_name(name, icno, email, applyIntake_id, modeOfStudy_id,
choice1_id, dob, nationalityType, gender_id, race_id, highestEducation_id,
address1, address2, postcode, city, state_id, permanent_address1,
permanent_address2, permanent_postcode, permanent_city, permanent_state,
telephoneNo, mobileNo)
VALUES('$name', '$icno', '$email', '$applyIntake_id', '$modeOfStudy_id',
'$choice1_id', '$dob', '$nationalityType', '$gender_id', '$race_id',
'$highestEducation_id', '$address1', '$address2', '$postcode', '$city',
'$state_id', '$permanent_address1', '$permanent_address2',
'$permanent_postcode', '$permanent_city', '$permanent_state',
'$telephoneNo', '$mobileNo')";
$result=mysql_query($sql);
ALTER TABLE document MODIFY COLUMN document_id INT AUTO_INCREMENT;
There are a couple of reasons that your SQL might not work. First, you must re-specify the data type (INT in this case). Also, the column you are trying to alter must be indexed (it does not have to be the primary key, but usually that is what you would want). Furthermore, there can only be one AUTO_INCREMENT column for each table. So, you may wish to run the following SQL (if your column is not indexed):
ALTER TABLE document MODIFY document_id INT AUTO_INCREMENT PRIMARY KEY;
You can find more information in the MySQL documentation: http://dev.mysql.com/doc/refman/5.1/en/alter-table.html for the modify column syntax and http://dev.mysql.com/doc/refman/5.1/en/create-table.html for more information about specifying columns.

Select Max inside a mysql Insert Query

Max(sequence) + 1 (based on my knowledge) should be returning the highest sequence with $_GET['business_id'] in the database + 1 - existing values in the database are 0, 1, and 3 - so max(sequence)+1 should be 4 - so something must be wrong with the line of code. Any ideas?
$insertQuery = "
INSERT INTO owner_business_media
(business_id, sequence, type, filename, title, secret)
VALUES (
'".$_GET[businessid]."',
'(SELECT MAX(sequence)+1 FROM owner_business_media WHERE business_id=".$_GET['businessid'].")',
'$type',
'$fullfile',
'$filename',
'1')
";
Remove single quotes that surround the inner SELECT and instead of the regular INSERT go with INSERT ... SELECT:
$insertQuery = "
INSERT INTO owner_business_media
(business_id, sequence, type, filename, title, secret)
SELECT
'".intval($_GET['businessid'])."',
(SELECT MAX(obm.sequence)+1 FROM owner_business_media obm WHERE obm.business_id=".intval($_GET['businessid']).") AS next,
'$type',
'$fullfile',
'$filename',
'1'
";
Also, never embed a GET variable directly without validating or sanitizing it's contents (see intval($_GET['businessid'])). Otherwise the code gets exposed to SQL injection.
Almost the same as the other answer, yet not completely the same:
$insertQuery = "
INSERT INTO owner_business_media
(business_id, sequence, type, filename, title, secret)
SELECT
'".intval($_GET['businessid'])."',
MAX(sequence)+1 AS next,
'$type',
'$fullfile',
'$filename',
'1'
FROM owner_business_media
WHERE business_id=".intval($_GET['businessid']);
Maybe it would be safer to use IFNULL with sequence (in case the table is empty), like this:
MAX(IFNULL(sequence, 0))+1 AS next

Categories