Jump to content

terungwa

Members
  • Posts

    90
  • Joined

  • Last visited

Everything posted by terungwa

  1. $text = preg_replace("/([^[])/me([^\n\r$]*)([\n\r$])/", "\\1<span class=\"me\">*".$ubbObj->username." \\2 *</span>\\3", $text); $text = nl2br($text); $text = preg_replace("([^[])/me([^\n\r$]*)([\n\r$])", "\\1[me=".$this->username."]\\2[/me]\\3", $text); I was editing the code bit above (converting eregi_replace to preg_replace) but i am getting a warning: Warning: preg_replace(): Unknown modifier '(' could you help me resolve the right regex synthax. Thanks.
  2. I am trying to extract the strings that match this pattern @xyz from an input string using the preg_match_all() php function. My results are to be outputted to $match, as an array where $match[0] should contain all full matches. But what I am getting is an empty array. I would appreciate a clue to resolve this. My code is below. <?php $data = "@mike The party will start at 10:30 pm and @John run untill 12:30 am. Please @Emeka, could you inform @Joseph?"; preg_match_all('/^@(a-z)*$/', $data, $match, PREG_PATTERN_ORDER); echo "Matches: <br>"; print_r($match[0]); ?> Thanks.
  3. Currently, I am subjecting the insert query to the PHP function checkdate(), which is smart enough to know when its a leap year and prevents mistakes such as September 31. but again, If i can remove it from the front-end, the better. if (!checkdate($month,$day,$year)) { echo 'You have used an invalid date'; } else{ // create SQL $sql = 'INSERT INTO date ( date_value ) VALUES(?)'; if ($stmt->prepare($sql)) { // bind parameters and execute statement $stmt->bind_param('s', $date_value); // execute and get number of affected rows $stmt->execute(); if ($stmt->affected_rows > 0) { $OK = true; } } // redirect if successful or display error if ($OK) { header('Location: insert_success.php'); exit; } else { $error = $stmt->error; } }}
  4. My date selection list below allows selection of 30th, 31st and 29th february (on non-leap year). Although I noticed the mysql database is treating those entries as invalid and records them as 0000-00-00. How may i remove this from the user input interface? <?php $monthName = array(1 => "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); $today = time(); //stores today's date $f_today = date("M-d-Y",$today); //formats today's date $startyear = date("Y")-60; echo "<div style = 'text-align: center'>\n"; echo "<form action='$_SERVER[PHP_SELF]' method='POST'>\n"; /* build selection list for the month */ $todayMO = date("n",$today); //get the month from $today echo "<select name='month'>\n"; for ($n=1;$n<=12;$n++) { echo " <option value=$n"; if ($todayMO == $n) { echo " selected"; } echo " > $monthName[$n]\n\n</option>"; } echo "</select>\n"; /* build selection list for the day */ $todayDay= date("d",$today); //get the day from $today echo "<select name='day'>\n"; for ($n=1;$n<=31;$n++) { echo " <option value=$n"; if ($todayDay == $n ) { echo " selected"; } echo " > $n</option>\n"; } echo "</select>\n"; /* build selection list for the year */ echo "<select name='year'>\n"; for ($n=$startyear;$n<=$startyear+200;$n++) { echo " <option value=$n"; if ($startyear == $n ) { echo " selected"; } echo " > $n</option>\n"; } echo "</select>\n"; echo "<p> <input type=\"submit\" name=\"insert\" value=\"Insert New Entry\" id=\"insert\"> </p></form></div>\n"; ?> Thanks.
  5. Thank you Barand, this is my final functional insert SQL query: <?php $errors = array(); if (isset($_POST['insert'])) { $month=$_POST['month']; $day=$_POST['day']; $year=$_POST['year']; $date_value="$year-$month-$day"; require_once('./includes/connection.inc.php'); // initialize flag $OK = false; // create database connection $conn = dbConnect('read'); // initialize prepared statement $stmt = $conn->stmt_init(); // create SQL $sql = 'INSERT INTO date ( date_value ) VALUES(?)'; if ($stmt->prepare($sql)) { // bind parameters and execute statement $stmt->bind_param('s', $date_value); // execute and get number of affected rows $stmt->execute(); if ($stmt->affected_rows > 0) { $OK = true; } }
  6. I have this php script below that produces a date selection list. Considering that this form has three date field components, that is year, month and day. I am at a loss as to how to insert the three variables into one variable for inclusion into the database. <?php $monthName = array(1 => "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"); $today = time(); //stores today's date $f_today = date("M-d-Y",$today); //formats today's date $startyear = date("Y")-60; echo "<div style = 'text-align: center'>\n"; echo "<form action='$_SERVER[PHP_SELF]' method='POST'>\n"; /* build selection list for the month */ $todayMO = date("n",$today); //get the month from $today echo "<select name='month'>\n"; for ($n=1;$n<=12;$n++) { echo " <option value=$n"; if ($todayMO == $n) { echo " selected"; } echo " > $monthName[$n]\n\n</option>"; } echo "</select>\n"; /* build selection list for the day */ $todayDay= date("d",$today); //get the day from $today echo "<select name='day'>\n"; for ($n=1;$n<=31;$n++) { echo " <option value=$n"; if ($todayDay == $n ) { echo " selected"; } echo " > $n</option>\n"; } echo "</select>\n"; /* build selection list for the year */ echo "<select name='year'>\n"; for ($n=$startyear;$n<=$startyear+200;$n++) { echo " <option value=$n"; if ($startyear == $n ) { echo " selected"; } echo " > $n</option>\n"; } echo "</select>\n"; echo "<p> <input type=\"submit\" name=\"insert\" value=\"Insert New Entry\" id=\"insert\"> </p></form></div>\n"; ?> I tried insert SQL query to post into the mysql database but it did not work: <?php $errors = array(); if (isset($_POST['insert'])) { $month=$_POST['month']; $day=$_POST['day']; $year=$_POST['year']; $date_value="$year-$month-$day"; echo "YYYY-mm-dd format :$date_value<br>"; require_once('./includes/connection.inc.php'); // initialize flag $OK = false; // create database connection $conn = dbConnect('read'); // initialize prepared statement $stmt = $conn->stmt_init(); // create SQL $sql = 'INSERT INTO date ( id, month, day, year, date_value ) VALUES(?, ?, ?, ?, ?)'; if ($stmt->prepare($sql)) { // bind parameters and execute statement $stmt->bind_param('issss', $_POST['id'], $_POST['month'], , $_POST['day'], , $_POST['year'], , $_POST['date_value']); // execute and get number of affected rows $stmt->execute(); if ($stmt->affected_rows > 0) { $OK = true; } } but how do i structure the insert SQL query? Thanks.
  7. This Project has been awarded and thus closed. Thank you all.
  8. if $reg_errors is not empty, then the regex validation fails and the variable $reg_errors is displayed for the affected field for example in the last_name field i would have this: <?php if(isset($reg_errors['last_name'])) echo $reg_errors['last_name'];?> . I am using a flag $OK, which is a Boolean variable that is initialized to either true or false and i am using it to check whether my query has happened. For instance, i set $OK to false and reset to true only when a database query executes successfully, // what to do if successful or show me an error if ($OK) { echo 'success'; exit; } else { $error = $stmt->error; } I am then using this to to display the error message if the insert operation fails: but i am not getting any errors even though the insert query is obviously failing. <?php if (isset($error)) { echo "<p>Error: $error</p>"; } ?>
  9. My insert code is not posting into the database when i wrap the insert query between this conditional statement: if(empty($reg_errors)){ ...post to database..}, would appreciate your thoughts. <?php require_once('./includes/connection.inc.php'); // create database connection $conn = dbConnect('read'); if (isset($_POST['insert'])) { // Define post fields into simple variables $first_name = $_POST['first_name']; $last_name = $_POST['last_name']; $sex = $_POST['sex']; // verify register_donor input // check last_name field if(!preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST["last_name"])) { // set error for last_name field $reg_errors['last_name'] = '<p class="warning">Last Name must contain letters, dashes and spaces only and must start with upper case letter</p>'; } if(!preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST["first_name"])) { // set error for first_name field $reg_errors['first_name'] = '<p class="warning">First Name must contain letters, dashes and spaces only and must start with upper case letter</p>'; } if(!preg_match("/^[a-zA-Z]+$/", $_POST["sex"])) { // set error for sex field $reg_errors['sex'] = '<p class="warning">Donor sex must contain letters only and must start with upper case letter</p>'; } // if no errors are set, inset record into database if(empty($reg_errors)){ // initialize flag $OK = false; // initialize prepared statement $stmt = $conn->stmt_init(); // create SQL $sql = 'INSERT INTO register_donor ( donor_id, last_name, first_name, sex) VALUES(?, ?, ?, ?)'; if ($stmt->prepare($sql)) { // bind parameters and execute statement $stmt->bind_param('isss', $_POST['donor_id'], $_POST['last_name'],$_POST['first_name'], $_POST['sex']); // execute and get number of affected rows $stmt->execute(); if ($stmt->affected_rows > 0) { $OK = true; } } // redirect if successful or display error if ($OK) { echo 'success'; exit; } else { $error = $stmt->error; } }} ?> <!DOCTYPE html> <html lang="en"> <head> </head> <body> <div class="container_12 main"> <div class="grid_12"> <div class="grid_8 omega" id="main"> <fieldset> <legend><h2>Register New Donor:</h2></legend> <p> <?php if (isset($error)) { echo "<p class=\"warning\">Error: $error</li>"; } ?> </p> <form id="form1" method="post" action=""> <table border="0"> <tr> <th> <label for="last_name">Last Name:</label> </th> <td> <?php if(isset($reg_errors['last_name'])) echo $reg_errors['last_name'];?> <input type="text" name="last_name" class="widebox" required aria-required="true" id="last_name" <?php if(isset($reg_errors)) { echo 'value="'.htmlentities($last_name, ENT_COMPAT, 'UTF-8') .'"'; } ?>> </td> </tr> <tr> <th><label for="first_name">First Name:</label></th> <td> <?php if(isset($reg_errors['first_name'])) echo $reg_errors['first_name'];?> <input name="first_name" type="text" class="widebox" required aria-required="true" id="first_name" <?php if(isset($reg_errors)) { echo 'value="'.htmlentities($first_name, ENT_COMPAT, 'UTF-8') .'"'; } ?>> </td> </tr> <tr> <th><label for="sex">Sex:</label></th> <td> <?php if(isset($reg_errors['sex'])) echo $reg_errors['sex'];?> <select name="sex" id="sex" required aria-required="true"> <option> <option> <option> Male </option> <option> Female </option> </select> </td> </tr> </table> <p> <input type="submit" name="insert" value="Register New Donor" id="insert"> </p> </form> </fieldset> </div> </div> </body> </html>
  10. I am looking for a developer to develop this script for me. Thanks
  11. Regular expression for phone numbers with country code:I am trying to get the regular expression for phone number with this format: 0802-810-4000. I have come up with this regex if(!preg_match(@"^\(?([0-9]{4})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$", $_POST["phone_number"])) { // set error for phone_number field $reg_errors['phone_number'] = '<p class="warning">Phone must comply with this format: 0803-333-4000</p>'; } When i post data via my form using this phone number entry: 0802-810-4000 i get my error variable returned. What is the right regex format to use: Thanks.
  12. I wish to implement regular expressions as a way to check user input and so far i came up with code below. When i run my script, and input numbers for first and last names, they get posted successfully regardless of my regex and i get this error Notice: "Undefined index: last_name in......." and "Undefined index: first_name in.......". I was wondering what i may be doing wrong. <?php $errors = array(); // .....create database connection......// if (isset($_POST['insert'])) { $last_name = trim($_POST['last_name']); $first_name = trim($_POST['first_name']); // initialize flag $OK = false; // initialize prepared statement $stmt = $conn->stmt_init(); // create SQL $sql = 'INSERT INTO voter_tracking ( v_id, last_name, first_name) VALUES(?, ?)'; if ($stmt->prepare($sql)) { // bind parameters and execute statement $stmt->bind_param('iss', $_POST['v_id'], $_POST['last_name'], $_POST['first_name']); // execute and get number of affected rows $stmt->execute(); if ($stmt->affected_rows > 0) { $OK = true; } } // redirect if successful or display error if ($OK) { echo 'posted'; exit; } else { $error = $stmt->error; } } ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Home</title> </head> <body> <div id="main"> <fieldset> <legend><h2>Add New Voter Record:</h2></legend> <?php if (isset($error)) { echo "<p class=\"warning\">Error: $error</p>"; } ?> <form id="form1" method="post" action=""> <p> <label for="last_name">Last Name:</label> <?php // Full Name must contain letters, dashes and spaces only and must start with upper case letter. if(preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST["last_name"]) === 0) $errName = '<p class="errText">Last Name must be from letters, dashes, spaces and must not start with dash</p>';?> <input type="text" name="last_name" class="widebox" id="name" required aria-required="true"> </p> <p> <label for="first_name">First Name:</label> <?php // Full Name must contain letters, dashes and spaces only and must start with upper case letter. if(preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST["first_name"]) === 0) $errName = '<p class="errText">Last Name must be from letters, dashes, spaces and must not start with dash</p>';?> <input type="text" name="first_name" class="widebox" required aria-required="true"> </p> <p> <input type="submit" name="insert" value="Insert New Entry" id="insert"> </p> </form> </fieldset> </div> </body> </html>
  13. this solution gave me strange output: i got 0 days 4 hrs 30 mins instead of 30 days 4 hrs 30 mins from code below after changing dates. $date_reported='2013-12-27 10:30:00'; $date_resolved='2014-01-27 15:00:00'; $d1 = new DateTime($date_resolved); $d2 = new DateTime($date_reported); echo $d1->diff($d2)->format('%d days %h hrs %i mins'); //-> 0 days 4 hrs 30 mins
  14. I am trying to track the time of resolution of complaints in my application, so i have tried to get the difference between the time a problem was reported and date it was resolved as shown in code below. I have noticed that calculating the difference between the time expressions uses only the date parts of the values. so 5-01-2014 minus 27-12-2013 returns -2 days instead of 9 days. I have also noticed that the MySQL DATEDIFF() Function uses only the date parts of the values in the calculation too. How may I get the correct duration returned as days? Thanks. <?php require_once('./includes/connection.inc.php'); $conn = dbConnect('read'); $sql = 'SELECT date_format(reported, "%d-%m-%Y") AS date_reported, date_format(Date_Resolved, "%d-%m-%Y") AS date_resolved FROM complaints ORDER BY created DESC'; $result = $conn->query($sql) or die(mysqli_error()); $numRows = $result->num_rows; ?> <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>Customer Complaints</title> </head> <body> <table> <thead> <tr> <th>Resolution Period</th> </tr> </thead> <tbody> <?php while ($row = $result->fetch_assoc()) { $date1=$row["date_reported"]; $date2=$row["date_resolved"]; ?> <tr> <td> <?php echo $date2-$date1; ?> days</td> </tr> <?php } ?> </tbody> </table> </body> </html>
×
×
  • 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.