【问题标题】:PHP form is not submitting values to databasePHP表单未将值提交到数据库
【发布时间】:2013-10-15 15:46:22
【问题描述】:

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

 <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 中的验证代码工作正常,但表单提交的代码根本不工作.. 提交.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,但它停留在同一页面上

【问题讨论】:

  • 你遇到了什么错误?
  • mysql_* 函数已弃用。请查看 PDO:php.net/manual/fr/ref.pdo-mysql.php
  • 所有提交的值都在 submit.php 上吗?
  • 插入中的id 字段为空;对吗?
  • 您已经检查过该页面正在被访问,对吗?顶部有一个简单的回声来确认这一点。

标签: php html mysql forms


【解决方案1】:

你能像这样写 $date 变量吗:

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

我看不到任何重定向代码,所以你可以写下面的代码

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

希望这能解决您的问题

谢谢

【讨论】:

  • 代码 echo msg(1,"registered.html");如果我删除 sql 查询就可以工作
  • 日期变量是问题...thanx 为您的帮助...thanku alok
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-03
  • 2016-03-24
相关资源
最近更新 更多