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.
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')
^ ^
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')");