好的,现在我有一个运行Raspberry Pi的网络服务器,并且我有一个非常基本的表单,带有七个文本框。当我单击“提交”按钮时,我希望输入文本框中的值附加到数据库中。我有HTML代码来创建表单:
<!DOCTYPE html>
<html>
<head>
<title>Assignment Submission Form</title>
</head>
<body>
<form name="assi_subm" METHOD="POST" >
<p><label for="title">Title: </label><br><input id="title" name="title" type="text" size="25"></p>
<p><label for="password">Password: </label><br><input type="password" id="password" name="password" size="25" maxlength="20"></p>
<p><label for="soc">Statement of Contribution: </label><br><textarea style="width:300px;height:100px;" name="soc" id="soc"></textarea></p>
<p><label for="object">Project Objectives: </label><br><textarea style="width:300px;height:100px;" name="object" id="object"></textarea></p>
<p><label for="discuss">Review and Discussion of Technologies Used: </label><br><textarea style="width:300px;height:100px;" name="discuss" id="discuss"></textarea></p>
<p><label for="design">Design and Implementation: </label><br><textarea style="width:300px;height:100px;" name="design" id="design"></textarea></p>
<p><label for="references">References: </label><br><textarea style="width:300px;height:100px;" name="references" id="references"></textarea></p>
<p><input type="button" value="Submit"></p>
</form>
</body>
</html>
很好,可以按预期打开。但是,当我单击时,我无法使这些文本框中的数据实际附加到数据库中。我不能完全确定我是否打算使用PHP(我不认为我完全理解这种情况下的概念),但是我有以下代码试图通过检查以下内容将数据插入数据库中:按钮提交不为空?我不确定,我一直在尝试许多不同的事情,但此刻我只是一片空白,我真的很困惑,任何帮助都将不胜感激。这是我当前的PHP代码:
<?php
$con = mysql_connect("localhost", "root", "password") or die("Could not connect");
$database = mysql_select_db("assignment_submission", $con) or die("Could not do");
$title_IP = $_POST['title'];
$password_IP = $_POST['password'];
$soc_IP = $_POST['soc'];
$object_IP = $_POST['object'];
$discuss_IP = $_POST['discuss'];
$design_IP = $_POST['design'];
$references_IP = $_POST['references'];
if (!empty($_POST)){
mysql_query($database, "INSERT INTO file_data (title, password, soc, object, discuss, design, references) values ($title_IP, $password_IP, $soc_IP, $object_IP, $discuss_IP, $design_IP, $references_IP);
}
?>
您在值周围缺少双引号,在双引号和括号之间也有缺失。
您还使用了错误的变量$database
为您的插入,因为你正在使用你可以删除它mysql_
,而不是mysqli_
其中DB连接是强制性的。
另一件事是单词references
,它是保留单词,必须用反引号括起来。
`references`
替换为以下内容:
mysql_query("INSERT INTO file_data (title, password, soc, object, discuss, design, `references`) values ('$title_IP', '$password_IP', '$soc_IP', '$object_IP', '$discuss_IP', '$design_IP', '$references_IP')");
或者,您也可以使用:
if (!empty($_POST)){
$sql = "INSERT INTO file_data (title, password, soc, object, discuss, design, `references`) values ('$title_IP', '$password_IP', '$soc_IP', '$object_IP', '$discuss_IP', '$design_IP', '$references_IP')";
$query = mysql_query( $sql, $con );
if($query ){
echo "Success";
}
else{
die('Could not insert data: ' . mysql_error());
}
}
至少,请mysql_real_escape_string()
在POST变量周围使用。
即: $title_IP = mysql_real_escape_string($_POST['title']);
mysql_*
功能弃用通知:
http://www.php.net/manual/zh/intro.mysql.php
自PHP 5.5.0起不推荐使用此扩展,不建议编写新代码,因为将来会删除该扩展。相反,应使用mysqli或PDO_MySQL扩展名。另请参见MySQL API概述,以获取选择MySQL API时的更多帮助。
这些功能使您可以访问MySQL数据库服务器。有关MySQL的更多信息,请访问» http://www.mysql.com/。
可以在» http://dev.mysql.com/doc/中找到MySQL的文档。
开发期间
将错误报告添加到文件顶部
error_reporting(E_ALL);
ini_set('display_errors', 1);
这将发出发现错误的信号。
编辑
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$con = mysql_connect("localhost", "root", "password") or die("Could not connect");
$database = mysql_select_db("assignment_submission", $con) or die("Could not do");
if (isset($_POST['submit'])){
$title_IP = $_POST['title'];
$password_IP = $_POST['password'];
$soc_IP = $_POST['soc'];
$object_IP = $_POST['object'];
$discuss_IP = $_POST['discuss'];
$design_IP = $_POST['design'];
$references_IP = $_POST['references'];
$sql = "INSERT INTO file_data (title, password, soc, object, discuss, design, `references`) values ('$title_IP', '$password_IP', '$soc_IP', '$object_IP', '$discuss_IP', '$design_IP', '$references_IP')";
$query = mysql_query( $sql, $con );
if($query ){
echo "Success";
}
else{
die('Could not insert data: ' . mysql_error());
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Assignment Submission Form</title>
</head>
<body>
<form name="assi_subm" METHOD="POST" action="">
<p><label for="title">Title: </label><br><input id="title" name="title" type="text" size="25"></p>
<p><label for="password">Password: </label><br><input type="password" id="password" name="password" size="25" maxlength="20"></p>
<p><label for="soc">Statement of Contribution: </label><br><textarea style="width:300px;height:100px;" name="soc" id="soc"></textarea></p>
<p><label for="object">Project Objectives: </label><br><textarea style="width:300px;height:100px;" name="object" id="object"></textarea></p>
<p><label for="discuss">Review and Discussion of Technologies Used: </label><br><textarea style="width:300px;height:100px;" name="discuss" id="discuss"></textarea></p>
<p><label for="design">Design and Implementation: </label><br><textarea style="width:300px;height:100px;" name="design" id="design"></textarea></p>
<p><label for="references">References: </label><br><textarea style="width:300px;height:100px;" name="references" id="references"></textarea></p>
<p><input type="submit" name="submit" value="Submit"></p>
</form>
</body>
</html>
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句