PHP表单未向数据库提交值

时间:2013-10-15 07:05:55

标签: php html mysql forms

朋友我需要你的帮助..表单验证工作正常...但表单没有向数据库提交值 这是表格

 <form action="submit.php" method="post" id="regForm">
    <input id="fname" placeholder="First name" type="text" name="fname" />
    <input id="lname" placeholder="Last name" type="text" name="lname" />
    <input id="email" placeholder="E-mail" type="text" name="email" />
    <input id="email" placeholder="Password" type="password" name="pass"/>
    <div id="email">Birthday<input id="dat" type="text" placeholder="DD" name="day" maxlength="2" onkeypress="return yr(event)"/>/<input id="dat" type="text" maxlength="2" placeholder="MM" name="month" onkeypress="return yr(event)"/>/<input id="dat" maxlength="4" type="text" placeholder="YYYY" name="year" onkeypress="return yr(event)"/>
    </div>
    <div class="radio"><input type="radio" name="sex" value="Male">Male</input></div><div class="radio"><input type="radio" name="sex" value="Female">Female</input></div><div class="radiol"><input type="radio" name="sex" value="Others">Others</input></div>
    <input id="reg1" type="submit" value="Register" name="reg" />
    </form>
submit.php中的

验证代码工作正常,但表单提交的代码根本不起作用.. submit.php

<?php $con=mysql_connect('localhost','root',''); 
  if(!$con)
  {
die('could not connect'.mysql_error());
  }

  mysql_select_db("test",$con);

  // declared variables
  $fn=$_POST['fname'];
  $ln=$_POST['lname'];
  $em=$_POST['email'];
  $pswd=$_POST['pass'];
  $date=new DateTime($_POST['year'].'-'.$_POST['month'].'-'.$_POST['day']);
  $sex=$_POST['sex'];

  // we check if everything is filled in

  if(empty($fn) || empty($ln) || empty($em) || empty($pswd))
  {
die(msg(0,"All the fields are required"));
  }

  // is the birthday selected?

 if (!@checkdate($_POST['month'],$_POST['day'],$_POST['year'])) {
 die(msg(0,"Please fill correct birthday"));
  }

 // is the sex selected?

 if(!$sex)
  {
die(msg(0,"Please select your gender"));
   }


  // is the email valid?

  if(!(preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/",    $_POST['email'])))
{die(msg(0,"You haven't provided a valid email"));}



    //Check whether Email already exists in the database
    $e_check = mysql_query("SELECT email FROM registeration WHERE email='$em'");
    //Count the number of rows returned
    $email_check = mysql_num_rows($e_check);
    if($email_check > 0){die(msg(0,"This email is already used"));}
    // check the maximum length of password
    if (strlen($pswd)<5) {die(msg(0,"Please provide a longer password"));}
    //encrypt password and password 2 using md5 before sending to database
    $pswd = md5($pswd);
    $qu = mysql_query("INSERT INTO registeration (id,fname,lname,email,password,birthday,sex,activated) VALUES ('','$fn','$ln','$em','$pswd','$date','$sex','0')");
    if(!$qu) {
        die('could not connect'.mysql_error());
        }
        echo msg(1,"registered.html");

    function msg($status,$txt)
    {
return '{"status":'.$status.',"txt":"'.$txt.'"}';
    }
   ?>

成功提交表单后,用户应该在registered.html上重定向,但它仍然在同一页面上

1 个答案:

答案 0 :(得分:1)

请你写这样的$ date变量:

$date=$_POST['year'].'-'.$_POST['month'].'-'.$_POST['day'];

我无法看到任何重定向代码,因此您可以在下面编写代码

echo msg(1,"registered.html");
echo "<script>window.location='registered.html'</script>"; 

希望这能解决您的问题

由于

相关问题