Note: I know MySQL is not ideal, but it's already done.
Note: I understand commited is incorrectly spelled.
QUERY 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 'desc, cost, price, count, commited, ordered, cat, subcat, notes, rev_identifier,' at line 1
Query was INSERT INTO stock (name, desc, cost, price, count, commited, ordered, cat, subcat, notes, rev_identifier, sources, totalused, amountfailed) VALUES ('name', 'desc', '1', '1', '1', '0', '0', 'Apple', 'DEV', 'none', '1', 'none', '0', '0')
Query is written:
$db->query("INSERT INTO stock (name, desc, cost, price, count, commited, ordered, cat, subcat, notes, rev_identifier, sources, totalused, amountfailed) VALUES
('$name', '$desc', '$cost', '$price', '$count', '$commited', '$ordered', '$cat', '$subcat', '$notes', '$rev_identifier', '$sources', '$totalused', '$amountfailed')");
The $'s are...
$name = mysql_real_escape_string(strip_tags($_POST['name']));
$desc = mysql_real_escape_string(strip_tags($_POST['desc']));
$cost = mysql_real_escape_string(strip_tags($_POST['cost']));
$price = mysql_real_escape_string(strip_tags($_POST['price']));
$count = mysql_real_escape_string(strip_tags($_POST['count']));
$commited = 0;
$ordered = 0;
$cat = mysql_real_escape_string(strip_tags($_POST['cat']));
$subcat = mysql_real_escape_string(strip_tags($_POST['subcat']));
$notes = mysql_real_escape_string(strip_tags($_POST['notes']));
$rev_identifier = mysql_real_escape_string(strip_tags($_POST['rev_identifier']));
$sources = mysql_real_escape_string(strip_tags($_POST['sources']));
$totalused = 0;
$amountfailed = 0;
count is a reserved word in SQL, and you have a column called count. You'll need to put the column name in `backticks` or, better yet, rename the column.
You have used count, desc column name, it is reserved word so use backtick (“`”)` to wrap that column names
like
$db->query("INSERT INTO stock (name, `desc`, cost, price, `count`, commited, ordered, cat, subcat, notes, rev_identifier, sources, totalused, amountfailed) VALUES
('$name', '$desc', '$cost', '$price', '$count', '$commited', '$ordered', '$cat', '$subcat', '$notes', '$rev_identifier', '$sources', '$totalused', '$amountfailed')");
Related
I am inserting values in mysql database but i am receiving same error again and again. I also looked over stackoverflow but none of those questions solved my query. I also re-checked my query values with database column. All values are mapping to their corresponding columns in database:
Here is the PHP code for insert:
"INSERT INTO posts VALUES('','$title', '$category', '$tags' '$details', '$added_by', '$user_to', '$date_added', 'no', 'no', 0)"
Database fields are as follow:
id, title, category, tags, details, added_by, user_to, date_added, user_closed, deleted, likes
I have tried finding solution over internet and stackoverflow but every solution referred that columns count is mismatched in either database or insert query. But i didn't find this error in my code.
$query = mysqli_query($this->con, "INSERT INTO posts VALUES('','$title', '$category', '$tags' '$details', '$added_by', '$user_to', '$date_added', 'no', 'no', 0)");
I expect values to enter in database but they are not entering and i am facing error "Column count doesn't match value count at row 1"
use this structure on insert:
$strQuery = "INSERT INTO posts (id, title, category, tags, details, added_by, user_to, date_added, user_closed, deleted, likes) VALUES('','$title', '$category', '$tags' '$details', '$added_by', '$user_to', '$date_added', 'no', 'no', 0)";
$query = mysqli_query($this->con, $strQuery);
if ID is Auto-incrment key you can make the insert without it... like this (10 Column and Values):
$strQuery = "INSERT INTO posts (title, category, tags, details, added_by, user_to, date_added, user_closed, deleted, likes) VALUES('$title', '$category', '$tags' '$details', '$added_by', '$user_to', '$date_added', 'no', 'no', 0)";
$query = mysqli_query($this->con, $strQuery);
I've got two databases - one for restaurant users (user_reg), and one for the restaurant menu items (restaurant_menu).
One form stores this particular variable $rname in user_reg, which is the restaurant name. It then creates a table called $rname in restaurant_menu, with columns like item_name, category, price, etc.
What I want to do is insert records into the appropriate table of $rname, however since it is the specific user adding items, I need their items to go under the table for their specific restaurant name.
It works perfectly when I use a specific table name such as "kfc"
$sql = "INSERT INTO kfc (item_name, price, description, category, origin, sub_category) VALUES (('".$item_name."'), ('".$price."'), ('".$description."'), ('".$category."'), ('".$origin."'), ('".$sub_category."'))";
However this is what I have working:
$item_name = $_POST["item_name"];
$price = $_POST["price"];
$description = $_POST["description"];
$category = $_POST["category"];
$origin = $_POST["origin"];
$sub_category = $_POST["sub_category"];
$rname = $_POST["rname"];
$email = $_POST["email"];
$sql = "INSERT INTO $rname (item_name, price, description, category, origin, sub_category) VALUES (('".$item_name."'), ('".$price."'), ('".$description."'), ('".$category."'), ('".$origin."'), ('".$sub_category."'))";
It simply needs to record those inputted values into table $rname. Can this be done across two databases?
As mentioned, calling each value by it's variable would be best.
The syntax for inserting values is
INSERT INTO table (column1, column2, column3)
VALUES (value1, value2, value3);
So changes the values section to reflect the variables for each column.
/* Here we are going to use extract function which converts array key's into accessible variable. Not need to declare variables and store post value in that. However it's not good practice to send table name from User Interface It may leads to hack your system*/
Code showing here
extract($_POST);
$sql = "INSERT INTO $rname (item_name, price, description, category, origin, sub_category) VALUES ('$item_name','$price', '$description', '$category', '$origin', '$sub_category')";
Try This
for Mysql
$sql = mysql_query($dbconnectivity,("INSERT INTO $rname (item_name, price, description, category, origin, sub_category) VALUES ('$item_name', '$price', '$description', '$category', '$origin', '$sub_category')");
For Mysqli
$sql = mysqli_query($dbconnectivity,("INSERT INTO $rname (item_name, price, description, category, origin, sub_category) VALUES ('$item_name', '$price', '$description', '$category', '$origin', '$sub_category')");
Put this in your sql variable
$sql = "INSERT INTO $rname (item_name, price, description, category, origin, sub_category) VALUES ('$item_name', '$price', '$description', '$category', '$origin', '$sub_category'";
I prefer the mysqli_query($connectionhandler, $sql); method
I getting error "Query error!" with this code:
$result = $connection->query("INSERT INTO EMP_TBLE (NAME, AGE, CATEGORY, UPDATE_COUNT) VALUES('$NAME', '$AGE', $CAT, 1
ON DUPLICATE KEY UPDATE UPDATE_COUNT = UPDATE_COUNT + 1"));
echo "<br>".$result;die;
I also check different example like this example was running success but I found error in above code for my demo project.
The closing brace for VALUES is missing. There is also a non-needed brace at the end of your code line.
Please change
$result = $connection->query("INSERT INTO EMP_TBLE (NAME, AGE, CATEGORY, UPDATE_COUNT) VALUES('$NAME', '$AGE', $CAT, 1 ON DUPLICATE KEY UPDATE UPDATE_COUNT = UPDATE_COUNT + 1"));
to
$result = $connection->query("INSERT INTO EMP_TBLE (NAME, AGE, CATEGORY, UPDATE_COUNT) VALUES('$NAME', '$AGE', $CAT, 1) ON DUPLICATE KEY UPDATE UPDATE_COUNT = UPDATE_COUNT + 1");
Apparently you did not close the parentesis in the right place:
$result = $connection->query("INSERT INTO EMP_TBLE (NAME, AGE, CATEGORY,
UPDATE_COUNT) VALUES('$NAME', '$AGE', $CAT, 1)
ON DUPLICATE KEY UPDATE UPDATE_COUNT = UPDATE_COUNT + 1");
Also... you should at least escape all the variables before concatenating in queries like this to avoid SQL injection
I'm trying to get the last inserted id of multiple inserted rows.
record_id is auto increment
$sql = "INSERT INTO records (record_id, user_id, status, x) values ";
$varray = array();
$rid = $row['record_id'];
$uid = $row['user_name'];
$status = $row['status'];
$x = $row['x'];
$varray[] = "('$rid', '$uid', '$status', '$x')";
$sql .= implode(',', $varray);
mysql_query($sql);
$sql2 = "INSERT INTO status_logs (id, record_id, status_id, date, timestamp, notes, user_id, x) VALUES";
$varray2[] = "(' ', mysql_insert_id(), '$status', '$uid', '$x')";
$sql2 .= implode(',', $varray2);
mysql_query($sql2);
This is the result:
INSERT INTO records (record_id, user_id, notes, x) values ('', '1237615', 'this is a note', 'active')
INSERT INTO status_logs (log_id, record_id, status_id, date, timestamp, notes, user_id, x) VALUES('', INSERT INTO records (record_id, user_id, notes, x) values ('', '1237615', 'this is a note', 'active')
INSERT INTO status_logs (log_id, record_id, status_id, date, timestamp, notes, user_id, x) VALUES('', mysql_insert_id(), '1', '2013:05:16 00:00:01', '', this is a note'', '1237615', 'active'), '1', '2013:05:16 00:00:01', '', this is a note'', '1237615', 'active')
There is no value for mysql_insert_id().
You're mixing php function mysql_insert_id() and SQL INSERT statement syntax.
Either use MySQL function LAST_INSERT_ID() in VALUES clause of INSERT statement
INSERT INTO records (user_id, notes, x) VALUES('1237615', 'this is a note', 'active');
INSERT INTO status_logs (record_id, status_id, date, timestamp, notes, user_id, x)
VALUES(LAST_INSERT_ID(), '1', ...);
^^^^^^^^^^^^^^^^^
or retrieve the last inserted id by making a separate call to mysql_insert_id() right after first mysql_query(). And then use that value when you as a parameter to your second query.
$sql = "INSERT INTO records (user_id, ...)
VALUES(...)";
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error()); //TODO beter error handling
}
$last_id = mysql_insert_id();
// ^^^^^^^^^^^^^^^^^^
$sql2 = "INSERT INTO status_logs (record_id, ...)
VALUES $last_id, ...)";
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error()); //TODO beter error handling
}
Note:
You don't need to specify auto_incremented column in column list. Just omit it.
Use at least some sort of error handling in your code
On a side note: Instead of interpolating query strings and leaving it wide open to sql-injections consider to use prepared statements with either mysqli_* or PDO.
Unless I mis-reading your code, you're calling the PHP function mysql_insert_id from within the SQL?
What you need to do is grab that into a PHP variable first, then use the variable in the SQL. Something like this:
// Run the first query
mysql_query($sql);
// Grab the newly created record_id
$recordid= mysql_insert_id();
Then in the second INSERTs just use:
$varray2[] = "(' ', $recordid, '$status', '$uid', '$x')";
I have the following sql statement:
$sql = "INSERT INTO jos_vm_product_details (variant_id, global_variant_id,
code, size, details, color, top, price)
VALUES ('$variant_id', '$global_variant_id', '$code', '$size',
'$details', '$color', '$top', '$price')";
This is just a basic INSERT sql. Is there a way for it to Update a record if the 'variant_id' is already present. If the 'variant_id' is not already there it should Insert a new one.
Any help would be appreciated
You can check INSERT ... ON DUPLICATE KEY UPDATE
$sql = "INSERT INTO jos_vm_product_details (variant_id, global_variant_id,
code, size, details, color, top, price)
VALUES ('$variant_id', '$global_variant_id', '$code', '$size',
'$details', '$color', '$top', '$price')
ON DUPLICATE KEY UPDATE
global_variant_id = VALUES(global_variant_id),
code = VALUES(code),
size = VALUES(size),
details = VALUES(details),
color = VALUES(color),
top = VALUES(top),
price = VALUES(price)";
In MySQL you can use ON DUPLICATE KEY
INSERT INTO jos_vm_product_details (variant_id, global_variant_id,
code, size, details, color, top, price)
VALUES ('$variant_id', '$global_variant_id', '$code', '$size',
'$details', '$color', '$top', '$price')";
ON DUPLICATE KEY UPDATE `code`=VALUES(`code`);
In T-SQL (2008 an higher) you could use MERGE