• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP update submit button not updating specific row

220061

New Coder
hello my update button is not updating a specfic row that I selected instead it updates everything how do i stop it from doing this? my suspision is maybe the ID however I don't know what to really call with this? isn't the update statement: UPDATE table SET col1=?, col2=? col3=?;
PHP:
//variables:

$studentennummerErr = $beschrijvingErr = $beschikbaarErr = " ";
    $studentennummer = $beschrijving = $beschikbaar = " ";
   
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        //checkt of beschrijving is ingevult
        if (empty($_POST["studentennummer"])) {
        $studentennummerErr = test_input($_POST["studentennummer"]);
        } else {
        $studentennummer = test_input($_POST["studentennummer"]);
        }
        //checkt of beschrijving is ingevult
        if (empty($_POST["beschrijving"])) {
        $beschrijvingErr = " beschrijving is verplicht";
        } else {
        $beschrijving = test_input($_POST["beschrijving"]);
        }
        //checkt of beschikbaar is ingevult
        if (empty($_POST["beschikbaar"])) {
            $beschikbaarErr = " beschikbaar is verplicht";
        } else {
            $beschikbaar = test_input($_POST["beschikbaar"]);
        }


// submit button

if (isset($_POST["submit"])) {
            //update statement
            //update studentnummer beschrijving beschikbaar
            $sql = "UPDATE apparatuur SET beschrijving =?, beschikbaar =?, studentennummer =? WHERE ID = ? ";
            $stmt = $conn->prepare($sql) or trigger_error($conn->error."[$sql]");
             $stmt->bind_param('siii', $beschrijving, $beschikbaar, $studentennummer, $ID);
             $stmt->execute();
             function_alert("apparatuur is geupdate");
             $conn->close();
        }
 

220061

New Coder
I don't underd=stand how and why ?
<?php
//link voor het stoppen van empty values
//dit zou alles naar de database moeten sturen als je op de knop drukt
$ID = $_GET["ID"];
var_dump($ID);

// informatie naar de DB sturen
if (isset($_POST["submit"])) {

//update statement
//update studentnummer beschrijving beschikbaar
$sql = "UPDATE apparatuur SET beschrijving =?, beschikbaar =?, studentennummer =? WHERE ID =? ";
var_dump($sql);
$stmt = $conn->prepare($sql) or trigger_error($conn->error."[$sql]");
$stmt->bind_param('siii', $beschrijving, $beschikbaar, $studentennummer, $ID);
$status = $stmt->execute();
if ($status === false) {
trigger_error($stmt->error, E_USER_ERROR);
}
printf("%d Row inserted.\n", $stmt->affected_rows);

$conn->close();

}
?>
 

220061

New Coder
just wanted to add this to it maybe it helps? maybe I have done something wrong here ?
this code is for if you want to add something to the db. it works so there is no problem with this one but I tought maybe I did something wrong here and that it might be effecting my UPDATE statement?

because in this file I dont define the ID. but at the same time I don't think it needs this as the ID has AUTO_INCREMENT

this is my insert form ( I didn't knew how to select all the tags for html and js as well so I'm sorry if it looks weird):
PHP:
<?php
include "mysqli.php";
include "navbar.php";
?>
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<meta charset="UTF-8">
<title>Apparatuur toevoegen</title>
<style>
    *{
        padding:0;
        margin: 0;
    }
    .container{
    background-color: #33cc33;
    height: 500px;
    width: 500px;
    display: flex;
    align-items: center;
    border-radius: 25px;
    margin: auto;
    width: 40%;
    padding: 10px;
    margin-top:10px;
  }
  .form{
      margin: auto;
      width: 40%;
      padding: 10px;
  }
  .form h2{
      padding-bottom: 10px;
  }
  .error {
    color: #FF0000;
}

