Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am not able to read id from query string.
When i click edit link I am reading the the row correctly with no issue but when i update the the data id is not fetching and hence my query doesn't work.

    $id = $_GET['id'];
set null when clicking update button. thats why query is not running


What I have tried:

<?php
include 'master/header.php';
include 'config.php';
include './functions/functions.php';
?>
<?php
    $id = "";
if (isset($_GET['id'])) {
    $id = $_GET['id'];
}
$sql = "SELECT * FROM user WHERE id =$id ";
$result = $conn->query($sql);
$row = mysqli_fetch_assoc($result);


if (isset($_POST['submit'])) {
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];
    $email = $_POST['email'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $query = "UPDATE user SET firstname='$firstName',lastName='$lastName',email='$email',username='$username',password='$password' WHERE id= $id";
    echo $query;
    $updateResult = $conn->query($query);
    if (!$updateResult ) {
        die("qery not works" . mysqli_error($conn));
    } else {
        header("location: index.php");
    }
}
?>
<h1 class="text-center">Edit User</h1>
<form action="edit.php" method="post">
    <div class="container">
        <div class="row">
            <input type="hidden" class="" for="" value="<?php

?>">
            <label class="col-lg-6" for="firstName">First name</label>
            <input type="text" class="form-control col-lg-6" maxlength="25" name="firstName" placeholder="Enter First name" value="<?php
                   if (isset($row['firstname'])) {
                       echo $row['firstname'];
                   }
                   ?>" >
        </div>
        <div class="row">
            <label class="col-lg-6" for="lastName">Last name</label>
            <input type="text" maxlength="25" class="form-control col-lg-6" name="lastName" placeholder="Enter Last name" value="<?php
            if (isset($row['lastName'])) {
                echo $row['lastName'];
            }
                   ?>" >
        </div>
        <div class="row">
            <label class="col-lg-6" maxlength="50" for="lastName">Email</label>
            <input type="text" class="form-control col-lg-6" name="email" placeholder="Enter Email" value="<?php
            if (isset($row['email'])) {
                echo $row['email'];
            }
                   ?>" >
        </div>
        <div class="row">
            <label class="col-lg-6" for="username">Username</label>
            <input type="text" maxlength="50" class="form-control col-lg-6" name="username" placeholder="Enter username" value="<?php
            if (isset($row['username'])) {
                echo $row['username'];
            }
                   ?>">
        </div>
        <div class="row">
            <label for="password" class="col-lg-12">Password</label>
            <input type="password" class="form-control col-lg-12" name="password" placeholder="Enter password" value="<?php
            if (isset($row['password'])) {
                echo $row['password'];
            }
                   ?>"> 
        </div>
        <div class="row">
            <label for="confirmPassword" class="col-lg-12">Confirm Password</label>
            <input type="password" class="form-control col-lg-12" name="confirmPassword" placeholder="confirm password" > 
        </div>
        <div class="row">
            <input type="submit" class="btn btn-primary" name="submit" value="update" >
        </div>
    </div>
</form>
Posted
Updated 17-Jul-17 7:23am
v5
Comments
Harsh Kumar 17-Jul-17 13:22pm    
$id = $_GET['id'];
set null when clicking update button. thats why query is not running

1 solution

Return Values ¶

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

Now it is obvious that in the line $conn->query($sql) you got a boolean answer (probably an error)...
As query can return something different from a result-set you must check its return value and act accordingly...
 
Share this answer
 
Comments
Harsh Kumar 17-Jul-17 14:18pm    
i am not able to understand please explain
Kornfeld Eliyahu Peter 17-Jul-17 14:23pm    
You have to debug your code...
What I'm (and the documentation) states, that query() will not always return a result-set as you presumed. It may fail and return FALSE. So its return value has variable type, but you assumed it is always a result-set and passed it on to the next method... Unfortunately it got boolean (FALSE probably), so mysqli_fetch_assoc() failed...
You have to check the return value from query() before moving on...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900