I am trying to create a form in which users would enter data as well as an image file that ideal later be retrieved. At this moment I am having trouble that if the text fields are entered and no image is entered it does not go to the datase. However, once the image field is enter in any given record then the record is saved to my database. I did not have that issue before. I have spent much time to figure out why it has started doing that. If you have any idea, please help!
// Do this process if user has browse the file and click the submit button
if ($_POST['action'] == "Upload")
{
$file_name = $_FILES["userfile"]["name"];
$file_size = $_FILES["userfile"]["size"];
$file_type = $_FILES["userfile"]["type"];
if (in_array (strtolower ($file_type), $image_types))
{
$sql = "INSERT INTO car (price, year, make, model, kilometers, detail, image_type, image_size, image_name, image_location, image_prefix, image_extension, image_date) ";
$sql.= "VALUES ('$cprice', '$cyear', '$cmake', '$cmodel', '$ckilometers', '$cdetail', '$file_type', '$file_size', '$file_name', '$image_location', '$image_prefix', '" . $mime_to_ext[$file_type] . "', NOW())";
@mysql_query ($sql);
copy($_FILES['userfile']['tmp_name'], $image_location . $image_prefix . $file_name . $mime_to_ext[$file_type]);
Header("Location:".$_SERVER["PHP_SELF"]);
}
}
// Generate the HTML header
function GenerateHTMLHeader($message) {
printf("<HEAD> <TITLE> A B C AUTOS</TITLE> </HEAD>");
printf("<BODY TEXT=\"#000000\" BGCOLOR=\"#999999\" LINK=\"#0000EE\"
VLINK=\"#551A8B\" ALINK=\"#FF0000\">\n");
printf("<BR><H1 ALIGN=center> <FONT SIZE=+4>A B C AUTOS</FONT></H1><BR>");
printf("<TABLE CELLPADDING=4 CELLSPACING=0 BORDER=0 WIDTH=600>");
printf("<TR BGCOLOR=\"#DCDCDC\"><TD><FONT FACE=Arial><B>");
printf("%s</B></FONT><BR></TD>", $message);
printf("<TD ALIGN=right>");
printf("</FONT></TD></TR>");
printf("</TABLE>");
printf("<BR><BR>");
}
// Generates the main page
function GenerateFrontPage() {
printf("<FORM METHOD=post ACTION=main.php>");
printf("<INPUT TYPE=\"submit\" NAME=\"choice\" VALUE=\"Search Database\">");
printf(" ");
printf("<INPUT TYPE=\"submit\" NAME=\"choice\" VALUE=\"Add a Record\">");
printf("<BR><BR><BR><BR><BR><BR>");
printf("<UL>");
printf("<LI> Search Database by clicking on
<I>Search Database</I> button</LI>");
printf("<LI> Add records to the database by clicking on
<I>Add a Record</I> button </LI>");
printf("<LI> Modify an existing entry by clicking <I>Search Database</I>
button first and then choosing the entry to Modify</LI>");
printf("<LI> Delete an existing entry by clicking <I>Search Database</I>
button first and then choosing the entry to Delete</LI>");
printf("</UL>") ;
printf("</FORM>");
}
// Display error messages
function DisplayErrMsg( $message ) {
printf("<BLOCKQUOTE><BLOCKQUOTE><BLOCKQUOTE><H3><FONT COLOR=\"#CC0000\">
%s</FONT></H3></BLOCKQUOTE></BLOCKQUOTE></BLOCKQUOTE>\n", $message);
}
function ReturnToMain() {
printf("<BR><FORM ACTION=\"main.php\" METHOD=post>\n");
printf("<INPUT TYPE=submit VALUE=\"Click\"> to return to Main Page\n");
}
// Generate the HTML form for add/modify/search
function GenerateHTMLForm($formValues, $actionScript, $submitLabel) {
?>
<html>
<body>
<FORM METHOD="post" enctype="multipart/form-data">
<div align="center">
<table border="3" cellpadding="3" width="586" height="388">
<tr><td align="center" valign="baseline" width="153" height="23">
<p align="left">Price</td><td align="center" valign="baseline" width="407" height="23">
<p align="left">
<input name="cprice" TYPE ="text" maxlength="50" size="20">
</p>
</td></tr>
<center>
<tr><td width="153" height="23">Year</td></center><td width="407" height="23">
<p align="left">
<input name="cyear" TYPE = "text" maxlength="50" size="20">
</p>
</td></tr>
<center>
<tr><td width="153" height="23">Make</td></center><td width="407" height="23">
<p align="left">
<input name="cmake" TYPE ="text" maxlength="50">
</p>
</td></tr>
<center>
<tr><td width="153" height="27">Model</td></center><td width="407" height="27">
<p align="left">
<input name="cmodel" TYPE = "text" maxlength="50">
</p>
</td></tr>
<center>
<tr><td width="153" height="27">Kilometers</td></center><td width="407" height="27">
<p align="left">
<input name="ckilometers" TYPE "text" maxlength="50">
</p>
</td></tr>
<center>
<tr>
<td width="256" height="1">Detail</td><td width="304" height="1"><textarea rows="1" name="S1" cols="48"></textarea></td>
</tr>
<center>
<tr>
<td width="153" height="23">Select File </td></center><td width="407" height="23">
<p align="left">
<input type="file" name="userfile" size="40"><input type="submit" name="action" value="Upload">
</p>
</td>
</tr>
<tr><td colspan="2" width="570" height="84">
</td></tr>
</table>
</center>
</div>
<p> </p>
</form>
</body>
</html>
<?
}
?>
//add.php
//add.php
require("common.php") ;
$image_prefix = "image_"; //prefix used for all images
$image_location = "pictures/"; //path to where your images will be stored
$image_types = Array (
"image/bmp",
"image/jpeg",
"image/pjpeg",
"image/gif",
"image/x-png",
"image/jpg");
$mime_to_ext = Array (
"image/bmp" => ".bmp",
"image/jpeg" => ".jpeg",
"image/pjpeg" => ".jpeg",
"image/gif" => ".gif",
"image/x-png" => ".png",
"image/jpg" => ".jpg");
$addStmt = "Insert into $tableName(price, year, make, model, kilometers, detail, image_type, image_size, image_name, image_location, image_prefix, image_extension, image_date) values('$cprice', '$cyear', '$cmake', '$cmodel', '$ckilometers', '$cdetail', '$file_type', '$file_size', '$file_name', '$image_location', '$image_prefix', '" . $mime_to_ext[$file_type] . "', NOW())" ;
// Connect to the Database
if (!($link=mysql_pconnect($hostName, $userName, $password))) {
DisplayErrMsg(sprintf("error connecting to host %s, by user %s",
$hostName, $userName)) ;
exit() ;
}
// Select the Database
if (!mysql_select_db($databaseName, $link)) {
DisplayErrMsg(sprintf("Error in selecting %s database", $databaseName)) ;
DisplayErrMsg(sprintf("error:%d %s", mysql_errno($link), mysql_error($link))) ;
exit() ;
}
// Execute the Statement
if (!mysql_query(sprintf($addStmt,$cprice, $cyear, $cmake, $cmodel, $ckilometers, $cdetail, '$file_type', '$file_size', '$file_name', '$image_location', '$image_prefix', '" . $mime_to_ext[$file_type] . "'), $link))
{
DisplayErrMsg(sprintf("Error in executing %s stmt", $stmt)) ;
DisplayErrMsg(sprintf("error:%d %s", mysql_errno($link), mysql_error($link))) ;
exit() ;
}
GenerateHTMLHeader("The entry was added succesfully");
ReturnToMain();