input[type=submit] {
    padding:5px 15px;
    background:white;
    border:0 none;
    cursor:pointer;
    -webkit-border-radius: 5px;
    border-radius: 5px;
}
</style>
</head>
<body>
    <?php
    $naamErr = $beschrijvingErr = $beschikbaarErr = " ";
    $naam = $beschrijving = $beschikbaar = " ";
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        //checkt of naam is ingevult
        if (empty($_POST["naam"])) {
        $naamErr = " Naam is verplicht";
        } else {
        $naam = test_input($_POST["naam"]);
        }

        //checkt of naam is ingevult
        if (empty($_POST["beschrijving"])) {
        $beschrijvingErr = " beschrijving is verplicht";
        } else {
        $beschrijving = test_input($_POST["beschrijving"]);
        }
        //checkt of beschikbaarheid is ingevult
        if (empty($_POST["beschikbaar"])) {
            $beschikbaarErr = " beschikbaar is verplicht";
        } else {
            $beschikbaar = test_input($_POST["beschikbaar"]);
        }
    }
    function test_input($data) {
        $data = trim($data); //removes whitespaces
        $data = stripslashes($data); // removes backlashes like \
        $data = htmlspecialchars($data); // stops browsers from usings stuf as an special html event
        return $data; //returns data
    }
    function function_alert($message) {
      
        // Display the alert box
        echo "<script>alert('$message');</script>";
    }
    ?>
    <div class="container">
        <div class="form">
        <h2>Apparaten toevoegen</h2>
        <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
        <!--naam van het apparaat-->
        <label for="naam">Naam </label><br>
        <input type="text" name="naam" id="naam" required="required"><span class="error">* <?php echo $naamErr;?></span> <br><br/><br/>
        <!--beschrijving van het product -->
        <label for="beschrijving">Beschrijving </label>
        <input type="text" name="beschrijving" id="beschrijving" required="required">
        <span class="error">* <?php echo $beschrijvingErr;?></span> <br><br/><br/>
        <!--beschikbaar dropdown-->
        <label for="beschikbaar">Beschikbaar </label><br>
        <select name="beschikbaar" class="beschikbaar" value="true" required="required"'>
            <option hidden>--Select option--</option>
            <option value="1">Beschikbaar</option>
            <option value="0">Uitgeleend</option>
        </select>
        <!--submit button-->
        <br><br><input type="submit" value="Toevoegen" name="submit">
        <?php
        //link voor het stoppen van empty values
        //dit zou alles naar de database moeten sturen als je op de knop drukt
        // informatie naar de DB sturen
        if (isset($_POST["submit"])) {
            //dit nog veranderen voor als optie 0 word aangeklikt
            //prepared statement
                $stmt = $conn->prepare("INSERT INTO apparatuur (naam, beschrijving,beschikbaar)
                VALUES (?,?,?)");
                $stmt->bind_param("ssi", $naam, $beschrijving,$beschikbaar);
                $stmt->execute();
                function_alert("Apparaat is toegevoegd");
                $conn->close();
        }
        ?>
        </form>
    </div>
    </div>
</body>
<footer>
<script>
if ( window.history.replaceState ) {
  window.history.replaceState( null, null, window.location.href );
}
</script>
</footer>
</html>
 

Ghost

Active Coder
just wanted to add this to it maybe it helps? maybe I have done something wrong here ?
this code is for if you want to add something to the db. it works so there is no problem with this one but I tought maybe I did something wrong here and that it might be effecting my UPDATE statement?

because in this file I dont define the ID. but at the same time I don't think it needs this as the ID has AUTO_INCREMENT

this is my insert form ( I didn't knew how to select all the tags for html and js as well so I'm sorry if it looks weird):
PHP:
<?php
include "mysqli.php";
include "navbar.php";
?>
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<meta charset="UTF-8">
<title>Apparatuur toevoegen</title>
<style>
    *{
        padding:0;
        margin: 0;
    }
    .container{
    background-color: #33cc33;
    height: 500px;
    width: 500px;
    display: flex;
    align-items: center;
    border-radius: 25px;
    margin: auto;
    width: 40%;
    padding: 10px;
    margin-top:10px;
  }
  .form{
      margin: auto;
      width: 40%;
      padding: 10px;
  }
  .form h2{
      padding-bottom: 10px;
  }
  .error {
    color: #FF0000;
}

input[type=submit] {
    padding:5px 15px;
    background:white;
    border:0 none;
    cursor:pointer;
    -webkit-border-radius: 5px;
    border-radius: 5px;
}
</style>
</head>
<body>
    <?php
    $naamErr = $beschrijvingErr = $beschikbaarErr = " ";
    $naam = $beschrijving = $beschikbaar = " ";
   
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        //checkt of naam is ingevult
        if (empty($_POST["naam"])) {
        $naamErr = " Naam is verplicht";
        } else {
        $naam = test_input($_POST["naam"]);
        }

        //checkt of naam is ingevult
        if (empty($_POST["beschrijving"])) {
        $beschrijvingErr = " beschrijving is verplicht";
        } else {
        $beschrijving = test_input($_POST["beschrijving"]);
        }
        //checkt of beschikbaarheid is ingevult
        if (empty($_POST["beschikbaar"])) {
            $beschikbaarErr = " beschikbaar is verplicht";
        } else {
            $beschikbaar = test_input($_POST["beschikbaar"]);
        }
    }
    function test_input($data) {
        $data = trim($data); //removes whitespaces
        $data = stripslashes($data); // removes backlashes like \
        $data = htmlspecialchars($data); // stops browsers from usings stuf as an special html event
        return $data; //returns data
    }
    function function_alert($message) {
     
        // Display the alert box
        echo "<script>alert('$message');</script>";
    }
    ?>
    <div class="container">
        <div class="form">
        <h2>Apparaten toevoegen</h2>
        <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
        <!--naam van het apparaat-->
        <label for="naam">Naam </label><br>
        <input type="text" name="naam" id="naam" required="required"><span class="error">* <?php echo $naamErr;?></span> <br><br/><br/>
        <!--beschrijving van het product -->
        <label for="beschrijving">Beschrijving </label>
        <input type="text" name="beschrijving" id="beschrijving" required="required">
        <span class="error">* <?php echo $beschrijvingErr;?></span> <br><br/><br/>
        <!--beschikbaar dropdown-->
        <label for="beschikbaar">Beschikbaar </label><br>
        <select name="beschikbaar" class="beschikbaar" value="true" required="required"'>
            <option hidden>--Select option--</option>
            <option value="1">Beschikbaar</option>
            <option value="0">Uitgeleend</option>
        </select>
        <!--submit button-->
        <br><br><input type="submit" value="Toevoegen" name="submit">
        <?php
        //link voor het stoppen van empty values
        //dit zou alles naar de database moeten sturen als je op de knop drukt
        // informatie naar de DB sturen
        if (isset($_POST["submit"])) {
            //dit nog veranderen voor als optie 0 word aangeklikt
            //prepared statement
                $stmt = $conn->prepare("INSERT INTO apparatuur (naam, beschrijving,beschikbaar)
                VALUES (?,?,?)");
                $stmt->bind_param("ssi", $naam, $beschrijving,$beschikbaar);
                $stmt->execute();
                function_alert("Apparaat is toegevoegd");
                $conn->close();
        }
        ?>
        </form>
    </div>
    </div>
</body>
<footer>
<script>
if ( window.history.replaceState ) {
  window.history.replaceState( null, null, window.location.href );
}
</script>
</footer>
</html>
You are correct that auto-increment will add the ID for you. You do not need to supply the ID for an insert.
You do however need to add an ID for an update, so you're correct.

I do not see any issues with your code - As long as ID is actually set.

However, I wonder if you are passing in NULL values. I see that you have 'Leeg' which I think is 'NULL' and 'Nee' for 'No' so those columns *must* be updated with values. If you are passing in NULL to those columns, the update will fail. You also have 'None' as default, so even if you did allow NULL, you are still not giving it a default if a value is not passed in.

What shows up if you do (after execute() ):
echo $stmt->error
also what about var_dump($sql)

Can you tell us if you get any errors?

-----
Another thing you should check is if your database USER has permission to UPDATE. If your database user was not given permissions for UPDATE statements then all updates will fail. Similarly, if your databaser user does not have permission to LOCK a table you might not be able to UPDATE (depends on your database engine / table types).

Please try to check user permissions as well as the values you are passing in to the other columns (NULL, EMPTY?) to check.

I would love to help, but try those things first and I'll be back if they don't work - just please tag me so I am notified!
 

220061

New Coder
You are correct that auto-increment will add the ID for you. You do not need to supply the ID for an insert.
You do however need to add an ID for an update, so you're correct.

I do not see any issues with your code - As long as ID is actually set.

However, I wonder if you are passing in NULL values. I see that you have 'Leeg' which I think is 'NULL' and 'Nee' for 'No' so those columns *must* be updated with values. If you are passing in NULL to those columns, the update will fail. You also have 'None' as default, so even if you did allow NULL, you are still not giving it a default if a value is not passed in.

What shows up if you do (after execute() ):
echo $stmt->error
also what about var_dump($sql)

Can you tell us if you get any errors?

-----
Another thing you should check is if your database USER has permission to UPDATE. If your database user was not given permissions for UPDATE statements then all updates will fail. Similarly, if your databaser user does not have permission to LOCK a table you might not be able to UPDATE (depends on your database engine / table types).

Please try to check user permissions as well as the values you are passing in to the other columns (NULL, EMPTY?) to check.

I would love to help, but try those things first and I'll be back if they don't work - just please tag me so I am notified!
@Ghost sorry for the late reply
first of all you are right: Leeg means NULL there is nothing in there and Nee means No.
This is what I get when I tuse those codes:
1633416061935.png

1633416078026.png
I will check for user priviledges in a sec because I'm not really that good with that. I didn't assign a secial privledge though. except make a login system vs not logged in users but I will have a deeper look into that
 

220061

New Coder
@Ghost
priviledges:
translation for the words (used google translation for this so I hope you understand what I mean):
gebruikernaam: username
servernaam: servername
type: type
Rechten: Rights
toekennen: award
actie: action
globaal: global
Ja: Yes
rechten bewerken: edit rights
exporteren: export
1633416829664.png
 

220061

New Coder
full code of edit script:
I define ID on line 93
Code:
<?php
//misschien cookies gebruiken om oude gegevens te otnhouden
include "mysqli.php";
include "navbar.php";
?>
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<meta charset="UTF-8">
<title>edit apparatuur</title>
<style>
    *{
        padding:0;
        margin: 0;
    }
    .container{
    background-color: #33cc33;
    height: 500px;
    width: 500px;
    display: flex;
    align-items: center;
    border-radius: 25px;
    margin: auto;
    margin-top:20px;
    width: 40%;
    padding: 10px;
    margin-top:10px;
  }
  .form{
      margin: auto;
      width: 40%;
      padding: 10px;
  }
  .form h2{
      padding-bottom: 10px;
  }
  .error {
    color: #FF0000;
}

input[type=submit] {
    padding:5px 15px;
    background:white;
    border:0 none;
    cursor:pointer;
    -webkit-border-radius: 5px;
    border-radius: 5px;
}
</style>
</head>
<body>
    <?php
    $studentennummerErr = $beschrijvingErr = $beschikbaarErr = " ";
    $studentennummer = $beschrijving = $beschikbaar = " ";
   
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        //checkt of beschrijving is ingevult    
        if (empty($_POST["studentennummer"])) {
        $studentennummerErr = test_input($_POST["studentennummer"]);
        } else {
        $studentennummer = test_input($_POST["studentennummer"]);
        }
        //checkt of beschrijving is ingevult
        if (empty($_POST["beschrijving"])) {
        $beschrijvingErr = " beschrijving is verplicht";
        } else {
        $beschrijving = test_input($_POST["beschrijving"]);
        }
        //checkt of beschikbaar is ingevult
        if (empty($_POST["beschikbaar"])) {
            $beschikbaarErr = " beschikbaar is verplicht";
        } else {
            $beschikbaar = test_input($_POST["beschikbaar"]);
        }
    }
    function test_input($data) {
        $data = trim($data); //removes whitespaces
        $data = stripslashes($data); // removes backlashes like \
        $data = htmlspecialchars($data); // stops browsers from usings stuf as an special html event
        return $data; //returns data
    }
    function function_alert($message) {
     
        // Display the alert box
        echo "<script>alert('$message');</script>";
    }
    ?>
    <div class="container">
        <div class="form">
        <h2>Edit apparaten</h2>
        <form method="post">
        <input type="hidden" name="ID" value="ID"> <!--defining ID??????-->
        <!--beschrijving van het product -->
        <label for="beschrijving">Beschrijving </label>
        <input type="text" name="beschrijving" id="beschrijving" required="required"/>
        <span class="error">* <?php echo $beschrijvingErr;?></span> <br><br/><br/>
        <!--beschikbaar dropdown-->
        <label for="beschikbaar">Beschikbaar </label><br>
        <select name="beschikbaar" class="beschikbaar" value="true" required="required">
            <option hidden>--select--</option>
            <option value="1">Beschikbaar</option>
            <option value="0">Uitgeleend</option>
        </select>
         <!--studentennummer-->
         <br>
         <label for="studentennummer">studentennummer</label>
         <input type="text" name="studentennummer" type="text" placeholder="studentennummer">
       
        <!--submit button-->
        <br><br><input type="submit" value="Updaten" name="submit">
       
        <?php
        //link voor het stoppen van empty values
        //dit zou alles naar de database moeten sturen als je op de knop drukt
       
        // informatie naar de DB sturen
        if (isset($_POST["submit"])) {
           

            //update statement
            //update studentnummer beschrijving beschikbaar
            $sql = "UPDATE apparatuur SET beschrijving =?, beschikbaar =?, studentennummer =? WHERE beschrijving =? ";
           
            $stmt = $conn->prepare($sql) or trigger_error($conn->error."[$sql]");
             $stmt->bind_param('sii', $beschrijving, $beschikbaar, $studentennummer);
             $status = $stmt->execute();
             var_dump($sql);
            printf("Error: %s.\n", $stmt->error);
             if ($status === false) {
                trigger_error($stmt->error, E_USER_ERROR);
              }
              printf("%d Row inserted.\n", $stmt->affected_rows);
             
             $conn->close();
           
        }
        ?>
        </form>
    </div>
    </div>
