在PHP中向数据库中的表提交数据的表单页
我是PHP新手,对它没有太多的研究。我在一个页面上工作,我需要在提交按钮时将数据从一个文本框保存到一个表中。但不知何故,按一下按钮似乎什么也不起作用 以下是我的页面代码:在PHP中向数据库中的表提交数据的表单页,php,forms,submit,Php,Forms,Submit,我是PHP新手,对它没有太多的研究。我在一个页面上工作,我需要在提交按钮时将数据从一个文本框保存到一个表中。但不知何故,按一下按钮似乎什么也不起作用 以下是我的页面代码: <?php require_once('auth.php');?> <?php if(isset($_POST['formSubmit'])) { $errorMessage = ""; if(empty($_POST['category'])){ $errorMessage
<?php
require_once('auth.php');?>
<?php
if(isset($_POST['formSubmit']))
{
$errorMessage = "";
if(empty($_POST['category'])){
$errorMessage .= "<li>You forgot to enter a Category!</li>";
}
$varCategory = $_POST['category'];
if(empty($errorMessage)) {
require_once('config.php');
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE)
or die ('Cannot connect to db');
$result = $conn->query("insert into category (name) values ('".$varCategory."')");
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Member Index</title>
<link href="loginmodule.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Categories</h1><br>
<table>
<tr><td><a href="member-profile.php">My Profile</a> | <a href="logout.php">Logout</a></td></tr>
<tr><td>Welcome <?php echo $_SESSION['SESS_FIRST_NAME'];?></td></tr>
<tr><td><table>
<tr><td>Category</td>
<td>
<form action="member-index.php" method="post">
<input type="text" name="category" maxlength="50" value="<?=$varCategory;?>" />
<input type="submit" name="formSubmit" value="Save" />
</form>
</td>
</tr>
<tr>
<td>
<?php
require_once('config.php');
$conn = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_DATABASE)
or die ('Cannot connect to db');
$result = $conn->query("select catid, name from category");
echo "<select name='id'>";
while ($row = $result->fetch_assoc()) {
unset($id, $name);
$id = $row['catid'];
$name = $row['name'];
echo '<option value="'.$id.'">'.$name.'</option>';
}
echo "</select>";
?>
</td>
<td><a href="member-profile.php">Delete</a></td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
应该是
if(isset($_POST['formSubmit']))
改变这个
if($_POST['formSubmit'] == "Submit") // this condition will never true
到
或
同时从此处移除模具
和退出
echo "working"; //die; // your insert query will never execute because you placed die here
$result = $conn->query("insert into category (name) values ('".$varCategory."')");
//exit;
如果您有一个名为auth.php file的连接类,其函数为query(),则可以使用:
$sql = "insert into category(name) values('".$varCategory."')";
$conn->query($sql);
我很抱歉我的整个代码都不可见谢谢imsiso的编辑谢谢Yogesh第一行是我的错误,我为一些测试添加了die。我已经更正了我的代码,现在它运行良好。再次感谢您,还有一个问题,在保存数据后,我将如何清空文本框
if(isset($_POST['formSubmit']))
echo "working"; //die; // your insert query will never execute because you placed die here
$result = $conn->query("insert into category (name) values ('".$varCategory."')");
//exit;
$sql = "insert into category(name) values('".$varCategory."')";
$conn->query($sql);