我在数据库中有一个表疾病,其中包含 diid , diseasename 和 descrption 列称为 hcp 。
我想简单地从HTML表单输入;到目前为止,这是我的代码。
<html><head><title>Disease Inssert</title></head>
<body>
<form action="diseaselist.php" method="post">
Disease Name : <input type="text" name="txtDiseaseName" id="textbox" /><br />
Description : <input type="text" name="txtDiseaseDescription" id="textbox" /><br />
<input type="submit" name="btnDiseaseSubmit" id="button"/>
</form>
</body>
</html>
<?php
mysql_connect("localhost","root","");
mysql_select_db("hcp");
if($_REQUEST['btnDiseaseSubmit'])
{
$queryDiseaseInsert="INSERT INTO disease (`diseasename` ,`description`) VALUES ('".$_REQUEST['txtDiseaseName']."', '".$_REQUEST['txtDiseaseDescription']."');";
$resultDI=mysql_query($queryDiseaseInsert) or die(mysql_error());
}
?>
提交后,页面重定向到diseaselist.php
,但数据未保存在数据库中。
有人可以看到我的查询有什么问题吗?
答案 0 :(得分:2)
表单直接提交到列表<form action="diseaselist.php" method="post">
。
永远不会执行保存到数据库的代码。
你可以创建一个中间页面,假设process.php
包含添加到数据库的代码:
<?php
mysql_connect("localhost","root","");
mysql_select_db("hcp");
if($_REQUEST['btnDiseaseSubmit'])
{
$queryDiseaseInsert="INSERT INTO disease (`diseasename` ,`description`) VALUES ('". mysql_real_escape_string($_REQUEST['txtDiseaseName'])."', '".mysql_real_escape_string($_REQUEST['txtDiseaseDescription'])."');";
$resultDI=mysql_query($queryDiseaseInsert) or die(mysql_error());
// insert is complete, redirect to list
header("location: diseaselist.php");
} else {
// the form was not submitted
// redirect back to the form or show error message
}
?>
的变化:
现在,在您当前的文件中,只保留html代码并更新action
属性以将表单提交到process.php
脚本:
<html><head><title>Disease Inssert</title></head>
<body>
<form action="process.php" method="post">
Disease Name : <input type="text" name="txtDiseaseName" id="textbox" /><br />
Description : <input type="text" name="txtDiseaseDescription" id="textbox" /><br />
<input type="submit" name="btnDiseaseSubmit" id="button"/>
</form>
</body>
</html>
的变化:
action
属性更新为process.php
答案 1 :(得分:2)
您的脚本存在一些问题。首先,在尝试插入数据之前,您没有检查数据。 HTML表单的典型流程如下:
现在已经说了上述内容,请点击此处:
<?php
$mysql = mysql_connect("localhost","root",""); // assign to variable
mysql_select_db("hcp", $mysql);
// Get into the habit of using $_POST and $_GET instead of $_REQUEST
if(isset($_POST['btnDiseaseSubmit']) && !empty($_POST['btnDiseaseSubmit']))
{ // Form was completely submitted
$sql = "INSERT INTO disease (`diseasename` ,`description`) VALUES ('".$_POST['txtDiseaseName']."', '".$_POST['txtDiseaseDescription']."');"; // No need for massive variable names
$resultDI = mysql_query($sql, $mysql) or die(mysql_error());
mysql_close($mysql); // Close database connection - cleans things up
echo "Successfolly ran database query!";
} else { // Form was not completely submitted, if at all - display form.
?>
<html>
<head><title>Disease Inssert</title></head>
<body>
<!-- If action is not present, the form will submit to the current page. ie. If current page is a.php and we leave out action, the form will submit to a.php! -->
<form method="post">
Disease Name : <input type="text" name="txtDiseaseName" id="textbox" /><br />
Description : <input type="text" name="txtDiseaseDescription" id="textbox" /><br />
<input type="submit" name="btnDiseaseSubmit" id="button"/>
</form>
</body>
</html>
<?php
}
?>
这应该对你有用。请注意代码中的注释,以便您知道我做了什么以及为什么我这样做了。谷歌周围有更多的例子。很快就会很清楚。
答案 2 :(得分:0)
diseaselist.php
页面上尝试使用以下代码
<?php
mysql_connect("localhost", "root", "");
mysql_select_db("hcp");
if (isset ($_REQUEST['btnDiseaseSubmit'])) {
$queryDiseaseInsert = "INSERT INTO disease ('diseasename' , 'description') VALUES('" . $_REQUEST['txtDiseaseName'] . "','" . $_REQUEST['txtDiseaseDescription'] . "')";
$resultDI = mysql_query($queryDiseaseInsert) or die(mysql_error());
}
?>