Jump to content
Larry Ullman's Book Forums

margaux

Members
  • Posts

    453
  • Joined

  • Last visited

  • Days Won

    52

Everything posted by margaux

  1. The i that you refer to makes the expression case-insensitive. Since the string is being used to verify a password, the match must be exact, hence the i is omitted.
  2. This is perplexing. The first time I tried the above, I got the +VERSION error your post referred to, so I sorted that out ran the above command again and the message returned was Usage: ./mysql_tzinfo_to_sql timezonedir ./mysql_tzinfo_to_sql timezonefile timezonename ./mysql_tzinfo_to_sql --leap timezonefile which looked promising, though I don't know what that means. However, I am still getting null values when I run the CONVERT_TZ query in phpmyadmin. I've spent alot of time searching the web for similar scenarios and one suggestion was to stop and start the mysql server which after much seaching I think I accomplished but to be honest most of what is returned by the terminal commands is well and truly greek to me. If you have any more suggestions I would be grateful as I can see alot of uses for this function. For anyone who has issues which require use of terminal commands this link helped me make sense of some of what I was keying in. I used it for amongst other things, moving the +VERSION file as the /usr/ directory is locked. Now I know what sudo means!
  3. I really thought that was going to do the trick because I can see mysql_tzinfo_to_sql in the /Applications/MAMP/Library/bin/ directory. I've tried entering it numerous times to ensure I don't have a typo. The error returned is -bash: mysql_tzinfo_to_sql: command not found Is it possible that that file is corrupt and if so can I download it from somewhere? thanks for you help.
  4. if ( !empty($result) ) $result returns a boolean so you will want to check if its true or false, not empty. What error message are you receiving - can't help you unless you provide more information.
  5. You could check that the number of rows returned is equal to 1. Using a limit clause would make the query stop after 1 row was found so saving time searching through the whole table once a match was found. $q ="SELECT * FROM credentials WHERE username={$_POST['username']} LIMIT 1"; $r = mysqli_query($dbc, $q); if (mysqli_num_rows($r) == 1) { echo '<p class="error">Username already exists, please choose another.</p>'; }
  6. Thanks Larry for your reply. I'm surprised that your article didn't come up when I googled mysql convert_tz. Your post was informative but I think I have an issue with mysql on my machine. I ran this command in the terminal mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql the response is -bash: mysql_tzinfo_to_sql: command not found -bash: /usr/local/mysql/bin/mysql: No such file or directory the usr/share/zoneinfo folder exists but the usr/local/mysql/bin/mysql folder does not. Do you have any suggestions on what I need to do to get this to work. I have a usr/local/bin folder but no mysql folder within it. I'm using a mac with OS X 10.6.8. Thanks for any suggestions. I would like to get the convert_tz functionality as it would be useful for some other sites I'm working on.
  7. I’m creating the auction site example from the last chapter of Modern Javascript. It’s a very good example to test one’s understanding of php and mysql queries. I’m struggling with the conversion of timezones. All my queries work fine except when I try to use CONVERT_TZ on any of the dates. For example the following query returns all my rows with a value for itemId, item and bid but null for the column which converts the date to the user's timezone. SELECT itemId, item, FORMAT(COALESCE(MAX(bid), openingPrice),2), IF (CONVERT_TZ(dateClosed, 'UTC', 'Asia/Tokyo') < DATE_ADD(UTC_TIMESTAMP(), INTERVAL 24 HOUR), DATE_FORMAT(dateClosed,'%l:%i %p'), DATE_FORMAT(CONVERT_TZ(dateClosed, 'UTC', 'Asia/Tokyo'), '%M %D @ %l:%i %p')) FROM items LEFT JOIN bids USING (itemId) GROUP BY itemId ORDER BY dateClosed I'm starting to think that I need to configure php or mysql in some way to deal with timezone conversion but I'm not sure in which folder to look for a timezone file. I did try entering this command in the terminal, and restarted MAMP but that did not work either. # mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root mysql Any ideas?
  8. A prepared statement allows you to use placeholders (instead of values or variables) in your query on initial set up. The values are then sent when you are ready to execute the query. I can't think of a situation where you would need to use a placeholder in an ORDER BY clause. Could you be more specific about what you are trying to achieve?
  9. you will need to install MAMP, which you can download from this link, http://www.mamp.info/en/index.html. Ensure that you have the correct operating system required. Once you've installed MAMP, you will hve a new test environment and you can use phpmyadmin to set passwords.
  10. see this post If you've set up the tables with indices to the other tables you can then use a join in your mysql query. But I wonder if you are trying to run before you can walk ( which I too often do!). If you read through each chapter in order and try out the examples you'll find alot of your questions answered.
  11. You will need to confirm that your host is set up for sending emails. If you're running this on a localhost using xampp/mamp, sending emails is not supported. There is a way to configure it to send emails but I have never tried it.
  12. The best way to grasp arrays, particularly multidimensional and associative arrays is to start coding with them. I find associative arrays are very useful when programming with a database. e.g. I select a record from a d/b table which has fields firstname, middlename, surname, address1, address2, city, county, and postcode and put them into an array called $row. I can access each field by calling $row['firstname'], $row['middlename'] etc. if I am working with lots of rows - I can use a loop and I know what each field is referring to. If it was an indexed array, I would have to remember that $row[0]=firstname, $row[1]=middlename etc. You may want to get comfortable working with arrays (indexed and associative) first before moving onto multidimensional ones. Try some of the 'Pursue' questions at the end of the chapters for examples to test your understanding. If your arrays are going to end up in a database you want to keep them as simple as possible. To optimise performance, each table should hold data specific to one entity. For your example you will want a company table - to hold data about a specific company e.g. name, address etc. Then you will want tables for services and fields of work. You may find that some of your tables have only 2 or 3 columns, that is fine. You will use foreign keys to relate the tables to each other where required, for example your company table will have an index to services and to fields of work. You may find it useful to read the chapter on database design. With programming in general, you may end up including a lot of different files which initially seems counter productive but actually is not e.g. if you are continually using functions defined in those files, holding them in separate files and including the file once is a good way to streamline your code. btw - I'm not sure what you mean by a multivariable array. One array can hold different data types - strings, numbers, and arrays (that would be a multidimensional array).
  13. formattedMiddle = fullName.slice(6,11) will return a substring of fullName starting at position 6 and finishing at position 11. I agree with you Max - the code as originally written makes assumptions about what the user will input.
  14. The first line uses a regular expression to see if $name matches the string 'group' plus one numeral. The second gets that numeral by taking the last character from $name, starting from the end of the string (a positive number would start from the beginning). Then I create an array to use for the display text for each checkbox. After checking for an error, I just echo it all out. For select, you can use something similar if ($type == 'select') { if (preg_match('/group\d{1,}$/', $name)) { $data = array(0 => 'Select One', 'Value1', 'Value2', 'Value3', 'Value4', 'Value5'); } echo '<select name="' . $name . '" id="' . $label . '"'; if (array_key_exists($name, $errors)) echo 'class="error"'; echo '>'; Use a foreach loop to loop through your select array. foreach ($data as $k => $v) { echo "<option value=\"$k\""; if ($value == $k) echo ' selected="selected"' ; echo ">$v</option>\n"; } // end foreach echo '</select>'; } To use d/b values, $q = ("SELECT value1, value_id FROM table"); $r = mysqli_query($dbc, $q); ?> <select id="value_id" name="value_id"> <option value=''></option> <?php while ($row = mysqli_fetch_array($r)) { echo "<option value=\"".$row['value_id']."\">".$row['value1']."</option>\n "; } ?> </select>
  15. Not sure if you still have a question? However if you do, please will you put your code between code tags [code] [/code] Also is your college course really teaching you to write code this way? why have you put css inside php tags, why don't you just put it in the head section with the rest of the styling? Better yet, put it in an external stylesheet and provide a link to it in the head section. <link rel="stylesheet" href="mystyles.css">
  16. Since more than one checkbox can be checked, I would give each one a unique name. You can use a for loop in your form function. In your validation, also use a for loop to check if each one isset (checking with empty won't work). I changed 'option' to 'group' as I found it more meaningful but you can use whatever name you want. You can use the same code for 'medium' by just changing a few values. add this to the html part of your register page <div> <label for="group">Tutoring Option : </label> <div class="checkbox1"> <?php for ($i=1, $count=5; $i <= $count; $i++) { create_form_input('group' . $i, 'checkbox', $reg_errors); } ?> </div> this is the validation - I added the error message to group[1] to position it above the list. $group = array(); for ($i=1, $count=5; $i <= $count; $i++) { if (isset($_POST['group'. $i])) { $group[] = $_POST['group'. $i]; } } // end for loop if (empty($group)){ $reg_errors['group1'] = 'Please select at least one tuition option'; } add this code to your form function right after if ($type == 'checkbox'){ if (preg_match('/group\d{1}$/', $name)) { $i = substr($name, -1); $groupVal = array(1=>'Individual', 'Small Groups <small>( Below 10 )</small>', 'Medium Groups <small>( Between 10 to 20 )</small>', 'Big Groups <small>( Over 20 )</small>', 'Online Tuition'); // Display the error first: if (array_key_exists($name, $errors)) echo ' <span class="error">' . $errors[$name] . '</span>'; echo '<input type="' . $type . '" name="' . $name . '" value="' . $i . '" class="checkbox" /> ' . $groupVal[$i] . '<br />'; } }
  17. I did something similar - however you may want to check that the user has input a middle name var fullName = document.getElementById('fullName').value; var names = fullName.split(" "); var firstName = names[0]; if (names[2]) { var lastName = names[2]; var middleName = names[1]; } else { var lastName = names[1]; }
  18. Checkbox type is a different kind of input - you will need to loop thru the array of checkboxes and check if each one is 'checked'. If none are 'checked', then add to your error array. If I get time later, I'll try to put together some code.
  19. Traditional html does not support a maxlength attribute the way other input fields do, however HTML5 does <textarea maxlength="120"> You can use this in browsers which support HTML5 which of course excludes IE and I think Opera. For these browsers you will need a javascript solution. If you search online, I'm sure you'll find one. Initially though you probably want to alert your users - so perhaps you should set your textarea to be smaller and display an initial message that a minimum of 120 characters is allowed.
  20. <textarea> differs from <input> fields in that it does not have a value attribute. To make your textarea sticky, try <textarea name="comments" rows="7" cols="45"><?php if (isset($_POST['comments'])) {print htmlspecialchars($_POST['comments']); } ?></textarea>
  21. Can you show the code you've used for the form where the user enters his full name - presumably full name is one field and are you assuming the user enters his full name separated by spaces? If so you can use split to split the field on ' ' (space) into an array.
  22. that's great! and sorry about the errors in my code (too much javascript recently means I keep forgetting to prefix my variables with $).
  23. I think everyone understands what you are trying to ask - the code may look a little misleading if you're not used to it. When the html form is submitted, it is temporarily stored in the $_FILES superglobal. Your php script moves it to its permanent folder using the 'if' line (below). As Paul said previously, move_uploaded_file moves the file AND checks that the move is successful. if (move_uploaded_file ($_FILES['upload']['tmp_name'], "../uploads/{$_FILES['upload']['name'] }")) You can code it this way because of the way the move_uploaded_function works - it moves the file to the specified directory AND returns a boolean to indicate the success or otherwise of the move. Check out the php manual. What error are you getting?
  24. Oops, I hadnt finished editing... $r = mysqli_query($dbc,"SELECT selected_vehicles, event_year FROM event_entries ORDER BY event_year DESC"); while ($row=mysqli_fetch_array($r, MYSQLI_ASSOC)){ $selection=$row['selected_vehicles']; $vehicles=explode(',',selection); $numVehicles=count($vehicles); for ($i=0; $i<$numVehicles; $i++){ $r2 = mysqli_query($dbc,"SELECT * FROM vehicles AS v INNER JOIN users AS u USING user_id WHERE v.vehicle_id=$vehicles[i]"); $row2 = mysqli_query($r2,MYSQLI_ASSOC); //some code } // end for loop } // end while $row
  25. I don't understand this line of code $q = "SELECT * FROM vehicles WHERE vehicle_id IN ('".join("','", $selection)."')"; but I think you will need a couple of nested loops, one to loop through your exploded selected vehicle array. $r = mysqli_query($dbc,"SELECT user_id, selected_vehicles, event_year FROM event_entries ORDER BY event_year DESC"); while ($row=mysqli_fetch_array($r, MYSQLI_ASSOC)){ $selection=$row['selected_vehicles']; $vehicles=explode(',',selection); $numVehicles=count($vehicles); for ($i=0; $i<$numVehicles; i++){ $r2 = mysqli_query($dbc,"SELECT * FROM vehicles AS v INNER JOIN users AS u USING user_id WHERE v.vehicle_id=$vehicles[i]"); while ($row2 = mysqli_query($r2,MYSQLI_ASSOC)){ } } }
×
×
  • Create New...