I have this SQL:
$sql = "INSERT INTO orders (ID, Order_ID, Status, FName, LName, Email,
Phone)VALUES ($UID, $orderID, 'Pending', '$fname', '$lname', '$email',
'$phone');
INSERT INTO orders_inventory (Order_invID, Item_ID, Order_ID, Quantity)
VALUES
(NULL, $item_ID, $orderID, 1);";
This is how I connect it:
if(mysqli_query($db, $sql)){
echo "three";
}
I did an echo on the $sql and this is what I got:
INSERT INTO orders (ID, Order_ID, Status, FName, LName, Email, Phone)
VALUES (92, 625015841, 'Pending', '1', '1', '1#1', '1');
INSERT INTO orders_inventory (Order_invID, Item_ID, Order_ID, Quantity)
VALUES (NULL, 1, 625015841, 1);
The SQL works when I paste it into the database manually, but the database crashes when I use the website PHP. The $DB is to connect to the database and it works because I tested it, and I have also been using it throughout the whole website.
I then did an error check using mysqli_error(db) and I get this error:
"You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO orders_inventory (Order_invID, Item_ID, Order_ID, Quantity) VALUES (' at line 2"
Help would be greatly appreciated as I'm very stuck and don't know how to get around this or fix this problem
You're attempting to run two queries at once, which mysqli_query will not do. However you can use mysqli_multi_query instead:
if(mysqli_multi_query($db, $sql)){
echo "three";
}
Related
Using Variable in MySQL
I have tried many possibilities and consulted a number of sources but still not have been
able to insert a string into a MySQL command in php.
Code below works well
$SQL = 'INSERT INTO tb_addressbook (`ID`, `First_Name`, `Last_Name`, `Address`) VALUES (\'24\', \'JJ\', \'Gates\', \'Microsoft\');';
Code below does not work
$SQL = 'INSERT INTO tb_addressbook (`ID`, `First_Name`, `Last_Name`, `Address`) VALUES (\'27\', \''.'"$first"'.'\''.', \'Gates\', \'Microsoft\');';
Can you help?
P.S. Is there a special way to insert a string for numbers?
Hugh
hugh#hahaggerty.com
Try like this :
$SQL = "INSERT INTO tb_addressbook (ID, First_Name, Last_Name, Address) VALUES ('27', '".$first."', 'Gates', 'Microsoft')";
I am working on building an employee database but seem to have run into an interesting issue.
When I run my query to add a new user/employee, I get the error:
Column count doesn't match value count at row 1
From what I have researched, this seems to be an error with inserting more/less values than what is declared in the first part of an insert statement example:
INSERT INTO table (col1, col2) VALUES (val1, val2, val3)
The thing is though, I have looked over my query and the columns and values match perfectly (count wise). I have even looked for things in my query such as missing quotes, commas, etc.
Here is my code (query):
$db->query("INSERT INTO userdata (
Username,
Email,
Phone,
Password,
FirstName,
LastName,
Address,
City,
State,
Zip,
JobTitle,
UserGroup,
JobStatus,
Points,
Status,
BirthDate,
HireDate,
HourlyRate,
DateAdded,
SSN
) VALUES (
'$Username',
'$Email',
'$Phone',
'$Password',
'$FirstName',
'$LastName',
'$Address',
'$City',
'$State',
'$Zip',
'$JobTitle',
'$Group',
'$JobStatus',
0,
'$Status',
'$BirthDate',
'$HireDate',
'$HourlyRate'
'$TodaysDate',
'$SSN'
)") or die(mysqli_error($db));
Some things to note:
This not all of the columns in the table have data inserted here (I think its possible to do this and things such as auto incrementing ID's will fill themselves in and others will be left blank)
From the variable dumps I have done, all of these variables are valid.
I am really confused about this and any help would be appreciated.
Check the following portion again:
INSERT INTO userdata(...,
JobStatus,
UserGroup,
Points,
UserGroup,
Status,
.....
,)VALUES(...,
$JobStatus',
0,
'$Group',
'$Status',
......
)
In values(, ?, ?, ?), after jobstatus, there should be UserGroup and then Points. And UserGroup appeared twice.
I'm a newbie and I've been trying for over an hour to solve this simple query:
mysql_query("INSERT INTO `tracks` (artistID, albumID, format, trackID, niceTitle, title, trackNumber, description, pictureURL, playCount) VALUES('$artistID', '$albumID[$i]', 'hq','$ID[0]', '$trackName', '$title', '$j', '$description', '$pictureURL', '$playCount'") or die(mysql_error());
I just get this error every time:
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 '' at line 1
I've done mysql_escape_string() on all variables too. Any ideas?
You are missing the final closing ):
mysql_query("INSERT INTO `tracks` (artistID, albumID, format, trackID, niceTitle, title, trackNumber, description, pictureURL, playCount) VALUES('$artistID', '$albumID[$i]', 'hq','$ID[0]', '$trackName', '$title', '$j', '$description', '$pictureURL', '$playCount')") or die(mysql_error());
You have no ending parenthesis ")" in your query
echo "</br></br></br>" . $sql;
mysql_query($sql) or die("Entry not added to database.");
is spitting out:
INSERT INTO potentials (id, firstname, lastname, age, email, phone, twitter, timeofday, dayofweek, address, city, state, zip, joindate, parentname, parentnumber) VALUES (null, 'Rick', 'Bross', '14', 'rbross3#gmail.com', '8164896991', '#rick_bross', 'After 5:30PM', 'Weekdays', '1234 Cooper', 'Raymore', 'MO', '64130', '2013-04-09 20:10:06', 'Rick Bross II', '1234123412')Entry not added to database.
Why isn't it being inserted correctly? Can I check if one of the strings isnt fitting into my database column (type type char error or something)?
mysql_query($sql) or die(mysql_error());
try msql_error() function. It is globally show every error happens with your sql execution
I would like to use two different insert statements with two different tables such as
<?
mysql_query("INSERT INTO Customer (ID,Name,Subject, OrderDate) VALUES ('$ID', '$name', '$status', '$ODate')");
mysql_query("INSERT INTO Order (ID,Subject, Department, Status, OrderDate, Receive, Notes) VALUES ('$ID', '$status', 'Financial', 'Financial Department', '$ODate', 'NO', 'Notes')");
?>
It just works with the first table and does not work with the second table.
Can some one help solving this problem?
Thanks
You need to check for errors:
<?php
$query1 = "INSERT INTO Customer (ID,Name,Subject, OrderDate) VALUES ('$ID', '$name', '$status', '$ODate')";
if(!mysql_query($query1)) {
throw new Exception(mysql_error());
}
$query2 = "INSERT INTO Order (ID,Subject, Department, Status, OrderDate, Receive, Notes) VALUES ('$ID', '$status', 'Financial', 'Financial Department', '$ODate', 'NO', 'Notes')";
if(!mysql_query($query2)) {
throw new Exception(mysql_error());
}
I'm guessing you are getting an error because Order is a reserved word in MySQL and should be escaped accordingly:
$query2 = "INSERT INTO `Order` (ID,Subject, Department, Status, OrderDate, Receive, Notes) VALUES ('$ID', '$status', 'Financial', 'Financial Department', '$ODate', 'NO', 'Notes')";
It also seems to me like you're inserting a fixed value as a primary key - are you sure that's what you want?
As I said in the comments, you should stop using mysql_ functions completely and use MySQLi or PDO instead.
First of all thanks to DCoder who helped me to solve the problem and advised me to use PDO and MySQLi.
The problem was with the tabel name Order, when I replaced it with a new name, it works fine.
I thought the problem with using two mysql_query but it is not. The table name that I used is a reserved word in MySQL.
Thanks