0

我是 PHP 新手,在这方面工作不多。我正在处理一个页面,我需要在提交按钮时将数据从文本框保存到表格中。但不知何故,按下按钮似乎没有任何效果。

以下是我的页面代码:

<?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>
4

3 回答 3

0
if($_POST['formSubmit'] == "Submit")

应该

if(isset($_POST['formSubmit']))
于 2013-05-11T06:32:01.523 回答
0

改变这个

if($_POST['formSubmit'] == "Submit") // this condition will never true

if($_POST['formSubmit'] == "Save")

或者

if(isset($_POST['formSubmit']))

dieexit从这里删除

echo "working"; //die;  // your insert query will never execute because you placed die here

$result = $conn->query("insert into category (name) values ('".$varCategory."')");
//exit;
于 2013-05-11T06:32:08.840 回答
-1

如果你有一个名为 auth.php 文件的连接类,带有函数 query() 那么你可以使用这个:

$sql = "insert into category(name) values('".$varCategory."')";
$conn->query($sql);
于 2013-12-24T14:00:27.023 回答