/    Sign up×
Community /Pin to ProfileBookmark

Sorry for the massive code in advance, I have been going at this for a day and a bit and can’t seem to wrap my head around why I keep getting “Parse error: parse error in C:wampwwwFamily Tree Albumadd_news.php on line 97”. It works fine once you take out all the image content. So everything below // start of image information except the HTML submit button. I’m using WampServer Version 2.0, if anyone could help throw me in any direction except for this one, I would be greatly appreciated.

Oh, I am using PHP Version 5.3.0.

Thank you

[code=php]
<?php

include(“config.php”);

if(isset($_POST[‘submit’]))

{//begin of if($submit).

$title = mysql_escape_string($_POST[‘title’]);

$age = mysql_escape_string($_POST[‘age’]);

$testimonial = mysql_escape_string($_POST[‘testimonial’]);

//check if (title) field is empty then print error message.

if(!$title){

echo “Error: Entry title is a required field. Please fill it.”;

exit(); //exit the script and don’t do anything else.

}// end of if

$result = mysql_query(“INSERT INTO album (title, dtime, age, testimonial)

VALUES (‘$title’,NOW(),’$age’,’$testimonial’)”,$connect);

//print success message.

echo “<b>Thank you! News added Successfully!<br>You’ll be redirected to Home Page after (4) Seconds”;

echo “<meta http-equiv=Refresh content=4;url=index.php>”;

}//end of if($submit).

else

{//begin of else

?>

<br>

<h3>::Add News</h3>

<form method=”post” action=”<?php $_SERVER[‘PHP_SELF’] ?>”>

<p>Title:

<input name=”title” size=”40″ maxlength=”255″>

<br>

Age:

<textarea name=”age” rows=”7″ cols=”30″></textarea>

<br>

Testimonial:

<textarea name=”testimonial” rows=”7″ cols=”30″></textarea>

</p>

<p>
<?php

// start of image information

set_time_limit(0);

$link = mysql_connect($dbhost, $dbusername, $dbpassword) or die(“Could not connect to host.”); //put in your db connection details here

mysql_select_db(album) or die(“Could not find <strong class=”highlight”>database</strong>.”);

//define <strong class=”highlight”>a</strong> maxim size for the uploaded images

define (“MAX_SIZE”,”500″);

// define the width <strong class=”highlight”>and</strong> height for the thumbnail

<strong class=”highlight”>and</strong> are not fixed,

define (“WIDTH”,”150″);

define (“HEIGHT”,”120″);
// this is the function that will create the thumbnail <strong class=”highlight”>image</strong> from the uploaded <strong class=”highlight”>image</strong>

// the <strong class=”highlight”>resize</strong> will be done considering the width <strong class=”highlight”>and</strong> height defined, but without deforming the <strong class=”highlight”>image</strong>

function make_thumb($img_name,$filename,$new_w,$new_h)

{

//get <strong class=”highlight”>image</strong> extension.

$ext=getExtension($img_name);

//creates the new <strong class=”highlight”>image</strong> using the appropriate function from gd library

if(!strcmp(“jpg”,$ext) || !strcmp(“jpeg”,$ext))

$src_img=imagecreatefromjpeg($img_name);

if(!strcmp(“png”,$ext))

$src_img=imagecreatefrompng($img_name);

if(!strcmp(“gif”,$ext))

$src_img=imagecreatefromgif($img_name);

//gets the dimmensions of the <strong class=”highlight”>image</strong>

$old_x=imageSX($src_img);

$old_y=imageSY($src_img);

// next we will calculate the new dimmensions for the thumbnail <strong class=”highlight”>image</strong>

$ratio1=$old_x/$new_w;

$ratio2=$old_y/$new_h;

if($ratio1>$ratio2) {

$thumb_w=$new_w;

$thumb_h=$old_y/$ratio1;

}

else {

$thumb_h=$new_h;

$thumb_w=$old_x/$ratio2;

}

// we create <strong class=”highlight”>a</strong> new <strong class=”highlight”>image</strong> with the new dimmensions

$dst_img=ImageCreateTrueColor($thumb_w,$thumb_h);

// <strong class=”highlight”>resize</strong> the big <strong class=”highlight”>image</strong> to the new created one

imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);

// output the created <strong class=”highlight”>image</strong> to the file. Now we will have the thumbnail <strong class=”highlight”>into</strong> the file named by $filename

if(!strcmp(“png”,$ext))

imagepng($dst_img,$filename);

else

imagejpeg($dst_img,$filename);

if (!strcmp(“gif”,$ext))

imagegif($dst_img,$filename);

//destroys source <strong class=”highlight”>and</strong> destination images.

imagedestroy($dst_img);

imagedestroy($src_img);

}

