April 1, 201115 yr Hello all, I am trying to get some data that is in a variable to insert into a database field. The code I have is as follows. //Make a mysql connection to databasemysql_connect("localhost", "admin", "1admin") or die(mysql_error()); echo "Connected to MySQL<br />"; mysql_select_db("stock_checker") or die(mysql_error()); echo "Connected to Database"; //insert data from the variable into the retailer and price field mysql_query("INSERT INTO price_stock (retailer,price) VALUES('play.com', '$price_data' ) ") or die(mysql_error()); First of all, I am unsure if you can even just put the variable like I have and expect the variable data to go into the table. Also, I seem to get double entries in the database. All I want at the moment is for play.com to go into the retailer column, and for price_data to go into the price column. Thanks!
April 4, 201115 yr sorry if its stupid. but have u tried to see if echoing the variables gives out the value/?
April 4, 201115 yr Just a few minor syntax errors I seem to see in your query string (atleast to my expectations)... Not tested but try this? mysql_query("INSERT INTO `price_stock` (`retailer`, `price`) VALUES('play.com', $price_data)") or die(mysql_error()); Do you get any errors etc? Edited April 4, 201115 yr by webbhost
April 4, 201115 yr notice the difference between a ` and '.. the ` (top left of your keyboard) is used to reference objects ..e.g. table name, table entry names ... the ' (same key as @) is used to refernece data e.g. play.com etc. I believe because you are inserting a value $price_data into it, you don't need to use the '' as you don't acutally want to insert "$price_data" into the table, you want to insert the value of it... if that makes sense Also just worth noting what type of variable "price" is in the table.. i.e. integer? varchar?... Make sure that the value of $price_data is valid to be inserted. Edited April 4, 201115 yr by webbhost
April 4, 201115 yr notice the difference between a ` and '.. the ` (top left of your keyboard) is used to reference objects ..e.g. table name, table entry names ... the ' (same key as @) is used to refernece data e.g. play.com etc. I believe because you are inserting a value $price_data into it, you don't need to use the '' as you don't acutally want to insert "$price_data" into the table, you want to insert the value of it... if that makes sense Also just worth noting what type of variable "price" is in the table.. i.e. integer? varchar?... Make sure that the value of $price_data is valid to be inserted. U have to put single quotes around variables when inserting into mysql otherwise u will get a mysql syntex error, basically anything in the values section u have to have single quotes with the exception of now() and NULL sure theres prob more exceptions but i just gave u those 2, and the back ticks that ur refering to in the top left of ur keyboard are not required when listing the table names, i always use them but there not required for the query to work Edited April 4, 201115 yr by webdesigner93
Create an account or sign in to comment