if(isset($ _ POST ['add']))不起作用?

时间:2016-01-07 16:58:02

标签: php

<?php
session_start();
include_once 'dbconnect.php';
if (isset($_POST['add'])) {
    $username = ($_POST['username']);
    $phone = ($_POST['phone']);
    $email = ($_POST['email']);
    $password = ($_POST['password']);
    $address = ($_POST['address']);
    $studing = ($_POST['studing']);
    $birthday = ($_POST['birthday']);
    $stage = ($_POST['stage']);
    $college = ($_POST['college']);
    $department = (['department']);
    if (mysql_query("INSERT INTO de (username,phone,email,password,address,studing,birthday,stage,college,department)
                VALUES ('$username','$phonenumber','$email','$password','$address','$studing','$birthday','$stage','$college','$department')")) { ?>
        <script>alert('success ...');</script>
    <?php } else { ?>
        <script>alert('error ...');</script>
    <?php }
}
$res = mysql_query("SELECT * FROM users WHERE user_id=" . $_SESSION['user']);
$userRow = mysql_fetch_array($res);
?>
<html>
<head><title> welcome</title>
    <link rel="stylesheet" href="style.css" type="text/css"/>
</head>
<body>
<div id="header">
    <div id="left"><label>WELCOME</label></div>
    <div id="right">
        <div id="content"> hi <?php echo $userRow['username']; ?>&nbsp;<a href="logout.php?logout">Sign Out</a></div>
    </div>
</div>
<table align="left" width="30%" border="0">
    <tr>
        <td>name</td>
        <td><input type="text" name="usernamr" placeholder="stud name" required/></td>
    </tr>
    <tr>
        <td> phone number</td>
        <td><input type="text" name="phonenumber" placeholder="phone number" required/></td>
    </tr>
    <tr>
        <td>email</td>
        <td><input type="text" name="email" placeholder="email of stud" required/></td>
    </tr>
    <tr>
        <td>password</td>
        <td><input type="text" name="password" placeholder="password of stud" required/></td>
    </tr>
    <tr>
        <td>address</td>
        <td><input type="text" name="address" placeholder="address" required/></td>
    </tr>
    <tr>
        <td>studing</td>
        <td><input type="text" name="studing" placeholder="studing" required/></td>
    </tr>
    <tr>
        <td>birthday</td>
        <td><input type="text" name="birthday" placeholder="birthday" required/></td>
    </tr>
    <tr>
        <td>stage</td>
        <td><input type="text" name="stage" placeholder="stage" required/></td>
    </tr>
    <tr>
        <td>college</td>
        <td><input type="text" name="college" placeholder="college" required/></td>
    </tr>
    <tr>
        <td>department</td>
        <td><input type="text" name="department" placeholder="department" required/></td>
    </tr>
    <tr>
        <td>
            <button type="submit" name
            "add">ADD</button></td>
</table>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您的按钮名称属性未正确设置

<button type="submit" name
            "add">ADD</button></td>

像这样:

<button type="submit" name="add">ADD</button></td>

此外,表单元素周围没有表单标记:

<form action="" method="post">       //set action to the PHP file
//your table goes here

</form>

除此之外,请确保您的名称属性与post数组键相同..例如:

$username = ($_POST['username']);

无法获得以下内容的价值:

<input type="text" name="usernamr" placeholder="stud name" required/>

因为name username = usernamr ..