// This function reads the extension of the file.

// It is used to determine if the file is an <strong class=”highlight”>image</strong> by checking the extension.

function getExtension($str) {

$i = strrpos($str,”.”);

if (!$i) { return “”; }

$l = strlen($str) – $i;

$ext = substr($str,$i+1,$l);

return $ext;

}

// This variable is used as <strong class=”highlight”>a</strong> flag. The value is initialized with 0 (meaning no error found)

//and it will be changed to 1 if an error occures. If the error occures the file will not be uploaded.

$errors=0;

// checks if the form has been submitted

if(isset($_POST[‘Submit’]))

{

//reads the name of the file the user submitted for uploading

$image=$_FILES[‘cons_image’][‘name’];

// if it is not empty

if ($image)

{

// get the original name of the file from the clients machine

$filename = stripslashes($_FILES[‘cons_image’][‘name’]);

// get the extension of the file in <strong class=”highlight”>a</strong> lower case format

$extension = getExtension($filename);

$extension = strtolower($extension);

// if it is not <strong class=”highlight”>a</strong> known extension, we will suppose it is an error, print an error message

//and will not <strong class=”highlight”>upload</strong> the file, otherwise we continue

if (($extension != “jpg”) && ($extension != “jpeg”) && ($extension != “png”) && ($extension != “gif”))

{

echo ‘<h1>Unknown extension! Please use .gif, .jpg or .png files only.</h1>’;

$errors=1;

}

else

{

// get the size of the <strong class=”highlight”>image</strong> in bytes

// $_FILES[‘image’][‘tmp_name’] is the temporary filename of the file in which

//the uploaded file was stored on the server

$size=getimagesize($_FILES[‘cons_image’][‘tmp_name’]);

$sizekb=filesize($_FILES[‘cons_image’][‘tmp_name’]);

//compare the size with the maxim size we defined <strong class=”highlight”>and</strong> print error if bigger

if ($sizekb > MAX_SIZE*1024)

{

echo ‘<h1>You have exceeded the 1MB size limit!</h1>’;

$errors=1;

}

$rand= rand(0, 1000);

//we will give an unique name, for example <strong class=”highlight”>a</strong> random number

$image_name=$rand.’.’.$extension;

//the new name will be containing the full path where will be stored (images folder)

$consname=”image/”.$image_name; //change the image/ section to where you would like the original <strong class=”highlight”>image</strong> to be stored

$consname2=”image/thumb”.$image_name; //change the image/thumb to where you would like to store the new created thumb nail of the <strong class=”highlight”>image</strong>

$copied = copy($_FILES[‘cons_image’][‘tmp_name’], $consname);

$copied = copy($_FILES[‘cons_image’][‘tmp_name’], $consname2);

$sql=”UPDATE your table name SET <strong class=”highlight”>image</strong>= ‘$consname2’ WHERE id= ‘$lastid'”or die(mysql_error());

$query = mysql_query($sql)or die(mysql_error());

//we verify if the <strong class=”highlight”>image</strong> has been uploaded, <strong class=”highlight”>and</strong> print error instead

if (!$copied) {

echo ‘<h1>Copy unsuccessfull!</h1>’;

$errors=1;

}

else

{

// the new thumbnail <strong class=”highlight”>image</strong> will be placed in images/thumbs/ folder

$thumb_name=$consname2 ;

// call the function that will create the thumbnail. The function will get as parameters

//the <strong class=”highlight”>image</strong> name, the thumbnail name <strong class=”highlight”>and</strong> the width <strong class=”highlight”>and</strong> height desired for the thumbnail

$thumb=make_thumb($consname,$thumb_name,WIDTH,HEIGHT);

}

}

}

}

