【问题标题】:What is causing the error in the selecting part of my script?是什么导致我的脚本选择部分出现错误?
【发布时间】:2012-09-28 06:37:41
【问题描述】:

我收到以下错误:错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的“country FROM countries WHERE idcountry=4 VALUES ('','Almelo')' 附近使用正确的语法

提前感谢您帮助我解决此问题,因为我现在尝试修复它 3 天。

我通过 $POST 从我的表单中获取 2 条数据。这是表格:

form action="inc/add/addcity.php" method="post">
Select country: 
$query="SELECT idcountry,country FROM countries ORDER BY country ASC";
$result = mysql_query ($query);
echo "<select name='countrybox' id='countrybox' value=''>Country</option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[idcountry]>$nt[country]</option>";}
echo "</select>";<br>

City to add: <input type="text" name="addcity" id="addcity"/>
<input type="submit" name="sumit" value"sumit"/>
</form>    

选项值来自我的数据库 现在是动作 php 文件

include 'addmysql.php';
$data = $_POST;
$citypost = $data['addcity'];
$cityselected = $data['countrybox'];
$cityselected1 = "SELECT country FROM countries WHERE idcountry=$cityselected";
$citytable = "cities";
$citytable .= "$cityselected1";
$sql= "INSERT INTO $citytable VALUES ('','$citypost')";

if (!mysql_query($sql))
  {
  die('Error: ' . mysql_error());
  } 
echo "<script type='text/javascript'>window.location='../../add.php'; </script>"

【问题讨论】:

    标签: mysql sql forms select syntax


    【解决方案1】:

    我认为这行:

    $citytable .= "$cityselected1";
    

    正在引起悲伤。 $citytable 最终将 SELECT 查询夹在 INSERT 中。这就是您看到的特定错误的原因。

    但是,我不确定我是否完全理解您希望此代码执行的操作。

    【讨论】:

    • $citytable1 = mysql_query($citytable); $sql= "插入 $citytable1 值 ('','$citypost')";给出:错误:您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'VALUES ('','Almelo')' 附近使用正确的语法,从而解决部分错误!
    • 尝试打印查询以及die 行中的错误。然后你会看到它有什么问题。
    • 经过 3 天的搜索,我找到了 sql: $sql = "INSERT INTO $citytable1 VALUES (default,'$citypost')";
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多