简体   繁体   中英

A form page to submit data to table in database in PHP

I am new to PHP and haven't worked much on it. I am working on a page where I need to save data from a textbox to a table on submit of a button. But somehow nothing seems to be working on press of button click.

Following is my code for the page:

<?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($_POST['formSubmit'] == "Submit")

应该

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

Change this

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

to

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

OR

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

Also remove die and exit from here

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

$result = $conn->query("insert into category (name) values ('".$varCategory."')");
//exit;

if you have a connection class named auth.php file with function query() then you can use this:

$sql = "insert into category(name) values('".$varCategory."')";
$conn->query($sql);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM