Jump to content

php/form not parsing correctly


philip315

Recommended Posts

I have been trying to figure this out for more than a week but my form does not parse the information to send it to mysql. When I try to send it I get this error:

 

Parse error: syntax error, unexpected ')' in /home/content/19/6550319/html/listingpoopys.php on line 91

Can anyone maybe try the code out and figure out what is not right?

 

Here is the form. Underneath will be the php

 

<form method="post" action="something.php" enctype="multipart/form-data"> 



<label>Asking Price 

</label> 
<input type="text" name="price" /> 

<br /> 
<br /> 
<div style="text-align: left;"> 





</div> 

<label>Photo 1 
<span class="small">(required)</span> 
</label> 
<input type="hidden" name="size" value="350000" /> 
<input type="file" name="photo" /> 


<label>Photo 2 (Optional) 
<span class="small">no larger than 320px x 240px </span> 

</label> 
<input type="hidden" name="size" value="77000" /> 
<input type="file" name="phototwo" /> 

<label>Photo 3 (Optional) 
<span class="small">no larger than 320px x 240px </span> 

</label> 
<input type="hidden" name="size" value="77000" /> 
<input type="file" name="photothree" /> 

<label>Photo 4 (Optional) 
<span class="small">no larger than 320px x 240px </span> 

</label> 
<input type="hidden" name="size" value="77000" /> 
<input type="file" name="photofour" /> 


<label> description 
<span class="small">Ad Copy</span> 
</label> 

<textarea name="atDescription" rows="10" cols="35" onfocus="this.value=''; this.onfocus=null;">What you enter here will show up on the description area</textarea> 

<p>What is the condition?</p> 
<select name="condition"> 
<option value="Excellent">Excellent</option> 
<option value="Very Good">Very Good</option> 
<option value="Good">Good</option> 
<option value="Fair">Fair</option> 
</select> 


<p>How many GB?</p> 
<select name="giga"> 
<option value="16">16</option> 
<option value="32">32</option> 
<option value="64">64</option> 
</select> 

<p>Does It Come installed?</p> 
<select name="yesg"> 
<option value="Yes">Yes</option> 
<option value="No">No</option> 
</select> 




<br /><br /> 
<h1>Personal Information</h1> 
<p class="style">Information below will not appear on website</p> 
<label>First Name 

</label> 
<input type="text" name="firstName" /> 

<label>Last Name 

</label> 
<input type="text" name="lastName" /> 

<label>email 

</label> 
<input type="text" name="email" /> 

<br/> 
<br/> 



<input type="submit" value="Submit form" />
</form>

 

Here is the php:

 

<?php 


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



//This gets all the other information from the form 
$price=$_POST['price']; 
$pic=($_FILES['photo']['name']);
$pic2=($_FILES['phototwo']['name']);
$pic3=($_FILES['photothree']['name']);
$pic4=($_FILES['photofour']['name']);
$description=$_POST['atDescription']; 
$condition=$_POST['condition']; 
$gig=$_POST['giga']; 
$yesg=$_POST['yesg']; 
$fname=$_POST['firstName']; 
$lname=$_POST['lastName']; 
$email=$_POST['email']; 


// Connects to your Database 

// I'll assume you are using host, user and psssword, and have just removed them for security.
mysql_connect ("", "", "") or die(mysql_error()) ; 

// I'll assume you have the database name in here, and have just removed it for security.
mysql_select_db("") or die(mysql_error()) ; 


$price = mysql_real_escape_string ($price); 
$description = mysql_real_escape_string ($description); 
$condition = mysql_real_escape_string ($condition); 
$gig = mysql_real_escape_string ($gig); 
$yesg = mysql_real_escape_string ($yesg); 
$fname = mysql_real_escape_string ($fname); 
$lname = mysql_real_escape_string ($lname); 
$email = mysql_real_escape_string ($email); 



