Forum Moderators: coopster

Message Too Old, No Replies

HTML form doesn't insert data in MySQL row

The new row is created, but doesn't have data

         

trismegisto

4:02 pm on Dec 8, 2002 (gmt 0)

10+ Year Member



i’m new in PHP, and i’m trying to put a product catalogue online. my database contains a table called “productos”, and i want to populate it using an HTML form. I used some scripts from an online tutorial, and i think the code is correct. maybe the problem is i should use POST instead of GET, but i’m not sure. this is my HTML form:

<html>
<body>
<form action=feg-submitform2.php method=GET>
Clave: <br><input type=text name=clave size=25 maxlength=25><br><br>
Sección: <br><input type=text name=seccion size=25 maxlength=25><br><br>
Nombre: <br><input type=text name=nombre size=25 maxlength=25><br><br>
Descripción: <br><textarea name="descripcion" rows="10" cols="50"></textarea><br><br>
Extras: <br><textarea name="extras" rows="10" cols="50"></textarea><br><br>
<p>
<input type=submit>
</form>
</body>
</html>

and this is feg-submitform2.php:

<html>
<body>
<?php
mysql_connect (localhost, root, password);
mysql_select_db (fegal);
mysql_query ("INSERT INTO productos (clave, seccion, nombre, descripcion, extras)
VALUES ('$clave', '$seccion', '$nombre', '$descripcion', '$extras')
");
print ($clave);
print (" ");
print ($seccion);
print (" ");
print ($nombre);
print (" ");
print ($descripcion);
print (" ");
print ($extras);
print ("<p>");
print ("El producto anterior fue registrado");
?>
</body>
</html>

when i press submit in the HTML form, the information i entered in the form is not inserted in MySQL, i just see the message: “El producto anterior fue registrado”, i can’t see the product information i entered, and i should be able to see printed that data (clave, sección, and so on). i check in phpmyadmin and i can see that a new row is created but is completely empty, so i suppose the form inserts data in the database, but the text i type in the form is not passed to the database table. i have tried processing the info in the same page (using action="<?php echo $PHP_SELF?>"). i do not know what’s wrong. can anyone help me please? Thanx in advance.

ruserious

8:55 pm on Dec 8, 2002 (gmt 0)

10+ Year Member



In newer versions the default setting for register_globals is set to off, therefore the GET/POST parameters passed from forms/url are not automatically translated to variables. This is for security reasons.

If you want to access those you will need to use

$_GET['$nombre'] or $_POST['$nombre']
instead of just $nomre

(respectively $HTTP_GET_VARS['$nombre'] or $HTTP_POST_VARS['$nombre'] if you are using an older version of PHP < 4.1.0, highly unlikely though ;))

For more information read the php-documentation:
[php.net...]
[php.net...]

trismegisto

1:11 pm on Dec 9, 2002 (gmt 0)

10+ Year Member



hi ruserius, thank you very much for your advice…
but it’s still not working. i changed:
$clave, $seccion, $nombre, $descripcion and $extras

to:
$_GET['$clave'], $_GET['$seccion'], $_GET['$nombre'], $_GET['$descripcion'], $_GET['$extras']

and then for:
$_POST['$clave'], $_POST['$seccion'], $_POST['$nombre'], $_POST['$descripcion'], $_POST['$extras']

and it doesn’t work, when i press submit in my form, i get a message:
“Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /Users/rodrigos/Sites/testing/feg-submitform2.php on line 6”.

