...

View Full Version : I can't insert data from forms in the database...



kitalouka
06-30-2007, 12:29 AM
Help me please! :confused:

I'm portuguese informatic girl and I need some help please!!

It gives the following error:
Parse error: syntax error, unexpected T_VARIABLE in C:\wamp\www\formularios\inserir_clientes.php on line 105

Here is my code:
(NOTE: The variables names are in portuguese...)
<?php
$cod_cliente = $_POST['cod_cliente'];
$nome = $_POST['nome'];
$morada = $_POST['morada'];
$localidade = $_POST['localidade'];
$cod_postal = $_POST['cod_postal'];
$telefone = $_POST['telefone'];
$fax = $_POST['fax'];
$e_mail = $_POST['e_mail'];
$prazo_pagamento = $_POST['prazo_pagamento'];
$desc = $_POST['desc'];
$num_contribuinte = $_POST['num_contribuinte'];
$pessoa_contacto = $_POST['pessoa_contacto'];
$utilizador = $_POST['utilizador'];
$password = $_POST['password'];
if (!$cod_cliente || !$nome || !$morada|| !$localidade || !$cod_postal || !$telefone || !$fax ||!$e_mail || !$prazo_pagamento || !$desc || !$num_contribuinte || !$pessoa_contacto || !$utilizador || !$password) {
echo '<p align=center>Campos em falta.
<a href="inserir_clientes.html">Volte atrás</a> e tente de novo.'; exit;}

$ligax = mysql_connect('localhost', 'root','');
if (!$ligax)
{echo '<p> Erro: Falha na ligação.'; exit;}
mysql_select_db($ligax, 'joana');
line 105 ----->$insert = "insert into 'cad_clientes' values ('"$cod_cliente"','"$nome"','"$morada"','"$localidade"','"$cod_postal"','"$telefone"','"$fax"','"$prazo_pagamento"','"$desc"','"$num_contribuinte"','"$pessoa_contacto"','"$utilizador"','"$password"')";
$result = mysql_query($ligax, $insert);
if ($result==1) echo "<p>Dados inseridos<br>";
else "<p>Dados não inseridos<br>";
?>

It was a time that it didn't give any error but it didn't insert the data in the db either.~
Can anyone help me please?? I need this 'till sunday!! Please, please, please!!!
Thank you very much
Kisses 4 all
Kitalouka

Fumigator
06-30-2007, 12:41 AM
Which line is 105?

Iszak
06-30-2007, 06:33 AM
Ok well I looked at it, and I've escaped all your speech marks and that seemed to do it, you don't really need the speech marks though, also I saw this "$pr azo_pagamento" and I didn't know if that is meant to be there? Anyhow so I left it.. but there is no longer a PHP error. At the last line you've got else "<p>Dados não inseridos<br>"; are you meant to echo that? also I'll use html entities to make it use escape characters. valid.



<?php
$cod_cliente = $_POST['cod_cliente'];
$nome = $_POST['nome'];
$morada = $_POST['morada'];
$localidade = $_POST['localidade'];
$cod_postal = $_POST['cod_postal'];
$telefone = $_POST['telefone'];
$fax = $_POST['fax'];
$e_mail = $_POST['e_mail'];
$prazo_pagamento = $_POST['prazo_pagamento'];
$desc = $_POST['desc'];
$num_contribuinte = $_POST['num_contribuinte'];
$pessoa_contacto = $_POST['pessoa_contacto'];
$utilizador = $_POST['utilizador'];
$password = $_POST['password'];
if (!$cod_cliente || !$nome || !$morada|| !$localidade || !$cod_postal || !$telefone || !$fax ||!$e_mail || !$prazo_pagamento || !$desc || !$num_contribuinte || !$pessoa_contacto || !$utilizador || !$password) {
echo '<p align=center>Campos em falta.
<a href="inserir_clientes.html">Volte atrás</a> e tente de novo.'; exit;}

$ligax = mysql_connect('localhost', 'root','');
if (!$ligax)
{echo '<p> Erro: Falha na ligação.'; exit;}
mysql_select_db($ligax, 'joana');
$insert = "insert into 'cad_clientes' values ('\"$cod_cliente\"','\"$nome\"','\"$morada\"','\"$localidade\"','\"$cod_postal\"','\"$telefone\"','\"$fax\"','\"$pr azo_pagamento\"','\"$desc\"','\"$num_contribuinte\"','\"$pessoa_contacto\"','\"$utilizador\"','\"$password\"')";
$result = mysql_query($ligax, $insert);
if ($result==1) echo "<p>Dados inseridos<br>";
else "<p>Dados não inseridos<br>";
?>

kitalouka
07-01-2007, 12:09 AM
It still doesn't work mate...Now it's giving me this errors:
Warning: mysql_select_db(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\formularios\inserir_clientes.php on line 104

Warning: mysql_query(): supplied argument is not a valid MySQL-Link resource in C:\wamp\www\formularios\inserir_clientes.php on line 106

mysql_select_db($ligax, 'joana'); --->(line 104)
$result = mysql_query($ligax, $insert); ---->(line 106)

If you could help me once again It would be great!!
Thank you!!
Kisses

Mwnciau
07-01-2007, 12:21 AM
Shouldnt that be


mysql_select_db('joana', $ligax); --->(line 104)
$result = mysql_query($insert, $ligax); ---->(line 106)

?

kitalouka
07-01-2007, 12:40 AM
Thank you!! It doesn't give any error now!!
But It still won't insert the data...I'll post the code again:
<?php
$cod_cliente = $_POST['cod_cliente'];
$nome = $_POST['nome'];
$morada = $_POST['morada'];
$localidade = $_POST['localidade'];
$cod_postal = $_POST['cod_postal'];
$telefone = $_POST['telefone'];
$fax = $_POST['fax'];
$e_mail = $_POST['e_mail'];
$prazo_pagamento = $_POST['prazo_pagamento'];
$desc = $_POST['desc'];
$num_contribuinte = $_POST['num_contribuinte'];
$pessoa_contacto = $_POST['pessoa_contacto'];
$utilizador = $_POST['utilizador'];
$password = $_POST['password'];
if (!$cod_cliente || !$nome || !$morada|| !$localidade || !$cod_postal || !$telefone || !$fax || !$e_mail || !$prazo_pagamento || !$desc || !$num_contribuinte || !$pessoa_contacto || !$utilizador || !$password) {
echo '<p align=center>Campos em falta.
<a href="inserir_clientes.html">Volte atrás</a> e tente de novo.'; exit;}

$ligax = mysql_connect('localhost', 'root','');
if (!$ligax)
{echo '<p> Erro: Falha na ligação.'; exit;}
mysql_select_db('joana', $ligax);
$insert = "insert into 'cad_clientes' values ('\"$cod_cliente\"','\"$nome\"','\"$morada\"','\"$localidade\"','\"$cod_postal\"','\"$telefone\"','\"$fax\"','\"$prazo_pagamento\"','\"$desc\"','\"$num_contribuinte\"','\"$pessoa_contacto\"','\"$utilizador\"','\"$password\"')";
$result = mysql_query($insert, $ligax);
if ($result==1) echo "<p>Dados inseridos<br>";
?>

I don't understand what's wrong in here...Please help me again!!
Thank you so much!!
Kisses

fl00d
07-01-2007, 01:32 AM
well I noticed on the last line, your missing brackets on the if statement, but that should generate an error...


if ($result==1) echo "<p>Dados inseridos<br>"


if ($result==1) {
echo "<p>Dados inseridos<br>";
}

Maybe you have a typo somewhere, or the database and/or table doesn't actually exist.

kitalouka
07-01-2007, 01:53 AM
What's a typo?? Sorry for my ignorance *blush*

the database and the table exists, I'm sure of that :)
This is really weird...

Mwnciau
07-01-2007, 02:20 AM
If your only doing 1 thing inside an if satement you dont need curly brackets, it just does the code up to the next semi-colon, the curly brackets just groups the lines together. A typo is a misspelling on a computer where someone has typed something in wrong, eg helol should be hello.

Try putting


echo mysql_error();

at the end of your script.

kitalouka
07-01-2007, 02:42 AM
Thank you for your tip :)
But it still doesn't work...What should I do?
Thank you

kitalouka
07-01-2007, 02:44 AM
My actual code is the following:

<?php
$cod_cliente = $_POST['cod_cliente'];
$nome = $_POST['nome'];
$morada = $_POST['morada'];
$localidade = $_POST['localidade'];
$cod_postal = $_POST['cod_postal'];
$telefone = $_POST['telefone'];
$fax = $_POST['fax'];
$e_mail = $_POST['e_mail'];
$prazo_pagamento = $_POST['prazo_pagamento'];
$desc = $_POST['desc'];
$num_contribuinte = $_POST['num_contribuinte'];
$pessoa_contacto = $_POST['pessoa_contacto'];
$utilizador = $_POST['utilizador'];
$password = $_POST['password'];
if (!$cod_cliente || !$nome || !$morada|| !$localidade || !$cod_postal || !$telefone || !$fax || !$e_mail || !$prazo_pagamento || !$desc || !$num_contribuinte || !$pessoa_contacto || !$utilizador || !$password) {
echo '<p align=center>Campos em falta.
<a href="inserir_clientes.html">Volte atrás</a> e tente de novo.'; exit;}

$ligax = mysql_connect('localhost', 'root','');
if (!$ligax)
{echo '<p> Erro: Falha na ligação.'; exit;}
mysql_select_db('joana', $ligax);
$insert = "Insert into cad_clientes values ('\"$cod_cliente\"','\"$nome\"','\"$morada\"','\"$localidade\"','\"$cod_postal\"','\"$telefone\"','\"$fax\"','\"$prazo_pagamento\"','\"$desc\"','\"$num_contribuinte\"','\"$pessoa_contacto\"','\"$utilizador\"','\"$password\"')";
$result = mysql_query($insert, $ligax);
if ($result==1){
echo "<p>Dados inseridos<br>";
}
?>

Can you give me some suggestions? Can you see something wrong??
Please help!! :D

_Aerospace_Eng_
07-01-2007, 03:24 AM
Try this

<?php
// Get post variables
$cod_cliente = $_POST['cod_cliente'];
$nome = $_POST['nome'];
$morada = $_POST['morada'];
$localidade = $_POST['localidade'];
$cod_postal = $_POST['cod_postal'];
$telefone = $_POST['telefone'];
$fax = $_POST['fax'];
$e_mail = $_POST['e_mail'];
$prazo_pagamento = $_POST['prazo_pagamento'];
$desc = $_POST['desc'];
$num_contribuinte = $_POST['num_contribuinte'];
$pessoa_contacto = $_POST['pessoa_contacto'];
$utilizador = $_POST['utilizador'];
$password = $_POST['password'];

// If any of the post variables are blank, then die and print out custom message
if ($cod_cliente == '' || $nome == '' || $morada == '' || $localidade == '' || $cod_postal == '' || $telefone == '' || $fax == '' || $e_mail == '' || $prazo_pagamento == '' || $desc == '' || $num_contribuinte == '' || $pessoa_contacto == '' || !$utilizador == '' || $password == '')
die('<p align=center>Campos em falta.<a href="inserir_clientes.html">Volte atrás</a> e tente de novo.')

// Connect to the database, if the connection fails, then die saying error could not connect
$ligax = mysql_connect('localhost', 'root','') or die('<p> Erro: Falha na ligação.</p>');

// Select the appropiate database using the $ligax connection or die if database couldn not be selected
mysql_select_db('joana', $ligax) or die('<p>Could not select database called "joana"');

// INSERT into the database, if sql statement is in double quotes then no need to use double quotes around variables
$insert = "INSERT INTO cad_clientes values ('$cod_cliente','$nome','$morada','$localidade','$cod_postal','$telefone','$fax','$prazo_pagamento', '$desc','$num_contribuinte','$pessoa_contacto','$utili zador','$password')";

// Run the query, if you get an error it will die telling you the error and the query
$result = mysql_query($insert, $ligax) or die('The mysql error was: '.mysql_error().'<br>The query was: '.$sql);

// Finally to find out if the query returns anything you should use mysql_num_rows(), if 1 echo, if not do nothing
if (mysql_num_rows($result)==1)
echo "<p>Dados inseridos<br>";
?>
If that doesn't work then your sql statement is incorrect. Usually sql statements look something like this

$insert = "INSERT INTO cad_clientes(fieldname1,fieldname2,fieldname3,fieldname4,fieldname5,fieldname6,fieldname7,fieldname8 ,fieldname9,fieldname10,fieldname11,fieldname12,fieldname13) values ('$cod_cliente','$nome','$morada','$localidade','$cod_postal','$telefone','$fax','$prazo_pagamento', '$desc','$num_contribuinte','$pessoa_contacto','$utilizador','$password')";
You also had a space in one of your variables in your query.

kitalouka
07-01-2007, 11:34 PM
Thank you so much for your help guys!! It was very usefull!!! It works now!!!
Thank you, thank you very much!!
Tomorrow I'll defend my project in front of a jury and when I get my result I'll let you know :P
eheheh
Thank you all for your time and patience ;)

Namii
01-11-2010, 06:27 PM
Hey guys. Sorry i have to continue this topic because my code doesn't insert info on my database.. Either it doesn't do anything.. or it simply gives the error

"The mysql error was: Column count doesn't match value count at row 1"

I changed it again and again.. Looked up this post over and over and i can't find the solution.

My actual code is:

<?php
$nome = $_POST['nome'];
$telefone = $_POST['telefone'];
$e_mail = $_POST['e_mail'];
$empresa = $_POST['empresa'];

if ($nome == '' || $telefone == '' || $e_mail == '' || $empresa == ''){
echo '<p align=center>Campos em falta.
<a href="adicionar_fornec.html">Clique</a> para voltar e tente de novo.'; exit;}

$connection = mysql_connect('localhost','root','');

if (!$connection)
{echo '<p> Erro: Falha na ligação.'; exit;}

mysql_select_db('database', $connection);

$insert = "INSERT INTO fornecedores values ('$nome','$telefone','$e_mail','$empresa')";

$result = mysql_query($insert, $connection) or die('The mysql error was: '.mysql_error());
if (mysql_num_rows($result)==1){
echo "<p>Dados inseridos<br>";
}
?>

Sorry for bothering and i will apreciate the help.

Thank You!

Fumigator
01-11-2010, 09:04 PM
You really should have started your own thread for your issue.

The error you are receiving is pretty self-explanatory. Another way to say it is: "The number of columns you are loading doesn't match the number of columns in the table."

Namii
01-11-2010, 11:46 PM
Ok, but I mean if the ID is AutoIncremented i should'nt have to everytime i want to add new info chose an ID number right? :confused: i should add a new one and the database autoincremented by itself.

Thanks.

_Aerospace_Eng_
01-12-2010, 02:13 AM
That is correct however you didn't specify the column names so you must insert EVERY column into the table. You can use this

$insert = "INSERT INTO fornecedores values ('','$nome','$telefone','$e_mail','$empresa')";
assuming your table fields are autoid, nome, telefone, email, empresa in that order. Any other order and data will be put in the wrong place. You can also do this

$insert = "INSERT INTO fornecedores(column1,column2,column3,column4) values ('$nome','$telefone','$e_mail','$empresa')";
Just leave out the auto increment column and make sure you use your actual column names.

Namii
01-12-2010, 04:33 PM
Oh i get it now.. It was so simple :s.

Thank you very much!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum