1

So, I need to make sure this sets the right parameters to the DB when pressing the buttons. I just want to get the calls and comparisons right so it does what it should when I hit the buttons. There should be one delete button for each row from database, and update page when I press it.

It should be possible to update the text/numbers in the forms presented by MySQL by changing the forms and press Save-button, then refresh the page.

$counter = 1;

if(isset($_POST['save']) and something is changed from the forms compared to DB) {
Update MySQL

Refresh page
<script>
window.location.replace("thispage.php");
</script>

}

if(isset($_POST['del'])) {
DELETE MySQL

Refresh page
<script>
window.location.replace("thispage.php");
</script>

}

echo "<tr><td>ID</td><td>Namn</td><td>Platser</td><td>Fullbokad</td><td>Ta bort</td></tr>";

$sqlListSections = "SELECT * FROM avdelningar WHERE user = {$_SESSION['id']}";
$queryListSections = mysqli_query($mysqli, $sqlListSections);
$del = [];

while($rowListSections = mysqli_fetch_array($queryListSections)) 
{   

    if($counter%2) 
    {
        echo "\n<tr bgcolor=#F1F1F2>\n\n";
    }else
    {
        echo "\n<tr bgcolor=#FFFFFF>\n\n";
    }

    $counter++;



    echo "

    <td>".$rowListSections['id']."</td>
    <td>
        <input type=text value=".$rowListSections['namn']."></td>

    <td>
        <input type=text value=".$rowListSections['platser']."></td>
    <td>";
    if($rowListSections['prio'] == 1) 
    {
        echo "<select name=platser>
            <option selected value=".$rowListSections['prio'].">".$rowListSections['prio']."</option>
            <option value='0'>0</option>".$rowListSections['prio'];
    }elseif($rowListSections['prio'] == 0)
    {
        echo "<select name=platser>
            <option selected value=".$rowListSections['prio'].">".$rowListSections['prio']."</option>
            <option value='1'>1</option>".$rowListSections['prio'];
    }
    echo "</td>
    <td>
        <form method=post action=thispage.php>
        <input type=submit value=Delete name=del>";
    </td>
    </form>
    </tr>";


}

echo "<form method=post action=thispage.php>
<input type=submit value=Save name=save>";
`






2
  • can you please give detail idea about this? what you want to achieve? Commented Sep 16, 2019 at 10:58
  • 1
    I want to mark those sections I want to delete, press Save button and delete all that are checked and show the new result Commented Sep 16, 2019 at 11:13

1 Answer 1

1

in your checkbox change naming as array.

<input type=checkbox name="del[]" value={$rowListSections['id']}>

like

echo $rowListSections["id"].' '.$rowListSections["namn"].' '.$rowListSections["platser"].' '.

$rowListSections["prio"].'';

and in your if(isset($_POST)) you can get a del array so you can loop this array like below.

foreach($del as $val){
    $id = $val;
    $sql_query_for_update = "update table_name set field = 1 where id= '$id' ";
}
Sign up to request clarification or add additional context in comments.

34 Comments

I don't understand. I can't write name=del[] without errors in my checkbox row. Could you give me all the code example?
I still dont understand. I cant write del[] in <checkbox>? And is that an array, without the $?
Yes! if you put [] in name, it will be treated as array. for eg: if you submit this form then you will get all the checked checkboxes are as an array. ( $_POST['del'] = [0 => 'val', 1 => 'val1'] )
let me know if you don't get this :)
Looks Great! I will try it later tonight when I get home! I hope The del[] will work now! Thanks!
|

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.