我在使用表单在数据库中发布数据时遇到问题。非常基本的东西,我很确定,但我很困惑:/
我可以使用select from database例程从我的数据库中获取东西。所以我知道与数据库的连接可能不是问题。
这是我的'upload.php':
<html>
<head>
<title>Upload</title>
</head>
<body>
<form action="action.php" method="post">
<fieldset class="first">
Name:
<input type="text" name="author" />
Heading:
<input type="text" name="heading" />
Text:
<textarea type="text" name="thecontent"> </textarea>
</fieldset>
<fieldset>
<input type="submit"/>
</fieldset>
</form>
</body>
</html>
这是我的'action.php':
<html>
<head>
<title>Send!</title>
</head>
<body>
<?php
ini_set('display_errors', 1); error_reporting(E_ALL);
$link = mysql_connect('localhost','name','pasword')
or die ("Unable to connect");
$mydb = mysql_select_db('the_database',$link)
or die ("No database found");
$author = $_POST['author'];
$heading = $_POST['heading'];
$thecontent = $_POST['thecontent'];
$mysql_query="INSERT INTO articles ('heading', 'author', 'content')
VALUES ('$heading','$author','$thecontent')" or die(mysql_error());
echo "This was send: $author $heading $thecontent <br> ";
mysql_close()
?>
</body>
</html>
非常感谢所有帮助!!
干杯, 齐格
感谢所有帮助人员!我正在尝试使用mysqli来插入数据但是它还没有工作这是我在action.php中的新代码:
<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>
我做错了什么? 非常感谢帮助!
干杯, 齐格
答案 0 :(得分:1)
您构建了 INSERT 字符串,但您从未调用过方法来使用它来重新 INSERT 数据库。
此外,旧的mysql_*
方法已被弃用,请改用PDO
或mysqli
API,请参阅http://www.php.net/manual/en/mysqlinfo.api.choosing.php
另请参阅stackoverflow帖子:mysqli or PDO - what are the pros and cons?
一些PDO
准备好的语句示例PDO
:http://www.php.net/manual/en/pdo.prepare.php
答案 1 :(得分:-1)
$mysql_query="INSERT INTO articles ('heading', 'author', 'content')
VALUES ('$heading','$author','$thecontent')";
mysql_query($mysql_query);
//required to run the query..
And mysql_close(); // missing :p