Inserting blank fields into my database...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dirtyoldgoat
Forum Newbie
Posts: 4
Joined: Fri Feb 06, 2004 4:37 pm

Inserting blank fields into my database...

Post 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...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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
dirtyoldgoat
Forum Newbie
Posts: 4
Joined: Fri Feb 06, 2004 4:37 pm

Post by dirtyoldgoat »

Thank you Duff! That worked brilliantly...

Thanks for all your suggestions everyone... :D
Post Reply