Jump to content

Undefined Variable and Not submitting


bullbreed

Recommended Posts

Hi

 

Does anyone know why I get the following notice;

Notice: Undefined variable: pfrontimage in C:\xampp\htdocs\admin\addproduct2.php on line 116

 

Also when I submit the form I get the "Please complete all fields" message.

 

But if I remove any referrence to the images including the variables, file uplead and form fields the form submits to the database ok.

 

 

<?php
if (isset($_POST['submit'])) {

$submit = $_POST['submit'];

//Form data
$existprodcat = mysql_real_escape_string($_POST['existprod_cat']);
$newprodcat = mysql_real_escape_string($_POST['newprod_cat']);
$prodname = mysql_real_escape_string($_POST['prod_name']);
$prodptitle = mysql_real_escape_string($_POST['prod_ptitle']);
$prodpdescription = mysql_real_escape_string($_POST['prod_pdescription']);
$prodpkeywords = mysql_real_escape_string($_POST['prod_pkeywords']);

if (isset($_POST['pfrontimage'])) {
$pfrontimage = mysql_real_escape_string($_POST['pfrontimage']);
}

if (isset($_POST['pbackimage'])) {
$pbackimage = mysql_real_escape_string($_POST['pbackimage']);
}

$pcolours = mysql_real_escape_string(serialize($_POST['pcolours']));
$psizes = mysql_real_escape_string(serialize($_POST['psizes']));
$prodprice = mysql_real_escape_string($_POST['prod_price']);
$proddesc = mysql_real_escape_string($_POST['prod_desc']);
$date = date('d/m/Y \a\t g:i.s a');



//Open the database
    mysql_connect("localhost","root","");
    mysql_select_db("customfight"); //Select the database

if ($existprodcat !== ""){
        $pcat = $existprodcat;
}else{
        $pcat = $newprodcat;
}	

// Check for a front image.
if(isset($_FILES['pfrontimage'])){
if (is_uploaded_file ($_FILES['pfrontimage']['tmp_name'])) {
	if (move_uploaded_file($_FILES['pfrontimage']['tmp_name'], "uploads/{$_FILES['pfrontimage']['name']}")) { // Move the file over.
		echo '<p>The image has been uploaded!</p>';
	} else { // Couldn't move the file over.
		echo '<p><font color="red">The image could not be moved.</font></p>';
		$i = FALSE;
	}
	$i = $_FILES['pfrontimage']['name'];
} else {
	$i = FALSE;
}
}

// Check for a back image.
if(isset($_FILES['pbackimage'])){
if (is_uploaded_file ($_FILES['pbackimage']['tmp_name'])) {
	if (move_uploaded_file($_FILES['pbackimage']['tmp_name'], "uploads/{$_FILES['pbackimage']['name']}")) { // Move the file over.
		echo '<p>The image has been uploaded!</p>';
	} else { // Couldn't move the file over.
		echo '<p><font color="red">The image could not be moved.</font></p>';
		$i = FALSE;
	}
	$i = $_FILES['pbackimage']['name'];
} else {
	$i = FALSE;
}
}
		//Check for existing fields
if($prodname && $pcat && $prodptitle && $prodpdescription && $prodpkeywords && $pfrontimage && $pbackimage && $pcolours && $psizes && $prodprice && $proddesc && $date){


$pagecheck = mysql_query("SELECT prod_name FROM products WHERE prod_name = '$prodname'");echo mysql_error();
    $count = mysql_num_rows($pagecheck);

    if ($count != 0){
        echo("<font size=\"2\" color=\"#ff0000\">That product already exists. Please edit the existing product!</font>");echo mysql_error();
    }else{

//Enter into Database
	$queryreg = mysql_query("INSERT INTO `customfight`.`products` (`id`, `prod_cat`, `prod_name`, `prod_ptitle`, `prod_pdescription`, `prod_pkeywords`, `prod_fimage`, `prod_bimage`, `prod_colours`, `prod_sizes`, `prod_price`, `prod_desc`, `date`) VALUES (NULL, '$pcat', '$prodname', '$prodptitle', '$prodpdescription', '$prodpkeywords', '$pfrontimage', '$pbackimage', '$pcolours', '$psizes', '$prodprice', '$proddesc', '$date')");echo mysql_error();
	echo("<font size=\"2\" color=\"#00cc00\">Your product has been created! </font>");echo mysql_error();
}
}else{
	echo ("<font size=\"2\" color=\"#ff0000\">Please complete <b>ALL</b> fields</font>");echo mysql_error();
	 }
}	//end Check for existing fields
//end if submit
$sql = "SELECT * FROM `products`";
$query = mysql_query($sql);echo mysql_error();

