dsafq2131321 2014-01-25 21:58
浏览 75
已采纳

mySql query.Unknown列''in'字段列表'出错

I have a database with 4 columns Date,to,message,from one of date format and all varchar. Following is the error that i am getting:

Error: Database Error Unknown column 'anything i enter into the form's first field' in 'field list'. Here is my code:

form:

<form method='post' name:mail>
<label>
<p>
  Send to Username:<p>
  </label>
    <input type="text" name="user" hint="Enter username" id="user"   placeholder = "Name" >


  </label><p>
  Message:
  <p>
    <label>

<textarea   name="message" cols="40" rows="5">

</textarea><br>
<input type="submit" name="submit" id="submit"  value="Send" />

I know i should not have used $POST_[] directly into the query but i am just testing it.

php code:

<?php

$con = mysqli_connect("localhost","","","");
if (!$con)
  {
     echo" Not connected to database";
  die('Could not connect: ' . mysqli_error());
  }

  if(isset($_POST['submit']))
{
$username1=$_SESSION["username"];
$sql = "INSERT INTO anengine_dbase.mail(`Date`,`to`,`message`,`from`) VALUES (CURDATE(),`$_POST[user]`, `$_POST[message]`,`$username1`)";

$xy=mysqli_query($con,$sql);
if (!$xy)
  {
  die('Database Error ' . mysqli_error($con));
  }
echo "message successfully recorded ";

}

?>
  • 写回答

2条回答 默认 最新

  • duanpi5733 2014-01-25 22:05
    关注

    This is your SQL statement:

    INSERT INTO anengine_dbase.mail(`Date`,`to`,`message`,`from`)
        VALUES (CURDATE(),`$_POST[user]`, `$_POST[message]`,`$username1`);
    

    Unless the values are all numbers, you will have a problem. In other words, you need single quotes rather than backticks for the values:

    INSERT INTO anengine_dbase.mail(`Date`, `to`, `message`, `from`)
        VALUES (CURDATE(), '$_POST[user]', '$_POST[message]', '$username1');
    

    By the way, as a general rule, it is a good idea to avoid using reserved words (such as to and from) as the names of objects in the database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型