0

i created an appointments table where user1 would add appointments and user2 would accept/reject the appointment. now my problem is when the accept and reject are clicked it is displayed in the table respectively but i just cant understand how to add the insert query into it so that it could be inserted into the db. i tried several methods in trying to insert the state[accept/reject] into the db but i found no success. i would request someone to pls provide me some help inn fixing the issue. Thanks.

app (apportionment db)

 <form method="post" action="delete.php" > 

       <table cellpadding="0" cellspacing="0" border="0" class="table table-condensed" id="example">
               <thead>
                          <tr>
                                <th>appoinment ID</th>
                              <th>Date</th>
                                <th>time</th>
                              <th>teacher</th>
                             <th>parent</th>
                              <th> accept/reject </th>
                              <th>label</th>
                            </tr>
                   </thead>
                   <tbody>
                        <?php 
                            $query=mysqli_query($conn, "select * from `app` left join `par` on par.par_id=app.par_id
                            left join `tea` on tea.tea_id=app.tea_id
                            ORDER BY app_id DESC");

                                if($query === false)
                                {
                                    throw new Exception(mysql_error($conn));
                                }
                                while($row=mysqli_fetch_array($query))
                                {
                                    $ann_id=$row['app_id'];
                                    $date=$row['date'];
                                    $msg=$row['time'];

                                    $username = $row['username'];
                                     $username = $row['p_username'];
                        ?>
                <tr>
                    <td><?php echo $row['app_id'] ?></td>
                     <td> <?php echo date('j/m/y',strtotime($row['date'])); ?></td>
                    <td><?php echo $row['time'] ?></td>
                   <td><?php echo $row['p_username'] ?></td>
                <td><?php echo $row['username'] ?></td>

                    <td>
        <a href="#" class="reject">reject</a> 
        <a href="#" class="accept">accept</a>
    </td>
    <td>
        <div class="chgtext">PENDING</div>
    </td>

                </tr>

                        <?php   
                                }

                       ?>

                  </tbody>
              </table>
         </div>
      </form>
  • for one thing, `mysql_error($conn)` won't work; you can't mix different apis, so use `mysqli_error($conn)` with the added "i" and then see what happens. – Funk Forty Niner Mar 02 '18 at 02:26
  • @FunkFortyNiner i know how to insert data into the db but here i cant understand what really happens and how to add the insert query – The KingMaker Mar 02 '18 at 02:30
  • Note: The object-oriented interface to `mysqli` is significantly less verbose, making code easier to read and audit, and is not easily confused with the obsolete `mysql_query` interface. Before you get too invested in the procedural style it’s worth switching over. Example: `$db = new mysqli(…)` and `$db->prepare("…")` The procedural interface is an artifact from the PHP 4 era when `mysqli` API was introduced and should not be used in new code. – tadman Mar 02 '18 at 02:31
  • A lot of problems can be detected and resolved by [enabling exceptions in `mysqli`](https://stackoverflow.com/questions/14578243/turning-query-errors-to-exceptions-in-mysqli) so mistakes aren't easily ignored. – tadman Mar 02 '18 at 02:32
  • Why do you say INSERT, when you need to UPDATE e.g. `UPDATE appointments SET state = 'whatever' WHERE app_id = 53` ? – Mikey Mar 02 '18 at 02:32
  • @tadman i am using mysqli...even if i use mysql i be displayed with a warning so i dont use mysql – The KingMaker Mar 02 '18 at 02:35
  • What I'm saying is to avoid using the procedural style because in many cases it's just one letter off from the obsolete API that's been removed in PHP7. It's easy to type `mysql_error` instead of `mysqli_error`, but a lot harder to accidentally type that if you're dong `$db->error` instead. – tadman Mar 02 '18 at 02:36
  • @Mikey i need the app_id to be set as a variable so that the state corresponding to the app_id is automatically updated – The KingMaker Mar 02 '18 at 02:38
  • @TheKingMaker Easiest way is to pass it in the query string. – Mikey Mar 02 '18 at 02:39
  • @tadman you mean thatt i must change mysql_error($conn) into mysqli_error($conn) – The KingMaker Mar 02 '18 at 02:41
  • @Mikey can u pls show me how to do it – The KingMaker Mar 02 '18 at 02:42
  • I mean change that to `$conn->error` and you'll never make that mistake again. – tadman Mar 02 '18 at 03:39
  • @tadman thanks can u help m with a fix to insert the accept/reject into the db – The KingMaker Mar 02 '18 at 03:45

1 Answers1

1

One way is to pass the ID via query string (GET). So you would update the appointment if the appropriate query string key-pairs are given.

Personally, I believe you should not mix querying while outputting. Do the database stuff at the top first and leave the outputting at the bottom.

Note: I am not too familiar with mysqli_ but it would be something like this:

<?php 
// accept or reject appointment with ID
if (isset($_GET['state'], $_GET['app_id'])) {
    $stmt = mysqli_prepare($conn, "UPDATE app SET state = ? WHERE app_id = ?");
    mysqli_stmt_bind_param($stmt, "sd", $_GET['state'], $_GET['app_id']);
    mysqli_stmt_execute($stmt);
    mysqli_stmt_close($stmt);
}

// query appointments
$query = mysqli_query($conn, "
    SELECT * FROM app 
    LEFT JOIN par ON par.par_id = app.par_id
    LEFT JOIN tea on tea.tea_id = app.tea_id
    ORDER BY app_id DESC
");
?>

<form method="post" action="delete.php" > 
    <table cellpadding="0" cellspacing="0" border="0" class="table table-condensed" id="example">
        <thead>
            <tr>
                <th>appoinment ID</th>
                <th>Date</th>
                <th>time</th>
                <th>teacher</th>
                <th>parent</th>
                <th>accept/reject</th>
                <th>label</th>
            </tr>
        </thead>
        <tbody>
        <?php while($row = mysqli_fetch_array($query)) : ?>
            <tr>
                <td><?= $row['app_id'] ?></td>
                <td><?= date('j/m/y', strtotime($row['date'])) ?></td>
                <td><?= $row['time'] ?></td>
                <td><?= $row['p_username'] ?></td>
                <td><?= $row['username'] ?></td>
                <td>
                    <!-- upon clicking a link, it will redirect to the same page with a query string -->
                    <a href="?state=reject&app_id=<?= $row['app_id'] ?>" class="reject">reject</a> 
                    <a href="?state=accept&app_id=<?= $row['app_id'] ?>" class="accept">accept</a>
                </td>
                <td>
                    <div class="chgtext">PENDING</div>
                </td>
            </tr>
        <?php endwhile ?>
        </tbody>
    </table>
</form>
Mikey
  • 6,432
  • 4
  • 20
  • 36
  • thank for the advice & ill try to separate the querying and outputting later but currently im stuck in updating the accept/reject. i think that the, if (isset......) was the only additional code that u have added. i tried adding it into mine but doesnt work – The KingMaker Mar 02 '18 at 03:15
  • @TheKingMaker And the HTML has changed - look at the links. [Turn on error reporting](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) also. – Mikey Mar 02 '18 at 03:16
  • mysqli_stmt_bind_param($stmt, "sd", $_GET['state'], $_GET['app_id']); can u tell me what is "sd" – The KingMaker Mar 02 '18 at 03:21
  • 1
    @TheKingMaker [mysqli_stmt_bind_param](http://php.net/manual/en/mysqli-stmt.bind-param.php) The 2nd argument of the function is the types of your columns. Assuming `state` is a VARCHAR, CHAR, or any string type, then use `s`. `app_id` is an INTEGER, so use `i`. Adjust accordingly. – Mikey Mar 02 '18 at 03:26
  • state is a varchar... so u mean that i must change `"sd"` into `s. app_id` – The KingMaker Mar 02 '18 at 03:29
  • @TheKingMaker Check the revision. That's as much as I can help you. – Mikey Mar 02 '18 at 03:33