简体   繁体   中英

mysql php crud problem

Can anybody tell me why is the record not inserted? This is my one page :-

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>


        <form action="Adduser.php" method="post">

        <center>
        <table>
            <tr>
                <td>
                    Username :
                </td>
                <td>
                    <input type="text" id="username" />
                </td>
            </tr>

            <tr>
                <td>
                    Password :
                </td>
                <td>
                    <input type="text" id="password"/>
                </td>
            </tr>

            <tr>
                <td>
                    Email Address :
                </td>
                <td>
                    <input type="text" id="emailaddress"/>
                </td>
            </tr>

            <tr>
                <td>
                    Address :
                </td>
                <td>
                    <input type="text" id="Address"/>
                </td>
            </tr>
            <tr>
                <td colspan="2">
                    <input type="Submit" name="submit" value="Add"/>
                </td>
            </tr>

        </table>
        </center>
        <?php
                     mysql_connect('localhost', 'root', '');
            mysql_select_db('user');
            $query = mysql_query("Select * from tbluser");
            echo "<center>";
            echo '<table style="border:solid 2px black;">';
            while(($row = mysql_fetch_array($query)) != NULL) {
                echo '<tr>';
                echo '<td>' . $row['UserName'] . '</td>';
                echo '<td>' . $row['Password'] . '</td>';
                echo '<td>' . $row['EmailAddress'] . '</td>';
                echo '<td>' . $row['Address'] . '</td>';
                echo '</tr>';
            }
            echo '</table>';
            echo "</center>";
        ?>
            </form>
    </body>
</html>

Problem lies here (this is Adduser.php) :-

The if condition never gets executed. Can anybody tell me why is it not working? The $_POST array contains nothing when printed via var_dump

You have to use name attribute for each input fields like

<input type="text" id="username" name="username" />

Thanks

Is this the code that is on Adduser.php page? If it is a different page, when a user clicks submit button the Adduser.php page will receive the http request.

Not that you need to use name attribute on inputs. So when this form above is submitted it will receive a http request containing $_POST['submit'], $_POST['username'], $_POST['password']. Which is why at top of the form I first check to see if the submit button was pretty by using PHP::isset() function.

IE.

<?php
if(isset($_POST['submit'])){
//process post submission
//perform mysql insert and notify user the result status
}else{
//initialize form values
$_POST['userName'] = '';
$_POST['password'] = '';
}
?>
<html>
<body>
<form action="<?=$_SERVER['PHP_SELF']?>">
<input type="text" name="username" value="<?=$_POST['userName']?>" />
<input type="text" name="password" value="<?=$_POST['password']?>" />
<input type="submit" name="submit" value="Add user"/>
</form>
</body>
</html>

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