Page 1 of 1

strang php in html form... help me understand?

Posted: Sun Nov 21, 2010 10:07 pm
by iansane
Hi,

I am having a terrible time trying to figure out why my picture files aren't uploading and MySql UPDATES not updating the picture link.

First I have narrowed it down by placing echo statements in the code to the point my echo doesn't show up any more and that is at the file upload part.
Second the html in the form is strange to me becuase of the .($add ? <html here> ): more html here.

It seems strange to have the input type=file part twice but this is a copy of the file upload that works in a "addPrize.php" file. This one is the "editPrize.php". In the addPrize.php it works because before hand the row is created without the picturelink and then when it gets to this part it just uses the UPDATE statement.
I've checked to make sure the prizeid in my UPDATE statement is right by echoing it out before this runs and then manually did the update in phpMyAdmin with no problem.

Code: Select all

echo 'test before file if statement'; //this one shows up meaning we're good so far
		if((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/png") )
		&& ($_FILES["file"]["size"] < 512000)) //500kb size limit
  		{
echo 'test after files if';//this one doesn't show, something is going wrong
			$picturelink = $_FILES["file"]["name"];
			$ext = pathinfo($picturelink);
			$picturelink = 'prize_' . $_POST['prizeid'] . '_' . rand(0,300) . '.' . $ext['extension'];
			
			if (file_exists("media/" . $picturelink))
			{
				echo $_FILES["file"]["name"] . " already exists. ";
			}
			else
			{
				move_uploaded_file($_FILES["file"]["tmp_name"],
				"media/" . $picturelink);
		
				//echo "Stored in: " . "media/" . $_FILES["file"]["name"];
			}
							
			/*$sql = "UPDATE Prizes SET picturelink ='media/".$picturelink."' WHERE prizeid = " . $_POST['prizeid'];*/
			$sql = "UPDATE Prizes SET picturelink = 'media/'".$picturelink."'WHERE prizeid ='".$_POST['prizeid']."';";
			echo '<!-- ' . $sql . ' -->';
			mysql_query($sql)or die(mysql_error(). " : ".$sql);
							
			echo "<h2>Success!</h2><br />Prize successfully edited!<br />";
		}
Here's the html in the form with the strange php. I should add that the $add variable is set to "1" earlier in the script so that's .(1? in the html

Code: Select all

<td>'.($add ? '<input type="checkbox" id="picture" name="picture" value="'.$picturelink.'" checked="checked" />Use <a href="'.$picturelink.'" target="_blank" style="color:#000000;">Existing Picture</a><br /><input type="file" id="file" name="file" style="display:none" />' : '<input type="file" name="file" />').'
Thanks for any assistance with this

Re: strang php in html form... help me understand?

Posted: Sun Nov 21, 2010 11:03 pm
by McInfo
This is the ternary (three-part) operator.

Code: Select all

condition ? true_value : false_value
It is a shortcut.

Code: Select all

if (condition)
    return true_value
else
    return false_value
Try submitting your file upload form to a simple script and see what is in $_FILES.

Code: Select all

<?php
print_r($_FILES); // or
var_dump($_FILES);
You might find this syntax preferable.

Code: Select all

$acceptableFormats = array (
    'image/png',
    'image/jpeg',
    'image/gif',
    'image/pjpeg'
);
if (in_array($_FILES['file']['type'], $acceptableFormats));

Re: strang php in html form... help me understand?

Posted: Sun Nov 21, 2010 11:07 pm
by s992
The code in your second snippet is using a ternary operator, which is essentially a shorthand if/else statement.

These two pieces of code both do the exact same thing:

Code: Select all

echo (isset($variable)) ? "the variable is set" : "the variable is not set";

if(isset($variable)) {
	echo "the variable is set";
} else {
	echo "the variable is not set";
}
EDIT: McInfo beat me too it..damn!

Re: strang php in html form... help me understand?

Posted: Sun Nov 21, 2010 11:39 pm
by iansane
Thanks McInfo and s992

Thats a lot of good information and fast response. guess I hit the forum at the right time tonight. :-)

so in the html code I posted is that saying this?

Code: Select all

if(isset($add)){
'<input type="checkbox" id="picture" name="picture" value="'.$picturelink.'" checked="checked" />Use <a href="'.$picturelink.'" target="_blank" style="color:#000000;">Existing Picture</a><br /><input type="file" id="file" name="file" style="display:none" />'
}
else{
'<input type="file" name="file" />'
}
the whole thing is inside echo ' '; so does that mean it will only echo based on where $add is set or not?

It is confusing to me because the checkbox allways needs to display and $add is set to 1 and the whole FILE part is already inside a if(isset($add))

Thanks again :-)

I'll take your advice and print out $FILE and see what I can come up with now that I know about the ternary operator.

Re: strang php in html form... help me understand?

Posted: Mon Nov 22, 2010 12:07 am
by McInfo
It says

Code: Select all

if ($add) {
    echo '<input type="checkbox" ...';
} else {
    echo '<input type="file" ...';
}
The condition means "if $add is equivalent to true". $add could be TRUE, a number other than zero, a non-empty array, a resource, etc.

I'm guessing about the echo part -- you left that out of your original code snippet.

Re: strang php in html form... help me understand?

Posted: Wed Nov 24, 2010 7:15 pm
by iansane
Here's the complete section of code and yes it is in a echo statement. I forgot for a minute there that isset and true aren't the same thing. Thanks

Code: Select all

echo
'<form id="form2" method="post" action="'.$action.'" enctype="multipart/form-data">
<tr>
	<td><label for="shortname">Short Name:</label></td>
	<td><input type="text" style="border:1px solid #abc6dd;width:25em;" name="shortname"'.($add ? ' value="'.$prizename.'"' : '').' /></td>
</tr>
<tr>
	<td valign="top"><label for="file">Prize Picture:</label></td>
	<td>'.($add ? '<input type="checkbox" id="picture" name="picture" value="'.$picturelink.'" checked="checked" />Use <a href="'.$picturelink.'" target="_blank" style="color:#000000;">Existing Picture</a><br /><input type="file" id="file" name="file" style="display:none" />' : '<input type="file" name="file" />').'
	</td>
</tr>
<tr>
	<td><label for="category">Category:</label></td>
	<td>
		<select name="category" style="border:1px solid #abc6dd;">';