Click here to Skip to main content
15,886,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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:

<?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>
Posted
Updated 12-Jun-20 21:25pm

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:
PHP
$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.
 
Share this answer
 
PHP
$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[^]
 
Share this answer
 
v2

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