

PHP 初心者です！  
今データを更新する練習してるのですが、このコードでデータを更新すると

[code]

    You 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
    
[/code]

というエラーが出ますが 色々調べたんですが 何が違うのかわかりません。  
初歩的な質問ですみません。お願いします！

[code]

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

function.phpのファイル

[code]

    <?php include "db.php";
    
    function showAllData() {
    
        global $connection;
        $query = "SELECT * FROM users";
    
        $result = mysqli_query($connection, $query);
    
        if(!$result){
            die('query connection failed' . mysqli_error());
        }
    
        while($row = mysqli_fetch_assoc($result)) {
            $id = $row['id'];
            echo "<option value=''>$id</option>";
        } 
    
    }
    
[/code]

db.phpのファイル

[code]

    <?php
    
    $connection = mysqli_connect('localhost', 'root', 'root','loginapp');
    
    if(!$connection){
        die('Database connection failed');
    }
    
    ?>
    
[/code]


CC BY-SA 4.0
2019-06-03T04:46:29.537
55473
2019-06-03T05:02:36.450
2019-06-03T05:02:36.450
3060
34567
question
php sql
UPDATE の使い方に関して: You have an error in your SQL syntax