//If no errors registred, print the success message <strong class=”highlight”>and</strong> how the thumbnail <strong class=”highlight”>image</strong> created

if(isset($_POST[‘Submit’]) && !$errors)

{

echo “<h5>Thumbnail created Successfully!</h5>”;

echo ‘<img src=”‘.$thumb_name.'”>’;

echo $lastid;

}

echo “<form name=”newad” method=”post” enctype=”multipart/form-data” action=””>”;

echo “<input type=”file” name=”cons_image” >”;

echo “<input name=”Submit” type=”submit” id=”image1″ value=”<strong class=”highlight”>Upload</strong> image” />”;

echo “</form>”;

// end of image information

?>

&nbsp;</p>

<p><br>

<input type=”submit” name=”submit” value=”Add News”>

</p>

</form>

<?php

}//end of else

?>

[/code]

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 24.2009 — This line needs to have the quotes around "highlight" escaped with back-slashes or else changed to single quotes:
[code=php]
mysql_select_db(album) or die("Could not find <strong class="highlight">database</strong>.");

[/code]

A few lines after that you have a line of HTML in the middle of PHP code. You either need to jump out of PHP mode there, or else echo/print that text.

Toward the end you have the same sort of quoting problem as above on this line:
[code=php]
$sql="UPDATE your table name SET <strong class="highlight">image</strong>= '$consname2' WHERE id= '$lastid'"or die(mysql_error());
[/code]
...and then this line:
[code=php]
echo "<input name="Submit" type="submit" id="image1" value="<strong class="highlight">Upload</strong> image" />";
[/code]
Copy linkTweet thisAlerts:
@WayniacauthorNov 24.2009 — Thank you thank you, that worked wonderfully. I'm very new to php, and am going through multiple tutorials. Could someone tell me what the field names are that are connecting via SQL to my database "album"?

Correction, I mean to say, I don't know where the image is going into the database at, I currently don't have any field setup within the table of the database "album" to accept it. Where at in the php code does it say the field name.

Previosuly, I would send data like so:

[code=php]$title = mysql_escape_string($_POST['title']);

$age = mysql_escape_string($_POST['age']);

$testimonial = mysql_escape_string($_POST['testimonial']);[/code]


But I don't see anyway for the image to do that since its located below this content.
Copy linkTweet thisAlerts:
@WayniacauthorNov 24.2009 — Perhaps I should elabourate further due to my lack of understanding.

What I am trying to explain is that since this is example script I am practicing with, I am not certain where the in the php code the image field is being declared. I noticed that the $lastid variable was in there, but I keep getting "Notice: Undefined variable: lastid in C:wampwwwFamily Tree Albumadd_news.php on line 249" error for that one. So I believe I have to add it to my table which is simple enough, but I don't see where it says the field name in the script.

A quick example would be:

$title = mysql_escape_string($_POST['title']);

$title is declared, and what would go in the database > table > which is the field IS present. Variable $lastid is being declared, but NOT present thus making it not sent off to the database. How can I make it so it stores the image in the database.

Sorry for my lack of understanding, please bare with me.
Copy linkTweet thisAlerts:
@WayniacauthorNov 24.2009 — I found the issue was that I had to rename the table and then add a field in the sql database, which I called "image". But still I keep getting these evil errors.

Here are all the problems that I am stuck with, which seem to be connected together, especially since they are came at one.

Notice: Undefined index: cons_image in C:wampwwwFamily Tree Albumadd_news.php on line 183

Notice: Undefined variable: thumb_name in C:wampwwwFamily Tree Albumadd_news.php on line 251

Notice: Undefined variable: albumid in C:wampwwwFamily Tree Albumadd_news.php on line 252
×

Success!

Help @Wayniac spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...