?>                  
            	<div class="sechead"><p>Add a product</p><br />
                
                </div>
<form action="addproduct2.php" method="post" enctype="multipart/form-data" name="addproduct" target="_self" id="addproduct">               
                <div class="seccont">
				<div class="infocont">
					<div class="genwrap">
						<div class="arrowwrap"></div>
							<div class="titlewrap"><p>Product Name</p></div>
                        </div> 
                        
                        <div class="genwrap">
						<div class="arrowwrap"></div>
							<div class="titlewrap"><p>Create product category</p></div>
                        </div> 
                        
                        <div class="genwrap">
						<div class="arrowwrap"></div>
							<div class="titlewrap"><p>Select existing category</p></div>
                        </div>
                        
                        <div class="genwrap">
							<div class="titlewrap"><input name="prod_name" type="text" id="prod_name" size="25" />
							</div>
                        </div> 
                        
                        <div class="genwrap">
							<div class="titlewrap"><input name="newprod_cat" type="text" id="newprod_cat" size="25" />
							</div>
                        </div> 
                        
                        <div class="genwrap">
							<div class="titlewrap">
                            <?php
							//Open the database
							mysql_connect("localhost","root","");
							mysql_select_db("customfight"); //Select the database

							$sql = "SELECT `prod_cat` FROM products";
							$query = mysql_query($sql) or die(mysql_error());

							$sections = array();

							while ($row = mysql_fetch_array($query)){
									$sections[] = $row[0];
							}
							$sections = array_unique($sections);
						?>
                                <select name="existprod_cat" id="existprod_cat">
                                <option selected></option>
                            <?php
                				foreach($sections as $section){
							echo '<option value="'.$section.'">'.$section.'</option>';
							}
						?>
                                </select>
                                </div>
                        </div> 
                        
                  </div>
                 
                 </div>
                 
                 <div class="sechead">
                 <p>Meta Data</p>
                 </div>
                
                <div 
                 class="seccont">
                 
                 		<div class="genwrapmeta">
						<div class="arrowwrap"></div>
							<div class="titlewrap"><p>Title</p></div>
                    	 </div>
                         
                         <div class="genwrapmeta">
                         <input name="prod_ptitle" id="prod_ptitle" type="text" size="80" maxlength="100" />
                    	 </div>
                        
                        <div class="genwrapmeta">
						<div class="arrowwrap"></div>
							<div class="titlewrap"><p>Description</p></div>
                        </div>
                        
                        <div class="genwrapmeta">
                         <input name="prod_pdescription" type="text" id="prod_pdescription" size="80" maxlength="200" />
               	  </div>
                        
                        <div class="genwrapmeta">
						<div class="arrowwrap"></div>
							<div class="titlewrap"><p>Keywords</p></div>
                        </div>
                        
                        <div class="genwrapmeta">
                         <input name="prod_pkeywords" type="text" id="prod_pkeywords" size="80" maxlength="200" />
               	  </div>
                         
                 </div>
                 
                 <div class="sechead">Product Front Image</div>
                
                <div class="seccont"><input name="pfrontimage" type="file" /></div>
                
                <div class="sechead">Product Back Image</div>
                
                <div class="seccont"><input name="pbackimage" type="file" /></div>
                 
                 
                 
                 <div class="sechead">Product Sizes</div>
                
                <div class="seccont">
                <label>S1</label>
        <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" />
        
        <label>S2</label>
        <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" />
        
        <label>S3</label>
        <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" />
        
        <label>S4</label>
        <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" />
        
        <label>S5</label>
        <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" />
        
        <label>S6</label>
        <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" />
        
        <label>S7</label>
        <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" />
        <br /><br />
        <label>S8</label>
        <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" />
        
        <label>S9</label>
        <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" />
        
        <label>S10</label>
        <input name="psizes[]" type="text" id="psizes[]" size="5" maxlength="20" />
        </div>
                 
                 <div class="sechead">Page Colours</div>
                
                <div class="seccont">
                <div class=" colourswrap">
                <div class="colours"><img src="uploads/black.jpg" alt="Black" /></div><br /><br />
                <input type="checkbox" name="pcolours[]" id="pcolours[]" value="Black" />
            </div>
            
            <div class=" colourswrap">
                <div class="colours"><img src="uploads/white.jpg" alt="White" /></div><br /><br />
                <input type="checkbox" name="pcolours[]" id="pcolours[]" value="White" />
            </div>
            
            <div class=" colourswrap">
                <div class="colours"><img src="uploads/blue.jpg" alt="Blue" /></div><br /><br />
                <input type="checkbox" name="pcolours[]" id="pcolours[]" value="Blue" />
            </div>
            
            <div class=" colourswrap">
                <div class="colours"><img src="uploads/red.jpg" alt="Red" /></div><br /><br />
                <input type="checkbox" name="pcolours[]" id="pcolours[]" value="Red" />
            </div>
            
            <div class=" colourswrap">
                <div class="colours"><img src="uploads/pink.jpg" alt="Pink" width="22" height="22" /></div>
            <br /><br />
                <input type="checkbox" name="pcolours[]" id="pcolours[]" value="Pink" />
            </div>
                </div>
                 
                 <div class="sechead">Product Price</div>
                
                <div class="seccont">
        			<input type="text" name="prod_price" id="prod_price" />
        		
                </div>
                 
                 <div class="sechead">Page Content</div>
                
                <div class="seccont">
                 	<textarea name="prod_desc" cols="80" rows="20" id="prod_desc"></textarea>
                 </div>
                 
                 <div class="secbtm"><input type="submit" value="Save" name="submit" /></div>
                 </form>
                 </div>
                 
            </div>
            
        
        </div>

