Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with entering data into database 2

Status
Not open for further replies.

likelylad

IS-IT--Management
Jul 4, 2002
388
GB
I was hoping someone could really help me on this one.
I have have been looking at it that long that I can not see the forest from the trees

I am trying to get the following code to work

<html>
<body>

<form method=&quot;post&quot; action=&quot;
First name:<input type=&quot;Text&quot; name=&quot;first&quot;><br>

Last name:<input type=&quot;Text&quot; name=&quot;last&quot;><br>

Address:<input type=&quot;Text&quot; name=&quot;address&quot;><br>

Position:<input type=&quot;Text&quot; name=&quot;position&quot;><br>

<input type=&quot;Submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot;>

</form>

<?PHP

If ($submit==&quot;Submit&quot;) {
$db = mysql_connect(&quot;localhost&quot;, &quot;root&quot;);

mysql_select_db(&quot;mydb&quot;,$db);

$sql = &quot;INSERT INTO employees SET first='$_POST[first]',last='$_POST[last]',address='$_POST[address]',position='$_POST[address]'&quot;;

$result = mysql_query($sql);

echo &quot;Thank you! Information entered.\n&quot;;
}

?>

</body>
</html>

I know the problem is with the &quot;IF&quot; statement as when I remove it, data will enter into the database. However with the &quot;IF&quot;statement gone I am getting an empty line every time I open the browser.
 
you have to use insert like this:

insert into table (name, last) values ('$first', $last)

you've mixed it up with update andreas owen
aowen@arcade.ch
 
Sorry my mistake, I pasted the wrong code, the SQL statement is in the correct format; insert into table (name, last) values ('$first', $last) format.

As I said, if I remove the &quot;IF&quot; statement, the data will be passed into the database, but everytime I open up the browser, a blank line is obviously being passed into the database

 
Does your preferred MySQL management tool show the same thing? ______________________________________________________________________
TANSTAAFL!
 
Yes the management tool is showing the same thing.

 
Show me the exact SQL statement you are using to perform the insert ______________________________________________________________________
TANSTAAFL!
 
I think I see your problem and it's not in the SQL. Assuming you've copied and pasted your code the problem is very simple. In your IF statement you ask for the value of the submit button as &quot;Submit&quot;. Why? If you're processing the form then the user has obviously clicked the submit button. I'm not sure that submit button values are always posted. Do you have two different submit buttons for the same form with different values? If so, there's a much more effective way of handling that than a conditional statement that tries to catch the value of a button.

If you are processing the form on the same page as the form itself, and are using that statement to veryify that the submit button has been clicked, try using

if(isset('$name'))

or some other variable in the form. That way your script only gets processed if the form has been submitted. If you don't have any form elements that are certain to have values place a hidden element with a value in your form. Then you can check for that value with the isset() function rather than looking for a submit button value.

The only way you would be inserting empty rows is if the script is on the same page as the form and it's processing the if() statement before the form is completed, or if you are not properly describing your variables in the SQL. The fact that the if() statement causes the error should be a flashing red light.
 
How about trying $_POST[&quot;submit&quot;] in your if statement?

What is your register_globals setting? [smurf]
01101000011000010110010001110011
 
Hi hads and GiffordS

I also posted the same question on the HTML forum and I got the same suggestion as you both had given.

What worked for me was the following
if(isset($_POST[&quot;submit&quot;] ))

Thanks for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top