1

I am trying to get results from a database to appear and say "You're device is still under warranty." or "This device's warranty has expired." depending on if the warranty expiration date is older than todays date.

I am using a custom Wordpress page template to connect to a remote database. Here's what I have so far. I am connected to the database and can return results. I am just struggling with creating a search form that echoes the results in a way that is useful.

 <?php $con = mysqli_connect("localhost","my_user","my_password","my_db");


 // check connection
 if ($con->connect_error) {
 die("Connection failed: " . $con->connect_error);
 }
 echo "Connected successfully";
 ?>    
 <form action="" method="post">  
   Search: <input type="text" name="term" /><br />  
   <input type="submit" value="Submit" />  
</form>  
<?php
   if (!empty($_REQUEST['term'])) {
   $term = mysql_real_escape_string($_REQUEST['term']);     

$sql = "SELECT * FROM warranty WHERE serial LIKE '%".$term."%'"; 
$r_query = mysql_query($sql); 

What I want to do here is say IF the serial number entered "warexp" has a date value older than today echo "This product's warranty is expired." and if it returns a date that has not yet passed echo as my ELSE statement "Your device is still under warranty.". I am really confused about this part and how to get it to work on a custom page template in Wordpress or if that even matters.

while ($row = mysql_fetch_array($r_query)){  
?????????????????????????
?>

Thank you in advance for any insight. I hope my description of the problem is clear.

cjvalotta
  • 19
  • 1
  • 5
  • Please stop using the `mysql_*` functions. They were deprecated in PHP 5.5, which is so old it no longer even receives security updates, and completely removed in PHP 7. Instead, use PDO or `mysqli_*`. See http://stackoverflow.com/q/12859942/354577 for details. – ChrisGPT was on strike Nov 18 '16 at 15:54
  • 1
    The warranty on the `mysql_*` functions has expired as well... And you can't mix it with mysqli. – jeroen Nov 18 '16 at 15:54
  • I'll look into that, but it doesn't really help the problem I am seeking a solution for. However let me retype the connection so that is up to standards. – cjvalotta Nov 18 '16 at 15:58
  • @cjvalotta, well part of jeroen's comment _does_ help you. You can't use `mysqli_connect()` and `mysql_query()` / `_fetch_array()` / `_real_escape_string()` etc. – ChrisGPT was on strike Nov 18 '16 at 16:08
  • @cjvalotta, specifically which part of this is giving you trouble? Comparing to today's date? – ChrisGPT was on strike Nov 18 '16 at 16:10
  • I want to do a query from the search form, I guess the appropriate way to format the results so they display to the user a basic "your covered" or "your not" by the user entering their serial number – cjvalotta Nov 18 '16 at 16:22

1 Answers1

0

try this:

 <?php $con = mysqli_connect("localhost","my_user","my_password","my_db");
  // check connection
 if ($con->connect_error) {
  die("Connection failed: " . $con->connect_error);
 }
 echo "Connected successfully";
 ?>    
 <form action="" method="post">  
   Search: <input type="text" name="term" /><br />  
   <input type="submit" value="Submit" />  
 </form>  
 <?php
 if (!empty($_REQUEST['term'])) {
   $term = "%{$_REQUEST['term']}%";
   $sql = "SELECT * FROM warranty WHERE serial LIKE ?"; 
   $prepare = $con->prepare($sql);
   $prepare->bind_param('s',$term);
   $prepare->execute();
   $r_query = $prepare->get_result();
   while($row = $r_query->fetch_assoc()){
     if((time() - (60 * 60 * 24)) <= strtotime($row['date_value']))
     {
       echo 'Your device is still under warranty.' . '<br>';
     }
     else
     {
       echo 'This product's warranty is expired.' . '<br>';
     }
   }
 }

Read more on php oficial documentation

  • 3
    [`get_result()`](http://php.net/manual/en/mysqli-stmt.get-result.php) isn't available for everyone to use and will depend on server configuration since the manual states: *"MySQL Native Driver Only Available only with [mysqlnd](http://php.net/manual/en/book.mysqlnd.php)"*. **Plus,** *"Try this"* answers are often frowned upon; just thought you'd like to know that ;-) Stack is about knowing "how" things work and not "hope" it works. – Funk Forty Niner Nov 18 '16 at 17:39
  • *grazie very mooch* @Drew – Funk Forty Niner Nov 18 '16 at 17:47