douao1854 2016-10-06 19:43
浏览 48
已采纳

mariadb的意外语法错误(xampp)[关闭]

<?php

// 1. create a database connection

$dbhost = "localhost" ;
$dbuser = "widget_cms";
$dbpass = "secretpassword";
$dbname = "widget_corp";

$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

// test if connection occured
if(mysqli_connect_errno())
{
    die("database connection failed:". mysqli_connect_error() . 
        " " . mysqli_connect_errno() . " "  );
}


?>

<?php 
    //often these are $_POST values through a web form.
    // 2. Perform the database query

    $id = 5;
    $menu_name = "delete me";
    $position = 4;
    $visible = 1;

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

    $result =  mysqli_query($connection, $query);

    if ($result)
    {
        //success
        //redirect to somepage.php
        echo "success!";
    }
    else
    {
        //failure
        //$message = "subjet update  failed.";
        die("database query failed. " . mysqli_error($connection));
    }
?>

<DOCTYPE HTML>
    <head>
        <title> databases </title>
    </head>

    <body>



    </body> 

</html>


<?php
    // 5. close the database connection
    mysqli_close($connection);
?>

When i try to run this page in firefox,this is the error that pops up. And i don't know how to solve it. please help!

"database query failed. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'WHERE id = 5' at line 1"

TYSM. :)

  • 写回答

1条回答 默认 最新

  • dslh32311 2016-10-06 19:49
    关注

    things need to change:-

    1.

    if (mysqli_connect_error()) {
      die('Database connection failed: '  . mysqli_connect_error()); // remove second  mysqli_connect_error()
    }
    

    2.

     $query .= "menu_name = '{ $menu_name}', "; // $ missed
    

    3.

    $query .= "visible = {$visible} "; // remove trailing `,`
    

    Note:- This is not the end, lot of improvement can be done in your code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流