0

I like to have a standard value filled in the input field.
I have this code:

$stma = $conn->prepare("SELECT * FROM `users` WHERE ID = '".$_GET['gebruiker']."' ");
$stma->execute();
$row_count = $stma->rowCount(); // returns 1
foreach ($conn->query($stma) as $rows) {
    $Username = $rows['Username'];
}

/// my form
echo '<form method="POST" >
    <table>
    <th colspan="3"><h1>Gebruiker bewerken</h1></th>
        <tr>
            <th>
                <h3>Gebruikersnaam: </h3>
            </th>
            <td>
                <input style="width: 70%;" type="text" READONLY value="'.$Username.'" > 
                // the value must be filled in this input field
            </td>
        </tr>
        <tr>
            <th>
                <h3>Wachtwoord: </h3>
            </th>
            <td>
                <input style="width: 70%;" type="password"  name="wachtwoord" REQUIRED>
            </td>
        </tr>
        <tr>
            <th>
            </th>
            <td colspan="2">
                <input type="submit" name="bewerken" class="button" style="vertical-align:middle" value="Opslaan">
            </td>
        </tr>
        '.$error.'
    </table>
</form>';

The code doesn't fill in the value i got from the database.
I still get an empty form field.
My query returns 1 result row (i checked)
Does someone see my mistake?
I don't see the mistake i've made (it must me my mistake, it worked for me on other forms too)

9
  • 1
    Inside your foreach() put a print_r($rows) and check what it contains? Commented Jan 23, 2017 at 10:01
  • @MayankPandeyz it doesn't show anything when i add print_r(rows) Commented Jan 23, 2017 at 10:03
  • This is the reason you are getting nothing in your input text bro Commented Jan 23, 2017 at 10:08
  • If print_r() doesn't show any value, then I guess the problem is from your $_GET['gebruiker']. check to make sure it returns a value. Commented Jan 23, 2017 at 10:09
  • Yeah but why? is there something wrong with my foreach? Commented Jan 23, 2017 at 10:09

3 Answers 3

1

To make sure it outputs all errors and warnings (for debugging), this might help:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Place above mentioned code at the top of your file. And you might want to prevent any SQL injection as well:

$stma = $conn->prepare("SELECT * FROM `users` WHERE ID = ? ");
$stma->bindParam(1, $_GET['gebruiker'], PDO::PARAM_INT);
$stma->execute();
$stma->debugDumpParams(); // you could use this to check whether or not all parameters are set correctly
$row_count = $stma->rowCount(); // returns 1
foreach ($conn->query($stma) as $rows) {
    $Username = $rows['Username'];
}
Sign up to request clarification or add additional context in comments.

Comments

1

Below is a working example.

PHP

try {
        $conn = new PDO('mysql:host=localhost;dbname=YourDBname', 'root', '');
    } catch (PDOException $e) {
        echo $e->getMessage();
    }

    $id = $_GET['gebruiker'];

    $sql = "SELECT * FROM `users` WHERE id = :id";
    $stm = $conn->prepare($sql);

    $stm->execute(['id'=>$id]);

    $user = $stm->fetchObject();

    $username = $user->username;

HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Test</title>
</head>
<body>
    <form action="POST">
        <input type="text" value="<?php echo (isset($username)) ? $username : 'No value' ; ?>">
    </form>
</body>
</html>

If your set gebruiker from your url, then you just have do it like: script.php?gebruiker = 1 You can replace 1 with any ID value that exists in your table.

1 Comment

If ID is a primary key in your users table, then you don't need a foreach, since your query will always return 1 value from your table of users.
-1
please try this code
$stma = $conn->prepare("SELECT * FROM `users` WHERE ID = '".$_GET['gebruiker']."' ");

$stma->execute();

$row_count = $stma->rowCount(); // returns 1

foreach ($conn->query($stma) as $rows) {
  $Username = $rows['Username'];
}

**please replace this code**
$res = $conn->query("SELECT * FROM users WHERE ID = '".$_GET['gebruiker']."' ");

$allRows = $res->fetch_assoc(); 

$Username = $allRows['UserName']; 

Comments

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.