-3

I am creating a carpool website and I need help with something. I have created a button and if people click on that button than the value of free spaces in the car will decrement by one(meaning the one who will click it will reserve the place). So this value must be changed in the database and also when I refresh the page it will change on the table where I display info.This is what I have written so far but doesn't seem to make a change.Thanks in advance!

     <tbody>
                            <?php while ($row=mysqli_fetch_array($results)) {

                             ?>
                                <tr>
    <td><?php echo $row['username_krijim']; ?> </td>
    <td><?php echo $row['nisja']; ?> </td>
    <td><?php echo $row['destinacioni']; ?> </td>
    <td><?php echo $row['data_krijim']; ?> </td>
    <td><?php echo $row['ora_krijim']; ?> </td>
    <td ><?php echo $row['vende_krijim']; ?> </td>
    <td><?php echo $row['cmimi_krijim']; ?> </td>
    <td><?php echo $row['mesazhi_krijim']; ?> </td>
    <td><form action="index_show.php" method="POST"><input class="btn btn-primary" style="background:#f2545f;" type="submit" name="rezervo" value="Rezervo"></form></td>
<?php 
 if(isset($_POST['rezervo'])){
    $id_krijim=$row['id_krijimi'];
    $sql = "UPDATE krijo_itinerar SET vende_krijim=vende_krijim-1 WHERE id_krijimi='$id_krijim'";
    mysqli_query($db, $sql);
}
?>
    </tr>
    <?php } ?>
    </tbody>
9
  • You need to add the action to the button, maybe using a form via POST or use AJAX to call the script Commented Aug 20, 2018 at 19:26
  • @juanbits not familiar with AJAX Commented Aug 20, 2018 at 19:27
  • 1
    if you aren't familiar with ajax, you can use a form. or search about how you can do that. you can read something at stackoverflow.com/questions/41391067/… Commented Aug 20, 2018 at 19:31
  • 1
    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. Commented Aug 20, 2018 at 20:30
  • 1
    WARNING: When using mysqli you should be using parameterized queries and bind_param to add user data to your query. DO NOT use string interpolation or concatenation to accomplish this because you have created a severe SQL injection bug. NEVER put $_POST, $_GET or any user data directly into a query, it can be very harmful if someone seeks to exploit your mistake. Commented Aug 20, 2018 at 20:31

1 Answer 1

0

You can use a form to send the POST request to tell the PHP to initialize.

<td><form action="" method="POST"><input class="btn btn-primary" style="background:#f2545f;" type="button" name="rezervo" value="Rezervo"></form></td>
<?php 
if (isset($rezervo)){
    $sql = "UPDATE krijo_itinerar SET vende_krijim=vende_krijim-1 WHERE username_krijim='".$username_krijim."'";
    mysqli_query($db, $sql);
}
?>

I also don't see the variable $username_krijim being set. I don't know if it's somewhere else in your code but that may also be contributing to your problem.

Edit:

Ok so the problem I see are that that the variable is not being sent through PHP. That previous edit I provided won't work I'll try and think of something.

Sign up to request clarification or add additional context in comments.

20 Comments

Tried it but still nothing
Where does the $username variable get sent from? @Besart
From the database. Meaning I use another file where drivers via a form decide how many free spaces they have and send it to the database via that form. At this file I am now I just connect to the database. Is there something else I should do?
Can you post more of your code (like where the $username_krijim is set?) My concern is that it's not being passed to the form therefor the query is not executing as it is incomplete.
I made a mistake I believe it should be id_krijimi='$id_krijimi' . This id is auto incremented every time a new trip is created.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.