keeping values in drop down lists after form submittion - php

I am trying to retain the value which is selected in my drop down list, I am currently using:
<?php if ($_POST['day']==1) {echo "selected='selected'"; } ?>
to achieve this, I am just wondering if there is a more effective way of doing this because in my day drop down list I have 32 values including default, month 13 including default and year 51 including default. any suggestions would be appreciated.

Use a for loop.
For example, using a month with 31 days:
for($i=1; $i<32; $i++){
echo "<option value='$i'".($i==$_POST["day"] ? " selected" : null).">$i</option>";
}
If $_POST["day"]==1, this will output:
<option value='1' selected>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
<option value='9'>9</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
<option value='13'>13</option>
<option value='14'>14</option>
<option value='15'>15</option>
<option value='16'>16</option>
<option value='17'>17</option>
<option value='18'>18</option>
<option value='19'>19</option>
<option value='20'>20</option>
<option value='21'>21</option>
<option value='22'>22</option>
<option value='23'>23</option>
<option value='24'>24</option>
<option value='25'>25</option>
<option value='26'>26</option>
<option value='27'>27</option>
<option value='28'>28</option>
<option value='29'>29</option>
<option value='30'>30</option>
<option value='31'>31</option>
Completed:
//Days
echo "<select name='day'><option value='default' disabled>Select a day</option>";
for($i=1; $i<32; $i++){
echo "<option value='$i'".($i==$_POST["day"] ? " selected" : null).">$i</option>";
}
echo "</select>";
//Months
echo "<select name='month'><option value='default' disabled>Select a month</option>";
for($i=0; $i<30; $i++){
echo "<option value='$i'".($i==$_POST["month"] ? " selected" : null).">$i</option>";
}
echo "</select>";
//Years
echo "<select name='year'><option value='default' disabled>Select a year</option>";
for($i=2016; $i<2056; $i++){
echo "<option value='$i'".($i==$_POST["year"] ? " selected" : null).">$i</option>";
}
echo "</select>";

Related

HTML select option selected does not work

