如果存在变量则更新否则回显错误消息?

问题描述:

How can I make this script to where if it finds that the fname and lname do not exist that it will pop up a message saying that they never signed in.

<?php
session_start();
include_once("connect.php");
date_default_timezone_set("America/Winnipeg");
$date = ("m-d-Y");
$timeout = date("g:i:s a");
if ("SELECT EXISTS(
        SELECT *
        FROM signin_out
        WHERE   
            lname='" . $_POST['lastname'] . "'
            AND fname='" . $_POST['firstname'] . "'  
            AND date='" . $date . "')"
) {
    mysql_query("
        UPDATE signin_out
            SET timeout='" . $timeout . "' 
        WHERE
            lname='" . $_POST['lastname'] . "'
            AND fname='" . $_POST['firstname'] . "'
            AND timeout=''
    ");
    header("Location: ../index.html");
} else {
    echo "<script type='text/javascript>'";
    echo "alert('<p>Oops! You never signed in!</p><br><p>Please contact a
                    Librarian</p>');'";
    echo "</script>'";
    header('Location: ../index.php?notsignedin');
}
?>

This is an intranet site for a highschool.

如果发现fname和lname不存在会弹出它,我怎样才能将这个脚本放到哪里 一条消息说他们从未登录过。 p>

 &lt;?php 
session_start(); 
include_once(“connect.php”); 
date_default_timezone_set(“America / Winnipeg”  ); 
 $ date =(“mdY”); 
 $ timeout = date(“g:i:sa”); 
if(“SELECT EXISTS(
 SELECT * 
 FROM FROMinin_out 
 WHERE 
 lname)  ='“。$ _POST ['lastname']。”'
 AND fname ='“。$ _POST ['firstname']。”'
 AND date ='“。$ date。”')“
)  {
 mysql_query(“
 UPDATE signin_out 
 SET timeout ='”。$ timeout。“'
 WHERE 
 lname ='”。$ _POST ['lastname']。“'
 AND fname ='”  。$ _POST ['firstname']。“'
 AND timeout =''
”); 
 header(“Location:../ index.html”); 
} else {
 echo“&lt;  script type ='text / javascript&gt;'“; 
 echo”alert('&lt; p&gt;哎呀!你从未登录过!  &lt; / p&gt;&lt; br&gt;&lt; p&gt;请联系
图书馆员&lt; / p&gt;');'“; 
 echo”&lt; / script&gt;'“; 
标题('位置:..  /index.php?notsignedin');
}
?>
nn

这是一所高中的内部网站。 p>

$sql = "SELECT COUNT(*) signedin FROM signin_out
        WHERE lname = '" . mysql_real_escape_string($_POST['lastname']) . "'
        AND fname = '" . mysql_real_escape_string($_POST['lastname']) . "'
        AND date = '$date'";
$result mysql_query($sql) or die(myqsl_error());
$row = mysql_fetch_assoc($result);
if ($row['signedin'])) {
    // update table
} else {
    // Report not signed in
}

However, you really should switch to mysqli or PDO so you can use parametrized queries instead of concatenating strings, so you don't have to worry as much about escaping them.

This is only one part of the answer, @Barmar gave u how to handle the query itself.

Change

echo "<script type='text/javascript>'";
echo "alert('<p>Oops! You never signed in!</p><br><p>Please contact a
        Librarian</p>');'";
    echo "</script>'";
header('Location: ../index.php?notsignedin');

To

echo "<script type='text/javascript>'";
echo "alert('Oops!
You never signed in!
Please contact a
        Librarian');'";
echo "window.location.href='../index.php?notsignedin';";
echo "</script>'";

The reason: Strings which echo go into the web server buffer before being sent as a package to the browser.
This may cause your code to reach and do the header command, and then either you will redirect immediatly, or get an error message on the lines of '...you can not send headers after output...'

And seriously consider everybody's suggestion about PDO/Mysqli and using a more centralized/abstracted way to use the DB.

Check how many rows are returned by the query,if is more than 1 then fname and lname exists in database,you can also use count(*) but i won't to change your query :

  $result = mysql_query("SELECT * FROM signin_out WHERE lname='".$_POST['lastname']."' AND fname='".$_POST['firstname']."' AND date='".$date);
  $num_rows = mysql_num_rows($result);//count number of rows returned by query
  if($num_rows >=1) { 

     //Update here

   }
  else {
    //alert and redirect here
  }

I understand that your site is for intranet use only , but i suggest to use PDO or Mysqli