将表单插入数据库

时间:2013-07-04 09:45:28

标签: php forms validation

我正在尝试将表单信息插入已创建的数据库中。到目前为止,我有:

    <form action="insert.php" method="post">
      Username:
      Password:
      Confirm Password:
      <input type="submit">
      if loops {
        send alert and return back to form page; }
    </form>

我的问题是:使用此代码,如果if循环被激活,用户信息是否仍会被发送到数据库文件,或者在每个if循环后我是否需要退出语句? (如果if循环被激活,我不希望发送任何信息。)

由于

2 个答案:

答案 0 :(得分:0)

您的表单上需要inputs

 <form action="insert.php" method="post">
       Username: <input type="text" name="username">
       Password: <input type="password" name="password"> 
       Confirm Password: <input type="password" name="confirm">
       <input type="submit" name="submit">
     </form>

然后在insert.php

if (isset($_POST['submit'])){
  $Error = 0;
 if (!isset($_POST['username'])){
  $Error++;
 }
 if (!isset($_POST['password'])){
  $Error++;
 }
 if (!isset($_POST['confirm'])){
  $Error++;
 }

 if ($Error > 0){
  echo "Error in HTML Validation";
  exit; 
 }


// continue post verification here. 

}

答案 1 :(得分:0)

您还可以在客户端验证您的表单,它更快,并减少服务器负载:

 <form name="myForm" action="insert.php" method="post"  onsubmit="return validateForm()>
      Username:
      Password:
      Confirm Password:
      <input type="submit">
    </form>

并编写一个javascript来验证您的表单:

<script type="text/javaScript">
function validateForm()
{
  //Here will be your validation logic
  //if input doesn't meet your requirement then return false 
  var x=document.forms["myForm"]["email"].value;
  if(x is not an email address) 
  return false;
  }
}
</script>