Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
您的SQL语法有错误;检查与MySQL服务器对应的手册-限制1_Mysql_Google Chrome - Fatal编程技术网

您的SQL语法有错误;检查与MySQL服务器对应的手册-限制1

您的SQL语法有错误;检查与MySQL服务器对应的手册-限制1,mysql,google-chrome,Mysql,Google Chrome,我正在尝试使用这个简单的DB。然而,我不断地遇到错误——“发生了一个错误:您的SQL语法有一个错误;请查看与您的MySQL服务器版本对应的手册,在第1行的“limit 1”附近使用正确的语法” 我正在使用MYSQL 5.0,并尝试了不同的方式来显示$query 任何想法都很好。还希望将数量更改为下拉菜单,但不确定如何操作,并且仍将信息存储在数据库中 <?php $hostname = 'localhost'; $username = 'username'; $passwo

我正在尝试使用这个简单的DB。然而,我不断地遇到错误——“发生了一个错误:您的SQL语法有一个错误;请查看与您的MySQL服务器版本对应的手册,在第1行的“limit 1”附近使用正确的语法”

我正在使用MYSQL 5.0,并尝试了不同的方式来显示$query

任何想法都很好。还希望将数量更改为下拉菜单,但不确定如何操作,并且仍将信息存储在数据库中

<?php

$hostname     = 'localhost';
$username     = 'username';
$password     = 'password';
$databasename = 'database';

if (!$connection_result = mysql_connect($hostname, $username, $password)) {
    die('Error Connecting to MySQL Database: ' . mysql_error());
}

if (!$db_result = mysql_select_db($databasename, $connection_result)) {
    die('Error Selecting the MySQL Database: ' . mysql_error());
}

// if we made it here, we are one with the database

switch($_GET['action']){
    case 'delete':
        $title = 'Delete an Item';
        $content = DeleteItem();
        break;
    case 'edit':
        $title = 'Edit an Item';
        $content = ManageItem();
        break;
    case 'new':
        $title = 'Create an Item';
        $content = ManageItem();
        break;
    case 'save':
        $title = 'Save an Item';
        $content = SaveItem();
        break;
    case 'print':
        PrintList();
        break;
    default:
        $title = 'The List';
        $content = ShowList();
        break;
}


function ShowList(){
    // we are showing the table, no initial actions are needed
    // so we'll jump straight into the table

    $output = '<table border="1" width="700" cellpadding="5" style="border-collapse: collapse">
                <tr>
                    <th  width="50">ItemID</th>
                    <th>Item Name</th>
                    <th>Owner</th>
                    <th  width="70">Quantity</th>
                    <th  width="76">Reoccuring</th>
                    <th  width="76">Purchased</th>
                    <th width="76">Action</th>
                </tr>';

    // run the query, we're putting the things that aren't purchased at the top
    $query = mysql_query("select * from thelist order by purchased asc");

    // loop all the records
    while($row = mysql_fetch_assoc($query)){
        $output .= '<tr>';
        $output .= '<td>' . $row['itemid'] .'</td>';
        $output .= '<td>' . $row['name'] . '</td>';
        $output .= '<td>';

        // if this field is empty, its not for anyone specific
        if(empty($row['owner'])){
            $output .= "Everyone";
        }else{
            $output .= $row['owner'];
        }

        $output .= '</td>';
        $output .= '<td>' . $row['quantity'] .'</td>';
        $output .= '<td>';

        if($row['reoccuring'] == 1){
            $output .= "yes";
        }else{
            $output .= "no";
        }
        $output .= '</td>';
        $output .= '<td>';

        if($row['purchased'] == 1){
            $output .= "yes";
        }else{
            $output .= "no";
        }
        $output .= '</td>';
        $output .= '<td width="76"><a href="?action=edititem&itemid=' . $row['itemid'] . '">Edit</a> - ';

        // need to add slashes as we're dealing with javascript here
        $output .= '<a href="javascript:checkDelete(\'' . addslashes($row['name']) . '\',' . $row['itemid'] . ');">Delete</a></td>';
        $output .= '</tr>';
    }
    $output .= '</table>';

    return $output;
}

function DeleteItem(){

    // in the query we convert it to an integer to prevent any injection
    if(mysql_query("delete from thelist where itemid='".(int)$_GET['itemid']."'")){
        $output = '<b>Item deleted successfully!</b><br/><br/>';
    }else{
        $output = '<b>An Error Occurred: ' . mysql_error() . '</b><br><br>';
    }
    // show the list
    $output .= ShowList();

    return $output;
}

