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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Entering Data into Database 1

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.
 
&quot;submit&quot;'s value isn't needed. You can do it like this:
---------
...
<input type=&quot;Submit&quot; name=&quot;submit&quot;>
...
---------
and then the if:
---------
<?
if(isset($submit)){
...
---------

I don't remember if the IF is case sensitive, but it must be, since this works.

Rick
 
Also remember that clicking the &quot;Submit&quot; button will fire the ACTION property automatically - no other tests are needed! There's always a better way...
 
Got it!!!!!!!!!!!!!!

I just had to change your code sightly to get it to work.

The following worked for me

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

Thanks for your help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top