line 6 is as follows:
mysql_query ("INSERT INTO productos (clave, seccion, nombre, descripcion, extras)
VALUES ($_GET['$clave'], $_GET['$seccion'], $_GET['$nombre'], $_GET['$descripcion'], $_GET['$extras'])
");

i also tried using $HTTP_POST_VARS but i get the same error.
security is not a problem with my data, because i’ll be the only one populating the database and i’m the only person using this computer.
i know this is a very stupid problem, that’s what i think that (probably) the answer is not so hard. please somebody with the answer help me. thanx in advance.

andreasfriedrich

1:40 pm on Dec 9, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Within double quoted strings or here docs you need to use $_GET[clave] or {$_GET['clave']}.

[php.net...]

[edited by: andreasfriedrich at 1:55 pm (utc) on Dec. 9, 2002]

trismegisto

1:53 pm on Dec 9, 2002 (gmt 0)

10+ Year Member



what an idiot! i got it!,

this error:
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in /Users/rodrigos/Sites/testing/feg-submitform2.php on line 6

means i don’t have to put “$_GET['$nombre']” in the SQL query.

instead i have done this:
<?php
$_GET['$clave'];
$_GET['$seccion'];
$_GET['$nombre'];
$_GET['$descripcion'];
$_GET['$extras'];
mysql_connect ("localhost", "root", "password");
mysql_select_db ("fegal");
mysql_query ("INSERT INTO productos (clave, seccion, nombre, descripcion, extras)
VALUES ('$clave', '$seccion', '$nombre', '$descripcion', '$extras')
");

and instead of an error i get a warning:
Warning: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /Users/rodrigos/Sites/testing/feg-submitform2.php on line 6
Warning: MySQL Connection Failed: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /Users/rodrigos/Sites/testing/feg-submitform2.php on line 6
Warning: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /Users/rodrigos/Sites/testing/feg-submitform2.php on line 7
Warning: MySQL Connection Failed: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /Users/rodrigos/Sites/testing/feg-submitform2.php on line 7
Warning: MySQL: A link to the server could not be established in /Users/rodrigos/Sites/testing/feg-submitform2.php on line 7
Warning: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /Users/rodrigos/Sites/testing/feg-submitform2.php on line 10
Warning: MySQL Connection Failed: Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2) in /Users/rodrigos/Sites/testing/feg-submitform2.php on line 10
Warning: MySQL: A link to the server could not be established in /Users/rodrigos/Sites/testing/feg-submitform2.php on line 10

what all of this mean?
how do i have to use the “$_GET['$nombre']”?
i just need some more hints to make it on my own, someone give me more advice please. thanx in advance

trismegisto

2:11 pm on Dec 9, 2002 (gmt 0)

10+ Year Member



that’s something new andreasfriedrich, i did what you said, thank you very much. i think is the same if i put $_GET['$nombre'] outside the query that if i use it in the query without the single quotes. but i get the same error. maybe now the problem is with my connection with the database.

now, i have this code:
<?php
mysql_connect ("localhost", "root", "password");
mysql_select_db ("fegal");
mysql_query ("INSERT INTO productos (clave, seccion, nombre, descripcion, extras)
VALUES ('$_GET[$clave]', '$_GET[$seccion]', '$_GET[$nombre]', '$_GET[$descripcion]', '$_GET[$extras]')
");

could it be possible that the connection with MySQL is not working?
… i think that’s the problem, i can’t even access MySQL with phpmyadmin!
what can i do to restart the connection with MySQL?

andreasfriedrich

2:23 pm on Dec 9, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$_GET, $_POST, etc. are superglobal arrays that contain the values submitted via GET or POST. To access what a user entered into <input type=text name=clave size=25 maxlength=25> you need to use either $_GET['clave'] or $_POST['clave'] depending on the method used to submit the form.

How to restart mysql depends on whether you are on a shared host or have root access. In the former case you may need to have your provider restart it or there might be a way to do that from your admin area. In the latter case shut mysqld down using mysqladmin and restart it again.

Andreas

trismegisto

3:06 pm on Dec 9, 2002 (gmt 0)

10+ Year Member



andreasfriedrich, i understand now how to use $_GET and $_POST. i’m testing locally. i have restarted MySQL.

and now i have this code:
<?php
mysql_connect ("localhost", "root", "password");
mysql_select_db ("fegal");
mysql_query ("INSERT INTO productos (clave, seccion, nombre, descripcion, extras)
VALUES ('$_GET[$clave]', '$_GET[$seccion]', '$_GET[$nombre]', '$_GET[$descripcion]', '$_GET[$extras]')
");

but still, the information entered in the form doesn’t get it through the database. i check in phpmyadmin and i just see a new row without the information i entered in the form.
what’s wrong now?

andreasfriedrich

3:16 pm on Dec 9, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



mysql_query ("INSERT INTO productos (clave, seccion, nombre, descripcion, extras)
VALUES ('$_GET[$clave]', '$_GET[$seccion]', '$_GET[$nombre]', '$_GET[$descripcion]', '$_GET[$extras]')
");

should read

mysql_query ("INSERT INTO productos (clave, seccion, nombre, descripcion, extras)
VALUES ('$_GET[clave]', '$_GET[seccion]', '$_GET[nombre]', '$_GET[descripcion]', '$_GET[extras]')
");

Andreas

trismegisto

12:50 pm on Dec 10, 2002 (gmt 0)

10+ Year Member



andreasfriedrich, you absolutely rule! it works now! thank you very much man. i owe you one

andreasfriedrich

2:08 pm on Dec 10, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Always glad to help trismegisto.