Jump to content

How can I put a condition with this studentno and date at the same ?


Recommended Posts

Quote
if(isset($_POST[‘register’])) {
$uname = clean($_POST['username']); 
$pword = clean($_POST['password']); 
$studno = clean($_POST['studentno']); 
$fname = clean($_POST['firstname']); 
$lname = clean($_POST['lastname']); 
$section = clean($_POST['section']); 
$status = clean($_POST['status']); 


$query = "SELECT username FROM students WHERE username = '$uname' && date_joined= NOW()";
$result = mysqli_query($con,$query);

if(mysqli_num_rows($result) == 0) {

  $query = "SELECT studentno FROM students WHERE studentno = '$studno'";
  $result = mysqli_query($con,$query);

  if(mysqli_num_rows($result) == 0) {

    $query = "INSERT INTO students (username, password, studentno, firstname, lastname, section, status, date_joined)
    VALUES ('$uname', '$pword', '$studno', '$fname', '$lname', '$section', '$status ',NOW())";

    if(mysqli_query($con, $query)) {

      $_SESSION['prompt'] = "Account registered. You can now log in.";
      header("location:index.php");
      exit;

    } else {

      die("Error with the query");

    }

  } else {

    $_SESSION['errprompt'] = "That student number already exists.";

  }

} else {

  $_SESSION['errprompt'] = "Username already exists.";

}
}

?>

$query = "SELECT username FROM students WHERE username = '$uname' && date_joined= NOW()"; // 

how to pud date statement, I'm making attendance system and only this date will be save , it will promp "you are already have attendance",  If I change the date then it will be save again.

 

date.png

I'm guessing your problem is with this query: 

6 hours ago, camille said:
$query = "SELECT username FROM students WHERE username = '$uname' && date_joined= NOW()";

You can't "invent" SQL syntax.  You have to use what your DBMS supplies to you. 

The keyword to group two conditions logically and require both to be TRUE is "AND". 
Whilst the "&&" operator does the same thing in PHP, C# and other languages, MySQL only understands "AND". 

 

This bit might cause you problems as well: 

6 hours ago, camille said:
date_joined= NOW()

IIRC, the MySQL Now() function returns the time as well as the date.  If you're only storing the date, then the two will never match, as in: 

'2017-05-28' != '2017-05-28 00:00:00'

You'll probably have to do some truncation on the returned value (say, using the DATE() function). 

 

Also, read up about Prepared Statements, to protect yourself against SQL Injection Attacks. 
Obligatory XKCD Reference - Little Bobby Tables

Regards,
   Phill  W.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.