0

我正在尝试使用 mysqli 将表单中的数据插入数据库。但是我没有让它工作:/

这是您填写表格后进入的页面中的代码。表单不是问题,因为变量 $headin $author 和 $thecontent 中都有数据。并且在真实代码数据库中用户名密码和名称具有真实值:)

<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>
4

1 回答 1

1

您不能'在行名称上添加单引号,并且必须为 INSERT 添加双引号:

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

还要转义你的字符串:

$author = $db->real_escape_string($_POST['author']); 
$heading = $db->real_escape_string($_POST['heading']);
$thecontent = $db->real_escape_string($_POST['thecontent']);
于 2012-10-04T08:59:09.427 回答