Inserting Product in database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hmsg
Forum Commoner
Posts: 42
Joined: Sun May 14, 2006 9:48 am

Inserting Product in database

Post by hmsg »

Hello ppl!

I need help, i'm building a form and processing him by php script. the form consist in 6 fields.

in the php script i've already catch all the variables (i try it to print them and i know i have the values in the correct variables) but i'm not understand why the insert statement didn't work someone could help me?

Here is the php code i'm using to do the insert:

$sql = 'INSERT INTO `Produtos` (`ID`, `Nome`, `Descricao`, `Categoria`, `Preco`, `Num_img`) VALUES ('.$id.','.$nome.','.$descricao.','.$categoria.','.$preco.','.$num_img.');';


What i'm doing wrong?

I've tried to insert values instead of my variables and work! What is worng with my variable?



With the best regards

Hugo Gomes
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

If the last query failed mysql_error() returns a messages that describes the error.
Simplest form:

Code: Select all

$sql = 'INSERT INTO `Produtos` (`ID`, `Nome`, `Descricao`, `Categoria`, `Preco`, `Num_img`) VALUES ('.$id.','.$nome.','.$descricao.','.$categoria.','.$preco.','.$num_img.');';
mysql_query($sql) or die(mysql_error());
string literals have to be marked for mysql as well as for php.
Try

Code: Select all

$sql = "INSERT INTO
		`Produtos`
		(`ID`, `Nome`, `Descricao`, `Categoria`, `Preco`, `Num_img`)
	VALUES
		('$id','$nome','$descricao','$categoria','$preco','$num_img')
	";
To see the difference print both "versions" of $sql.
Post Reply