//Writes the photos to the server and store the names in $picname[]
foreach ($_FILES["photos"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["photos"]["tmp_name"][$key];
        $name = $_FILES["photos"]["name"][$key];
        move_uploaded_file($tmp_name, "upload/$name");
        $picname[] = $name;
    }
else {
	//Gives and error if its not 
	echo "Sorry, there was a problem uploading your files."; 
} 
// setup empty names so that the variable will exist
$pic = "";
$pic2 = "";
$pic3 = "";
$pic3 = "";

// overwrite the empty pic names if they exist
if (count($picname) > 0) {  //at least 1 pic
$pic = $picname[0];
}
if (count($picname) > 1) {  //at least 2 pics
$pic2 = $picname[1];
}
if (count($picname) > 2) {  //at least 3 pics
$pic3 = $picname[2];
}
if (count($picname) > 3) {  //at least 4 pics
$pic4 = $picname[3];
}  // don't care if there's more than 4


// print variables to screen for debugging
echo "price is ".$price."<br/>";
echo "description is ".$description."<br/>";
echo "condition is ".$condition."<br/>";
echo "gig is ".$gig."<br/>";
echo "yesg is ".$yesg."<br/>";
echo "fname is ".$fname."<br/>";
echo "lname is ".$lname."<br/>";
echo "email is ".$email."<br/>";
// remove or comment out when debugging complete

//Writes the information to the database 
$sql = "INSERT INTO artdod (price,photo,phototwo,photothree,photofour,atDescription,condition,giga,yesg,firstName,lastName,email) 
VALUES ('$price', '$pic', '$pic2', '$pic3', '$pic4', '$description', '$condition', '$gig', '$yesg', '$fname', '$lname', '$email')") ; 

//Print the sql to screen after all the variable substitutions. 
echo $sql."<br/>"; 
// remove or comment out when debugging complete

//Writes the information to the database 
mysql_query($sql) or die(mysql_error()); // and catch any error 



echo date("m/d/y : H:i:s", time()) 


?>

 

 

Link to comment
Share on other sites

Hi:

 

As for line 91 I tried taking out the extra bracket and also adding in an extra opening bracket before ("Insert into...to make it even.

 

My error respnse was syntax error.

 

Here is the exact words of the error:

 

  INSERT INTO artdod (price,photo,phototwo,photothree,photofour,atDescription,condition,giga,yesg,firstName,lastName,email) VALUES ('32.', '', '', '', '', 'jhgjhgj', 'Excellent', '16', 'Yes', 'hj', 'hj', 'hjhj')

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'condition,giga,yesg,firstName,lastName,email) VALUES ('32.', '', '', '', '', '' at line 1

Link to comment
Share on other sites

That's a completely different error. The other error was a PHP parse error, that's a MySQL error. The error is triggered by the word condition, which is a MySQL reserved word. As such it must be enclosed in `backticks` any time it is used in the query string, or you can rename the field to something that isn't a reserved word.

Link to comment
Share on other sites

Thank you for letting me know that condition should be `condition`. You also said that the foreach part of the code has no closing bracket

//Writes the photos to the server and store the names in $picname[]
foreach ($_FILES["photos"]["error"] as $key => $error) 
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["photos"]["tmp_name"][$key];
        $name = $_FILES["photos"]["name"][$key];
        move_uploaded_file($tmp_name, "upload/$name");
        $picname[] = $name;
    }
else {
	//Gives and error if its not 
	echo "Sorry, there was a problem uploading your files."; 
} 

 

I think the opening bracket was right in front of the word {if. I took it out to make opening and closing brackets equal. This did not work. Here is my warning message:

 

Warning: Invalid argument supplied for foreach() in /home/content/19/6550319/html/listingpoopysss.php on line 46

 

Problem is that I was given this code which is related to the code after it to work out the fact that the data was not going to mySQL. I understand only that this code represents an array, but I am not clear as to how it sets up a name for my pictures (whether it be empty or a picture file that someone actually uploads to the php) and I really dont understand where to put the brackets yet.

Link to comment
Share on other sites

You shouldn't remove an opening brace when what you need to do is add the closing brace . . .

 

//Writes the photos to the server and store the names in $picname[]
foreach ($_FILES["photos"]["error"] as $key => $error) { // <<<---------- Put this brace back in
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["photos"]["tmp_name"][$key];
        $name = $_FILES["photos"]["name"][$key];
        move_uploaded_file($tmp_name, "upload/$name");
        $picname[] = $name;
    }
else {
	//Gives and error if its not 
	echo "Sorry, there was a problem uploading your files."; 
}
}  // <<<-------------------- ADD THIS <<< --------------------------
// setup empty names so that the variable will exist
$pic = "";

 

$_FILES['photos']['error'] won't be an array (which is what foreach() expects), so let's get the exact structure of the $_FILES array. At the beginning of the script, add the following line, and post the output it generates after the form has been submitted.

echo '$_FILES array:<pre>'; print_r($_FILES); echo '</pre>';

 

 

Link to comment
Share on other sites

Okay I added the brackets back in and I added the code you gave me to the top of the page. With the code as it is I do not get the photos but I get the data to mySQL. The result gives this message:

 

$_FILES array:

Array

(

    [photo] => Array

        (

            [name] => 009.jpg

            [type] => image/pjpeg

            [tmp_name] => /tmp/phpcMumuD

            [error] => 0

            => 2239300

        )

 

    [phototwo] => Array

        (

            [name] =>

            [type] =>

            [tmp_name] =>

            [error] => 4

            => 0

        )

 

    [photothree] => Array

        (

            [name] =>

            [type] =>

            [tmp_name] =>

            [error] => 4

            => 0

        )

 

    [photofour] => Array

        (

            [name] =>

            [type] =>

            [tmp_name] =>

            [error] => 4

            => 0

        )

 

)

 

 

Warning: Invalid argument supplied for foreach() in /home/content/19/6550319/html/listingpoopysss.php on line 44

price is 29.

description is ytryty

condition is Excellent

gig is 16

yesg is Yes

fname is tyt

lname is tyty

email is tytyt

INSERT INTO artdod (price,photo,phototwo,photothree,photofour,atDescription,`condition`,giga,yesg,firstName,lastName,email) VALUES ('29.', '', '', '', '', 'ytryty', 'Excellent', '16', 'Yes', 'tyt', 'tyty', 'tytyt')

03/20/11 : 18:44:13

 

 

Link to comment
Share on other sites

I have not heard from anyone in a couple of days and I am still having problems getting multiple pictures transferred to my server. I have been testing over the past couple of days and I renamed the "photos" from different names to one name so that the php can read it as an array. See below. I will write more after I post code:

 

$price=$_POST['price']; 
$pic=($_FILES['photo']['name']);
$pic2=($_FILES['photo']['name']);
$pic3=($_FILES['photo']['name']);
$pic4=($_FILES['photo']['name']);
$description=$_POST['atDescription']; 
$condition=$_POST['condition']; 
$gig=$_POST['giga']; 
$yesg=$_POST['yesg']; 
$fname=$_POST['firstName']; 
$lname=$_POST['lastName']; 
$email=$_POST['email']; 

 

I did the above so that this following code will work. I will write another bit after the code:

 

//Writes the photos to the server and store the names in $picname[]
foreach ($_FILES["photo"]["error"] as $key => $error) {   
     if ($error == UPLOAD_ERR_OK) {        
	     $tmp_name = $_FILES["photo"]["tmp_name"][$key];        
			 $name = $_FILES["photo"]["name"][$key];        
			 move_uploaded_file($tmp_name, "upload/$name");        
			 $picname[] = $name;    
	 }	
	else {		
	 //Gives and error if its not 		
	 echo "Sorry, there was a problem uploading your files."; 	
	 }
	 }  



// setup empty names so that the variable will exist
$pic = "";
$pic2 = "";
$pic3 = "";
$pic3 = "";

// overwrite the empty pic names if they exist
if (count($picname) > 0) {  //at least 1 pic
$pic = $picname[0];
}
if (count($picname) > 1) {  //at least 2 pics
$pic2 = $picname[1];
}
if (count($picname) > 2) {  //at least 3 pics
$pic3 = $picname[2];
}
if (count($picname) > 3) {  //at least 4 pics
$pic4 = $picname[3];
}  // don't care if there's more than 4

 

Now I get an error message. I will give you the error message now and place the two line code that the error message is associated with below. Here is the error message:

 

Warning: basename() expects parameter 1 to be string, array given in /home/content/19/6550319/html/listingpoopysss.php on line 6

Sorry, there was a problem uploading your files.Sorry, there was a problem uploading your files.

 

Now below is lines 5 - 6 associated with the above error message:

 

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

 

Can  you help me deciphor

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.