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, ";
$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, ";
$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>
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, ";
$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, ";
$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>