dpzlz08480
2018-06-25 12:05
浏览 39
已采纳

SQL语句不起作用,我做错了什么> [重复]

This question already has an answer here:

I'm creating an admin panel with a form that creates a blog post. When I try to insert this into the database, I'm getting an error. This is my SQL statement:

INSERT INTO blog_posts ('title', 'author', 'tags', 'category', 'body') VALUES ('$title', '$author', '$tags', '$category', '$body');

What am I doing wrong? Full code:

$servername = "host";
$username = "username";
$password = "password";
$dbname = "dbname";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}

$title = $_POST['title'];
$author = $_POST['author'];
$tags = $_POST['tags'];
$category = $_POST['category'];
$body = $_POST['editor1'];

// sql to insert a record

$sql = "INSERT INTO blog_posts ('title', 'author', 'tags', 'category', 'body') VALUES ('$title', '$author', '$tags', '$category', '$body');";

if (mysqli_query($conn, $sql)) {
  echo "Succes";
} else {
  echo "Error";
}

mysqli_close($conn);

So, what do I do now?

</div>

图片转代码服务由CSDN问答提供 功能建议

此问题已经存在 这里有一个答案:

  • 何时在MySQL中使用单引号,双引号和反引号 12 answers

    我正在创建一个 管理面板,其中包含可创建博客帖子的表单。 当我尝试将其插入数据库时​​,我收到一个错误。 这是我的SQL语句:

      INSERT INTO blog_posts('title','author','tags','category','body')VALUES('$ title'  ,'$ author','$ tags','$ category','$ body'); 
       
     
     

    我做错了什么? 全码:< / p>

      $ servername =“host”; 
     $ username =“username”; 
     $ password =“password”; 
     $ dbname =“dbname”; 
     \  n //创建连接
     $ conn = mysqli_connect($ servername,$ username,$ password,$ dbname); 
     //检查连接
    if(!$ conn){
     die(“连接失败:”。mysqli_connect_error  ()); 
    } 
     
     $ title = $ _POST ['title']; 
     $ author = $ _POST ['author']; 
     $ tags = $ _POST ['tags']; 
      $ category = $ _POST ['category']; 
     $ body = $ _POST ['editor1']; 
     
     // sql插入记录
     
     $ sql =“INSERT INTO blog_posts('title'  ,'作者','标签','类别','正文')VALUES('$ title','$ author','$ tags','$ category','$ body');“; 
     \  nif(mysqli_query($ conn,$ sql)){
     echo“Succes”; 
    } else {
     echo“Error”; 
    } 
     
    mysqli_close($ conn); 
      <  / pre> 
     
     

    那么,我现在该怎么办?< / p>

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanhunlou7051 2018-06-25 12:09
    已采纳
    1. You should not use single quote or double quote for table column field.
    2. End with two time semicolon

    Instead of:

    $sql = "INSERT INTO blog_posts ('title', 'author', 'tags', 'category', 'body') VALUES ('$title', '$author', '$tags', '$category', '$body');";
    

    Try this

     $sql = "INSERT INTO blog_posts (`title`, `author`, `tags`, `category`, `body`) VALUES ('$title', '$author', '$tags', '$category', '$body')";
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douping1993 2018-06-25 12:10

    One thing that is immediately obvious (as stated in comments), is your use of quotes around the field names, quotes are reserved for strings, not field/column names, so where you have;

    INSERT INTO blog_posts ('title', 'author', 'tags', 'category', 'body') VALUES 
    

    Should instead be;

    INSERT INTO blog_posts (`title`, `author`, `tags`, `category`, `body`) VALUES 
    

    The above uses backticks, you can discard these, but be aware of special words in MySQL

    Also to note - you are wide open to SQL injection attacks, make use of prepared statements, these are best, but there are a couple of other options;

    With this such as this, as per my comment, it is often appropriate (and helpful) to make use of some of the debugging tools provided to you.
    In this instance, mysqli_error would have been of great use to you (and in turn us) as it gives quite a good indication of what went wrong / where

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题