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);
Related
This question already has answers here:
Retrieve id of each INSERT statement in multi query
(5 answers)
Closed 3 years ago.
I've been working with this project, now am stuck where i have to insert the id of the first query of which it identifies the relations of that table to others, i want the id of the first query to be saved as a variable and then be inserted into the following queries
I've tried to set value to be used but its not working, as i want all these to work in one multi-query, down here are some code of the queries
$query = "INSERT INTO guests (
user_id,
first_name,
last_name,
nationality,
status,
sign_up)
VALUES (
'$user_id',
'$first_name',
'$last_name',
'$nationality',
'$status',
NOW());";
$query = "SELECT #last_id := LAST_INSERT_ID();"
$query .= "INSERT INTO bookings (
user_id,
guest_id,
coming_from,
going_to,
date_in,
date_out,
sign_up)
VALUES (
'$user_id',
#last_id,
'$coming_from',
'$going_to',
'$date_in',
'$date_out',
NOW());";
$query .= "INSERT INTO preference (
guest_id,
prefer,
alergy,
sign_up)
VALUES (
#last_id,
'$prefer',
'$alergy',
NOW());";
$query .= "INSERT INTO rooms (
user_id,
guest_id,
room_number,
room_type,
room_price,
sign_up)
VALUES (
'$user_id',
#last_id,
'$room_number',
'$room_type',
'$room_price',
NOW())";
if(mysqli_multi_query($conn, $query))
{
echo "Guest Received successfully!";
} else {
echo "Failed! Data input error!";
}
}
i expected it to fetch the first query guest id and insert it into #last_id column, Kindly any ideas?
On occasion, I've had to use this:
SELECT `auto_increment` FROM INFORMATION_SCHEMA.TABLES WHERE table_name = 'tablename'
or
SELECT AUTO_INCREMENT FROM information_schema.tables WHERE table_name = your_tablename' AND table_schema = DATABASE( ) ;
Perhaps that will work for you
References:
https://dev.mysql.com/doc/refman/8.0/en/show-table-status.html
https://php.net/manual/en/mysqli.insert-id.php
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 want the date to be added to an orders table I have created, once the user proceeds to checkout. The code I currently has just prints:
"Error: Column count doesn't match value count at row 1"
Here's my code:
$sql = "INSERT INTO orders (customer_id, total, order_date) VALUES ('$name', '$total', 'DATETIME: Auto NOW()', NOW() )";
The name and total columns store but the date does not. How can I solve this?
This way:
$sql = "INSERT INTO orders (customer_id, total, order_date) VALUES ('$name', '$total', NOW())";
You want to insert data inside customer_id, total, order_date (3 rows) but you are sendig '$name', '$total', 'DATETIME: Auto NOW()', NOW(), FOUR.
Your error means that you that the number of fields does not match the number of values. That seems correct: your query tries to insert 4 values into 3 fields. You'd probably have to rewrite the query to
$sql = "INSERT INTO orders (customer_id, total, order_date) VALUES ('$name', '$total', NOW() )";
It looks like you need 3 values: customer_id, total, order_date
but you give 4 :'$name', '$total', 'DATETIME: Auto NOW()', NOW()
Maybe it should look like this:
"INSERT INTO orders (customer_id, total, order_date) VALUES ('$name', '$total', NOW() )";
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')");
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