Hi, I am trying to create a form in order to allow users to enter information directly into the database. I have the HTML form and the PHP code to send the SQL queries to the server all on one page. The problem here is that I am not sure how to triger the PHP code, I have tried many if-else lines but keep getting an error saying that I have an undefined variable in that line, some lines don't show any errors but still doesn't work. I hope someone might be able to help me.
*THE LINES ARE IN BOLD
THANKS FOR ANY HELP GIVEN!
<html>
<head>
<title>Employee List</title>
</head>
<body>
<?php
// Connection variables
$host = "localhost";
$username = "root";
//$password = "pass";
$db = "mydb";
$table = "employees";
// Connection to host
$connect = @mysql_connect($host, $user) or die("could not connect to host!");
// Select database
$db_select = @mysql_select_db($db) or die("could not connect to database!");
// Process form
//if ($submit)
//if (@$submit)
//if ($submit=="Submit")
//if ($REQUEST_METHOD=="POST")
//if ($HTTP_POST_VARS['sent'])
if (isset($sent)) {
$sqlreceive = "INSERT INTO employees SET first='$POST[first]',last='$POST[last]',address='$POST[address]',position='$POST[address]'";
echo "New Link Added\n";
} else {
// SQL
$sqldisplay = "SELECT * FROM $table";
// Query the database
$resultdisplay = @($sqldisplay) or die("could not complete query!");
// Display results - loop required as results are in an array
if ($row = @mysql_fetch_array($resultdisplay)) {
echo "<table border=1>\n";
echo "<tr><td>Id</td><td>First name</td><td>Last name</td><td>Address</td><td>Position</td></tr>\n";
do {
printf("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",$row["id"],$row["first"],$row["last"],$row["address"],$row["position"]);
} while($row = @mysql_fetch_array($resultdisplay));
echo "</table><br>";
} else {
echo "Sorry, no records were found!\n";
}
?>
<form name="employee" action="employee.php" method="post" enctype="text/plain">
<table>
<tr><td><b>First name:</b></td><td><input type="text" name="first" size="40" maxlength="40"><br></td></tr>
<tr><td><b>Last name:</b></td><td><input type="text" name="last" size="40" maxlength="40"><br></td></tr>
<tr><td><b>Address:</b></td><td><input type="text" name="address" size="40" maxlength="40"><br></td></tr>
<tr><td><b>Position:</b></td><td><input type="text" name="position" size="40" maxlength="40"><br></td></tr>
</table><br>
<input type="submit" value="Add Record">
<input type="reset" value="Reset form">
</form>
<?php }
?>
</body>
</html>