function ManageItem(){
    if(isset($_GET['itemid'])){
        // if we're editing we need to grab the stuff from the database

        // convert to integer (if its not a number it'll become zero
        $itemid= (int)$_GET['itemid'];

        $query = mysql_query("select * from thelist where itemid='" . $itemid . "' limit 1");
        $row = mysql_fetch_assoc($query);
    }else{
        // set up blank array
        $row['itemid'] = '';
        $row['name'] = '';
        $row['purchased'] = '';
        $row['owner'] = '';
        $row['reoccuring'] = '';
        $row['quantity'] = '';
    }

    // we have lots of HTML here, so we're breaking out of PHP, but we need to stop it outputting
    // so we'll use output buffering and capture the result

    ob_start();
    ?>
    <form method="POST" action="<?=$_SEVER['PHP_SELF']?>?action=save">
    <?php

    if(isset($_GET['itemid']) && $itemid > 0){
        echo '<input type="hidden" name="itemid" value="' . $itemid . '">';
    }

    ?>
    <table border="0" width="450">
        <tr>
            <td><font size="2">Item Name:</font></td>
            <td><input type="text" name="name" size="20" value="<?php echo htmlspecialchars($row['name']); ?>"></td>
        </tr>
        <tr>
            <td><font size="2">For someone specific? </font><font size="1">(leave blank if not)</font></td>
            <td><input type="text" name="owner" size="20"  value="<?php echo htmlspecialchars($row['owner']); ?>"></td>
        </tr>
        <tr>
            <td><font size="2">Quantity: </font></td>
            <td><input type="text" name="quantity" size="4"   value="<?php echo $row['quantity']; ?>"></td>
        </tr>
        <tr>
            <td><font size="2">Reoccuring?</font></td>
            <td><input type="checkbox" name="reoccuring" value="yes"   <?php if($row['reoccuring'] == 1){echo "checked"; } ?>></td>
        </tr>
        <tr>
            <td height="23"><font size="2">Purchased already?</font></td>
            <td height="23">
            <input type="checkbox" name="purchased" value="yes" <?php if($row['purchased']== 1){echo "checked"; } ?>></td>
        </tr>
        <tr>
            <td colspan="2">
            <p align="center">
            <input type="submit" value="Add/Edit Item" name="submit"></td>
        </tr>
    </table>
    </form>
    <?php
    // get output buffer and then clean it up
    $output = ob_get_contents();
    ob_end_clean();

    return $output;
}

function SaveItem(){

    if(isset($_POST['itemid'])){
        // we are updating
        // using our custom db escape function
        $query = 'update `thelist` set ';
        $query .= " `name`='".db_escape($_POST['name'])."', ";
        $query .= " `owner`='".db_escape($_POST['owner'])."', ";
        $query .= " `quantity`='".(int)$_POST['quantity']."', ";

        if($_POST['reoccuring'] == 'yes'){
            $query .= " `reoccuring`='1', ";
        }else{
            $query .= " `reoccuring`='0', ";
        }

        if($_POST['purchased'] == 'yes'){
            $query .= " `purchased`='1' ";
        }else{
            $query .= " `purchased`='0' ";
        }

        $query .= " where itemid='".(int)$_POST['itemid']."' limit 1";

        if(mysql_query($query)){
            $output = '<b>Item updated successfully!</b><br/><br/>';
        }else{
            $output = '<b>An Error Occurred: ' . mysql_error() . '</b><br><br>';
        }

    }else{
        // we are adding

        // we are updating
        // using our custom db escape function
        $query = "insert into `thelist` (`name`,`owner`,`quantity`,`reoccuring`,`purchased`) values ";
        $query .= "('".db_escape($_POST['name'])."','".db_escape($_POST['owner'])."','".(int)$_POST['quantity']."'";

        if($_POST['reoccuring'] == 'yes'){
            $query .= "'1', ";
        }else{
            $query .= "'0', ";
        }

        if($_POST['purchased'] == 'yes'){
            $query .= "'1' ";
        }else{
            $query .= "'0' ";
        }

        $query .= " limit 1";

        if(mysql_query($query)){
            $output = '<b>Item added successfully!</b><br/><br/>';
        }else{
            $output = '<b>An Error Occurred: ' . mysql_error() . '</b><br><br>';
        }
    }
    // show the list
    $output .= ShowList();

    return $output;

}

function PrintList(){

    // this is a printer version, so we cna just echo it straight out
    $q = mysql_query("select * from thelist where purchased=0 order by owner asc");
    echo "<table border=0>";
    while($row = mysql_fetch_assoc($q)){
        echo "<tr>";
        echo "<td>";
        echo $row['quantity'] . " x";
        echo "</td><td>";
        echo $row['name'];
        if(!empty($row['owner'])){
            echo " for ".$row['owner'];
        }
        echo "</td></tr>";
    }
    echo "</table>";

    die();
}

function db_escape($string){
    if(function_exists('mysql_real_escape_string')){
        return mysql_real_escape_string($string);
    }else{
        return mysql_escape_string($string);
    }
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
  <title> The Interspire Shopping List </title>
  <link rel="shortcut icon" href="cart.png" type="image/png">
 </head>
<style>
body,table, td, th, tr {
font-family: georgia;
font-size: 10pt;
}
</style>
<script>
function checkDelete(name,itemid){
if(confirm('Are you sure you want to delete "'+ name +'" ?')){
    window.location = '?action=delete&itemid='+itemid;
}else{

}
}
</script>
<body>

<h1>The Shopping List - <?=$title?></h1>

<a href="<?=$_SERVER['PHP_SELF']?>">List Home</a>  |  <a href="?action=new">Add Item</a>  | <a href="?action=print">Printer Version</a><br><br>

<?=$content?>
</body>
</html>

项目名称:
摆脱这一行:

    $query .= " limit 1";

这将被添加到您的
INSERT
查询中,但这是无效的。

作为一般指南,您应该在开发过程中打印出每个查询,然后再尝试执行它。这样做几乎肯定会使您的问题变得显而易见,因此您将成为一名更好的开发人员/调试器


在这种特殊情况下,您会发现您的
插入到。。。值…
query有一个
limit
子句,即。

在尝试执行
exec
it之前打印出每个查询-您的问题会变得很明显,因此您将是一个更好的调试器。您的代码中有几个查询,哪个查询会出现错误?尝试使用
echo$query
查看完整查询的外观。看起来您正在将
LIMIT 1
添加到
INSERT
查询的末尾。在
INSERT
上也没有限制条款。必须不要使用mysql、使用mysqli或pdo以及准备好的语句