2

I have a page that has two buttons "Yes" and "no". When I click the "yes" button, everything works fine, but when I click the "No" button, I get the error.

However, the database is still updated when i get this error but what I want is for it to go back two pages, the same way that the "Yes" button does in the code below.

edit.php

<form action="dbconfig.php" method="post">
<select name="studentId" id="" class="form-control">
    <option value="">Select Student</option>
    <option value="35290">student1</option>
    <option value="35316">student2</option>
    <option value="35294">student3</option>
    <option value="35377">student4</option>
</select>

    <select name="createdAt" id="" class="form-control">
                <option value="">Select Time</option>
                <option value="2019-04-02 11:00:00">11:00</option>
                <option value="2019-04-02 12:00:00">12:00</option>
                <option value="2019-04-02 13:00:00">13:00</option>
                <option value="2019-04-02 15:00:00">15:00</option>
    </select>       
<p>
Did the student attend class?
<button name="attended" type="submit" value="1" >Yes</button>
<button name="attended2" type="submit" value="0" >No</button>

dbconfig.php

    <?php

    $con=mysqli_connect("localhost","root","","billy") or die ("Database                                           
           not selected");

    $studentId=$_POST['studentId'];
    $attended=$_POST['attended'];
    $createdAt=$_POST['createdAt'];


    if(isset($_POST['attended']))
      {
        $query=mysqli_query($con,"insert into attendance (studentId, attended, 
            createdAt) values ('$studentId','$attended','$createdAt')") or die                             
            ("Error");
        echo '<script type="text/javascript">'
                   , 'history.go(-2);'
                   , '</script>';
      }
    else (isset($POST['attended2'])) 
      {
           $query=mysqli_query($con,"DELETE FROM attendance WHERE 
                studentId='$studentId' AND createdAt='$createdAt'") or die ("Error")       
      }
    ?>
user11281949
  • 105
  • 6
Bill
  • 53
  • 1
  • 4
  • 1
    you are trying to get $_POST with $attended=$_POST['attended'];, but when you click NO, no 'attended' provided in $_POST. Move the line 6 ($attended=$_POST['attended'];) inside true case of your if – Viktar Pryshchepa Apr 13 '19 at 10:42
  • `else (isset($POST['attended2'])) ` this is not valid I guess. Either you should create two separate `if` statements or use `else if ()`. – Johannes Apr 13 '19 at 11:00
  • Possible duplicate of [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Progman Apr 13 '19 at 16:33
  • use else if. when you add if condition else statement is not working with the your if condition. so else state not checking any condition again. you have to replace it with else if – Dileepa Nipun Salinda May 14 '19 at 16:33

3 Answers3

2

You don't need if-else condition just use IF condition for each button. Example,

if(isset($_POST['attended'])) {
....
}

if(isset($_POST['attended2'])) {
...
}
1

Just move your code from line 6 ($attended=$_POST['attended'];) inside true case of your If condition if(isset($_POST['attended'])) and use elseif of simple else

1

I think ,In the "YES" button and "no" button clicking check is not right you use

if (logic){
runs if logic is true;
}else{
runs if logic is wrong;
}

TRY THIS ONE

  if(isset($_POST['attended'])){
     $query=mysqli_query($con,"insert into attendance (studentId, attended, 
     createdAt) values ('$studentId','$attended','$createdAt')") or die                             
     ("Error");
     echo '<script type="text/javascript">'
           , 'history.go(-2);'
           , '</script>';}

  if (isset($POST['attended2'])) 
      {
       $query=mysqli_query($con,"DELETE FROM attendance WHERE 
    studentId='$studentId' AND createdAt='$createdAt'") or die ("Error")       
      }