Jump to content
Larry Ullman's Book Forums

lou22

Members
  • Posts

    8
  • Joined

  • Last visited

lou22's Achievements

Newbie

Newbie (1/14)

2

Reputation

  1. OK, so I found the answer by doing a bit of internet research. For anyone who has a similar problem: 1. I forgot to change the database name when I copied mysqli_connect.php to a new folder. 2. the problem seemed to be in the connection so I added: if (!$dbc) { die('Connect Error: ' . mysqli_connect_error()); }else{ echo '<p>connection seems fine</p>'; } after defining the db connection to check if it was OK. I got 'connection seems fine' so I could eliminate the problem being in the connection. 3. According to another forum: //prepare the statement: $stmt = mysqli_prepare($dbc, $q); was returning 'false', hence the boolean in the warning, so I tested the prepare statement by adding: if ( !$stmt ) { die('mysqli error: '.mysqli_error($dbc)); }else{ echo '<p>mysqli_prepare seems fine</p>'; } and this gave the detailed error message: mysqli error: Unknown column 'parent_id' in 'field list' on checking my db I found i'd omitted the parent_id column. Whole thing now works. Hope this helps for anyone else with similar problems.
  2. Thanks for your helpful reply - sorry I didn't reply sooner - my kids have been sick. I still can't get it to work although I did notice that I hadn't changed the database name in mysqli_connect.php which I have copied to the same folder as post_message.php. I think that the error is probably coming from: //prepare the statement: $stmt = mysqli_prepare($dbc, $q); and / or possibly: //connect to database: $dbc = mysqli_connect('localhost','username','password','forum'); //substituted my username and password but I still can't see where I've made the error. I would be grateful for any further help.
  3. Hi, I wonder if anyone can help spot what I've done wrong. I keep getting the following error messages when I submit a comment on the post_message.php form: Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in D:\xampp\htdocs\PHPMYSQL\Chapter 12\post_message.php on line 25 Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given in D:\xampp\htdocs\PHPMYSQL\Chapter 12\post_message.php on line 35 Warning: mysqli_stmt_affected_rows() expects parameter 1 to be mysqli_stmt, boolean given in D:\xampp\htdocs\PHPMYSQL\Chapter 12\post_message.php on line 38 Your message could not be posted. Warning: mysqli_stmt_error() expects parameter 1 to be mysqli_stmt, boolean given in D:\xampp\htdocs\PHPMYSQL\Chapter 12\post_message.php on line 45 Warning: mysqli_stmt_close() expects parameter 1 to be mysqli_stmt, boolean given in D:\xampp\htdocs\PHPMYSQL\Chapter 12\post_message.php on line 50 All these warnings refer to lines where I refer to the $stmt. I have written the script twice but get same result. I've compared it line for line with the script from the book and the downloaded post_message.php file. I can't find the error. My code is: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Post a Message</title> </head> <body> <?php #Script 12.4 -post_message.php if(isset($_POST['submitted'])){ //validate the data omitted! //connect to database: $dbc = mysqli_connect('localhost','username','password','forum'); //substituted my username and password //make the query: $q = 'INSERT INTO messages(forum_id, parent_id, user_id, subject, body, date_entered) VALUES(?,?,?,?,?, NOW())'; //prepare the statement: $stmt = mysqli_prepare($dbc, $q); //bind the variables: mysqli_stmt_bind_param($stmt, 'iiiss', $forum_id, $parent_id, $user_id, $subject, $body); //assign the values to the variables: $forum_id = (int)$_POST['forum_id']; $parent_id = (int)$_POST['parent_id']; $user_id = 3; $subject = strip_tags($_POST['subject']); $body = strip_tags($_POST['body']); //execute the query: mysqli_stmt_execute($stmt); //Print a message based on the result: if(mysqli_stmt_affected_rows($stmt)==1){ echo '<p>Your message has been posted.</p>'; }else{ echo '<p style="font-weight:bold; color:#c00">Your message could not be posted.</p>'; echo '<p>'.mysqli_stmt_error($stmt).'</p>'; } //close the statement mysqli_stmt_close($stmt); //close the connection: mysqli_close($dbc); }//end of submission IF //display form ?> <form action="post_message.php" method="post"> <fieldset> <legend>Post a message:</legend> <p><b>Subject</b>:<input name="subject" type="text" size="30" maxlength="100"/></p> <p><b>Body</b>:<textarea name="body" rows="3" cols="40"></textarea> </fieldset> <div align="center"><input type="submit" name="submit" value="Submit"/></div> <input type="hidden" name="submitted" value="TRUE"/> <input type="hidden" name="forum_id" value="1"/> <input type="hidden" name="parent_id" value="0"/> </form> </body> </body> </html> I'm using Vista home basic,Dreamweaver cs4, xampp v3.02 with Apache 2.0 Handler and phpmyadmin version 3.4.5 . phpinfo gives the configuration path for php.ini as C:\Windows and Loaded configuration file as D:xampp\php\php.ini. Thanks in advance for any help.
  4. Thanks Larry - I love the book by the way! The files had uploaded - I found them in d:\xampp\ instead of d:\xampp\tmp - and all the file names were prefixed with 'tmp' - I'd missed out the last backslash in the file path. I've now successfully uploaded a jpeg to d:\xampp\tmp. I appreciate that d:\xampp\tmp is temporary but I just wanted to get it working. I've now changed the path in the move_upload_file to d:\xampp\upload\ and successfully tested that too. Thanks for your help.
  5. Sorry, I'm having trouble editing the post. The code is: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Upload an Image</title> <style type="text/css" title="text/css" media="all"> .error{ font-weight: bold; color:#c00 } </style> </head> <body> <?php #Script 10.3 upload_image.php //check if the form has been submitted: if(isset($_POST['submitted'])){ //check for an upload file: if(isset($_FILES['upload'])){ //Validate the type. Should be JPEG or PNG: $allowed = array('image/pjpeg','image/jpeg','image/jpeg','image/JPG','image/X-PNG','image/PNG','image/png','image/x-png'); if(in_array($_FILES['upload']['type'],$allowed)){ //move the file over if(move_uploaded_file($_FILES['upload']['tmp_name'],"../../../tmp{$_FILES['upload']['name']}")){//php.info says default upload file is xampp/tmp echo '<p><em>The file has been uploaded!</em></p>'; }//End of move IF }else{//invalid type echo '<p class=""error">Please upload a JPEG or PNG image.</p>'; } }//End of isset($_FILES['upload']) IF //check for an error: if($_FILES['upload']['error']>0){ echo '<p class=""error">The file could not be uploaded because: <strong>'; switch($_FILES['upload']['error']){ case 1: print 'The file exceeds the upload_max_filesize setting in php.ini.'; break; case 2: print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.'; break; case 3: print 'The file was only partially uploaded.'; break; case 4: print 'No file was uploaded.'; break; case 6: print 'No temporary folder was available.'; break; case 7: print 'Unable to write to the disk.'; break; case 8: print 'File upload stopped.'; break; default: print 'A system error occurred.'; break; }//end of switch print '</strong></p>'; }// End of error IF //delete the file if it still exists: if(file_exists($_FILES['upload']['tmp_name'] && is_file($_FILES['upload']['tmp_name']))){ unlink($_FILES['upload']['tmp_name']); } }//End of the submitted conditional ?> <div align="center"> <form enctype="multipart/form-data" action="upload_image.php" method="post" style="width:400px"> <input type="hidden" name="MAX_FILE_SIZE" value="524288" > <fieldset><legend>Select a JPEG or PNG image of 512KB or smaller to be uploaded:</legend> <p><b><input type="file" name="upload"/></b></p> </fieldset> <div align="center"><input type="submit" name="submit" value="Submit"/></div> <input type="hidden" name="submitted" value="TRUE"/> </form> </div> </body> </html>
  6. Hi, I've managed to get script 10.3 upload_image.php working in as far as the form displays, I can select a file, submit it, and get a message saying that the file has uploaded. But I can't find the file in the uploads folder. Not sure what is going wrong. I'm using Vista home basic,Dreamweaver cs4, xampp v3.02 with Apache 2.0 Handler. phpinfo gives the configuration path for php.ini as C:\Windows and Loaded configuration file as D:xampp\php\php.ini. The upload_tmp_dr is D:\xampp\tmp. according to phpinfo and d:\xampp\php\php.ini. The properties box for d:\xampp\tmp dir doesn't specifically say whether it's private or not but I successfully saved a copy of the upload_image.php file there from Dreamweaver so I assume not, although I am logged in as administrator. My code is: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Upload an Image</title> <style type="text/css" title="text/css" media="all"> .error{ font-weight: bold; color:#c00 } </style> </head> <body> <?php #Script 10.3 upload_image.php //check if the form has been submitted: if(isset($_POST['submitted'])){ //check for an upload file: if(isset($_FILES['upload'])){ //Validate the type. Should be JPEG or PNG: $allowed = array('image/pjpeg','image/jpeg','image/jpeg','image/JPG','image/X-PNG','image/PNG','image/png','image/x-png'); if(in_array($_FILES['upload']['type'],$allowed)){ //move the file over if(move_uploaded_file($_FILES['upload']['tmp_name'],"../../../tmp{$_FILES['upload']['name']}")){//php.info says default upload file is xampp/tmp echo '<p><em>The file has been uploaded!</em></p>'; }//End of move IF }else{//invalid type echo '<p class=""error">Please upload a JPEG or PNG image.</p>'; } }//End of isset($_FILES['upload']) IF //check for an error: if($_FILES['upload']['error']>0){ echo '<p class=""error">The file could not be uploaded because: <strong>'; switch($_FILES['upload']['error']){ case 1: print 'The file exceeds the upload_max_filesize setting in php.ini.'; break; case 2: print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.'; break; case 3: print 'The file was only partially uploaded.'; break; case 4: print 'No file was uploaded.'; break; case 6: print 'No temporary folder was available.'; break; case 7: print 'Unable to write to the disk.'; break; case 8: print 'File upload stopped.'; break; default: print 'A system error occurred.'; break; }//end of switch print '</strong></p>'; }// End of error IF //delete the file if it still exists: if(file_exists($_FILES['upload']['tmp_name'] && is_file($_FILES['upload']['tmp_name']))){ unlink($_FILES['upload']['tmp_name']); } }//End of the submitted conditional ?> <div align="center"> <form enctype="multipart/form-data" action="upload_image.php" method="post" style="width:400px"> <input type="hidden" name="MAX_FILE_SIZE" value="524288" > <fieldset><legend>Select a JPEG or PNG image of 512KB or smaller to be uploaded:</legend> <p><b><input type="file" name="upload"/></b></p> </fieldset> <div align="center"><input type="submit" name="submit" value="Submit"/></div> <input type="hidden" name="submitted" value="TRUE"/> </form> </div> </body> </html> .error{ font-weight: bold; color:#c00 } //check if the form has been submitted: if(isset($_POST['submitted'])){ //check for an upload file: if(isset($_FILES['upload'])){ //Validate the type. Should be JPEG or PNG: $allowed = array('image/pjpeg','image/jpeg','image/jpeg','image/JPG','image/X-PNG','image/PNG','image/png','image/x-png'); if(in_array($_FILES['upload']['type'],$allowed)){ //move the file over if(move_uploaded_file($_FILES['upload']['tmp_name'],"../../../tmp{$_FILES['upload']['name']}")){//php.info says default upload file is xampp/tmp echo 'The file has been uploaded! '; }//End of move IF }else{//invalid type echo 'Please upload a JPEG or PNG image. '; } }//End of isset($_FILES['upload']) IF //check for an error: if($_FILES['upload']['error']>0){ echo 'The file could not be uploaded because: '; switch($_FILES['upload']['error']){ case 1: print 'The file exceeds the upload_max_filesize setting in php.ini.'; break; case 2: print 'The file exceeds the MAX_FILE_SIZE setting in the HTML form.'; break; case 3: print 'The file was only partially uploaded.'; break; case 4: print 'No file was uploaded.'; break; case 6: print 'No temporary folder was available.'; break; case 7: print 'Unable to write to the disk.'; break; case 8: print 'File upload stopped.'; break; default: print 'A system error occurred.'; break; }//end of switch print ' '; }// End of error IF //delete the file if it still exists: if(file_exists($_FILES['upload']['tmp_name'] && is_file($_FILES['upload']['tmp_name']))){ unlink($_FILES['upload']['tmp_name']); } }//End of the submitted conditional ?> Select a JPEG or PNG image of 512KB or smaller to be uploaded: I'd be grateful for any help</p>
  7. Thanks for the reply. Yes, I've realised I need to familiarise myself with php and mysql first.
  8. Hi, I wonder if anyone can help me - I'm a newbie and using phpmyadmin to set up the database in Knowledge is Power - what is the phpmyadmin equivalent to mySql 'key' index in code setting up database tables in Chapter 3? There isn't an option for 'key' just 'primary' 'unique' 'index' 'full text' in phpmyadmin. eg, KEY 'category_id' ('category_id') Thanks
×
×
  • Create New...