0

I want to update all data's inside in the while loop. But the problem is only one data is updating. For example:

Imagine that I have 3 data's in my tbl_rooms

columns: id, roomname, capacity

and the data will be fetch in the while loop, then all textboxes will be thrice cause of the while loop. Then I want to update all data's inside of the while loop. Please I need you guys. Thankyou!

Here's the code:

$db = mysqli_connect('localhost', 'root', '', 'psbr'); 
$query = mysqli_query($db, "SELECT * FROM rooms")
<script src = "http://code.jquery.com/jquery-1.9.1.js"></script>

  <?php while ($row = mysqli_fetch_array($query)) { ?>
  <input type="text" name="room_id" id="room_id" value="<?php echo $row['id']; ?>">
  <?php } ?>
  <input type="submit" name="submit" onclick="return chk()">

<script type="text/javascript">

function chk()
{
var roomid = document.getElementById('room_id').value;
var dataString = 'roomid='+ roomid;
$.ajax({
  type: "post",
  url: "sample_server.php",
  data:dataString,
  cache:false,
  success: function(html){
    alert("success!");
  }
});
return false;
}


</script>

//sample_server.php

<?php 
$db = mysqli_connect("localhost", "root", "", "psbr");

$roomid = $_POST['roomid'];

    $update_status = "UPDATE rooms SET capacity = capacity - 1 WHERE id = '$roomid'";
    mysqli_query($db, $update_status);

 ?>
10
  • "the problem is only one data is updating."... ok so 1) all your textboxes will have the same "room_id" name, you're just going to be sending back 3 variables with the same name, so the server can't tell the difference between them. See this post for a solution: stackoverflow.com/questions/7880619/… 2) where's your PHP code for doing the updates? We need to check there isn't a problem in there too. Commented Jun 12, 2018 at 19:34
  • sorry I forgot sir, I already updated! @ADyson Commented Jun 12, 2018 at 19:39
  • 1
    ok so follow the solution in my first link to allow submitting multiple values of the same name. Then in the PHP you can loop through the "room_id" POST variable (since it will now be an array) and execute one UPDATE query for each item submitted. P.S. Your code is currently vulnerable to SQL injection attacks - a malicious user could enter values which could allow them to steal, corrupt or delete your data. See bobby-tables.com for an explanation and also some examples of writing queries safely using PHP / mysqli. Commented Jun 12, 2018 at 19:42
  • sir @ADyson I don't really get the point in the link, please help me out of this problem. :(( Commented Jun 12, 2018 at 19:43
  • 1
    And your app should never log in to MySQL as "root" - root can do whatever it likes, including deleting all the databases et etc. Instead create a separate SQL login account for the application which has only the privileges it actually needs in order to work. Combined with the SQL injection vulnerability, this leaves your database an open book for hackers to mess with Commented Jun 12, 2018 at 19:43

1 Answer 1

3

You are displaying multiple input fields with the same name, and as such PHP has no idea which input field corresponds with which room ID. Your current code will output something like this:

<input type="text" name="room_id" id="room_id" value="1"></input>
<input type="text" name="room_id" id="room_id" value="2"></input>
<input type="text" name="room_id" id="room_id" value="3"></input>

So if you submitted the form to PHP, it would receive this:

$_POST = array(
    'room_id' => 1,
    'room_id' => 2,
    'room_id' => 3
)

or maybe

$_POST = array(
    'room_id' => 3
)

Either way, $_POST["room_{$id}"] will be null, as all your data is stored under the same name.

What you need to do instead is provide a unique name and id attribute to every field, like this:

while($row = mysqli_fetch_assoc($query)) { ?>
    <input type="text" name="room_<?php echo $row['id'] ?>" id="room_<?php echo $row['id'] ?>" value="<?php echo $row['id'] ?>"></input>
<?php } ?>

This will output:

<input type="text" name="room_1" id="room_1" value="1"></input>
<input type="text" name="room_2" id="room_2" value="2"></input>
<input type="text" name="room_3" id="room_3" value="3"></input>

Which means the your $_POST array will look like:

$_POST = array(
    'room_1' => 1,
    'room_2' => 2,
    'room_3' => 3
)

Then, you can retrieve the values from POST using the following for loop:

for($i = 1; isset($_POST["room_{$i}"]); $i++) {
    mysqli_query($db, "UPDATE rooms SET capacity = capacity - 1 WHERE id={$i}");
    // Or whatever you want to do
}

And I would recommend using checkboxes or something instead of text fields, if you are decrementing by a fixed amount, or number fields (but then you'll need to sanitize your user input with something like intval($_POST["room_{$i}"])

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

10 Comments

Thankyou so much sir! But the problem is, still not working. What should I do in to my sampleserver.php <?php $db = mysqli_connect("localhost", "root", "", "psbr"); for($i = 1; isset($_POST["room_{$i}"]); $i++) { mysqli_query($db, "UPDATE rooms SET capacity = capacity - 1 WHERE id={$i}"); } ?> LIKE THAT?
sir, how can I declare the id using jquery? Please sir help me out of this problem :((((
@pacificskybeachresort Assuming that you want to subtract 1 from the capacity of every room in the input field, yes.
If you want to do that via an AJAX POST request to sampleserver.php, you would do pretty much the same thing except in JavaScript:
@plato to make it easier if the textbox and button were wrapped in a form tag (as they conventionally should), and the "submit" event handled using a jQuery handler, then the the ajax could simply do data: $(this).serialize() to get all the fields, much neater and easier
|

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.