I have the following query which is working fine in inputting the arrays $stid and $attendance_status. However, I also want to populate the column called SCH_TIMESLOT with a fixed number 22 or eventually a string value which is the same for all records. I am getting an error stating:
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 '22' at line 2
This is my query:
for($i=0;$i<count($attendance_status);$i++){
mysql_query("INSERT INTO `ATTENDANCE` (`ID`, `STUDENT_ID`, `ATTENDANCE`, `SCH_TIMESLOT`) VALUES
(NULL, $stid[$i], $attendance_status[$i]), 22") or die (mysql_error());
Thank you for your help
You have missed out 22 from your VALUES brackets. It has to be inside.
<?php
for($i=0;$i<count($attendance_status);$i++){
mysql_query("INSERT INTO `ATTENDANCE` (`ID`, `STUDENT_ID`, `ATTENDANCE`, `SCH_TIMESLOT`) VALUES
(NULL, $stid[$i], $attendance_status[$i], 22)") or die (mysql_error());
You have a syntax error.
$stid[$i], $attendance_status[$i]), 22") you are closing the brackets before 22
Try it like this
"INSERT INTO `ATTENDANCE` (`ID`, `STUDENT_ID`, `ATTENDANCE`, `SCH_TIMESLOT`)
VALUES
(NULL, $stid[$i], $attendance_status[$i], 22)"
Try this, i replace ")" before "22" and move it after.
for($i=0;$i<count($attendance_status);$i++){
mysql_query("INSERT INTO `ATTENDANCE` (`ID`, `STUDENT_ID`, `ATTENDANCE`, `SCH_TIMESLOT`) VALUES
(NULL, $stid[$i], $attendance_status[$i], 22)") or die (mysql_error());
Change your sql query as below
for($i=0;$i<count($attendance_status);$i++){
mysql_query("INSERT INTO `ATTENDANCE` (`ID`, `STUDENT_ID`, `ATTENDANCE`, `SCH_TIMESLOT`) VALUES `(NULL, $stid[$i], $attendance_status[$i], 22") or die (mysql_error());`
Related
In PHP 4, how can I easily get the last id after an insert in db?
For example:
$queryInsert = "INSERT INTO `table` (`id`, `info`) VALUES (NULL, '".$INFO_VAR."');";
mysql_query($queryInsert) or die (mysql_error());
use mysql_insert_id();, you find the doc here
i'm making a query to insert some values to a table and update other in other table all this in the same function.
The problem i have is with the Insert query, when has been execute, give me a syntax error that you can see below.
I reviewed the code many times but i don't see any error. When I remove the Insert query works fine and if I remove de update query and leave just the insert give me the same error.
Here the query
if (isset($_POST['goodalt']) && isset($_POST['gengood'])){
$goodalt = mysqli_real_escape_string($con, $_POST['goodalt']);
$genid = mysqli_real_escape_string($con, $_POST['gengood']);
$res = mysqli_query($con, "SELECT * FROM `generators` WHERE `name` = '$genid'") or die(mysqli_error($con));
while($row = mysqli_fetch_assoc($res)) {
$genname = $row['name'];
}
mysqli_query($con, "INSERT INTO `user_accounts` (`username`, `accs`, `genid`, `gen-name`, `date`, `status`) VALUES ('$username', '$goodalt', '$genid', '$genname', '$date', '1'") or die(mysqli_error($con));
mysqli_query($con, "UPDATE `generator$genid` SET `status` = '3' WHERE `alt` = '$goodalt'") or die(mysqli_error($con));
}
Any help? Thanks.
Apart form error, there is a BIG issue about concurrency and threading model.
Every time you split INSERT/UPADATE in more separate PHP calls two mysql, you can analyze CAREFULLY if separate queries maintain DB consistent.
Consider carefully is two (or more) web requests (executing the same PHP script) can be safely run in parallel, especially on code where You did:
mysqli_query($con, "INSERT INTO `user_accounts` (`username`, `accs`, `genid`, `gen-name`, `date`, `status`) VALUES ('$username', '$goodalt', '$genid', '$genname', '$date', '1'") or die(mysqli_error($con));
mysqli_query($con, "UPDATE `generator$genid` SET `status` = '3' WHERE `alt` = '$goodalt'") or die(mysqli_error($con));
Insert and Updates are atomic, but here you call two separate queries
Your missing a close bracket in your insert...
mysqli_query($con, "INSERT INTO `user_accounts` (`username`, `accs`, `genid`, `gen-name`, `date`, `status`) VALUES ('$username', '$goodalt', '$genid', '$genname', '$date', '1')") or die(mysqli_error($con));
BUT you should also be using prepared statements and bind variables...
The issue that you're facing is because of the unnecessary usage of backticks or single quotation marks. You can follow the following article to make the changes and the issue should be sorted out.
Article here
Change these from:
mysqli_query($con, "INSERT INTO `user_accounts` (`username`, `accs`, `genid`, `gen-name`, `date`, `status`) VALUES ('$username', '$goodalt', '$genid', '$genname', '$date', '1'") or die(mysqli_error($con));
to
mysqli_query($con, "INSERT INTO user_accounts (username, accs, genid, gen-name, date, status) VALUES ('$username', '$goodalt', '$genid', '$genname', '$date', '1'") or die(mysqli_error($con));
Hope that helps!
$bzSendMail = mysqli_query($Connection, "INSERT INTO messages_inbox (from, towho, subject, text, rcvdat) VALUES ('$MyID', '$SenderID', '$subject', '$text' ,'$sentat')");
I'm trying to make this query works, but it keeps showing me the following 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 'from, towho, subject, text, rcvdat) VALUES ('1', '2', 'd', 'd' ,'2014-09-07 17:0' at line 1
Anyone can help me?
you are using
FROM
as a column name in your table. You can use '' to specify the column name but it is always better not to use that kind of names as your column names.
$bzSendMail = mysqli_query($Connection, "INSERT INTO messages_inbox (`from`, `towho`, `subject`, `text`, `rcvdat`) VALUES ('$MyID', '$SenderID', '$subject', '$text' ,'$sentat')");
From is a key word in Mysql use backward quotes to skip this as follows
$bzSendMail = mysqli_query($Connection, "INSERT INTO messages_inbox (`from`, `towho`, `subject`, `text`, `rcvdat`) VALUES ('$MyID', '$SenderID', '$subject', '$text' ,'$sentat')");
from is a reserved word in sql. Make backticks around it.
My string field won't insert into my database.
(The columns follower_username and following_username they are VARCHAR(200) don't insert )
The: follower and following column values insert work.
mysql_query("INSERT INTO `follow` (`follower`, `following`, `follower_username`, `following_username`) VALUES ('".$userid."', '".$get_user_id."', '".$username."', '".$get_user."')");
Strings:
$get_user = mysql_real_escape_string($row['username']);
$get_user_id = (int)mysql_real_escape_string($row['id']);
$userid = (int)mysql_real_escape_string($user_data['id']);
$username = mysql_real_escape_string($user_data['username']);
I have no idea what to do, whether it is the PHP or the database itself :S
Thanks in advance :)
You could try echoing the mysql statement just before the mysql_query, i.e.
echo "INSERT INTO `follow` (`follower`, `following`, `follower_username`, `following_username`) VALUES ('".$userid."', '".$get_user_id."', '".$username."', '".$get_user."')";
and check if the string is what you expected it to be. If it is what you expected, try manually copying the string and pasting it into the mysql console and see if any errors occur.
try this :
mysql_query("INSERT INTO follow (`follower`, `following`, `follower_username`, `following_username`) VALUES ('".$userid."', '".$get_user_id."', '".$username."', '".$get_user."')");
don't use single quotes around table name.
Try adding mysql_error to your statement to find out what error is it so you can fix it:
mysql_query("INSERT INTO `follow` (`follower`, `following`, `follower_username`,
`following_username`) VALUES ('".$userid."', '".$get_user_id."', '".$username."',
'".$get_user."')") or die (mysql_error());
For debug and simple work I recommended you store SQL query in variable.
$query = "INSERT INTO `follow` (`follower`, `following`, `follower_username`, `following_username`) VALUES ('".$userid."', '".$get_user_id."', '".$username."', '".$get_user."')";
echo "DEBUG:".$query;
mysql_query($query);
Try this:
$objQuery = mysql_query("INSERT INTO `follow` (`follower`, `following`, `follower_username`,
`following_username`) VALUES ($userid, $get_user_id, '".$username."',
'".$get_user."')") or die (mysql_error());
if(!$objQuery){
echo "something went wrong!";
}
I am using php and trying to add a item to my MySQL database.
If I use the following code it works:
mysql_query("INSERT INTO `intranet`.`product_form` (`id`, `ProductName`, `ProductInitiatedBy`) VALUES (NULL, 'item1', 'item2')") or die("Could not perform select query - " . mysql_error());;
However, if I use the following code it doesn't work:
$product_name=("tom");
mysql_query("INSERT INTO `intranet`.`product_form` (`id`, `ProductName`, `ProductInitiatedBy`) VALUES (NULL, $product_name, 'item2')") or die("Could not perform select query - " . mysql_error());;
I get an error that says:
Could not perform select query - Unknown column 'ProductName1234' in
'field list'
The ProductName1234 is the data from $product_name and should be the data I am trying to add and not the column.
When you insert strings like that, you need to surround them in quotes, else MySQL will think you are trying to specify a column from somewhere to insert data from.
mysql_query("INSERT INTO intranet.product_form (id, ProductName, ProductInitiatedBy) VALUES (NULL, \"$product_name\", 'item2')");
Change:
mysql_query("INSERT INTO `intranet`.`product_form` (`id`, `ProductName`, `ProductInitiatedBy`) VALUES (NULL, $product_name, 'item2')") or die("Could not perform select query - " . mysql_error());;
to:
mysql_query("INSERT INTO `intranet`.`product_form` (`id`, `ProductName`, `ProductInitiatedBy`) VALUES (NULL, '$product_name', 'item2')") or die("Could not perform select query - " . mysql_error());
You need to add ' to $product_name:
VALUES (NULL, '$product_name', 'item2')
^ ^