Page 1 of 1

Inserting Product in database

Posted: Mon Jul 31, 2006 5:59 pm
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

Posted: Mon Jul 31, 2006 6:05 pm
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.