Data won't transfer from form to database

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

liquidchaz
New php-forum User
New php-forum User
Posts: 3
Joined: Sun Feb 02, 2003 10:25 pm

Data won't transfer from form to database

Postby liquidchaz » Sun Feb 16, 2003 11:18 am

For some reason, the data from a form won't add to a database. The variables aren'ts being carried from the form to the add script. For example, look at this code:

<html>
<body>

<?php

if ($submit) {

// process form

$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
$sql = "INSERT INTO employees (first,last,address,position) VALUES ('$first','$last','$address','$position')";
$result = mysql_query($sql);
echo "Thank you! Information entered.\n";

} else{

// display form

?>

<form method="post" action="<?php echo $PHP_SELF?>">

First name:<input type="Text" name="first"><br>

Last name:<input type="Text" name="last"><br>

Address:<input type="Text" name="address"><br>

Position:<input type="Text" name="position"><br>

<input type="Submit" name="submit" value="Enter information">

</form>

<?php
} // end if
?>

</body>
</html>

Now this code checks to see if submit has been pressed. If it has, it adds the form data to the code. If it hasn't, it shows the form.

However, the $submit value NEVER turns out true so the data is never added (and even if I remove $submit, all the form variables turn up null).

I've tried several variations of this code (including code that doesn't use $PHP_SELP? to recall itself... but I get the same result... null variables from the form.

Did I do something wrong? Could I have configured something wrong on my OmniHTTPd server? Please Help! Thx.

Chaz

User avatar
Redcircle
Moderator
Moderator
Posts: 830
Joined: Tue Jan 21, 2003 10:42 pm
Location: Michigan USA
Contact:

Postby Redcircle » Sun Feb 16, 2003 12:50 pm

try this

Code: Select all

<?php

if (isset($_POST['submit']) {

// process form

$db = mysql_connect("localhost", "root");
mysql_select_db("mydb",$db);
$sql = "INSERT INTO employees (first,last,address,position) VALUES ('".$_POST['first']."','$_POST['last']."','".$_POST['address']."','".$_POST['position']."')";
$result = mysql_query($sql);
echo "Thank you! Information entered.\n";

} else{

// display form

?>


It sounds like you have register_globals off.. which is a very good thing but makes following tutorials hard to get to work cause until php 4.2 most servers had it turned on.. SO they wrote the tutorials with register_globals on in mind.

liquidchaz
New php-forum User
New php-forum User
Posts: 3
Joined: Sun Feb 02, 2003 10:25 pm

Postby liquidchaz » Mon Feb 17, 2003 12:41 pm

It sounds like this global variable thing is my problem. When I tried your code, I get a Parse Error on this line:

if (isset($_POST['submit']) {

Any idea what the problem is? Sorry, I'm a PHP newbie.

Thx.

Chaz

PaulG
New php-forum User
New php-forum User
Posts: 8
Joined: Wed Nov 27, 2002 12:18 pm
Location: London
Contact:

Postby PaulG » Tue Feb 18, 2003 4:07 am

Error on this line:

if (isset($_POST['submit']) {


Try adding the second ')'
if (isset($_POST['submit'])) {

Paul. :wink:


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot] and 5 guests