2013-12-16 02:20
浏览 70


Recently I've given a task to create a data inquiry system. The system required user to fill in the form. When it is submitted, the data will be validated by the admin and connected to the database.

This is my form.html:

<!DOCTYPE html>
    <meta charset="utf-8">
    <link rel="stylesheet" media="screen" href="styles.css" >
<body bgcolor="#13b4ff">

<div id="header" style="background-color: #4169E1;"><hr>

<form class="form" action="submit.php" method="post" name="form" >

         <span class="required_notification">* Denotes Required Field</span>
        <label for="name">Name:</label>
        <input type="text" name="name" required />
        <label for="position:">Jawatan:</label>
        <input type="text" name="position" />
        <label for="unit">Unit:</label>
        <input type="text" name="unit" required />
        <label for="institute">Institute:</label>
        <input type="text" name="institute" required />
        <label for="telefon">No. Telefon:</label>
        <input type="number" name="telefon" placeholder="eg: 012-345-6789" required />
        <label for="faks">No. Faks:</label>
        <input type="number" name="faks" placeholder="eg: 03-12345678" />
        <label for="email">E-mail:</label>
        <input type="email" name="name" placeholder=""/ required>
    <span class="form_hint">proper format<script type="text/javascript">
/* <![CDATA[ */
(function(){try{var s,a,i,j,r,c,l,b=document.getElementsByTagName("script");l=b[b.length-1].previousSibling;a=l.getAttribute('data-cfemail');if(a){s='';r=parseInt(a.substr(0,2),16);for(j=2;a.length-j;j+=2){c=parseInt(a.substr(j,2),16)^r;s+=String.fromCharCode(c);}s=document.createTextNode(s);l.parentNode.replaceChild(s,l);}}catch(e){}})();
/* ]]> */
        <label for="data">Data Required:</label>
        <input type="text" name="data" required/>
        <label for="purpose">Purpose:</label>
        <input type="text" name="purpose" required/>
    <button class="submit" type="submit">Submit</button>

This is the styles.css for the form:

