2016-12-25 9 views
0

私はこのエラーを取得しています:私はmysqlのエラーを取得していますany1は私を助けることができますか?

UPDATE subjects SET menu_name = 'Delete me', position = 4, visible = 1, WHERE id = 6 Database query failed: 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 'WHERE id = 6' at line 1

<?php 
//1. create a database connection 
    $dbhost = "localhost"; 
    $dbuser = "widget_cms"; 
    $dbpass = "funkyguy"; 
    $dbname = "widget_corp"; 
    $connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname); 
    // Test database connection 
    if(mysqli_connect_errno()) { 
     die("database connection failed: " . 
       mysqli_connect_error() . 
       "(" . mysqli_connect_errno() . ")" 
     ); 
    } 
?> 
<?php 

    //this are the form values from $_POST 
    $id = 6; 
    $menu_name = "Delete me"; 
    $position = 4; 
    $visible = 1; 

    //2. perform database query 
    $query = "UPDATE subjects SET "; 
    $query .= "menu_name = '{$menu_name}', "; 
    $query .= "position = {$position}, "; 
    $query .= "visible = {$visible}, "; 
    $query .= "WHERE id = {$id}"; 

    echo $query; 

    $result = mysqli_query($connection, $query); 
    // test if there was a query error 
    if($result) { 

     echo "success"; 
    }else { 
    die(" Database query failed: " . mysqli_error($connection)); 
    } 
?> 

<!doctype html> 
<html lang="en"> 

<head> 
    <title>Database</title> 
</head> 

<body> 



</body> 
</html> 

<?php 
    //5. Close the connection 
mysqli_close($connection); 
?> 
+3

クエリの 'where'の前にカンマを削除します。 –

答えて

2

あなたはwhere前に冗長なカンマを持っています。それを削除するとOKになります。

$query = "UPDATE subjects SET "; 
$query .= "menu_name = '{$menu_name}', "; 
$query .= "position = {$position}, "; 
$query .= "visible = {$visible} "; 
# Comma removed here ----------^ 
$query .= "WHERE id = {$id}"; 
+0

Thanx alot @Mureinikその本当にうまく働いた –

関連する問題