0

There is something wrong with my code :

On the edit page, I want to show the user the previous value in the input box.

But one error I keep getting about the value is the following :

Notice: Undefined variable: gebruikers_naam in C:\xampp\htdocs\website_herkansing\edit_gebruiker.php on line 72

I think there is something wrong with the isset/submit part but I just can not figure it out..

Here is the code I'm working with

 <?php

session_start();

define('DB_NAME', 'ochtendgloren');
$servername = "localhost";
$username = "root";
$password = "";
$db = "ochtendgloren";
$tbl = "members";

// Create connection
$conn = new mysqli($servername, $username, $password, $db);
//Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if (isset($_POST['submit']))
{
    $gebruikers_naam = mysqli_real_escape_string($db, $_POST['gebruikers_naam']);

    htmlentities($gebruikers_naam);

    $id = $_GET['id'];

    $query = "UPDATE members 
    SET gebruikers_naam = '$gebruikers_naam'
    WHERE id = '$id' " ;

    $result = $conn->query($query);

    if($result){
        echo ("<SCRIPT LANGUAGE='JavaScript'>
    window.alert('edit succesvol!')
    window.location.href='admin_members.php';
    </SCRIPT>");
    } 
}
?>

<html>
<head>
    <link rel="stylesheet" href="boekingsform.css">
    <link  rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abel">
</head>

<div class="boeken">
    <h1>Wijzig hier de gebruiker</h1>
    <form action="editrij.php?id=<?= $id ?>"  method="post" >
        <div class="row">
            <div class="col-25">
                <label for="gebruikers_naam"> vul hier de nieuwe gebruikers naam in: </label>
            </div></div>
        <br>
    <div class="row">
        <div class="col-75">
            <input type="text" name="voornaam" required="required" value="<?= $gebruikers_naam['gebruikers_naam'] ?>"/>
        </div>
    </div>
<br>
    <input type="submit" value="submit" name="submit" />
    </form>
</div>
</html>
Mickaël Leger
  • 3,426
  • 2
  • 17
  • 36
Rachelle
  • 31
  • 4
  • 4
    Possible duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Jens Jun 12 '18 at 12:03
  • Learn about prepared Statements to prevent SQL injection – Jens Jun 12 '18 at 12:04
  • should be ``. The `label` tag doesn't get sent to php. you know that right – Rotimi Jun 12 '18 at 12:06
  • the function mysqli_real_escape_string() requires a database connection ($conn in your code, not $db as is now) `mysqli_real_escape_string($conn, $_POST['gebruikers_naam']);` – jibsteroos Jun 12 '18 at 12:08
  • @AkintundeOlawale ah i see stupid mistake, copied an old code but forgot to change it, but it still doens't tho – Rachelle Jun 12 '18 at 12:09
  • @jibsteroos ah thanks, ofcourse! still doesnt fix the problem with the value tho ): – Rachelle Jun 12 '18 at 12:11

3 Answers3

0

You aren't sending the variable gebruikers_naam, but voornaam. This line of code

<input type="text" name="voornaam" required="required" value="<?= $gebruikers_naam['gebruikers_naam'] ?>"/>

Should be

<input type="text" name="gebruikers_naam" required="required" value="<?= $gebruikers_naam['gebruikers_naam'] ?>"/>

Also, because you are using the variables $id and $gebruikers_naam in the form, you should assign them a value before the if clause.

  • yeah stupid mistake, thanks! changed it but my code still shows the same error ): – Rachelle Jun 12 '18 at 12:14
  • Maybe the problem is that you are using $id and $gebruikers_naam in the html, but they have no value until you send the form. Before the line `if (isset($_POST['submit']))` you could make a select and assign them a value. – Susana González Jun 12 '18 at 12:23
0

I think the problem is related with your variable usage and name of input box it must be gebruikers_naam

On line :

<input type="text" name="voornaam" required="required" value="<?= $gebruikers_naam['gebruikers_naam'] ?>"/>

You may use $gebruikers_naam only to print the name. Also you must assign a global variable before before using it in if clause. Just assign null like $gebruikers_naam = ""; after variables declarations.

Arakin
  • 26
  • 2
0

Still not completely sure the 'flow' of your code makes a lot of sense, but following should help a bit:

        <?php
session_start();

//define('DB_NAME', 'ochtendgloren'); // not used in your code, commented out
$servername = "localhost";
$username = "root";
$password = "";
$db = "ochtendgloren";
$tbl = "members";

// Create connection
$conn = new mysqli($servername, $username, $password, $db);
//Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

if (isset($_POST['submit']))
{
    if(isset($_POST['gebruikers_naam']) && !empty($_POST['gebruikers_naam'])) { // added condition: we need a 'gebruikers_naam'
    $gebruikers_naam = mysqli_real_escape_string($conn, $_POST['gebruikers_naam']);     // changed $db to $conn

    // the return value (encoded string) of htmlentities is not used in your code
    // so commented it out
    //htmlentities($gebruikers_naam); 

    $id = $_GET['id'];

    $query = "UPDATE members 
    SET gebruikers_naam = '$gebruikers_naam'
    WHERE id = '$id' " ;

    $result = $conn->query($query);
    }

    if($result){
        echo ("<SCRIPT LANGUAGE='JavaScript'>
    window.alert('edit succesvol!')
    window.location.href='admin_members.php';
    </SCRIPT>");
    } else {                                                // added else {} statement
    echo "<script> alert('Error: could not update the database'); </script>"; // added: error message
    }       
}
?>

<html>
<head>
    <link rel="stylesheet" href="boekingsform.css">
    <link  rel="stylesheet" href="https://fonts.googleapis.com/css?family=Abel">
</head>

<div class="boeken">
    <h1>Wijzig hier de gebruiker</h1>
    <form action="editrij.php?id=<?php echo $id; ?>"  method="post" > <!-- you need to echo $id and close with ';' - changed: echo $id; -->
        <div class="row">
            <div class="col-25">
                <label for="gebruikers_naam"> vul hier de nieuwe gebruikers naam in: </label>
            </div></div>
        <br>
    <div class="row">
        <div class="col-75">
            <?php $gebruikers_naam = (isset($gebruikers_naam)) ? $gebruikers_naam : 'N/A'; ?> <!-- added a test to see if $gebruikers_naam is available -->
            <input type="text" name="voornaam" required="required" value="<?php echo $gebruikers_naam; ?>"/> <!-- variable is $gebruikers_naam, changed (echo and ';') -->
        </div>
    </div>
<br>
    <input type="submit" value="submit" name="submit" />
    </form>
</div>
</html>
jibsteroos
  • 1,366
  • 2
  • 7
  • 13