I am trying to iimplement a form in which a user will enter some data as well as upload a file that then will be sent to mysql server. I have got them both working in terms of seperate forms in one file, but cant work it out togather, I guess for the reason that one is in html and other in php and having problem putting them togather. Therefore, at one time I can either click on upload to upload the file, or Add a record to add text fields.
If you can help, it would be highly apprecaited!
Thanks!
$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");
$conn = mysql_connect("localhost", "root", "");
@mysql_select_db ("ABC") OR DIE (mysql_error());
// 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 (image_type, image_size, image_name, image_location, image_prefix, image_extension, image_) ";
$sql.= "VALUES ('$file_type', '$file_size', '$file_name', '$image_location', '$image_prefix', '" . $mime_to_ext[$file_type] . "', NOW())";
@ ($sql);
copy($_FILES['userfile']['tmp_name'], $image_location . $image_prefix . $image_number . $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);
}
// Generate the HTML form for add/modify/search
function GenerateHTMLForm($formValues, $actionScript, $submitLabel) {
printf("<FORM METHOD=post ACTION=\"%s\"><PRE>\n", $actionScript);
printf("Price:
<INPUT TYPE=text SIZE=35 NAME=cprice VALUE=\"%s\">
<BR>\n", ($formValues) ? $formValues["cprice"] : "");
printf("Year:
<INPUT TYPE=text SIZE=35 NAME=cyear VALUE=\"%s\">
<BR>\n", ($formValues) ? $formValues["cyear"] : "");
printf("Make:
<INPUT TYPE=text SIZE=35 NAME=cmake VALUE=\"%s\">
<BR>\n", ($formValues) ? $formValues["cmake"] : "");
printf("Model:
<INPUT TYPE=text SIZE=35 NAME=cmodel VALUE=\"%s\">
<BR>\n", ($formValues) ? $formValues["cmodel"] : "");
printf("Kilometers:
<INPUT TYPE=text SIZE=35 NAME=ckilometers VALUE=\"%s\">
<BR>\n", ($formValues) ? $formValues["ckilometers"] : "");
printf("Detail:
<TEXTAREA cols=\"30\" rows=\"3\" Name=detail VALUE=\"%s\"></TEXTAREA>
<BR>\n", ($formValues) ? $formValues[cdetail] : "");
printf("<INPUT TYPE=submit NAME=action VALUE=\"%s\">", $submitLabel );
printf("</PRE></FORM>" );
}
function ReturnToMain() {
printf("<BR><FORM ACTION=\"main.php\" METHOD=post>\n");
printf("<INPUT TYPE=submit VALUE=\"Click\"> to return to Main Page\n");
}
?>
<form method="post" enctype="multipart/form-data">
Select Image File: <input type="file" name="userfile" size="40">
<input type="submit" name="action" value="Upload">
</form>
</body>
</html>