I have a form document (below) and would like to store that data in a mysql database.
Form.php:
<html>
<head>
<title>Form.php</title>
</head>
<body>
<?
echo β
<form method=\"post\" action=\"handleform.php \">
<table border=\"1\">
<tr><td>Doctor: </td><td>
<select name = \"doctornames\">
<option value=\"blank\" selected=\"selected\"></option>
<option value=\"McEnroe\">Dr John McEnroe</option>
<option value=\"Becker\">Dr Boris Becker</option>
<option value=\"Borg\">Dr Bjorn Borg</option>
<option value=\"Navratilova\">Dr Martina Navratilova</option>
<option value=\"Williams\">Dr Serena Williams</option>
</select></tr>
</table>
";
echo "
<p><input type=\"submit\" name=\"mybutton\" value=\"Submit\" />
<input type=\"reset\" value=\"Reset Form\" />
</p>
</form>
";
?>
</body>
</html>
I am using PHP 4.0 thatβs why I am using $HTTP_POST_VARS instead of _POST
handleform.php :
<body> <?php
global $HTTP_POST_VARS; global $doctor;
for ($x=0; $x < count($HTTP_POST_VARS['doctornames']); $x++) {
$doctor = $HTTP_POST_VARS['doctornames[$x]'];
if (isset($HTTP_POST_VARS['mybutton'])) {
echo "1.You clicked the button\n";
echo "2.You typed ". $doctor . " in the textbox.<br>\n";
$Host = "";
$DBName = "";
$TableName = "";
$Link = mysql_connect ($Host, '', '***'); if ($Link==false) { echo mysql_errno().": ".mysql_error()."<BR>\n";
}
mysql_select_db ($DBName, $Link);
$Query = "INSERT INTO $TableName VALUES ('{$doctor}')";
if (mysql_query ($Query, $Link)) {
print "The query was successfully executed!<BR>\n";
}
else {
print "The query could not be executed!<BR>\n";
echo mysql_errno().": ".mysql_error()."<BR>\n";
}
mysql_close($Link);
}
else {
echo "you must have come here from somewhere else.\n";
}
?>
</body>
I also used switch()
switch ($HTTP_POST_VARS['doctornames']) {
case "blank":
$doctor = $HTTP_POST_VARS['doctornames'];
break;
case "McEnroe":
$doctor = $HTTP_POST_VARS['doctornames'];
break;
case "Becker":
$doctor = $HTTP_POST_VARS['doctornames'];
break;
case "Borg":
$doctor = $HTTP_POST_VARS['doctornames'];
break;
case "Navratilova":
$doctor = $HTTP_POST_VARS['doctornames'];
break;
case "Williams":
$doctor = $HTTP_POST_VARS['doctornames'];
break;
}
instead of lines
for ($x=0; $x < count($HTTP_POST_VARS['doctornames']); $x++) {
$doctor = $HTTP_POST_VARS['doctornames[$x]'];
but it is not storing the data. What is the problem? Could u help me?
π