.form { 
margin: 0 auto; 
height: 825px;

body {font: 14px/21px "Lucida Sans", "Lucida Grande", "Lucida Sans Unicode", sans-serif;}
.form h2, .form label {font-family:Georgia, Times, "Times New Roman", serif;}
.form_hint, .required_notification {font-size: 11px;}

.form ul {
.form li{
    border-bottom:1px solid #eee;

.form li:first-child, .form li:last-child {
    border-bottom:2px solid #777;

.form h2 {
    display: inline;
.required_notification {
    margin:5px 0 0 0;

.form label {
    margin-top: 3px;
.form input {
    padding:5px 8px;
.form textarea {padding:8px; width:300px;}
.form button {margin-left:156px;}

.form input, .form textarea {
    border:1px solid #aaa;
    box-shadow: 0px 0px 3px #ccc, 0 10px 15px #eee inset;
    -moz-transition: padding .25s;
    -webkit-transition: padding .25s;
    -o-transition: padding .25s;
    transition: padding .25s;
.form input:focus, .form textarea:focus {
    background: #fff;
    border:1px solid #555;
    box-shadow: 0 0 3px #aaa;

button.submit { 
margin: 250 auto; 

button.submit {
    background-color: #2F68B1;
    background: -webkit-gradient(linear, left top, left bottom, from(#2F68B1), to(#10468B));
    background: -webkit-linear-gradient(top, #2F68B1, #10468B);
    background: -moz-linear-gradient(top, #2F68B1, #10468B);
    background: -ms-linear-gradient(top, #2F68B1, #10468B);
    background: -o-linear-gradient(top, #2F68B1, #10468B);
    background: linear-gradient(top, #2F68B1, #10468B);
    border: 1px solid #2C6BB8;
    border-bottom: 1px solid #5b992b;
    border-radius: 3px;
    -webkit-border-radius: 3px;
    -moz-border-radius: 3px;
    -ms-border-radius: 3px;
    -o-border-radius: 3px;
    box-shadow: inset 0 1px 0 0 #9fd574;
    -webkit-box-shadow: 0 1px 0 0 #9fd574 inset ;
    -moz-box-shadow: 0 1px 0 0 #9fd574 inset;
    -ms-box-shadow: 0 1px 0 0 #9fd574 inset;
    -o-box-shadow: 0 1px 0 0 #9fd574 inset;
    color: white;
    font-weight: bold;
    padding: 7px 20px;
    text-align: center;
    text-shadow: 0 -1px 0 #396715;
button.submit:hover {
    cursor: pointer;
button.submit:active {
    border: 1px solid #20911e;
    box-shadow: 0 0 10px 5px #356b0b inset;
    -webkit-box-shadow:0 0 10px 5px #356b0b inset ;
    -moz-box-shadow: 0 0 10px 5px #356b0b inset;
    -ms-box-shadow: 0 0 10px 5px #356b0b inset;
    -o-box-shadow: 0 0 10px 5px #356b0b inset;

input:required, textarea:required {
    background: #fff url(asterisk.jpg) no-repeat 98% center;

/*invalid, valid*/
.form input:focus:invalid, .form textarea:focus:invalid { /* when a field is considered invalid by the browser */
    background: #fff url(invalid.jpg) no-repeat 98% center;
    box-shadow: 0 0 5px #d45252;
    border-color: #b03535

.form input:required:valid, .form textarea:required:valid { /* when a field is considered valid by the browser */
    background: #fff url(valid.jpg) no-repeat 98% center;
    box-shadow: 0 0 5px #5cd053;
    border-color: #28921f;

.form_hint {
    background: #d45252;
    border-radius: 3px 3px 3px 3px;
    color: white;
    padding: 1px 6px;


And the form will be transfered to submit.php (I did the database using mysql). I use wampserver.


    //debug mode
    ini_set('display_errors', 'On');

    //to show some error is smthng went wrong
    $errors = array();

    function connect(){
        $connection = mysql_connect(localhost, root, "" ); //I have no idea what the password is. where could I set one?
        $db = mysql_select_db(permohonan_data,$connection);

    if (!$connection || !$db){
        return false;
        return true;

echo "first stage";

    //will run if user did submit the form
    if (!empty($_POST)){

echo "second stage";

    //connect sql server:
    if (!connect()){
        $errors[] = "Can't establish link to MySql server";

        $name = $_POST['name'];
        $position = $_POST['position'];
        $unit = $_POST['unit'];
        $institute = $_POST['institute'];
        $telefon = $_POST['telefon'];
        $faks = $_POST['faks'];
        $email = $_POST['email'];
        $data = $_POST['data'];
        $purpose = $_POST['purpose'];

echo "third stage";

    //no error til here
    if (empty($error)){

    //prevent SQL injection
    $name = mysql_real_escape_string($name);    
    $position = mysql_real_escape_string($position);
    $unit = mysql_real_escape_string($unit);
    $institute = mysql_real_escape_string($institute);
    $telefon = mysql_real_escape_string($telefon);
    $faks = mysql_real_escape_string($faks);
    $email = mysql_real_escape_string($email);
    $data = mysql_real_escape_string($data);
    $purpose = mysql_real_escape_string($purpose);


echo "fourth stage";

    //try insert value
    $query = "INSERT INTO 'user'
        VALUES ('$name', '$position', '$unit', '$institute', '$telefon', '$faks', '$email', '$data', '$purpose')";

    if (!mysql_query($query)){
        $errors[] = "Can't insert the values";
    else {
        //on success




Which the result gives no errors, I've put the reporting error syntax, but also nothing is displayed to the screen. The script are still visual at the page source though. And when I put the echo part, the script just runs until "first stage". It didn't redirect to thankyou.php. What did I do wrong? And how can I connect this to the database? I did the $connect part, but it still vague to me. please help.

This is the thankyou.php:

    <meta charset="utf-8">
    <title>thank you!</title>
    <link rel="stylesheet" media="screen" href="stylesphp.css" >
<body bgcolor="#13b4ff">
        <div class="boxed";>
        <div id="thankyou" style= "text-align:center; font-size:50px;">THANK YOU</div><br>
        &nbsp;Your inquiry has been submitted. Please wait 24 hours. <br>
        &nbsp;You will be notify by email.
        <p>&nbsp;return to <a class="a" href="" target="_self">main page.</a>

EDIT, thanks to Fred -ii- and Joni Salmi, I have fix the error.

but it shows this instead:

Notice: Undefined index: email in C:\wamp\www\FORM\submit.php on line 39.

this is line 39

$email = $_POST['email'];
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • douxiong4892
    douxiong4892 2013-12-16 02:34

    As per your original question

    $_POST is a "superglobal" and MUST be in uppercase.

    You have:

    if (!empty($_post)){

    Change it to:

    if (!empty($_POST)){

    That seems to be the most likely part of the problem.

    As far as I can tell, the rest of your code seems OK to me, besides the fact that you're using a deprecated mysql_ function.


    As Joni Salmi pointed out in a comment,

    you are using msql_connect which should be mysql_connect

    Credit to Joni (Salmi)


    $connection = msql_connect(localhost, root, "" );
    missing "y" => ^


    $connection = mysql_connect(localhost, root, "" );

    Also as per your comment about Undefined index: email error message.

    Change this line:

    <input type="email" name="name" placeholder=""/ required>


    <input type="email" name="email" placeholder=""/ required>

    You had:

    <label for="name">Name:</label>
    <input type="text" name="name" required />

    with the same named input field of name="name" as your email input.

    Edit #2

    Use backticks instead of quotes for your table name.

    You have:

    $query = "INSERT INTO 'user'

    Use (backticks):

    $query = "INSERT INTO `user`

    You can even remove the quotes: (but backticks are better)

    $query = "INSERT INTO user
    点赞 评论
  • drll85318
    drll85318 2013-12-17 10:43

    RE: Undefined index:

    You have incorrectly named the input fied that is to hold the email address in the HTML

    <input type="email" name="name" placeholder=""/ required>

    Change it to

     <input type="email" name="email" placeholder=""/ required>

    Note the name="email" The names you give the fields in HTML control the fieldnames used in the $_POST array.

    点赞 评论