-1

I have an online software that use php, html, js and MySQL as database.

I have two tables:

1- First table contains [name, imei, object_expire, object_expire_dt] - gs_objects

2- Second table contains [object_id, user_id, imei] - gs_user_objects

The code should be done in php where the user_id is got from the session, then the first query should get the imeis that matches the user_id from second table then it should get the expire date 'object_expire_dt' of each imei from the first table

after that it should check if there is an expire date that will expire within 20 days, if true, it should show alert message

Here is incomplete code that I tried to do

//notification for objects expiration
checkUserSession();

loadLanguage($_SESSION["language"], $_SESSION["units"]);

// check privileges
if ($_SESSION["privileges"] == 'subuser')
{
    $user_id = $_SESSION["manager_id"];
}
else
{
    $user_id = $_SESSION["user_id"];
}

$q = "SELECT * FROM `gs_user_objects` WHERE `user_id`='".$user_id."' ORDER BY `object_id` ASC";
$r = mysqli_query($ms, $q);
while($row=mysqli_fetch_array($r))
{
    $q2 = "SELECT * FROM `gs_objects` WHERE `imei`='".$row['imei']."' ORDER BY `object_id` ASC";
    $r2 = mysqli_query($ms, $q2);
    while($row=mysqli_fetch_array($r2))
    {
        $Date_e = date("Y-m-d");

        if ( $row['object_expire_dt'] > date('Y-m-d', strtotime($Date_e. ' - 20 days')))
        {
            alert("You have objects are going to expire soon");
        }
    }

}

the code didn't work, I need some help in it. Thanks in advance

3 Answers3

0

Here's how all this works: Your php program runs on your server, and accesses your database on the server. The purpose of your php program is to create programs to run on your users' browsers. Those programs written by php use the HTML, Javascript, and CSS languages.

If you want something to happen in a user's browser (like an alert box) that thing has to appear in a Javascript program written by your php program and sent to the browser. php doesn't have its own alert() function

Here's an easy, but somewhat sloppy, way to do that in your php program.

echo "<script type='text/javascript'>window.onload=function(){alert('$msg'))</script>";

What's going on here?

  • echo tells php to write its parameter to the html page
  • <script> whatever </script> is the way to embed Javascript in html
  • window.onload = function () { whatever } tells the browser to run a Javascript function when your html page finishes loading.
  • alert(message), in the function, pops up the alert message.

When you're troubleshooting this kind of thing, View Source ... is your friend.

O. Jones
  • 103,626
  • 17
  • 118
  • 172
0

you can use alert in javascript not in php also you should use prepared statement.

//notification for objects expiration
checkUserSession();

loadLanguage($_SESSION["language"], $_SESSION["units"]);

// check privileges
if ($_SESSION["privileges"] == 'subuser'){
    $user_id = $_SESSION["manager_id"];
}else{
    $user_id = $_SESSION["user_id"];
}

$q = "SELECT * FROM gs_user_objects WHERE user_id = ? ORDER BY object_id ASC";
if ($r = $connection->prepare($q)) {
    // if user_id contains string and is not integer you must use "s"
    $r->bind_param("i",$user_id);
    if ($r->execute()) {
        $result = $r->get_result();
        // check if result match one condition
        if ($result->num_rows > 0) {
            echo "result found";
            while ($row = $result->fetch_assoc()) {
                echo $row['some_column_name'];
            }
        }
    }
}
-1

Thanks Nikolaishvili and Jones, Your answers helped me a lot I needed more edit on the if statements,

I did the code and the result is as I expected and it is online now, here the code is below so others can check it

//notification for objects expiration
// check privileges
if ($_SESSION["privileges"] == 'subuser')
{
    $user_id = $_SESSION["manager_id"];
}
else
{
    $user_id = $_SESSION["user_id"];
}

$q = "SELECT * FROM `gs_user_objects` WHERE `user_id`='".$user_id."' ORDER BY `object_id` ASC";
$r = mysqli_query($ms, $q);
$expiry_flag = 0;
$inactive_flag=0;

while($row=mysqli_fetch_array($r))
{
    $q2 = "SELECT * FROM `gs_objects` WHERE `imei`='".$row['imei']."'";
    $r2 = mysqli_query($ms, $q2);

    while($row2=mysqli_fetch_array($r2))
    {
        $Date_e = date("Y-m-d");


        if ( $row2['object_expire_dt'] < date('Y-m-d', strtotime($Date_e. ' + 20 days')))
        {
            if ($row2['object_expire_dt'] > '0000-00-00')
            {
                $expiry_flag = 1;
            }
        }

        if ( $row2['object_expire_dt'] < date("Y-m-d"))
        {
            if ($row2['object_expire_dt'] > '0000-00-00')
            {
                $inactive_flag = 1;
            }
        }

    }

}
        if ($expiry_flag == 1)
        {
            echo '<script type="text/javascript">';
            echo ' alert("my msg1")';
            echo '</script>';
        }
        if ($inactive_flag == 1)
        {
            echo '<script type="text/javascript">';
            echo ' alert("my msg2")'; 
            echo '</script>';
        }

Thanks