Jump to content

meOmy

Members
  • Posts

    11
  • Joined

  • Last visited

Profile Information

  • Gender
    Male
  • Age
    58

meOmy's Achievements

Member

Member (2/5)

0

Reputation

  1. Thank you both very much for your help. Based on your replies, I was able to create/use an array for the very first time. I also thought that sanitizeInput function looked wrong and outdated. I'm glad you took the time to mention it. It confirmed my suspicion and I removed all but the trim portion. PHP is a lot of fun to learn and I'm grateful to both of you for your assistance. Sometimes, I just can't find a solution no matter how much I search, and your help is greatly appreciated to keep going.
  2. Thank you for that. It never occurred to me, but I've since tested it and finally see exactly what you mean. Just to follow up with your comment, so you can see what it was. function sanitize_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } I don't have much learning experience with arrays as you suggest to use for errors, but when I get back from FL next week, I will be definitely going down the 'rabbit hole' on: if(empty($errors)) { // use the submitted data here... } Thank you very much for taking the time to write such a lengthy response. It was/is greatly appreciated.
  3. Oh, and you may want this, too. It's the validateInput.php file being called: // check for letters, apostrophe (\'), hypens (\-), and allow empty (\s) function is_valid_firstname($firstname) { return preg_match("/^[a-zA-Z\'\-\s]*$/", $firstname); } // check for letters, hyphens (\-), and allow empty (\s) function is_valid_lastname($lastname) { return preg_match("/^[a-zA-Z\'\-\s]*$/", $lastname); } // check for letters, numbers, hyphens (\-), and allow empty (\s) function is_valid_address1($address1) { return preg_match("/^[a-zA-Z0-9\-\s]*$/", $address1); } // check for letters, numbers, hyphens (\-), and allow empty (\s) function is_valid_address2($address2) { return preg_match("/^[a-zA-Z0-9\-\s]*$/", $address2); } // check for letters and allow empty (\s) function is_valid_city($city) { return preg_match("/^[a-zA-Z\s]*$/", $city); } // check for numbers and allow empty (\s) function is_valid_zipcode($zipcode) { return preg_match("/^[0-9\s]*$/", $zipcode); } // check for numbers and allow empty (\s) function is_valid_phone($phone) { return preg_match("/^[0-9\-\s]*$/", $phone); }
  4. So, this is what I have now, but NOW the if statements for validation are not halting the process. It's showing the error, but it continues on to the ELSE portion. Why is it no longer stopping when I purposely put a number inside an input that only allows/calls for letters? I'm not getting any PHP display errors, but I don't really know how to test an IF statement to see what's happening in the background. This used to work, but somewhere down the line with all my changes it stopped working and i can't find anything on the Internet that addresses my particular case. Thank you. if ($_SERVER['REQUEST_METHOD'] === 'POST') { require_once("../_includes/functions/validateInput.php"); if (!is_valid_firstname($firstname)) { $firstnameErr = "Only letters allowed"; } if (!is_valid_lastname($lastname)) { $lastnameErr = "Only letters allowed"; } if (!is_valid_address1($address1)) { $address1Err = "Only letters & numbers allowed"; } if (!is_valid_address2($address2)) { $address2Err = "Only letters & numbers allowed"; } if (!is_valid_city($city)) { $cityErr = "Only letters allowed"; } if (!is_valid_zipcode($zipcode)) { $zipcodeErr = "Only numbers allowed"; } if (!is_valid_phone($phone)) { $phoneErr = "Only numbers allowed"; } else { $firstname = sanitize_input($_POST["firstname"]); $lastname = sanitize_input($_POST["lastname"]); $address1 = sanitize_input($_POST["address1"]); $address2 = sanitize_input($_POST["address2"]); $city = sanitize_input($_POST["city"]); $state = sanitize_input($_POST["state"]); $zipcode = sanitize_input($_POST["zipcode"]); $phone = localize_us_number($_POST["phone"]); $stmt = $pdo->prepare("SELECT username FROM users WHERE username=?"); $stmt->execute([$myusername]); $user = $stmt->fetch(); if ($user) { if ($_SESSION["usertype"] == 5) { // The file templates/contact.php is used by both user and admin. However, when used by admin, it's necessary to update active_state. // This section not required by user as they can't update their active_state. $sql = "UPDATE users SET active_state=? WHERE username = ?"; $stmt = $pdo->prepare($sql)->execute([$active_state, $myusername]); } $sql = "UPDATE contact SET firstname=?, lastname=?, address1=?, address2=?, city=?, state=?, zipcode=?, phone=? WHERE username = ?"; $stmt = $pdo->prepare($sql)->execute([$firstname, $lastname, $address1, $address2, $city, $state, $zipcode, $phone, $myusername]); } else { $stmt = $pdo->prepare("INSERT INTO contact (firstname, lastname, address1, address2, city, state, zipcode, phone) VALUES (?, ?, ?, ?, ?, ?, ?, ?) "); $stmt->execute([$firstname, $lastname, $address1, $address2, $city, $state, $zipcode, $phone]); } } }
  5. Goodness! I must have been looking at this too quickly because I didn't even realize you actually showed me a function and a solution. Let me process this. Thank you for your time!
  6. Thanks for the name tip. Yes, it's a personal learning project, but I want to learn to do it better.
  7. Hello, I'm just playing around with PHP (I love it but am not very good at it) and now trying to find ways to clean things up and/or reduce the amount of code I see on any given page. My problem is: I have a lot more validation (preg_match) than I'm showing below, but to make it easier for you, I've removed all but two of them. When I look at that page, I think to myself, why not put all the validation in a separate file and then INCLUDE/REQUIRE the file that has the validation inside it? However, that doesn't work. I even thought maybe there's a way to do a function with all the validation in it and place that inside the IF/ELSE. However, I'm not very good at/with functions, so that solution has eluded me. The fact I can't seem to find an answer on the Internet is leading me to believe I'm going about it the wrong way. How do I remove the validation part from where it is right now and put it in a separate file or function and use/call it, so the page works? Thank you in advance for any help you can provide. if ($_SERVER['REQUEST_METHOD'] === 'POST') { if (!preg_match("/^[a-zA-Z\'\-\s]*$/", $firstname) == TRUE) { $firstnameErr2 = "Only letters allowed"; } if (!preg_match("/^[a-zA-Z\'\-\s]*$/", $lastname) == TRUE) { $lastnameErr = "Only letters allowed"; } else { $firstname = sanitize_input($_POST["firstname"]); $lastname = sanitize_input($_POST["lastname"]); $address1 = sanitize_input($_POST["address1"]); $address2 = sanitize_input($_POST["address2"]); $city = sanitize_input($_POST["city"]); $state = sanitize_input($_POST["state"]); $zipcode = sanitize_input($_POST["zipcode"]); $phone = localize_us_number($_POST["phone"]); $stmt = $pdo->prepare("SELECT username FROM users WHERE username=?"); $stmt->execute([$myusername]); $user = $stmt->fetch(); if ($user) { if ($_SESSION["usertype"] == 5) { $sql = "UPDATE users SET active_state=? WHERE username = ?"; $stmt = $pdo->prepare($sql)->execute([$active_state, $myusername]); } $sql = "UPDATE contact SET firstname=?, lastname=?, address1=?, address2=?, city=?, state=?, zipcode=?, phone=? WHERE username = ?"; $stmt = $pdo->prepare($sql)->execute([$firstname, $lastname, $address1, $address2, $city, $state, $zipcode, $phone, $myusername]); } else { $stmt = $pdo->prepare("INSERT INTO contact (firstname, lastname, address1, address2, city, state, zipcode, phone) VALUES (?, ?, ?, ?, ?, ?, ?, ?) "); $stmt->execute([$firstname, $lastname, $address1, $address2, $city, $state, $zipcode, $phone]); } } }
  8. Hi, I have a table `assessments` as shown below id date username provider ------------------------------------------------------------------------------------------ 1 2015-09-15 me1@me.com provider_1_name@me.com 2 2016-03-15 me1@me.com provider_1_name@me.com 3 2016-09-07 me1@me.com provider_1_name@me.com 4 2015-09-07 me2@me.com provider_2_name@me.com 5 2016-03-07 me2@me.com provider_2_name@me.com 6 2016-09-07 me2@me.com provider_2_name@me.com So, imagine provider 1 & 2 are high school counselors who need to be reminded to counsel a student 6 months after the last time the student was counseled. So, what I want/need to figure out is how can I query all the records for dates that are beyond 6 months old? I did it like this: $query = "SELECT date, username, provider FROM assessments WHERE date <= CURDATE() - INTERVAL 6 MONTH"; $results = mysqli_query($con, $query) or die (mysqli_error($con)); while ($row = mysqli_fetch_array($results)) { $provider = $row['provider']; // NOW I HAVE COUNSELOR'S EMAIL FOR OTHER QUERY $students = $row['username']; // NOW I HAVE STUDENT'S EMAIL FOR OTHER QUERY Works great! However, over time every record will be older than 6 months and a reminder would have been sent at the appropriate time, so I only want to get the last record for EACH username, so I can send an email to the counselor letting them know it's time for another assessment just as it hits 6 months. In the table above that means I only want ID 3 and ID 6. I can get the last record like this: $query = "SELECT DISTINCT username FROM assessments WHERE username = '".$students."' ORDER BY date DESC LIMIT 1"; $results = mysqli_query($con, $query) or die (mysqli_error($con)); Problem is... I don't know how to put these two queries together. I need the first query to get the last record for each username that is older than 6 months and THEN the second query to the the username and provider for each of the results from the first query. Can anyone help me with this?
  9. Hi guys, Never done anything like this, so wondering how you would go about doing it. Imagine a document that has three check boxes (Yes, No, Maybe) and one of them must be marked with an 'X' when printed and handed to some fictional office. Now online, I would prefer to code a single drop-down menu with the three options that saves the answer to my db. However, I'm not sure how I would get the saved data to be placed, in the proper checkbox, on the outputted form when I create a PDF. Would you save the data to one db field or would you create three separate db fields; one for each possible answer? This way, you could put the output code in each position on the form and then only one of them would appear? Sorry for this being such an ambiguous write-up. Bottom line, would you save all three possible answers to one db field or would you create three db fields; one for YES, one for NO, and one for MAYBE? I'll figure out how to do it later, I just want to know the best way to store the data to make it easier to get on a printed document/pdf later. Thank you!
  10. Hi All, The Problem: I'm trying to learn how to take records from the NAME row of one table (i.e. Tom, Jim, Chris, Mike) and put them into a SELECT (drop down menu) box, then select a name and save it to a DIFFERENT table where the row is also called NAME, and THEN have that selection I just saved show in the SELECT box as SELECTED when the page refreshes. I have found a hundred websites showing how to bind a SELECT box to a table and put them into a SELECT box via $key => $val and even how to save it to ANOTHER textbox on the page, which is a lot easier, but never a step further as described in the first paragraph above. Seems unbelievable to me since it's so common. Maybe I'm just not using the proper search terms/keywords. Here's an example: Let's say you wanted to notify 3 people of your birthday. So, on the notification page, you would enter three names of people you would want notified all in separate input textboxes with [keys]. So, you enter John, Chris, and Tom into three text boxes and save the page. Done. Now, on another page, I have -- say .... five select boxes. For arguments sake, let's just say I click on all of them one at a time. In each of them, I would only see three names (John, Chris, and Tom). Now, in the first SELECT box I click on John. In the second select box I click on Chris, and on the third I click on Tom. Then save the form. I would like to see John, Chris, and Tom in the first three boxes after refreshing and I would like to see SELECT A NAME on the last two Select boxes that we never did anything with. Remember, there are NO default names in the first table with row called NAMES. They are populated by the user and saved to the row. I'm attaching an image of what the page looks like that I want to see the names and be able to select them. Does anyone have a link to a site that shows how to do this... or maybe have something very basic that would show me the process? Or, if not too difficult, maybe you could show me? Thanks in advance for any help!
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.