Log in

View Full Version : Insert whether or not a file is uploaded



megs1328
07-13-2009, 04:47 PM
You guys are a great help to all of us newbies here!

I have a code that I want to, if a picture is set, to upload it and insert the comment and pic name into a table. If there is no picture set, to just insert the comment.

I can't get it to do both. I've either written it so it will only insert the comment whether or not a picture was selected (as shown below) or it will say that the picture is an invalid file type, even if there was no picture selected and won't insert the comment.

I am using Dreamweaver CS3 and have used their scripts and added others I have found. It's getting a bit to complicated for me to diagnose at this point. If you can decipher it, it would be a great help:




<?php
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "comment")) {
if ($_POST['eventcompic']){
$fileName = $_FILES['eventcompic']['name'];
$fileSize = $_FILES['eventcompic']['size'];
$fileType = $_FILES['eventcompic']['type'];
$target = "includes/eventcompic/";

//This function separates the extension from the rest of the file name and returns it
function findexts($fileName)
{
$filename = strtolower($fileName) ;
$exts = split("[/\\.]", $fileName) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}

//This applies the function to our file
$ext = findexts ($_FILES['eventcompic']['name']) ;

//This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
$ran = date("-m-d-y-H-i-s", time()) ;

//This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
$ran2 = $row_DetailRS1['eventID'] . $ran . ".";
$newName = $ran2 . $ext;

//This combines the directory, the random file name, and the extension
$target = $target . $newName;
$ok=1;

//This is our size condition in bytes
if ($fileSize > 1073741824) {
echo "Your file is too large,&nbsp;";
$ok=0; }

//This is our limit file type condition
if(!(
$fileType=='image/jpeg' ||
$fileType=='image/jpg' ||
$fileType=='image/png' ||
$fileType=='image/gif' ||
$fileType=='image/bmp'
)) {
echo "You have uploaded an invalid file type,&nbsp;";
$ok=0; }

//Here we check that $ok was not set to 0 by an error
if ($ok==0) {
echo "your picture was not uploaded.<br class=\"clear\"/>"; }

//If everything is ok we try to upload it
else {
if(move_uploaded_file($_FILES['eventcompic']['tmp_name'], $target)) {

$insertSQL = sprintf("INSERT INTO eventcmts (eventID, eventcoms, eventcomer, eventcompic) VALUES (%s, %s, %s, %s)",
GetSQLValueString($_POST['eventID'], "int"),
GetSQLValueString($_POST['eventcoms'], "text"),
GetSQLValueString($_POST['eventcomer'], "text"),
GetSQLValueString($newName, "text"));

mysql_select_db($database_everyscene, $everyscene);
$Result1 = mysql_query($insertSQL, $everyscene) or die(mysql_error());

echo "Your picture was uploaded successfully, your comment will be posted shortly.<br class=\"clear\"/>"; }
}}
else {
$insertSQL = sprintf("INSERT INTO eventcmts (eventID, eventcoms, eventcomer) VALUES (%s, %s, %s)",
GetSQLValueString($_POST['eventID'], "int"),
GetSQLValueString($_POST['eventcoms'], "text"),
GetSQLValueString($_POST['eventcomer'], "text"));

mysql_select_db($database_everyscene, $everyscene);
$Result1 = mysql_query($insertSQL, $everyscene) or die(mysql_error());

echo "Thank you, your review will be posted shortly.<br class=\"clear\"/>"; }

}
?>


<form action="<?php echo $editFormAction; ?>" method="POST" enctype="multipart/form-data" name="comment">
<fieldset>
<legend>Comment:</legend>
<p><span id="sprytextarea1">
<label for="eventcoms" class="top"></label>
<textarea name="eventcoms" id="eventcoms" cols="45" rows="5"></textarea>
<span class="textareaRequiredMsg">A value is required.</span></span></p>
<br class="clear"/>
<p>
<label for="eventcompic" class="top">Add a Picture:</label>
<input type="file" name="eventcompic" id="eventcompic" />
</p>
<br class="clear"/>
<p>
<label for="eventcomer" class="top">Your Name: </label>
<input type="text" name="eventcomer" id="eventcomer" />
</p>
<br class="clear"/>
<p>
<input type="submit" name="submit" id="submit" value="Submit" class="buttons"/>
<input name="reset" type="reset" class="buttons" value="Reset"/>
<input name="eventID" type="hidden" id="eventID" value="<?php echo $row_DetailRS1['eventID']; ?>" />
</p>
<span class="style1"><br class="clear"/>
</span>
</fieldset>
<input type="hidden" name="MM_insert" value="comment" />
</form>

megs1328
07-17-2009, 07:02 PM
Any ideas? This is killing me...

Total_me
07-18-2009, 05:05 AM
I can suggest easier way.
Create 1st file called like add_image.php

<form enctype="multipart/form-data" action="upload_image.php" method="POST">
<table width="568" border="0" align="center" bordercolor="#0066FF">
<tr>
<td width="133"><div align="right">*Name:</div></td>
<td width="425"><input name="name" type="text" class="txt_zone" value="Name" /></td>
</tr>
<tr>
<td><div align="right">Photo</div></td>
<td><input type="file" name="photo" class="txt_zone2" /></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input type="submit" class="btn" value="Upload" />
</div></td>
</tr>
</table>
</form>
Then create another file upload_image.php

<?php

//This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);

//This gets all the other information from the form
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$pic=($_FILES['photo']['name']);

// Connects to your Database
mysql_connect("localhost", "username", "pass") or die(mysql_error()) ;
mysql_select_db("you_name_db_table") or die(mysql_error()) ;

//Writes the information to the database
mysql_query("INSERT INTO `images` VALUES ('$name', '$email', '$phone', '$pic')") ;

//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

//Tells you if its all ok
echo "File". basename( $_FILES['uploadedfile']['name']). " have been uploaded..";
}
else {

//Gives and error if its not
echo "Error uploading your file.";
}
?>
<p align="center" class="style7">
<?php
// Connects to your Database
mysql_connect("localhost", "username", "pass") or die(mysql_error()) ;
mysql_select_db("you_name_db_table") or die(mysql_error()) ;

//Retrieves data from MySQL
$data = mysql_query("SELECT * FROM images") or die(mysql_error());

//Puts it into an array
while($info = mysql_fetch_array( $data ))
{

//Outputs the image and other data
Echo "<img src=images/".$info['photo'] ."<br>";
Echo "<b>Name:</b> ".$info['name'] . "<br> ";
}
?>

Here just no files formats ant upload limits ;) I hope I helpde you :) And if you use that script you need dbase Table Images

CREATE TABLE `images` (
`name` varchar(30) default NULL,
`email` varchar(30) default NULL,
`phone` varchar(30) default NULL,
`photo` varchar(30) default NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1;