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')
^ ^
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 am really new to php and I am trying to use simple insert to my mysql database from the form.
I know that this mysql connection/insertion is dangerous and not used anymore. so can anyone please help me with this simple thing? I tried to google, but nothing is working so far :/
<?
$text=$_POST['name'];
$text=$_POST['surename'];
mysql_connect("localhost", "db_name", "pass") or die(mysql_error());
mysql_select_db("db_name") or die(mysql_error());
$result = mysql_query("INSERT INTO `table` (name, surename)
VALUES (NOW(), '".mysql_real_escape_string($name)."', '".mysql_real_escape_string($surename)."')");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
?>
Maybe change
$text=$_POST['name'];
$text=$_POST['surename'];
to
$name = $_POST['name'];
$surename = $_POST['surename'];
PS: And also your column names don't match your values. Your query, after inserting params
"INSERT INTO `table` (name, surename) VALUES (NOW(), '".mysql_real_escape_string($name)."', '".mysql_real_escape_string($surename)."')"
will probably look like this
INSERT INTO `table` (name, surename) VALUES (NOW(), 'Jhon', 'Wick')
As you can see there's name, surename (which probably should be surname) and (NOW(), 'Jhon', 'Wick'). So either add a column (if you have that column in your database):
INSERT INTO `table` (created_at, name, surename) VALUES (NOW(), 'Jhon', 'Wick')
or remove NOW() from your values
INSERT INTO `table` (name, surename) VALUES ('Jhon', 'Wick')
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());`
I've deleted an old, badly worded question and am reposting to not waste anyone's time.
I'm trying to query stuff from two tables, rooms and items. Then in a nested loop, create an entry in a 3rd table using info from the first two.
'For each room, insert ALL the standard items'
<?php
mysql_connect("******", "****", "******") or die(mysql_error());
mysql_select_db("MaintenanceTracking") or die(mysql_error());// Check connection
//collect standard items names
$stditemdata = 'SELECT * FROM `StandardItems`';
$itemresult = mysql_query($stditemdata) or die("Couldn't execute query. ". mysql_error());
$itemarray = mysql_fetch_array( $itemresult ));
//collect room info
$roomdata = 'SELECT * FROM `Rooms`';
$roomresult = mysql_query($roomdata) or die("Couldn't execute query. ". mysql_error());
//repeat for each room
while($room = mysql_fetch_array( $roomresult ))
{
//repeat for each item
for ($i = 0; $i <= count($itemarray); $i++)
{
mysqlquery("INSERT into Items
(ItemNumber, Name, LocationCode)
VALUES
(NULL, $itemarray['Name'], $room['LocationCode'])");
}
}
?>
I'm pretty new to php and must appologize that the syntax sometimes gets me stumped...I notoriously miss the semi-colon at the ends of rows, for example.
A million thanks in advance to anyone and everyone who can help me out.
kindest regards
mysqlquery("INSERT into Items
(ItemNumber, Name, LocationCode)
VALUES
(NULL, $itemarray['Name'], $room['LocationCode'])");
It should be
mysql_query("INSERT into `Items`
(`ItemNumber`, `Name`, `LocationCode`)
VALUES
(NULL, $itemarray['Name'], $room['LocationCode'])");
You use mysqlquery instead of mysql_query
To avoid duplication of mysql-reserved names (f.e. date, table etc) use this syntax
`column_name` or `table_name`
UPDATE
oh.. i miss! look, you try to write some strings into DB here
mysql_query("INSERT into `Items`
(`ItemNumber`, `Name`, `LocationCode`)
VALUES
(NULL, $itemarray['Name'], $room['LocationCode'])");
All strings in queries must be concluded in quotes single ' or double ", so your query should looks like
mysql_query("INSERT into `Items`
(`ItemNumber`, `Name`, `LocationCode`)
VALUES
(NULL, \"$itemarray['Name']\", \"$room['LocationCode']\")");
(i use \ symbol before " to escape quote), but i suggest you to use syntaxys like this:
mysql_query("INSERT into `Items`
(`ItemNumber`, `Name`, `LocationCode`)
VALUES
(NULL, '".$itemarray['Name']."', '".$room['LocationCode']."')");
mysql_query ("
INSERT INTO items
(index, name, description, given_by,
cost_to_tcs, starting_bid, auction_type)
VALUES
('{$index_number}','{$name}','{$description}','{$donated_by}',
NULL,'{$auction_type}','{$starting_bid}')
")
or die("3: " . mysql_error());
Errors with:
3: 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 ''index', 'name', 'description', 'given_by', 'cost_to_tcs', 'starting_bid', 'auct' at line 1
Thanks for any help.
index is mysql Reserved keyword, wrap index with (back tick) `` `
INSERT INTO items
(`index`, `name`, `description`, `given_by`,
`cost_to_tcs`, `starting_bid`, `auction_type`)
Reserve key words
try like that:
mysql_query ("
INSERT INTO items (
`index`,
`name`,
`description`,
`given_by`,
`cost_to_tcs`,
`starting_bid`,
`auction_type`)
VALUES(
'$index_number',
'$name',
'$description',
'$donated_by',
NULL,
'$auction_type',
'$starting_bid')
")
or die("3: " . mysql_error());
also make sure to safe the data with mysql_real_escape_string();