Page 1 of 1

Inserting blank fields into my database...

Posted: Fri Feb 06, 2004 4:49 pm
by dirtyoldgoat
Greetings!

I'm having a problem inserting information into my database via forms...

Never had this problem before but it's suddenly cropped up when I switched webserver hosts.

When I try to insert information into my database via forms, the information either doesn't post (but no errors are returned) or blank fields are inserted into my database.

Here's my code for my form (newuser.php):

Code: Select all

<body bgcolor="white">
<FORM action="adduser.php" method="post"> 
Member Name: <input type="text" name="newname"><BR> 
Member Status: <input type="text" name="newstatus"><BR> 
<input type="submit" value="Add new Member">
</FORM>
Just a very basic form with fields for the new User's name, and their status...

Now the code that takes the form information 'newname' and 'newstatus' and inserts them into the database (adduser.php)...

Code: Select all

<? 

require ("connect.php");

$result = mysql_query ("INSERT INTO members (name, status) 
                VALUES ('$newname', '$newstatus')"); 
if(!$result) 
&#123; 
   echo "<b>Something not added:</b> ", mysql_error(); 
   exit; 
&#125; 
if($result) 
    &#123; 
    mysql_close($db); 
    print "Something added sucessfully!"; 
    &#125; 
?>
Any ideas and comments would be appreciated...

Posted: Fri Feb 06, 2004 4:53 pm
by John Cartwright
I duno it looks fine to me

try adding this

<body bgcolor="white">
<FORM action="adduser.php" method="post">
Member Name: <input type="text" name="newname"><BR>
Member Status: <input type="text" name="newstatus"><BR>
<input type="submit" value="submitmember">
</FORM>

Code: Select all

<? 


require ("connect.php"); 

if ($submitmember == "SUBMIT") { 

$result = mysql_query ("INSERT INTO members (name, status) 
                VALUES ('$newname', '$newstatus')"); 
}
if(!$result) 
{ 
   echo "<b>Something not added:</b> ", mysql_error(); 
   exit; 
} 
if($result) 
    { 
    mysql_close($db); 
    print "Something added sucessfully!"; 
    } 
?> 
?>
I think the reason why it is entering blank fields because it goes straight past the form and sees insert these into the db and they do so

Posted: Fri Feb 06, 2004 6:26 pm
by DuFF
You're new webhost has register_globals (in PHP.ini) set to off. This is good, as it is default for PHP v4.2.0 and higher. What register_globals does (when its off) passes all form elements through an array, either $_POST or $_GET, depending on which method you use in your <form> tag. Right now you are using POST. This is how to fix it:

Code: Select all

<?php
require ("connect.php");
$newname = $_POST['newname'];  //NOTICE THESE LINES
$newstatus = $_POST['newstatus'];

$result = mysql_query ("INSERT INTO members (name, status)
                VALUES ('$newname', '$newstatus')");
if(!$result)
{
   echo "<b>Something not added:</b> ", mysql_error();
   exit;
}
if($result)
    {
    mysql_close($db);
    print "Something added sucessfully!";
    }
?>
Remember, this holds true for all forms.
Go here for more info:
viewtopic.php?t=511

Posted: Fri Feb 06, 2004 7:20 pm
by dirtyoldgoat
Thank you Duff! That worked brilliantly...

Thanks for all your suggestions everyone... :D