</body>
</html>
 

220061

New Coder
did an update down here by saying ID=? and adding it to the bind_param but it still updates nothing
Code:
        // informatie naar de DB sturen
        if (isset($_POST["submit"])) {
            

            //update statement
            //update studentnummer beschrijving beschikbaar
            $sql = "UPDATE apparatuur SET beschrijving =?, beschikbaar =?, studentennummer =? WHERE ID =? ";
            
            $stmt = $conn->prepare($sql) or trigger_error($conn->error."[$sql]");
             $stmt->bind_param('siii', $beschrijving, $beschikbaar, $studentennummer, $ID);
             $status = $stmt->execute();
             var_dump($sql);
            printf("Error: %s.\n", $stmt->error);
             if ($status === false) {
                trigger_error($stmt->error, E_USER_ERROR);
              }
              printf("%d Row inserted.\n", $stmt->affected_rows);
              
             $conn->close();
            
        }
        ?>

1633417482523.png
 

220061

New Coder
or maybe the error is in here? this is a table. You click on edit and it will direct you to edit.php
PHP:
<?php
//print_r($_SESSION); laat de array zien met informatie over het ingelogde user
include "mysqli.php";
if(isset($_SESSION["ID"])) {
  //  ingelogd
  $sql = "SELECT ID, naam, beschrijving, beschikbaar FROM apparatuur";
$result = $conn->query($sql);
echo "<div class=center>";
echo "<table border=1>";
echo "<tr><th>Naam</th><th>Beschrijving</th><th>beschikbaar</th><th>Delete</th><th>Edit</th></tr>";
if ($result->num_rows > 0) {
  $count=0;
  // output data of each row
  while($row = $result->fetch_assoc()) {
    echo "<tr><td> " . $row["naam"]. "</td><td> " . $row["beschrijving"]."</td><td>";
    if($row['beschikbaar'] == 1) {
      ?><i class="fa fa-check" style="color: green;"></i><?php
    }else{
       ?><i class="fa fa-times" style="color: red;"></i><?php
    }// dit moet je kunnen bewerken als je ingelogd bent
    echo "<td><a class='button' href='delete.php?id=".$row['ID']."'>Delete</a></td>";
    //edit kan ook weg worden gedaan en als je ingelogd bent dat je dan het vinkje kan verwanderen
    echo "<td><a class='button' href='edit.php?id=".$row['ID']."'>Edit</a></td>";// dit moet je alleen zien als je ingelogd bent
    echo "</td></tr>";
    $count++;
  }
 

Ghost

Active Coder
In this image below, you have 4 question marks in your query, but only 3 bind parameters - but the code you are posting here on Codeforum has 4. Are you running different code than the code you posted?
1633416061935-png.1090

That is why your query is failing in that image - you need your 4th bind param set^^

In the code you are posting here you have:
PHP:
$sql = "UPDATE apparatuur SET beschrijving =?, beschikbaar =?, studentennummer =? WHERE ID =? ";
            
            $stmt = $conn->prepare($sql) or trigger_error($conn->error."[$sql]");
             $stmt->bind_param('siii', $beschrijving, $beschikbaar, $studentennummer, $ID);
 

220061

New Coder
In this image below, you have 4 question marks in your query, but only 3 bind parameters - but the code you are posting here on Codeforum has 4. Are you running different code than the code you posted?
1633416061935-png.1090

That is why your query is failing in that image - you need your 4th bind param set^^

In the code you are posting here you have:
PHP:
$sql = "UPDATE apparatuur SET beschrijving =?, beschikbaar =?, studentennummer =? WHERE ID =? ";
           
            $stmt = $conn->prepare($sql) or trigger_error($conn->error."[$sql]");
             $stmt->bind_param('siii', $beschrijving, $beschikbaar, $studentennummer, $ID);
no I was trying to see different possibilities to solve this problem en forgot that I edited it sorry about that I have solved the problem now:

Code:
<?php
//misschien cookies gebruiken om oude gegevens te otnhouden
include "mysqli.php";
include "navbar.php";
?>
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<meta charset="UTF-8">
<title>edit apparatuur</title>
<style>
    *{
        padding:0;
        margin: 0;
    }
    .container{
    background-color: #33cc33;
    height: 500px;
    width: 500px;
    display: flex;
    align-items: center;
    border-radius: 25px;
    margin: auto;
    margin-top:20px;
    width: 40%;
    padding: 10px;
    margin-top:10px;
  }
  .form{
      margin: auto;
      width: 40%;
      padding: 10px;
  }
  .form h2{
      padding-bottom: 10px;
  }
  .error {
    color: #FF0000;
}

input[type=submit] {
    padding:5px 15px;
    background:white;
    border:0 none;
    cursor:pointer;
    -webkit-border-radius: 5px;
    border-radius: 5px;
}
</style>
</head>
<body>
    <?php
    $studentennummerErr = $beschrijvingErr = $beschikbaarErr = " ";
    $studentennummer = $beschrijving = $beschikbaar = " ";
    
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        //checkt of beschrijving is ingevult     
        if (empty($_POST["studentennummer"])) {
        $studentennummerErr = test_input($_POST["studentennummer"]);
        } else {
        $studentennummer = test_input($_POST["studentennummer"]);
        }
        //checkt of beschrijving is ingevult
        if (empty($_POST["beschrijving"])) {
        $beschrijvingErr = " beschrijving is verplicht";
        } else {
        $beschrijving = test_input($_POST["beschrijving"]);
        }
        //checkt of beschikbaar is ingevult
        if (empty($_POST["beschikbaar"])) {
            $beschikbaarErr = " beschikbaar is verplicht";
        } else {
            $beschikbaar = test_input($_POST["beschikbaar"]);
        }
    }
    function test_input($data) {
        $data = trim($data); //removes whitespaces
        $data = stripslashes($data); // removes backlashes like \
        $data = htmlspecialchars($data); // stops browsers from usings stuf as an special html event
        return $data; //returns data
    }
    function function_alert($message) {
      
        // Display the alert box
        echo "<script>alert('$message');</script>";
    }
    ?>
    <?php

    $sql = "SELECT beschrijving, beschikbaar, Studentennummer FROM apparatuur WHERE ID = " . $_GET['id'];
    $result = $conn->query($sql);

    if ($result->num_rows == 1) {
    // output data of each row
        while($row = $result->fetch_assoc()) {
            ?>
            <div class="container">
            <div class="form">
            <h2>Edit apparaten</h2>
            <form method="post">
            <!--beschrijving van het product -->
            <label for="beschrijving">Beschrijving </label>
            <input type="text" name="beschrijving" id="beschrijving" value="<?php echo $row["beschrijving"];?>" required="required"/>
            <span class="error">* <?php echo $beschrijvingErr;?></span> <br><br/><br/>
            <!--beschikbaar dropdown-->
            <label for="beschikbaar">Beschikbaar </label><br>
            <select name="beschikbaar" class="beschikbaar" value="true" required="required">
                <?php
                    if ($row["beschikbaar"] == 1) {
                        ?>
                        <option hidden>--select--</option>
                        <option value="1" selected="selected">Beschikbaar</option>
                        <option value="0">Uitgeleend</option>
                        <?php
                    }else{
                        ?>
                        <option hidden>--select--</option>
                        <option value="1">Beschikbaar</option>
                        <option value="0"selected="selected">Uitgeleend</option>
                        <?php
                    }
                ?> 
            </select>
             <!--studentennummer-->
             <br>
             <label for="Studentennummer">Studentennummer</label>
             <input type="text" name="Studentennummer" type="text" value="<?php echo $row["Studentennummer"];?>">
            
            <!--submit button-->
            <br><br><input type="submit" value="Updaten" name="submit">
            <?php
        }
    } else {
        echo "0 results";
    }
    ?>

        <?php
        //link voor het stoppen van empty values
        //dit zou alles naar de database moeten sturen als je op de knop drukt
        
        // informatie naar de DB sturen
        if (isset($_POST["submit"])) {
            $id = $_GET['id'];
            //update statement
            //update studentnummer beschrijving beschikbaar
            $sql = "UPDATE apparatuur SET beschrijving =?, beschikbaar =?, studentennummer =? WHERE id =? ";
            
            $stmt = $conn->prepare($sql) or trigger_error($conn->error."[$sql]");
             $stmt->bind_param('siii', $beschrijving, $beschikbaar, $studentennummer, $id);
             $status = $stmt->execute();
            
            printf("Error: %s.\n", $stmt->error);
             if ($status === false) {
                trigger_error($stmt->error, E_USER_ERROR);
              }
              printf("%d Row inserted.\n", $stmt->affected_rows);
              
             $conn->close();
            
        }
        ?>
        </form>
    </div>
    </div>
</body>
</html>
 

220061

New Coder
no I was trying to see different possibilities to solve this problem en forgot that I edited it sorry about that I have solved the problem now:

Code:
<?php
//misschien cookies gebruiken om oude gegevens te otnhouden
include "mysqli.php";
include "navbar.php";
?>
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<meta charset="UTF-8">
<title>edit apparatuur</title>
<style>
    *{
        padding:0;
        margin: 0;
    }
    .container{
    background-color: #33cc33;
    height: 500px;
    width: 500px;
    display: flex;
    align-items: center;
    border-radius: 25px;
    margin: auto;
    margin-top:20px;
    width: 40%;
    padding: 10px;
    margin-top:10px;
  }
  .form{
      margin: auto;
      width: 40%;
      padding: 10px;
  }
  .form h2{
      padding-bottom: 10px;
  }
  .error {
    color: #FF0000;
}

input[type=submit] {
    padding:5px 15px;
    background:white;
    border:0 none;
    cursor:pointer;
    -webkit-border-radius: 5px;
    border-radius: 5px;
}
</style>
</head>
<body>
    <?php
    $studentennummerErr = $beschrijvingErr = $beschikbaarErr = " ";
    $studentennummer = $beschrijving = $beschikbaar = " ";
   
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        //checkt of beschrijving is ingevult    
        if (empty($_POST["studentennummer"])) {
        $studentennummerErr = test_input($_POST["studentennummer"]);
        } else {
        $studentennummer = test_input($_POST["studentennummer"]);
        }
        //checkt of beschrijving is ingevult
        if (empty($_POST["beschrijving"])) {
        $beschrijvingErr = " beschrijving is verplicht";
        } else {
        $beschrijving = test_input($_POST["beschrijving"]);
        }
        //checkt of beschikbaar is ingevult
        if (empty($_POST["beschikbaar"])) {
            $beschikbaarErr = " beschikbaar is verplicht";
        } else {
            $beschikbaar = test_input($_POST["beschikbaar"]);
        }
    }
    function test_input($data) {
        $data = trim($data); //removes whitespaces
        $data = stripslashes($data); // removes backlashes like \
        $data = htmlspecialchars($data); // stops browsers from usings stuf as an special html event
        return $data; //returns data
    }
    function function_alert($message) {
     
        // Display the alert box
        echo "<script>alert('$message');</script>";
    }
    ?>
    <?php

    $sql = "SELECT beschrijving, beschikbaar, Studentennummer FROM apparatuur WHERE ID = " . $_GET['id'];
    $result = $conn->query($sql);

    if ($result->num_rows == 1) {
    // output data of each row
        while($row = $result->fetch_assoc()) {
            ?>
            <div class="container">
            <div class="form">
            <h2>Edit apparaten</h2>
            <form method="post">
            <!--beschrijving van het product -->
            <label for="beschrijving">Beschrijving </label>
            <input type="text" name="beschrijving" id="beschrijving" value="<?php echo $row["beschrijving"];?>" required="required"/>
            <span class="error">* <?php echo $beschrijvingErr;?></span> <br><br/><br/>
            <!--beschikbaar dropdown-->
            <label for="beschikbaar">Beschikbaar </label><br>
            <select name="beschikbaar" class="beschikbaar" value="true" required="required">
                <?php
                    if ($row["beschikbaar"] == 1) {
                        ?>
                        <option hidden>--select--</option>
                        <option value="1" selected="selected">Beschikbaar</option>
                        <option value="0">Uitgeleend</option>
                        <?php
                    }else{
                        ?>
                        <option hidden>--select--</option>
                        <option value="1">Beschikbaar</option>
                        <option value="0"selected="selected">Uitgeleend</option>
                        <?php
                    }
                ?>
            </select>
             <!--studentennummer-->
             <br>
             <label for="Studentennummer">Studentennummer</label>
             <input type="text" name="Studentennummer" type="text" value="<?php echo $row["Studentennummer"];?>">
           
            <!--submit button-->
            <br><br><input type="submit" value="Updaten" name="submit">
            <?php
        }
    } else {
        echo "0 results";
    }
    ?>

        <?php
        //link voor het stoppen van empty values
        //dit zou alles naar de database moeten sturen als je op de knop drukt
       
        // informatie naar de DB sturen
        if (isset($_POST["submit"])) {
            $id = $_GET['id'];
            //update statement
            //update studentnummer beschrijving beschikbaar
            $sql = "UPDATE apparatuur SET beschrijving =?, beschikbaar =?, studentennummer =? WHERE id =? ";
           
            $stmt = $conn->prepare($sql) or trigger_error($conn->error."[$sql]");
             $stmt->bind_param('siii', $beschrijving, $beschikbaar, $studentennummer, $id);
             $status = $stmt->execute();
           
            printf("Error: %s.\n", $stmt->error);
             if ($status === false) {
                trigger_error($stmt->error, E_USER_ERROR);
              }
              printf("%d Row inserted.\n", $stmt->affected_rows);
             
             $conn->close();
           
        }
        ?>
        </form>
    </div>
    </div>
</body>
</html>
it broke again dont know why but I will try to find it out
 

OldMan

Coder
@22006, Your having a problem understanding where PHP is executed. It runs at the server, not in a browser or on the users computer. All PHP is run thousands of miles away from the users house; maybe on a mountain top in Siberia, not on his/hers' computer.
This line:
<span class="error">* <?php echo $beschrijvingErr;?></span> <br><br/><br/> can never have anything to echo because it comes from the submitted form. You could use AJAX if you want that, but a better way is with JS checking things before the form is sent off.

That brings up the next thing -> The form needs to be sent to your server via the action attribute of the form tag. And you need to place the PHP at the end of the HTML code in a file on the server.
Learn forms : https://www.w3schools.com/html/html_forms.asp
How to process form
PHP:
 : [URL]https://www.w3schools.com/php/php_forms.asp[/URL]
 

Top