-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>
Besart
  • 1
  • 2
  • You need to add the action to the button, maybe using a form via POST or use AJAX to call the script – juanbits Aug 20 '18 at 19:26
  • @juanbits not familiar with AJAX – Besart Aug 20 '18 at 19:27
  • An issue with understanding the difference between php and html. where to use what – Rinsad Ahmed Aug 20 '18 at 19:30
  • 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 https://stackoverflow.com/questions/41391067/jquery-ajax-button-click-using-variable – juanbits Aug 20 '18 at 19:31
  • I tried using a form with the help of @Evan but still nothing – Besart Aug 20 '18 at 19:35
  • Possible duplicate of [How to Call a PHP Function on the Click of a Button](https://stackoverflow.com/questions/20738329/how-to-call-a-php-function-on-the-click-of-a-button) – NonameSL Aug 20 '18 at 19:45
  • You could explore AJAX, but it's probably better to learn the fundamental action of simply submitting a regular HTML form. There's 100's of tutorials for this. – Kalnode Aug 20 '18 at 19:52
  • 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. – tadman Aug 20 '18 at 20:30
  • 1
    **WARNING**: When using `mysqli` you should be using [parameterized queries](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) 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](http://bobby-tables.com/). **NEVER** put `$_POST`, `$_GET` or **any** user data directly into a query, it can be very harmful if someone seeks to exploit your mistake. – tadman Aug 20 '18 at 20:31

1 Answers1

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.

Evan Edwards
  • 182
  • 11
  • Tried it but still nothing – Besart Aug 20 '18 at 19:34
  • Where does the $username variable get sent from? @Besart – Evan Edwards Aug 20 '18 at 19:35
  • 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? – Besart Aug 20 '18 at 19:38
  • 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. – Evan Edwards Aug 20 '18 at 19:39
  • 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. – Besart Aug 20 '18 at 19:46
  • Let me know if changing it works for you. If not I can take a look at the rest of the code and try to figure it out. – Evan Edwards Aug 20 '18 at 19:47
  • No it didnt work. As I said id_krijimi is created automatically by the database not the user,meaning everytime the user creates a trip and submits it that trip gets a unique id_krijimi in the database,but still I dont know why this WHERE doesnt make sense to me. Any idea what can i write after WHERE to solve it? Also where can I send the code cause it's too long for here – Besart Aug 20 '18 at 19:57
  • Your WHERE clause does look odd. Is the column called '$username_krijim' in MySQL? Because it looks like a PHP variable. Also how does the code know which ID to use? Right now it looks like you're calling a variable that does not exist. – Evan Edwards Aug 20 '18 at 20:02
  • I dont use username_krijim anymore I use id_krijimi and the column is called "id_krijimi" in the database.Exactly, theres something wrong with this whole idea.I'll try to explain better what I'm doing and see if you can help me. I have a page where the drivers can create trips.They create the trip and the data is saved in the database. Later in another page I display this data in a table so that the passegers can view the available trips and the last column of this table has buttons for every row(meaning for every trip)and if the user clicks this button ...I'll continue in the next comment – Besart Aug 20 '18 at 20:10
  • If the user clicks this button vende_krijim value of that exact trip will be decremented by one. But the thing is how to connect the button with the exact trip. The button is displayed at the side of the trip at the table – Besart Aug 20 '18 at 20:12
  • When the trip is being displayed beside the button in the table how is it being pulled? Could you edit your original question and add the code in so I can take a look? – Evan Edwards Aug 20 '18 at 20:13
  • Thats the problem the button doesnt seem to incoorporate into the whole thing. I edited the code – Besart Aug 20 '18 at 20:19
  • Check my edit, it should help you declare the variable. – Evan Edwards Aug 20 '18 at 20:22
  • @Besart disregard that edit it won't work. Let me try some things in a test environment and get back to you. – Evan Edwards Aug 20 '18 at 20:30
  • But is username_krijim the one I should use? Cause username is the name of the driver and one driver can create more than one trip. Will this decrement the value of vende_krijim of all trips of that driver? wouldnt it be better if I use id_krijimi that is unique for every trip? If so how can I do that? Or by using username_krjim everythnings ok? – Besart Aug 20 '18 at 20:32
  • It would be better to use the ID value. I was just basing my answer off the information provided in the original question. – Evan Edwards Aug 20 '18 at 20:36
  • Ok,any idea how can I do that? No need to use mine just something cause I really need to get this to work – Besart Aug 20 '18 at 20:42
  • So I tried some small changes to the code.Now it decrements when i click the button but the problem is it still doesn't understand which one to decrement but instead decrements them all. Any idea? – Besart Aug 20 '18 at 22:24
  • @Besart that's what I realized after testing it out (I'm at work and didn't have time to fully figure it out.) You need to find a way to specify which ID to decrease. Otherwise the button will be useless. Maybe just have one button at the bottom of the page and let the user type in the ID? I can write up that code for you if you'd like. – Evan Edwards Aug 20 '18 at 22:32
  • that wont work for me. I need one button for each trip I need to find a wahyto decrement only one. Maybe its just a matter of "{" or this isnt the right way at all but i cant seem to figure it out – Besart Aug 20 '18 at 22:41