duandanbeng1829 2012-10-04 08:57
浏览 22
已采纳

使用mysqli将输入放在数据库中

I'm trying to use mysqli to insert data from a form into a database. However I'm not getting it to work :/

This is my code from the page you get to after you filled in the form. The form is not the problem because the variables $headin $author and $thecontent all have data in them. And in the real code database username password and name have real values :)

<html>
<head>

<title>Send!</title>
</head>

<body>

<?php

 ini_set('display_errors', 1); error_reporting(E_ALL); 
$DB_HOST = 'localhost';
$DB_USER = '**';
$DB_PASS = '***';
$DB_NAME = '***';
@ $db = new mysqli($DB_HOST, $DB_USER, $DB_PASS, $DB_NAME);
if (mysqli_connect_errno()) {
echo 'Error.';
exit();
}

$author = $_POST['author']; 
$heading = $_POST['heading'];
$thecontent = $_POST['thecontent'];

$query = 'INSERT INTO articles ('heading', 'author', 'content')
 VALUES ('$heading','$author','$thecontent')';   

$result = $db->query($query);
    if ($result) {
    echo $db->affected_rows."This was added.";
    } 
    else {
    echo "somethings gone very wrong.";
    }

$db->close();


?> 

</body>
</html>
  • 写回答

1条回答 默认 最新

  • doupingmao1903 2012-10-04 08:59
    关注

    You cannot add single quotes ' on row names and you have to add double quotes for INSERT:

    $query = "INSERT INTO articles (`heading`, `author`, `content`)
     VALUES ('$heading','$author','$thecontent')"; 
    

    Also escape your strings:

    $author = $db->real_escape_string($_POST['author']); 
    $heading = $db->real_escape_string($_POST['heading']);
    $thecontent = $db->real_escape_string($_POST['thecontent']);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?