Wayniac Posted November 24, 2009 Share Posted November 24, 2009 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:\wamp\www\Family Tree Album\add_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 <?php include("config.php"); if(isset($_POST['submit'])) {//begin of if($submit). // Set global variables to easier names // and pervent sql injection and apostrophe to break the db. $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){ //this means If the title is really empty. echo "Error: Entry title is a required field. Please fill it."; exit(); //exit the script and don't do anything else. }// end of if //run the query which adds the data gathered from the form into the database $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). // If the form has not been submitted, display it! 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>."); // put your db name in here where the xxxx are //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 // note that theese dimmensions are considered the maximum dimmension <strong class="highlight">and</strong> are not fixed, // because we have to keep the <strong class="highlight">image</strong> ratio intact or it will be deformed define ("WIDTH","150"); //set here the width you want your thumbnail to be define ("HEIGHT","120"); //set here the height you want your thumbnail to be. // 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> // the next steps will be taken: // 1. calculate the ratio by dividing the old dimmensions with the new ones // 2. if the ratio for the width is higher, the width will remain the one define in WIDTH variable // <strong class="highlight">and</strong> the height will be calculated so the <strong class="highlight">image</strong> ratio will not change // 3. otherwise we will use the height ratio for the <strong class="highlight">image</strong> // as <strong class="highlight">a</strong> result, only one of the dimmensions will be from the fixed ones $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 ?> </p> <p><br> <input type="submit" name="submit" value="Add News"> </p> </form> <?php }//end of else ?> Quote Link to comment https://forums.phpfreaks.com/topic/182799-parse-error/ Share on other sites More sharing options...
mrMarcus Posted November 24, 2009 Share Posted November 24, 2009 there's more to that error message. you not think that'd be important information, though? the answer lies in the message. "Parse error: parse error in C:\wamp\www\Family Tree Album\add_news.php on line 97" should be an "unexpected something or other" in there. can't (won't) do anything 'til i see that message. Quote Link to comment https://forums.phpfreaks.com/topic/182799-parse-error/#findComment-964819 Share on other sites More sharing options...
Adam Posted November 24, 2009 Share Posted November 24, 2009 Think this may be your problem (although pointing out which line 97 is exactly would help): mysql_select_db(album) You need quotes around album. Edit: Edit: And the quotes around "highlight" need to be escaped. Quote Link to comment https://forums.phpfreaks.com/topic/182799-parse-error/#findComment-964820 Share on other sites More sharing options...
JAY6390 Posted November 24, 2009 Share Posted November 24, 2009 change that line to mysql_select_db('album') or die('Could not find <strong class="highlight">database</strong>.'); // put your db name in here where the xxxx are Quote Link to comment https://forums.phpfreaks.com/topic/182799-parse-error/#findComment-964821 Share on other sites More sharing options...
Wayniac Posted November 24, 2009 Author Share Posted November 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: $title = mysql_escape_string($_POST['title']); $age = mysql_escape_string($_POST['age']); $testimonial = mysql_escape_string($_POST['testimonial']); But I don't see anyway for the image to do that since its located below this content. Quote Link to comment https://forums.phpfreaks.com/topic/182799-parse-error/#findComment-964936 Share on other sites More sharing options...
mikesta707 Posted November 24, 2009 Share Posted November 24, 2009 I don't quite understand your question. If you don't have the column that will hold whatever value you want to save, why don't you just alter the table and add it, or create a new table Quote Link to comment https://forums.phpfreaks.com/topic/182799-parse-error/#findComment-964939 Share on other sites More sharing options...
Wayniac Posted November 24, 2009 Author Share Posted November 24, 2009 Thank you for replying Mikesta, 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:\wamp\www\Family Tree Album\add_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. Quote Link to comment https://forums.phpfreaks.com/topic/182799-parse-error/#findComment-964950 Share on other sites More sharing options...
Wayniac Posted November 24, 2009 Author Share Posted November 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:\wamp\www\Family Tree Album\add_news.php on line 183 Notice: Undefined variable: thumb_name in C:\wamp\www\Family Tree Album\add_news.php on line 251 Notice: Undefined variable: albumid in C:\wamp\www\Family Tree Album\add_news.php on line 252 Quote Link to comment https://forums.phpfreaks.com/topic/182799-parse-error/#findComment-965024 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.