
rick.emmet
Members-
Posts
68 -
Joined
-
Last visited
Everything posted by rick.emmet
-
Hello Kitchen, Thank you very much, I have never seen that before. Works like a charm! Also Pikachu2000, I am allowing up to five photos along with string data to be uploaded to the server. It's OK if they upload less then five photos. Thanks again for your help, Cheers, Rick
-
Hi Everyone, I have a rather stupid question, but I haven't been able to find the correct method / syntax to do this. I want to allow users to upload a maximum of 5 photos - less then 5 is OK too. I've been testing the following script and haven't gotten it to ignore error # 4 (no file uploaded): if ($_FILES['userfile']['error'] == 1 || 2 || 3 || 6 || 7) { echo 'Problem: '; switch ($_FILES['userfile']['error']) { case 1: echo 'File exceeded upload_max_filesize'; break; case 2: echo 'File exceeded max_file_size'; break; case 3: echo 'File only partially uploaded'; break; case 6: echo 'Cannot upload file: No temp directory specified.'; break; case 7: echo 'Upload failed: Cannot write to disk.'; break; } exit; } I will be using a while loop and incrementing through the script (this is simpler version). Does anyone know the proper syntax for the IF statement ' if ($_FILES['userfile']['error'] == 1 || 2 || 3 || 6 || 7)" ? Thanks much, Rick
-
Hi Requinix, Thanks for the reply! The queries are written before executing them. In the transaction, I run the first query and grab the last insert ID, then I run the other queries. How is this wrong? Cheers, Rick
-
Hi Everyone, I got a PHP / MySQL Transaction to function as a transaction, but I have some behavior I can't figure out. In the following script I insert data into the “events” table and then I grab the insert ID using mysqli_insert_id(). The subsequent queries do not insert this ID into the other tables; they insert a zero instead. Since I've registered a session variable and given it the last insert ID's value, it should be inserted into the other tables. <?php // SET THE SESSION VARIABLE session_register('insert'); // CODE HERE TO VARIFY USER // CODE TO CREATE VARIBLE NAMES, FILTER INPUT AND CREATE END DATE // DO PHP / MySQL TRANSACTION $query1 = ("INSERT INTO events VALUES ('".$user_id."', NULL, 04, NOW(), NOW(), '".$end_date."')"); $query2 = ("INSERT INTO equip_info VALUES (NULL, '".$_SESSION['insert']."' , 04, '".$make."', '".$model."', '".$style."', '".$year."', '".$descrp."', '".$settings."', 'No', 'Yes', NOW(), '".$user_name."')"); $query3 = ("INSERT INTO pay_info VALUES(NULL, '".$_SESSION['insert']."', 'Payment and number', 19.99, Now(), '".$user_name."')"); $query4 = ("INSERT INTO photo_bin VALUES (NULL, '".$_SESSION['insert']."', CONCAT('".$_SESSION['insert']."', 'a.jpg'), CONCAT('".$_SESSION['insert']."', 'b.jpg'), CONCAT('".$_SESSION['insert']."', 'c.jpg'), CONCAT('".$_SESSION['insert']."', 'd.jpg'), CONCAT('".$_SESSION['insert']."', 'e.jpg'))"); // Start the transaction mysqli_query($conn, "START TRANSACTION"); $result1 = mysqli_query($conn, $query1); $insert = mysqli_insert_id($conn); $_SESSION['insert'] = $insert; echo " From middle of queries - the ID is ".$_SESSION['insert']."<br />"; // ECHO FOR TESTING PURPOSES $result2 = mysqli_query($conn, $query2); $result3 = mysqli_query($conn, $query3); $result4 = mysqli_query($conn, $query4); if(!$result1 || !$result2 || !$result3 || !$result4) { mysqli_rollback($conn); // Dispaly the error message no_insert_qry(); } else { mysqli_commit($conn); // PROCESS THE PHOTOS AND MOVE THEM TO THE FOLDER // Check for user_file errors userfile_error(); // Check for black listed extensions black_list(); // Check for propper mime type mimeType(); // Move the images to the uploads folder in TFC site move_imgs(); // Change the name of each photo renameFile(); // THIS USES $_SESSION['insert'] AND RENAMES THE FILES CORRECTLY // display the page name in the tab do_html_header('Equipment Info Insert Page'); // display the banner and sidebars display_banner(); login_l_sidebar(); login_r_sidebar(); // Tell user what the title of the ad is insert_sucsess($title); } // CODE HER TO FINISH OUT THE PAGE ?> Farther down the script, I rename the uploaded photos and they end up with the proper number / letter combination. Does anyone know why this is happening? I've tried everything I can think of to solve the problem. Thanks much in advance! Cheers, Rick
-
Hi Everyone, I've been working on several different versions of an insert statement and the following code is the one that has worked the best. I tried using several PHP transaction methods and couldn't get any of them to work; they would input all the data for the first table and then nothing until the last table where they would input a portion of the data. I would only get an error message that indicated MySQL sent back a boolean – meaning the query failed. The thing is, I'm worried about the possibility of ending up with orphaned data if the transaction fails for some reason half way through the insert. I know that the default behavior is to rollback, but this looks a little ify to me. Could you please comment on this code? Will this work reliably or am I in for lots of problems down the line? <?php // CODE HERE TO VARIFIY THE USER // CODE TO CREATE VARIABLE NAMES FOR USER INPUT & END DATE // MySQL TRANSACTION IN MYSQLI_MULTI_QUERY FORMAT $QUERY = <<<EOT START TRANSACTION; INSERT INTO `events` VALUES ($user_id, NULL, 04, NOW(), NOW(), '$end_date'); SET @Inst = LAST_INSERT_ID(); INSERT INTO `equip_info` VALUES (NULL, @Inst, 04, '$title', '$style', '$make', '$model', '$year', '$descrp', '$price', '$contact', 'No', 'Yes', NOW(), '$user_name'); INSERT INTO `pay_info` VALUES (NULL, @Inst,'Payment & number', 29.99, Now(), '$user_name'); INSERT INTO `photo_bin` VALUES (NULL, @Inst, CONCAT(@Inst, 'a.jpg'), CONCAT(@Inst, 'b.jpg'), CONCAT(@Inst, 'c.jpg'), CONCAT(@Inst, 'd.jpg'), CONCAT(@Inst, 'e.jpg')); COMMIT; EOT; // RUN THE MULTI QUERY OR DIE if (!$query = mysqli_multi_query($conn, $QUERY)) { echo "You have a multi-query error ".mysqli_error($conn); exit; if ($query) { do { // store the result of each part of the multi-query if ($result = mysqli_store_result($conn)) { // This is said to be necessary even though there's nothing to handle mysqli_next_result($result); } // This is said to be necessary even though there is no divider needed if (mysqli_more_results($conn)) { // I can try to remove this to see what happens } } while (mysqli_next_result($conn)); } ?> Thanks much in advance for your time! Cheers, Rick
-
Another mysql_affected_rows error on update query
rick.emmet replied to rick.emmet's topic in PHP Coding Help
I know, I know - I spent quite a bit of time at the site looking at the documentation. I was seeing what I was expecting to see and did not see what I was supposed to see. Thanks again for the heads up, sometimes you just can't see what's right there in front of you. Cheers, Rick -
Another mysql_affected_rows error on update query
rick.emmet replied to rick.emmet's topic in PHP Coding Help
Well thank you very much, I would not have guessed that I would use $conn as the parameter! As I said, the book is not very explicit (as far as procedural PHP) at that point in the book. The OO PHP uses a different format. The change worked like a charm - here's the script that works: function confirm_update($username, $passwd) { // check username and password with db // if yes, return true // else throw error // connect to db $conn = db_connect(); // check if username is unique $result = mysqli_query($conn, "update clients set reg_var = 'Yes' where user_name='".$username."' and password = sha1('".$passwd."')"); if (!$result) { echo "There's a problem with the user nane or password, please try again (#1 tfc_user_auth_fns.php).<br />"; do_html_url('login.php', 'Login'); exit; } elseif (mysqli_affected_rows($conn)>0) { return true; } else { echo "You must first complete the registration process, please check your email for a comfirmation email (#2 tfc_user_auth_fns.php).<br />"; display_login_form(); exit; } mysqli_free_result($result); mysqli_close($conn); } Again, thanks very much for your help! Cheers, Rick -
Another mysql_affected_rows error on update query
rick.emmet replied to rick.emmet's topic in PHP Coding Help
Hello PFMaBiSmAd, Sorry, I'm not quite understanding what you mean. At the point in the book that I referred to, they are writing nothing but OO PHP, and I still can't write OO PHP (I have to translate what they have in the book). Unfortunately, they aren't very explicit with procedural PHP in those later chapters. I did make some changes to the script, trying to pass a different parameter – like so: $conn = db_connect(); $query = "update clients set reg_var = 'Yes' where user_name='".$username."' and password = sha1('".$passwd."')"; // check if username is unique and update the database $result = mysqli_query($conn, $query); if (!$result) { echo "There's a problem with the user nane or password, please try again (#1 tfc_user_auth_fns.php).<br />"; do_html_url('login.php', 'Login'); exit; } elseif (mysqli_affected_rows($query)>0) { return true; // REST OF THE SCRIPT HERE The book says that it is better to write a query first and then write “$result = mysqli_query($conn, $query); ”, though they never say why. With these changes, I now get a different error message: Warning: mysqli_affected_rows() expects parameter 1 to be mysqli, string given. I looked up this error message and what I found was cases where the person failed to use the database connection as a parameter. I have the db connection as a parameter in my script ($conn). What am I missing here? Thanks again, Rick -
Another mysql_affected_rows error on update query
rick.emmet replied to rick.emmet's topic in PHP Coding Help
Hello PFMaBiSmAd, Thanks for the quick reply! I going off of what we learned in my last PHP class and specifically what PHP & MySQL Web Development says about the use of mysqli_affected_rows(). The book states that "when you write queries that change the database, you should use mysqli_affected_rows() instead [of mysqli_affected_rows()]. So, I'm a bit confused - do you know what function I should be using instead? Thanks much! Rick -
Hi Everyone, I have a script that calls a function which updates a table in the database. I've looked threw the forum and haven't found the answer (I've also looked elsewhere) The value in the reg_var column is set to “No” in advance of the query and the update changes this value to “Yes”. Here's the function: 1 function confirm_update($username, $passwd) { 2 // check username and password with db 3 // if yes, return true 4 // else throw error 5 6 // connect to db 7 $conn = db_connect(); 8 9 // check if username is unique while updating the database 10 $result = mysqli_query($conn, "update clients set reg_var = 'Yes' where 11 user_name='".$username."' and password = sha1('".$passwd."')"); 12 13 if (!$result) { 14 echo "There's a problem with the user nane or password, please try again (#1 tfc_user_auth_fns.php).<br />"; 15 do_html_url('login.php', 'Login'); 16 exit; 17 18 } elseif (mysqli_affected_rows($result)>0) { 19 20 return true; 21 22 } else { 23 24 echo "You must first complete the registration process, please 25 check your email for a comfirmation email (#2 tfc_user_auth_fns.php).<br />"; 26 27 display_login_form(); 28 exit; 29 } 30 31 mysqli_free_result($result); 32 mysqli_close($conn); 33 34 } I've tested this function where the user name and password are correct and in these cases, the function should update the reg_var column to “No”. However, I'm getting the following MySQL error message: “Warning: mysqli_affected_rows() expects parameter 1 to be mysqli, boolean given...”. Now, what gets me is that this query updates the database every time, MySQL should return “1” as that is the number of rows updated by the query. I have had similar error messages with other queries that also ran perfectly, but throw an error every time as well. It is as if one of the .ini files is turned off, but I've looked and can't find anything that looks suspicious . Does anybody have an idea what's going on with this function or the system??? Thanks in advance for your assistance! Cheers, Rick