I am having a problem producing a relatively simple PHP code.
Whenever I run it it gives the following fault message:
Here is my code, please help
Whenever I run it it gives the following fault message:
Notice: Undefined index: Make in E:\domains\e\e-pageproductions .co.uk\user\htd ocs\savecar.php on line 98
Notice: Undefined index: Model in E:\domains\e\e-pageproductions .co.uk\user\htd ocs\savecar.php on line 98
Notice: Undefined index: Year in E:\domains\e\e-pageproductions .co.uk\user\htd ocs\savecar.php on line 98
Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<?php
/* program checks all the form fields for blank fields and incorrect format and saves the correct fields
* in the database.
*/
?>
<html>
<head>
<title>Save Car</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<?php
/* set up an array of text field labels.*/
$labels = array ("Make" => "Make", "Model" => "Model", "Year" => "Year");
/*check information from the form*/
foreach($_POST as $field => $value)
{
/*check each field for blank fields*/
if($value == "")
{
$blank_array[] = $field;
}
/* check format of each field*/
elseif(ereg("(name)",$field))
{
if(!ereg("^[A-Za-z' -]{1,50}$",$value))
{
$bad_format[] = $field;
}
}
elseif($field == "Year")
{
if(!ereg("^[0-9)( -]{7,20}(([xX]|(ext)|(ex))?[ -]?[0-9]{1,7})?$",$value))
{
$bad_format[] = $field;
}
}
} //end of foreach for $_POST
/*if any fields are not ok, display error msg and form*/
if(@sizeof($blank_array) >0 or @sizeof($bad_format)>0)
{
if(@sizeof($blank_array)>0)
{
/*display message for some text missing*/
echo "<b>You didn't fill in one of the boxes.
you must enter:</b><br>";
/*display a list of missing info*/
foreach($blank_array as $value)
{
echo " {$labels[$value]}<br>";
}
}
if(@sizeof($bad_format)>0)
{
/*display message for bad information*/
echo "<b>One or more fields have information that appears to be incorrect. Correct the format for:</b><br>";
/*display a list of bad information*/
foreach($bad_format as $value)
{
echo " {$labels[$value]}<br>";
}
}
/*redisplay form*/
echo "<p><hr />";
echo "<h3>Please enter your phone number below.</h3>";
echo "<form action='savecar.php' method='POST'>
<table>";
foreach($labels as $field => $label)
{
$good_data[$field]=strip_tags(trim($_POST[$field]));
echo "<tr>
<td style='text-align: right; font-weight: bold'>
$label</td>
<td>input type='text' name='$field' size='65'
maxlength='65' value='$good_data[$field]'></td>
</tr>";
}
echo "<tr>
<td colspan='2' style='text-align: center'>
<input type='submit' value='Submit Phone Number'>";
echo "</td></tr></table>
</form>";
exit();
}
else //if data is ok
{
$host="host";
$user="user";
$password="pwd";
$dbname="dbName";
$cxn = mysqli_connect($host,$user,$password,$dbname)
or die ("Couldn't connect to server.");
$fields_all = array_keys($labels);
foreach($fields_all as $field)
{
$good_data[$field] = strip_tags(trim($_POST[$field]));
if($field == "year")
{
$good_data[$field] = ereg_replace("[)( .-]","",$good_data[$field]);
}
$good_data[$field] = mysqli_real_escape_string($cxn,$good_data[$field]);
}
$query = "INSERT INTO Cars (Make, Model, Year)
VALUES ('$good_data[Make]','$good_data[Model]','$good_data[Year]')";
$result = mysqli_query($cxn,$query)
or die ("couldn't execute query.");
echo"<h4> New member added to database</h4>";
}
?>
</body>
</html>
Comment