Link to comment
https://forums.phpfreaks.com/topic/192302-undefined-variable-and-not-submitting/
Share on other sites

Hi bullbree,

The Notice you are getting is because your first usage of pfrontimage is within a conditional statement.

You can fix this by either putting @ before

 

@$pfrontimage = mysql_real_escape_string($_POST['pfrontimage']);

 

which will supress the Notice or by declaring the variable earlier in your code before it is used.

 

$pfrontimage = '';

 

I've only had a quick look at your code but I can't understand why your are doing a mysql_real_escape_string on pfrontimage if it is a file. Do you have two form fields named pfrontimage?

If not and I could be wrong, but you seem to be using the same variable as a string and a file in different parts of your code.

Ah right

 

pfrontimage and pbackimage are indeed image files and because I am new to php I thought I had to use my_real_escape_string.

 

What i'm trying to do is upload a front image and a back image of a product but the code suggests that I havent filled in all the form in the form. But when I delete all references to the images the form submits o the database.

 

Not sure what to do here because the file upload bit works and puts the images in to a file called uploads so I know the form is being filled in

 

ok so heres what i did.

 

I thought that because the $pfrontimage and $pbackimage were actually Input File Fields so you can select an image from your local computer to upload in to a folder on the server, I decided to remove the variables altogether because the File Upoad and Move script on the page will upload it to the folder on the server and it all works.

 

However, I obviously don't get a value in the database for the image because the image file is just sent straight to the uploads folder.

Would this be the correct ay of doing it because I thought that if I were to;

SELECT * FROM wherever to show the user all the data related to that product they wont get to see the images of that product.

You'd most likely want to relocate the uploaded image to another folder and then put the string with the url to new image location in your database.

If you're sure you want to store the image itself in the database (which doesn't have any use afaik and will most likely just make your host unhappy due the database increasing in size quite a bit at a rapid pace) you may want to look up some how to on the subject.

Hi and thank you very much for your reply.

 

The images don't upload to the database, they upload to a folder in the server directory.

What I can't seem to achieve at the moment is put the string with the url to new image location in your database.

 

How would you do that please.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.