Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Insert data from variable ?

Featured Replies

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 database

mysql_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!

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 by webbhost

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 by webbhost

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 by webdesigner93

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.