这是我的表格:
<form method="post" action="insert.php">
<label>Download Name:<br /><input type="textbox" id="name" name="name"></label><br /><br />
<label>Download Description:<br /><textarea rows="3" id="description" name="description"></textarea></label><br /><br />
<label>Category:<br /><input type="textbox" id="category" name="category"><br/></label><br /><br />
<label>Download Link:<br /><input type="textbox" id="link" name="link"><br/></label><br /><br />
<input type="submit" id="submit" name="submit" value="Insert"/>
这是我的insert.php:
<?php
$con=mysqli_connect("host","user","pass","db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$name = mysqli_real_escape_string($con, $_POST['name']);
$description = mysqli_real_escape_string($con, $_POST['description']);
$category = mysqli_real_escape_string($con, $_POST['category']);
$link = mysqli_real_escape_string($con, $_POST['link']);
$sql="INSERT INTO test (name, description, category, link)
VALUES ('$name', '$description', '$category', '$link')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
这是我的sql:
CREATE TABLE test
(
name varchar(255) NOT NULL,
description varchar(255),
category varchar(255),
link varchar(255)
)
正在发生的是用户填写表单。输入发送到insert.php,它连接到数据库并将其插入数据库。
发生的事情是它连接成功,但我的记录是空的。
感谢您的帮助。
答案 0 :(得分:0)
$sql = 'INSERT INTO test (name, description, category, link)
VALUES ("'.$name.'", "'.$description.'", "'.$category.'", "'.$link.'")';
$result = mysqli_query($con, $sql);
if (!$result)
die('Error: '.mysqli_error($con));
else
{
echo '1 record added';
mysqli_close($con);
}
这个怎么样?