Jump to content
Larry Ullman's Book Forums

Natt

Members
  • Content Count

    16
  • Joined

  • Last visited

Community Reputation

1 Neutral

About Natt

  • Rank
    Newbie
  1. I would like to modify the search section of the above scripts so that in a single text box I can search by user id (student_no), first name or last name. In my entry form, I am using a drop-down list for the user to select the search criteria first before typing in the search details. I have succeeded in searching by the student_no. However, I get the following error if I try to search by name: Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\celeste\handle_search_student2.php on line 37 Here is the code in the entry from: <form action="handle_search_student2.php" method="get"> <p> <label for="search_criteria">Search by:</label> <select name = "search_criteria" id="search_criteria"> <option value="student_no" >Student no.</option> <option value="first_name" >First name</option> <option value="last_name" >Last name</option> </select> <input type="character" name="search_item" size="20" maxlength="40" value="<?php if(isset($_POST['search_item'])) echo $_POST['search_item']; ?>" /> </p> <p><input type="submit" name="submit" value="Search for Student" maxlength="20" /></p> <input type="hidden" name="submitted" value="TRUE" /> </form> And here is part of the routine handling the form: require_once ('../../mysqli_connect.php'); // Connect to the db. $errors = array(); // Initialize an error array. //function declaration - Count no of Digits in Student No. function count_digit($number) { return strlen((string) $number); } // Check for a student no.: if (empty($_REQUEST['student_no'])&&empty($_REQUEST['first_name'])&&empty($_REQUEST['last_name']) ) { $errors[] = 'Please enter at least ONE search option.'; } else { if (!empty($_REQUEST['student_no'])) { $search_criteria = 'student_no'; $sc = mysqli_real_escape_string($dbc, trim($_REQUEST['student_no'])); } elseif (!empty($_REQUEST['first_name'])) { $search_criteria = 'first_name'; $sc = mysqli_real_escape_string($dbc, trim($_REQUEST['first_name'])); } else { $search_criteria = 'last_name'; $sc = mysqli_real_escape_string($dbc, trim($_REQUEST['last_name'])); } // Make the query: $q = "SELECT s.student_id, s.student_no, s.first_name, s.middle_name, s.last_name, s.gender, s.school, l.lab_name, c.country, s.email, s.campus FROM students AS s LEFT JOIN lab AS l USING (lab_id) LEFT JOIN countries AS c USING (ccode) WHERE $search_criteria = $sc"; $r = @mysqli_query($dbc, $q); // Run the query // Count the no. of returned rows: $num = mysqli_num_rows($r); if ($num == 1) { I just cant seem to identify the cause of the error? Is there a better way to do this? Regards.
  2. Hi Everyone; In my students database, each has a unique student_no made up of 7 (number) digits e.g. 1230045 where the digits signify: i) 12 (First TWO digits) - Year of enrollment ii) 3 (THIRD digit) - Department as well as level (Masters or PhD) iii) 0 (4th digit) - Type of student (Short/Long term) iv) 045 (Last 3 Digits) - the quarter of enrollment: April, July, October or January Q: In any given student no, what PHP function can I use to identify (isolate) the above sets of digits, which I can then use to identify the student completely? i.e. From the set how can I isolate the numbers; 12, 3, 0, 045 Which I can then use to perform complete identification of the student(s)?
  3. Hi Antonio; Thank you so much for taking your time to look at my code. I was so frustrated with my lack of progress until yesterday when there was a breakthrough. I used the idea you gave in the second part of your advice about the declaring parameters within functions. And voila, I got the results I needed. That was just such a nice feeling after 3 days of frustrations. However, when I looked at the advice you gave in the first section about writing advanced code, now I am dissatisfied again. At the moment the code looks like jargon to me because of my level but from the robustness, I think that it is the better approach. I will take more of my time and look carefully through it as well as try to implement using that code structure. Thank you once again. Natt
  4. Hello I have 3 tables in my database: students toeic_scores courses_taken These are connected using the student_no field I would like to list students who meet certain search criteria from the students table - using the while loop. However I have a column listing the no of attempts on the test (using a function, cnt_toeic_scores that searches the data from the toeic_scores table) and another column for the no of courses the student has taken (using a function, courses_cnt that searches the data from courses_taken table). However, this has failed miserably. I just dont know why it isnt SELECTING date from the toeic_scores and the courses_taken table. Can someone spot something I havent done correctly? Alternatively what is a better way to go about this? Here is the code: <?php # Script 9.4 - view_students.php #4 // This script retrieves all the records from the users table. // This new version links to edit and delete pages. $page_title = 'View students'; include('includes/header.inc.html'); echo '<h1>Registered Students</h1>'; require_once ('../../mysqli_connect.php'); // Connect to the db. $errors = array(); // Initialize an error array. // Function to count the no. of TOEIC attempts by a student function cnt_toeic_attempts($sn) { echo "$sn"; // Make the query: $query = "SELECT * FROM toeic_scores WHERE student_no = $sn "; $r = @mysqli_query ($dbc, $query); if ($r) { echo 'It ran OK'; // Count the no. of returned rows: $no_of_recs = mysqli_num_rows($r); return $no_of_recs; } else { Echo 'There is a problem'; } } // End of the function definition. // Function to count the no. of English Courses taken by a student function courses_taken($sn) { // Make the query: $query = "SELECT attend_id FROM course_attendance WHERE student_no = $sn "; $r = @mysqli_query ($dbc, $query); if ($r) { echo 'It ran OK'; // Count the no. of returned rows: $no_of_recs = mysqli_num_rows($r); return $no_of_recs; } else { Echo 'There is a problem'; } } // End of the function definition. // Check for a grouping criteria: if (empty($_REQUEST['group_criteria'])) { $errors[] = 'You forgot to select the grouping criteria.'; } else { $gc = mysqli_real_escape_string($dbc, trim($_REQUEST['group_criteria'])); } // Check for a listing criteria: if (empty($_REQUEST['list_criteria'])) { $errors[] = 'You forgot to select the listing criteria.'; } else { $lc = mysqli_real_escape_string($dbc, trim($_REQUEST['list_criteria'])); switch ($lc) { case "sn": $lc = 'student_no'; break; case "ln": $lc = 'last_name'; break; case "fn": $lc = 'first_name'; break; } } // Make the query: switch ($gc) { case "all": $q = "SELECT * FROM students ORDER BY $lc ASC"; break; case "jp": $q = "SELECT * FROM students WHERE ccode = 'JP' ORDER BY $lc ASC"; break; case "njp": $q = "SELECT * FROM students WHERE ccode != 'JP' ORDER BY $lc ASC"; break; } $r = @mysqli_query ($dbc, $q); // Table header. echo '<table align="center" cellspacing="3" cellpadding="3" width="75%"> <tr> <td align="left"><b>View Details (V.D.)</b></td> <td align="left"><b>Edit</b></td> <td align="left"><b>Student No.</b></td> <td align="left"><b>Name</b></td> <td align="left"><b>Gnd.</b></td> <td align="left"><b>Sch.</b></td> <td align="left"><b>Country</b></td> <td align="left"><b>Campus</b></td> <td align="left"><b>TOEIC Attempts</b></td> <td align="left"><b>Courses Taken</b></td> </tr> '; // Fetch and print all the records. . . $bg = '#eeeeee'; // Set the initial background color. while ($row = mysqli_fetch_array($r, MYSQLI_ASSOC)) { $bg = ($bg =='#eeeeee' ? '#ffffff' : '#eeeeee'); // Switch the initial background color. $name = $row['first_name'] .' '. $row['middle_name'] .' ' .$row['last_name'] ; // Confirm & write gender in full. $gd=$row['gender']; $sc = $row['school']; $cp = $row['campus']; $ctr = $row['ccode']; $e = $row['email']; // Write Laboratory name in full: if ($sc == 'is') { $sc = 'IS'; } elseif ($sc == 'ks') { $sc = 'KS'; } elseif ($sc == 'ms') { $sc = 'MS'; } else { $sc = NULL; } if ($cp == 'i') { $cp = 'I'; } else { $cp = 'T'; } // Call and assign values returned byt he functions $toeic_cnt = cnt_toeic_attempts($row['student_no']); $courses_cnt = courses_taken($row['student_no']); echo '<tr bgcolor = "' .$bg . '"> <td align="left"><a href="view_student_details.php?id=' . $row['student_id'] . '">V. D. here</a></td> <td align="left"><a href="edit_student.php?id=' . $row['student_id'] . '">Edit here</a></td> <td align="left">' . $sn .'</td> <td align="left">' . $name .'</td> <td align="left">' . $gd .'</td> <td align="left">' . $sc .'</td> <td align="left">' . $ctr .'</td> <td align="left">' . $cp .'</td> <td align="left">' . $toeic_cnt .'</td> <td align="left">' . $courses_cnt .'</td> </tr> '; } // End of the WHILE Loop. echo '</table>'; // Close the table. mysqli_free_result($r); mysqli_close($dbc); // Close the database connection. // Include the footer and close the script: include ('includes/footer.inc.html'); ?>
  5. Good evening; I have just been checking everything - my folder was disorganized. After organizing everything once again, (in the 'change password' menu) it recalls the entry form without a problem. However, I cant update the changes, even though I can see the records in view users. I get the error(s) - in red writing: The email address and password do not match those on the file. (apparently from line 81 in the code) I just cant seem to get to the stage where it updates the new password i.e. it isn't going into the IF statement on line 43 (And I don't know how I can display/test the content of IF (empty(errors)) on line 43. Please help. You maybe able to see something that I am not seeing. <?php # Script 8.7 - password.php #2 // This page lets a user change their password: $page_title = 'Change Your Password'; include('includes/header.html'); // Check if the form has been submitted: if (isset($_POST['submitted'])) { require_once ('../../mysqli_connect.php'); // Connect to the db. $errors = array(); //Initialize an error array. // Check for an email address: if (empty($_POST['email'])) { $errors[] = 'You forgot to enter your email adress.'; } else { $e = mysqli_real_escape_string($dbc, trim(stripslashes($_POST['email']))); } // Check for the current password: if (empty($_POST['pass'])) { $errors[] = 'You forgot to enter your password.'; } else { $p = mysqli_real_escape_string($dbc, trim(stripslashes($_POST['pass1']))); } // Check for a password and a match // against the confirmed password: if (!empty($_POST['pass1'])) { if ($_POST['pass1'] != $_POST['pass2']) { $errors[] = 'You new password did not match the confirmed password.'; } else { $np = mysqli_real_escape_string($dbc, trim(stripslashes($_POST['pass1']))); } } else { $errors[] = 'You forgot to enter your new password.'; } echo $e; // Testing variables echo $p; // Testing variables echo $np; // Testing variables if (empty($errors)) { // Everything is OK. // Check that they have entered the right email address/password combination: $q = "SELECT user_id FROM users WHERE (email='$e' AND pass=SHA1('$p'))"; $r = @mysqli_query($dbc,$q); $num =@mysqli_num_rows($r); if ($num == 1) { // Match was made. //Get the user ID: $row = msqli_fetch_array($r, MYSQLI_NUM); // Make the update query: $q = "UPDATE users SET pass=SHA1('$np') WHERE user_id=$row[0]"; $r = @mysqli_query($dbc,$q); if (mysqli_affected_rows($dbc) == 1) { // If it ran OK. // Print a message echo '<h1>Thank you!</h1> <p>Your password has been updated. In chapter 11 you will actually be able to log in!</p><p></P>'; } else { // If it did not run OK // Print a message: echo '<h1>System Error</h1> <p class="error">Your password could not be changed due to a system error. We apologize for any inconvenience.</p>'; // Debugging message: echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>'; } // Include the footer and quit the script (to not show the form). include(includes/footer.html); exit(); } else { // Invalid email address/password combination. echo '<h1>Error!</h1> <p class="error">The email address and password do not match those on the file.</p>'; } } else { // Report the errors. echo '<h1>Error!</h1> <p class="error">The following error(s) occurred:<br />'; foreach ($errors as $msg) { // Print each error. echo " - $msg<br> \n"; } echo '</p><p>Please try again.</p><p><br /></p>'; } // End of if (empty($errors)) IF. mysqli_close($dbc); // Close the database connection. } // End of the main Submit conditional. ?> <h1>Change Your Password</h1> <form action="password.php" method="post"> <p>Email Address: <input type="text" name="email" size="20" maxlength="80" value="<?php if(isset($_POST['email'])) echo $_POST['email']; ?>" /></p> <p>Current Password: <input type="password" name="pass" size="10" maxlength="20" /></p> <p>New Password: <input type="password" name="pass1" size="10" maxlength="20" /></p> <p>Confirm Password: <input type="password" name="pass2" size="10" maxlength="20" /></p> <p><input type="submit" name="submit" value="register" maxlength="20" /></p> <input type="hidden" name="submitted" value="TRUE" /> </form> <?php ?>
  6. Hi I am unable to access the 'sitename' database (and the corresponding 'users' table) that we are using for the hands-on tutorial for the book. I had been working through the book until the end of Chapter 8, script 8.7, the password module. Here is the error that I get: Warning: require_once(mysql_connect.php): failed to open stream: No such file or directory in C:\xampp\htdocs\password.php on line 10 Fatal error: require_once(): Failed opening required 'mysql_connect.php' (include_path='.;C:\xampp\php\PEAR') in C:\xampp\htdocs\password.php on line 10 Since I couldn't change any password successfully, I tried to access the database directly from the phpadmin panel to view the records. However, I am not able to question the database directly like I used to before. I am not sure what happened. On the left panel, if I select recent tables, in the drop down list I can see 'sitename','users' but if I select it, everything reverts back to select tables. One other thing, on the phpadmin, when I check the status of the server, I get the message: This MySQL server has been running for 4 days, 22 hours, 38 minutes and 13 seconds. It started up on Oct 05, 2012 at 06:55 AM. Nb. that my view users module (Script 8.4) seem to work very well. Has anyone experienced this problem before? Natt
  7. Hi; Thanks for all the input. The key was the lang="ja" in the header. Now most of the browsers are OK with a few glitches. I will work on those for now. Thanks once again. Natt
  8. Hi; Thanks. I am not sure of the term environment. However we all use different browsers: Google chrome, safari, Firefox (in my case), IE etc. So far firefox seem to experience the least amount of problems as it detects the Japanese characters. For Safari, the text must be set to utf-8 all the time. It doesnt work with the default setting of the encoding. For google chrome we are all lost and just trying to figure out what the problem could be. For IE we dont get problems when operating on the intranet mode. On the stricter internet mode the characters arent being detected automatically. Does this help in any way? Nat
  9. Thank you for that. I think that this must be the solution. I will try to implement a local virtual server. Thank you. Natt
  10. Hi; I managed to solve this problem by trial and error. I decided to load all my files into a test site (with URL) and voila, the include worked. I also adjusted the style sheet and it too worked. I cant understand why it aint working when I run it from my computers storage location. Thanks to the guys who offered advice. Natt
  11. Hi; First I am not sure what you mean by a virtual server! The CSS sheet is called in the header file. So if the header isnt working, then there is no way to confirm if the CSS is. However, if I load the header file by itself, then the CSS is OK. So it is the header/footer files that arent being called in the php file that refers to the two files. If you have the book, PHP6 and MySQL five, you can see the examples. I have just copied them as they are in the boon and adjusted/created the directories as directed but the includes arent just being recognized. By the way I am carrying out the tests on my computer - a mac, running on windows parallels. When upload everything to a URL test site, I only get a blanc page. When i run it on my machine then I get the results I have displayed above. I still just cant figure it out. I have tired other examples of includes I have found on the internet but non seem to work. Could it be my setup? Natt
  12. Hi; Thanks. Actually until March I used to live in Yokohama, Kannai. Now I am in Ishikawa. Regarding the characters, I think that I have declared in the header that the character enchoding is UTF-8. But many of the trial viewers, including my boss, are reporting problems especially with the Japanese pages. You can take a look at the site and try viewing the Japanese pages. Also, you can view the character settings by viewing the code. Regards. Natt
  13. Hi everyone; I am in the process of creating my first website - for our department. However, since the site will be displayed both in Emglish and Japanese, I am running into character encoding problems. The test URL is http://www.jaist.ac....test/index.html The problem is mainly grave in safari and google chrome browsers. How can such problems be resolved? Will appreciate any comments. Natt
  14. Hi; my index file is in the directory; C:\ . . \Natts Files\E-Commerce\Tutorials\PHP6&MySQL5 While the header, footer and style files are in the directory; C:\ . . \Natts Files\E-Commerce\Tutorials\PHP6&MySQL5\includes So from the directory position, relatively, I think that this should work. However the include function doesnt get the content of the included files. Any ideas about what I may not be seeing? Natt
  15. Hi; Thanks for the reply. I have just copies the files in Chapter 3 viz. Header HTML - Script 3.2, Footer HTML - script 3.3 and then called them in the PHP file, script 3.4. The files are; Header; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title><?php echo $page_title; ?></title> <link rel="stylesheet" href="includes/style.css" type="text/css" media="screen" /> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> </head> <body> <div id="header"> <h1>Your Website</h1> <h2>catchy slogan...</h2> </div> <div id="navigation"> <ul> <li><a href="index.php">Home Page</a></li> <li><a href="calculator.php">Calculator</a></li> <li><a href="dateform.php">Date Form</a></li> <li><a href="#">link four</a></li> <li><a href="#">link five</a></li> </ul> </div> <div id="content"><!-- Start of the page-specific content. --> <!-- Script 3.2 - header.html --> Footer: <!-- Script 3.3 - footer.html --> <!-- End of the page-specific content. --></div> <div id="footer"> <p>Copyright © <a href="#">Plain and Simple</a> 2007 | Designed by <a href="http://www.edg3.co.uk/">edg3.co.uk</a> | Sponsored by <a href="http://www.opendesigns.org/">Open Designs</a> | Valid <a href="http://jigsaw.w3.org/css-validator/">CSS</a> & <a href="http://validator.w3.org/">XHTML</a></p> </div> </body> </html> PHP Body content file: <?php # Script 3.4 - index.php $page_title = 'Welcome to this Site!'; include ('includes/header.html'); ?> <h1>Content Header</h1> <p>This is where the page-specific content goes. This section, and the corresponding header, will change from one page to the next.</p> <p>Volutpat at varius sed sollicitudin et, arcu. Vivamus viverra. Nullam turpis. Vestibulum sed etiam. Lorem ipsum sit amet dolore. Nulla facilisi. Sed tortor. Aenean felis. Quisque eros. Cras lobortis commodo metus. Vestibulum vel purus. In eget odio in sapien adipiscing blandit. Quisque augue tortor, facilisis sit amet, aliquam, suscipit vitae, cursus sed, arcu lorem ipsum dolor sit amet.</p> <?php include ('includes/footer.html'); ?> When I run the index file, this is what i get: Content Header This is where the page-specific content goes. This section, and the corresponding header, will change from one page to the next. Volutpat at varius sed sollicitudin et, arcu. Vivamus viverra. Nullam turpis. Vestibulum sed etiam. Lorem ipsum sit amet dolore. Nulla facilisi. Sed tortor. Aenean felis. Quisque eros. Cras lobortis commodo metus. Vestibulum vel purus. In eget odio in sapien adipiscing blandit. Quisque augue tortor, facilisis sit amet, aliquam, suscipit vitae, cursus sed, arcu lorem ipsum dolor sit amet.
×
×
  • Create New...