I want to set the current date by default but it works only on year's select (the last one).
I wouldn't like to use javascript in this case. Just html and php.
I have already tried "selected='selected'" and "autocomplete=off" as shown in different other posts.
Here my code
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php
for ($i=1; $i <= 31; $i++) {
if (sprintf('%02d', $i) == date("d")) {
echo "<option value=\"".sprintf('%02d', $i)."\" selected>".sprintf('%02d', $i)."</option>";
}
else{
echo "<option value=\"".sprintf('%02d', $i)."\">".sprintf('%02d', $i)."</option>" ;
}
}
?>
</select>
<select id="orders-month">
<?php
for ($i=1; $i <= 12; $i++) {
if (sprintf('%02d', $i) == date("m")) {
echo "<option value=\"".sprintf('%02d', $i)."\" selected>".sprintf('%02d', $i)."</option>";
}
else{
echo "<option value=\"".sprintf('%02d', $i)."\">".sprintf('%02d', $i)."</option>" ;
}
}
?>
</select>
<select id="orders-year">
<?php
for ($i=2012; $i <= 2020; $i++) {
if (sprintf('%02d', $i) == date("Y")) {
echo "<option value=\"".sprintf('%02d', $i)."\" selected>".sprintf('%02d', $i)."</option>";
}
else{
echo "<option value=\"".sprintf('%02d', $i)."\">".sprintf('%02d', $i)."</option>" ;
}
}
?>
</select>
<input type="submit" value="OK" />
</form>
here the plain HTML when the PHP has been run:
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<option value="01">01</option>
<option value="02" selected>02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select id="orders-month">
<option value="01">01</option>
<option value="02">02</option>
<option value="03" selected>03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<select id="orders-year">
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017" selected>2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
<input type="submit" value="OK" />
</form>
This is the code you are looking for
Remember to use single quotes (') when including double quotes inside the PHP echo or else if using double quotes use " inside the outer double quotes.
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php
for ($i=1; $i <= 31; $i++) {
if ($i == date("d")) {
echo '<option value="'.$i.'" selected>'.$i.'</option>';
}
else{
echo '<option value="'.$i.'">'.$i.'</option>';
}
}
?>
</select>
<select id="orders-month">
<?php
for ($i=1; $i <= 12; $i++) {
if ($i == date("m")) {
echo '<option value="'.$i.'" selected>'.$i.'</option>';
}
else{
echo '<option value="'.$i.'">'.$i.'</option>';
}
}
?>
</select>
<select id="orders-year">
<?php
for ($i=2012; $i <= 2020; $i++) {
if ($i == date("Y")) {
echo '<option value="'.$i.'" selected>'.$i.'</option>';
}
else{
echo '<option value="'.$i.'">'.$i.'</option>';
}
}
?>
</select>
Can you try like this.
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php
for ($i=1; $i <= 31; $i++) {
if ($i == date("d")) {
echo "<option value='$i' selected='selected'>$i</option>";
} else {
echo "<option value='$i'>$i</option>";
}
}
?>
</select>
<select id="orders-month">
for ($i=1; $i <= 12; $i++) {
if ($i == date("m")) {
echo "<option value='$i' selected='selected'>$i</option>";
} else {
echo "<option value='$i'>$i</option>";
}
}
</select>
<select id="orders-year">
<?php
for ($i=2012; $i <= 2020; $i++) {
if ($i == date("Y")) {
echo "<option value='$i' selected='selected'>$i</option>";
} else {
echo "<option value='$i'>$i</option>";
}
}
?>
</select>
<input type="submit" value="OK" />
</form>
Here's a more compact way of achieving this:
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php for ($d = 1; $d <= 31; $d++) { ?>
<option value="<?php echo $d; ?>" <?php if ($d == date('j')) { echo 'selected'; ?>><?php echo $d; ?></option>
<?php } ?>
</select>
<select id="orders-month">
<?php for ($m = 1; $m <= 12; $m++) { ?>
<option value="<?php echo $m; ?>" <?php if ($m == date('n')) { echo 'selected'; ?>><?php echo $m; ?></option>
<?php } ?>
</select>
<select id="orders-year">
<?php for ($y = 2012; $y <= 2020; $y++) { ?>
<option value="<?php echo $y; ?>" <?php if ($y == date('Y')) { echo 'selected'; ?>><?php echo $y; ?></option>
<?php } ?>
</select>
<input type="submit" value="OK" />
</form>
Have you tried autocomplete="off" for select? It works for me.
I know you said you tried this already, but your sample code doesn't show it. Here's how your tag should be formed:
<select>
<option value="01">01</option>
<option value="02" selected="selected">02</option>
<option value="03">03</option>
...
</select>
Your example above looks very different for the selected element. I'd suggest using whatever inspection tool is built into your web browser to see what exactly was rendered, and that will probably make it the solution obvious.
For reference, see:
Definition of select tag
Definition of option tag

Set selected option based on mysql field

I have an HTML form that a user can use to edit a row in a mysql database, the PHP script queries the database and then displays the current data in form. I have this working fine for text fields with something like:
Correct Answer:<input type="text" name="answer" value="<?php echo $row['CorrectAnswer']; ?>"><br>
and simple radio set up like:
<?php
if ($row['Hazardous'] == "no"){
?>
Hazardous?:<input type="radio" name="hazardous" value="yes">Yes
<input type="radio" name="hazardous" value="no" checked="checked">No<br>
<?php
}else{
?>
Hazardous?:<input type="radio" name="hazardous" value="yes" checked="checked">Yes
<input type="radio" name="hazardous" value="no" >No<br>
<?php } ?>
I also have a select option element like below:
Category: <select name="category">
<option value="hazardawareness">Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
Which im trying to set up, I could use:
<?php if ($row['Category'] == "hazardawareness"){ ?>
Category: <select name="category">
<option value="hazardawareness" selected="selected">Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
<?php }else if ($row['Category'] == "observation"){ ?>
Category: <select name="category">
<option value="hazardawareness">Hazard Awareness</option>
<option value="observation" selected="selected">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
<?php }else if ($row['Category'] == "insurance"){ ?>
Category: <select name="category">
<option value="hazardawareness">Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance"selected="selected">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
<?php }else if ($row['Category'] == "attitude"){ ?>
Category: <select name="category">
<option value="hazardawareness">Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude" selected="selected">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
<?php }else if ($row['Category'] == "knowledge"){ ?>
Category: <select name="category">
<option value="hazardawareness" >Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge" selected="selected">Gen. Knowledge</option>
</select><br>
<?php } ?>
But perhaps there is a better method to do this without duplicating so much code?
You can add an if clause on your option creation that will at the selected attribute if needed.
<?php $options = array(
"Hazard Awareness" => hazardawareness,
"Observation" => observation,
"Insurance" => insurance,
"Attitude" => attitude
); ?>
<select name="category">
<?php foreach($options as $display => $value) { ?>
<option value='<?= $value ?>' <?php if($row['Category'] == trim($value)) { ?>selected='selected'<?php } ?>>
<?= $display ?>
</option>
<?php } ?>
</select>
I think this will help you to do the work using less code:
<?php
$options = array(
"Hazard Awareness" => hazardawareness,
"Observation" => observation,
"Insurance" => insurance,
"Attitude" => attitude
);
echo 'Category: <select name="category">';
foreach($options as $display => $value) {
if ($row['Category'] == trim($value)) {
echo '<option value="' . $value . '" selected>' . $display .'</option>';
}
else {
echo '<option value="' . $value . '">' . $display . '</option>';
}
}
echo '</select>';
Put your option data in array().

Is there a quicker way to select the "selected" value of an HTML dropdown in PHP?

I'm currently using this garbage code to select the correct selected="selected" value for my HTML dropdown. There has got to be a better way... maybe with a switch statement or something else?
Any tips on how to make this more efficient?
if ($quantity == 0)
{
echo '
<option selected="selected" value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9+">9+</option>
';
}
else if ($quantity == 1)
{
echo '
<option value="0">0</option>
<option selected="selected" value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9+">9+</option>
';
}
else if ($quantity == 2)
{
echo '
<option value="0">0</option>
<option value="1">1</option>
<option selected="selected" value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9+">9+</option>
';
}
else if ($quantity == 3)
{
echo '
<option value="0">0</option>
<option value="1">1</option>
<option value="2">2</option>
<option selected="selected" value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9+">9+</option>
';
} etc....
This should work for you:
echo "<select>";
for($count = 0; $count < 10; $count++)
echo "<option " . ($quantity == $count ? 'selected': '') . " value='$count'>$count</option>";
echo "</select>";
You could loop through them and print each one's HTML code, like this:
for($i=0;$i<10;$i++){
$value=$i;
if($value==9) $value="9+";
if($i==$quantity){
echo '<option value="' . $value . '" selected="selected">' . $value . '</option>'
}
else{
echo '<option value="' . $value . '">' . $value . '</option>'
}
}
Using a loop is all you need :
$out='<option';
for($i=0;$i<10;i++){
if($quantity==$i)
$out.='selected="selected" ';
$out.=" value='$i'>$i</option";
echo $out;
}
I usually interpolate the 'selected' variable in to the loop
for($i=0;$i<10;$i++){
$selected = $i==$quantity? 'selected="selected"':'';
echo "<option value=\"$i\" $selected>$i</option>";
}
loop over it:
for($i = 0; $i < 9; $i++){
if($i == $quantity){
$selected = 'selected="selected"';
}else{
$selected = '';
}
$i2 = ($i == 9) ? '9+' : $i;
$select_options .= '<option value="'.$i2.'" '.$selected.'>'.$i2.'</option>';
}
echo $select_options;
Edit: looks like you can't set selected in the select its self, which is pretty stupid if you think about it. I've filed a complaint with the html5 standards people. its illogical to set the selected value of a select element in the individual options.

PHP MySQL image upload tmp to chosen directory

Ok, i want to upload an image from my computer to the host/database, everything works fine and it all uploads to the database (image name VARCHAR as to just be wording not image), except the image doesn't want to move and I get this warning:
Upload: 6285.jpg
Type: image/jpeg
Size: 68.1669921875 kB
Temp file: /tmp/php1Be59N
Warning: move_uploaded_file(/public_html/pages/CMX/images/products/oils/6285.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/littlepe/public_html/pages/CMX/pages/extras/allFunctions.php on line 875
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php1Be59N' to '/public_html/pages/CMX/images/products/oils/6285.jpg' in /home/littlepe/public_html/pages/CMX/pages/extras/allFunctions.php on line 875
Stored in: /public_html/pages/CMX/images/products/oils/6285.jpg Upload another? or Go home?
I have been through every type of directory, the first is the absolute path:
/home/public_html/pages/CMX/images/products/oils/6285.jpg
/public_html/pages/CMX/images/products/oils/6285.jpg
/pages/CMX/images/products/oils/6285.jpg
/CMX/images/products/oils/6285.jpg
/images/products/oils/6285.jpg
/products/oils/6285.jpg
/oils/6285.jpg
The directory is writeable and correct (I would show you but cannot post images as of yet).
My Form code:
$user_check=$_SESSION['login_user'];
$sqlCommand = "(SELECT * FROM userAccount WHERE email='$user_check')";
$query = mysqli_query($con,$sqlCommand) or die("Error: ".mysqli_error($con));
$column = mysqli_fetch_array($query);
if($column['admin'] != NULL){
echo "<section class='userName'><h3>".$column['firstName']." ".$column['surname']."</h3></section>";
echo "<section class='address'>".$column['addressLine1']."<br />".$column['addressLine2']."<br />".$column['county']."<br />".$column['country']."<br />".$column['postCode']."</section>";
echo "<section class='email'><h3>".$column['email']."</h3></section>";
echo "<hr />";
echo "<section class='upNewProduct' style='height:400px;overflow:auto;'><h3>Upload New Product</h3>
<form action='extras/uploadProcess.php' method='post' enctype='multipart/form-data'>
Product Name: <input type='text' name='productName' /><br />
Product Image Sub-Catagory: <select name='prodImgCat' id='prodImgCat'>
<option selected>Choose a Sub Catagory for Image</option>
<option value='oils/'>Oil</option>
<option value='chain/'>Chain</option>
<option value='coolant/'>Coolant</option>
<option value='filter/'>Filter</option>
</select><br />
Product Image: <input type='file' name='productImg' /><br />
Description: <textarea type='text' name='shortDesc' value='' maxlength='1500' cols='22' rows='6' tabindex='6' id='shortDesc'></textarea><br />
Features: <textarea type='text' name='longDesc' value='' maxlength='1500' cols='22' rows='6' tabindex='6' id='longDesc'></textarea><br />
Price: <input type='text' name='price' /> <strong style='font-size:8pt;'>(Do <u>NOT</u> enter £ / &dollar; / € currency symbols. <u>ONLY</u> enter numerals. <u>e.g:</u> 99.99 )</strong><br />
Catagory: <select name='catagory' id='catagory'>
<option selected>Choose a Catagory</option>
<option value='Helmets'>Helmets</option>
<option value='Gloves'>Gloves</option>
<option value='Boots'>Boots</option>
<option value='Body Protection'>Body Protection</option>
<option value='Clothing'>Clothing</option>
<option value='Bike Parts'>Bike Parts</option>
<option value='Accessories'>Accessories</option>
</select><br />
Sub-Catagory: <select name='subCat' id='subCat'>
<option selected>Choose a Sub Catagory</option>
<option value='Oil'>Oil</option>
<option value='Chain'>Chain</option>
<option value='Coolant'>Coolant</option>
</select><br />
Brand: <select name='brand' id='brand'>
<option selected>Choose a brand</option>
<option value='100%'>100%</option>
<option value='Acerbis'>Acerbis</option>
<option value='Airoh'>Airoh</option>
<option value='Akrapovic'>Akrapovic</option>
<option value='Alias'>Alias</option>
<option value='Alpinestars'>Alpinestars</option>
<option value='Answer Racing'>Answer Racing</option>
<option value='Apico'>Apico</option>
<option value='Arai'>Arai</option>
<option value='Artrax'>Artrax</option>
<option value='Asterisk'>Asterisk</option>
<option value='Beer Optics'>Beer Optics</option>
<option value='Bell'>Bell</option>
<option value='Blackrock'>Blackrock</option>
<option value='Bolt'>Bolt</option>
<option value='Cycra'>Cycra</option>
<option value='Deft Family'>Deft Family</option>
<option value='DEP'>DEP</option>
<option value='Dragon'>Dragon</option>
<option value='DRC'>DRC</option>
<option value='Dunlop'>Dunlop</option>
<option value='EKS Brand'>EKS Brand</option>
<option value='Engine Ice'>Engine Ice</option>
<option value='EVS'>EVS</option>
<option value='Factory Arme'>Factory Arme</option>
<option value='Factory Effex'>Factory Effex</option>
<option value='Fly Racing'>Fly Racing</option>
<option value='Forcefield Armour'>Forcefield Armour</option>
<option value='Forma'>Forma</option>
<option value='Fro Systems'>Fro Systems</option>
<option value='Gaerne'>Gaerne</option>
<option value='Goldentyre'>Goldentyre</option>
<option value='GoPole'>GoPole</option>
<option value='GoPro'>GoPro</option>
<option value='HiFlo Filtro'>HiFlo Filtro</option>
<option value='JT Braking'>JT Braking</option>
<option value='K-Edge'>K-Edge</option>
<option value='Kryptonite'>Kryptonite</option>
<option value='Lazer'>Lazer</option>
<option value='Leatt'>Leatt</option>
<option value='Matrix Concepts'>Matrix Concepts</option>
<option value='Mechanix Wear'>Mechanix Wear</option>
<option value='Motion Pro'>Motion Pro</option>
<option value='Motorex'>Motorex</option>
<option value='Muc Off'>Muc Off</option>
<option value='No Toil'>No Toil</option>
<option value='Oakley'>Oakley</option>
<option value='ODI Troy Lee'>ODI Troy Lee</option>
<option value='Ogio'>Ogio</option>
<option value='One Industries'>One Industries</option>
<option value='Park Tool'>Park Tool</option>
<option value='POD'>POD</option>
<option value='Polisport'>Polisport</option>
<option value='Pro Clean'>Pro Clean</option>
<option value='Progrip'>Progrip</option>
<option value='Putoline'>Putoline</option>
<option value='Race FX'>Race FX</option>
<option value='Raptor Titanium'>Raptor Titanium</option>
<option value='Renthal'>Renthal</option>
<option value='Rip n Roll'>Rip n Roll</option>
<option value='Risk Racing'>Risk Racing</option>
<option value='RK Chains'>RK Chains</option>
<option value='SBS Brakes'>SBS Brakes</option>
<option value='Science in Sport'>Science in Sport</option>
<option value='Scott'>Scott</option>
<option value='Seven'>Seven</option>
<option value='Shoei'>Shoei</option>
<option value='Shot'>Shot</option>
<option value='Sidi'>Sidi</option>
<option value='SixSixOne'>SixSixOne</option>
<option value='SM Pro Platinum'>SM Pro Platinum</option>
<option value='Smith'>Smith</option>
<option value='Spada'>Spada</option>
<option value='Sunline'>Sunline</option>
<option value='Suomy'>Suomy</option>
<option value='THH'>THH</option>
<option value='Thor'>Thor</option>
<option value='Troy Lee Designs'>Troy Lee Designs</option>
<option value='Twin Air'>Twin Air</option>
<option value='Venhill'>Venhill</option>
<option value='VP Racing'>VP Racing</option>
<option value='Wizard Wash'>Wizard Wash</option>
<option value='Yoshimura'>Yoshimura</option>
<option value='Zeta'>Zeta</option>
</select>
<input id='contactBtn' type='submit' name='submit' value='Submit Product' tabindex='7' />
</form>
</section>";
My PHP code:
if (!$_POST['productName'] | !$_POST['prodImgCat'] | !$_POST['shortDesc'] | !$_POST['longDesc'] | !$_POST['price'] | !$_POST['catagory'] | !$_POST['subCat'] | !$_POST['brand']){
die('You did not complete all of the required fields! Try Again?');
}
$productName = $_POST['productName'];
$prodImgCat = $_POST['prodImgCat'];
$shortDesc = $_POST['shortDesc'];
$longDesc = $_POST['longDesc'];
$price = $_POST['price'];
$catagory = $_POST['catagory'];
$subCat = $_POST['subCat'];
$brand= $_POST['brand'];
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["productImg"]["name"]);
$extension = end($temp);
if ((($_FILES["productImg"]["type"] == "image/gif")|| ($_FILES["productImg"]["type"] == "image/jpeg")|| ($_FILES["productImg"]["type"] == "image/jpg")|| ($_FILES["productImg"]["type"] == "image/pjpeg")|| ($_FILES["productImg"]["type"] == "image/x-png")|| ($_FILES["productImg"]["type"] == "image/png"))&& ($_FILES["productImg"]["size"] < 200000)&& in_array($extension, $allowedExts)){
if ($_FILES["productImg"]["error"] > 0){
echo "Return Code: " . $_FILES["productImg"]["error"] . "<br>";
}else{
echo "Upload: " . $_FILES["productImg"]["name"] . "<br>";
echo "Type: " . $_FILES["productImg"]["type"] . "<br>";
echo "Size: " . ($_FILES["productImg"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["productImg"]["tmp_name"] . "<br>";
if (file_exists("/public_html/pages/CMX/images/products/".$_POST['prodImgCat'].$_FILES['productImg']['name']."")){
echo $_FILES['productImg']['name']." already exists.";
}else{
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! LINE 875 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
move_uploaded_file($_FILES['productImg']['tmp_name'], "/public_html/pages/CMX/images/products/".$_POST['prodImgCat'].$_FILES['productImg']['name']);
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
echo "Stored in: "."/public_html/pages/CMX/images/products/".$_POST['prodImgCat'].$_FILES['productImg']['name']." <a href='http://www.littlepenguindesigns.co.uk/pages/CMX/pages/userAccount.php'>Upload another?</a> or <a href='http://www.littlepenguindesigns.co.uk/pages/CMX/pages/index.php'>Go home?</a>";
}
}
}else{
echo "Invalid file";
}
$productImgLocation = "".$_POST['prodImgCat']."".$_FILES['productImg']['name']."";
$insertProduct = ("INSERT INTO product (productName, productImg, shortDesc, longDesc, price, catagory, subCat, brand) VALUES ('".$_POST['productName']."', '".$productImgLocation."' '".$_POST['shortDesc']."', '".$_POST['longDesc']."', '".$_POST['price']."', '".$_POST['catagory']."', '".$_POST['subCat']."', '".$_POST['brand']."')");
$add_product = mysqli_query($con,$insertProduct);
mysqli_close($con);
So after all of that my question is this, what the flaming heck am I doing wrong here?, the file and directory are there and yet it doesn't want to move from the tmp folder to the chosen folder, please help as this is really starting to annoy me.
NOTE: My injection,etc. code is going to amended once i have completed the upload form, so please refrain from just posting about injections and the like, thank you.

header(Location) not working [duplicate]

This question already has answers here:
How to fix "Headers already sent" error in PHP
(11 answers)
Closed 9 years ago.
I'm aware of the fact that for header(Location) to work, no output must be sent before. The problem is that I've checked my code so many times but can't find what is actually being sent as output, thus preventing my header(Location) from working.
Can anyone spot the error?
<div id="content">
<h2>Lägg till</h2>
<p>Fyll i fälten och klicka på Lägg till för att skapa en ny kontakt i listan.</p>
<?php
$editid = $_GET['contact_id'];
$query = "SELECT *, Persons.p_id FROM Persons INNER JOIN Pictures ON (Pictures.p_id = Persons.p_id) WHERE Persons.p_id = " . $editid;
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$name = $row['name'];
$address = $row['address'];
$birthday = $row['birthday'];
$picture = $row['source'];
$p_id = $row['p_id'];
}
?>
<form action="" id="addressForm" method="post">
<ul>
<li><label for="name"><strong>Namn</strong></label><input type="text" name="name" id="name"/></li>
<li><label for="address"><strong>Adress</strong></label><input type="text" name="address" id="address"/></li>
<li><label for="year"><strong>Födelsedag</strong></label>
<select id="year" name="year">
<option value="2013">2013</option>
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
<option value="2009">2009</option>
<option value="2008">2008</option>
<option value="2007">2007</option>
<option value="2006">2006</option>
<option value="2005">2005</option>
<option value="2004">2004</option>
<option value="2003">2003</option>
<option value="2002">2002</option>
<option value="2001">2001</option>
<option value="2000">2000</option>
<option value="1999">1999</option>
<option value="1998">1998</option>
<option value="1997">1997</option>
<option value="1996">1996</option>
<option value="1995">1995</option>
<option value="1994">1994</option>
<option value="1993">1993</option>
<option value="1992">1992</option>
<option value="1991">1991</option>
<option value="1990">1990</option>
</select>
<select name="month">
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
<option value='9'>9</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
</select>
<select name="day">
<option value='1'>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
<option value='9'>9</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
<option value='13'>13</option>
<option value='14'>14</option>
<option value='15'>15</option>
<option value='16'>16</option>
<option value='17'>17</option>
<option value='18'>18</option>
<option value='19'>19</option>
<option value='20'>20</option>
<option value='21'>21</option>
<option value='22'>22</option>
<option value='23'>23</option>
<option value='24'>24</option>
<option value='25'>25</option>
<option value='26'>26</option>
<option value='27'>27</option>
<option value='28'>28</option>
<option value='29'>29</option>
<option value='30'>30</option>
<option value='31'>31</option>
</select>
</li>
<li><label for="picture"><strong>Bild (URL)</strong></label><input type="text" name="picture" id="picture"/></li>
<li><input type="submit" id="submit" name="submit" value="Lägg till"/></li>
</ul>
</form>
<?php
if(isset ($_POST['submit']))
{
$editname = mysql_real_escape_string(htmlspecialchars($_POST['name']));
$editaddress = mysql_real_escape_string(htmlspecialchars($_POST['address']));
$editpicture = mysql_real_escape_string(htmlspecialchars($_POST['picture']));
$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];
if ($month < 10)
{
$month = "0$month";
}
if ($day < 10)
{
$day = "0$day";
}
$editbirthday = $year . "-" . $month . "-" . $day;
if (!empty($name) && !empty($address)){
$update = "UPDATE Persons SET name = '$editname', address = '$editaddress', birthday = '$editbirthday' WHERE p_id = '$editid'";
$result = mysql_query($update);
$query = "SELECT * FROM Persons WHERE p_id = '$editid' LIMIT 1";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
$p_id = $row['p_id'];
$update = "UPDATE Pictures SET source = '$editpicture' WHERE p_id = '$editid'";
$result = mysql_query($update);
header('Location: index.php');
}
}
}
?>
</div>
Remember that an output could be:
Whitespace before <?php or after ?>
UTF-8 Byte Order Mark
Error messages or notices
print, echo
Raw <html> areas before <?php code.
So you're hitting the final point of this list...
You must put the header ABOVE any HTML output. You can just put it at the top of your document in this case. So just put all the PHP at the top.
Put the whole if statement from the following, to the top of the PHP file. I don't think this will cause any issues.
if(isset ($_POST['submit']))
I hope this helps
You already have output. Headers MUST be set before any output is sent.
"Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP."
Refer to the documentation about it. Hope this helps.
no output
no whitespace
save file without BOM

Categories