Click here to Skip to main content
14,638,900 members

Update in PHP get an eror

Member 14861771 asked:

Open original thread
well i am new in php... for now i learn database in php and i build a form that i can want to input username and password value pick the id and when i click submit it will update my table in my database

this is the eror i get:
Query failedYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1


when i try to debugg i figure that the problem is with the id veriable, it get nothing from the select element

What I have tried:

<?php
include "db.php";
include "functions.php";
if(isset($_POST['submit'])){
    $username= $_POST['username'];
    $password= $_POST['password'];
    $id= $_POST['id'];
    $query="UPDATE users SET password='$password',username='$username' WHERE id= $id ";
    $result=mysqli_query($connection,$query);//this function return true or false!
    if(!$result){
        die("Query failed".mysqli_error($connection));
    }
}
?>


<!DOCTYPE html>
<html>
<head>
    <title>Doctype</title>
</head>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
<body>
<div class="container">
    <div class="col-sm-6">
        <form action="Page1.php" method="post">
            <div class="form-group">
                <label for="username">Username</label>
                <input type="text" class="form-control" name="username">
            </div>
            <div class="form-group">
                <label for="password">Password</label>
                <input type="password" class="form-control" name="password">
            </div>
            <div  class="form-group">
                <select name="id" id="" >
                    <?php
                    global $connection;
                    $query="SELECT * FROM users";
                    $result=mysqli_query($connection,$query);
                    if(!$result){
                        die('Query failed'. mysqli_error());
                    }
                    while ($row = mysqli_fetch_assoc($result)) {
                        $id = $row['id'];
                        echo "<option value=''>$id</option>";
                    }
                    ?>
                </select>
            </div>
            <input class="btn btn-primary" type="submit" name="submit" value="Update" >
        </form>
    </div>
</div>

</body>
</html>
Tags: PHP, MySQL, database

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100