Click here to Skip to main content
14,643,812 members
Rate this:
Please Sign up or sign in to vote.
See more:
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:

include "db.php";
include "functions.php";
    $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!
        die("Query failed".mysqli_error($connection));

<!DOCTYPE html>
<link rel="stylesheet" href="" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<script src="" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
<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 class="form-group">
                <label for="password">Password</label>
                <input type="password" class="form-control" name="password">
            <div  class="form-group">
                <select name="id" id="" >
                    global $connection;
                    $query="SELECT * FROM users";
                        die('Query failed'. mysqli_error());
                    while ($row = mysqli_fetch_assoc($result)) {
                        $id = $row['id'];
                        echo "<option value=''>$id</option>";
            <input class="btn btn-primary" type="submit" name="submit" value="Update" >

Updated 12-Jun-20 21:25pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

PASSWORD is a MySQL reserved word, and should not be used as a column name, if you do, it needs to be escaped with backticks:
$query="UPDATE users SET `password`='$password',username='$username' WHERE id= $id ";

But ... never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^] - the code is C#, but it's pretty obvious and you will find similar hashing code for php online.

And remember: if this is web based and you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.
Rate this:
Please Sign up or sign in to vote.

Solution 2

$query="UPDATE users SET password='$password',username='$username' WHERE id= $id ";

Not necessary a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]

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

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