if(isset($ _ POST [' Submit'])){Not working

时间:2015-01-07 18:16:19

标签: php forms if-statement

我试图将用户输入的数据发布到我的数据库中但是我的if语句确保仅在按下提交按钮后才会发生这种情况。请帮忙!

以下代码..

<form>
    <form method="post" action="register.php" >
  <table align="center">
    <tr>
      <td align="right">First Name:</td>
      <td align="left"><input type="text" name="FirstName"></td>
    </tr>
    <tr>
      <td align="right">Last Name:</td>
      <td align="left"><input type="text" name="LastName"></td>
    </tr>
    <tr>
      <td align="right">Email:</td>
      <td align="left"><input type="text" name="Email" /></td>
    </tr>
    <tr>
      <td align="right">Password:</td>
      <td align="left"><input type="password" name="Password"></td>
    </tr>
    <tr>
      <td align="right">Gender:</td>
      <td align="left"><input type="text" name="Gender"></td>
    </tr>
    <tr>
      <td align="right">Age:</td>
      <td align="left"><input type="text" name="Age"></td>
    </tr>
  </table>
<input type="submit" value="Submit" name="Submit">
</form>

<?php include ("registersql.php"); ?>

我的regstersql.php

<?php
//include connection
 include ("connection.php");
//has form been submitted?
if(isset($_POST['Submit'])){
    $FirstName=$_POST['FirstName'];
    $LastName=$_POST['LastName'];
    $Email=$_POST['Email'];
    $Password=$_POST['Password'];
    $Gender=$_POST['Gender'];
    $Age=$_POST['Age'];
//Insert data
$query = "INSERT INTO Customers
(CustomerID, FirstName, LastName, Email, Password, Gender,Age)
VALUES
('$FirstName','$LastName','$Email','$Password','$Gender','$Age')";
mysqli_query($connection,$query);
} else
{
    echo "Error";
}

?>

4 个答案:

答案 0 :(得分:2)

isset()告诉您是否已定义变量,而不是是否已填充变量。

尝试这样的事情:

if(isset($_POST['submit']) && !empty($_POST['submit'])) {
    // there is something in the field, do stuff
} else {
    // trigger error
}

答案 1 :(得分:2)

您需要从

更改
<form method="post" action="register.php" >

<form method="post" action="" >  //Sends data on same page. There is no register.php exist.

答案 2 :(得分:0)

你做错了两件事:

1)您使用了两次标签:

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

带有一个结束标记

2)不要指定您可以使用require或require_once(&#39; yourfile.php&#39;)的文件位置,并且在此文件中,您可以使用if(isset($_POST['submit']))处理您的POST提交,然后你想要什么。

答案 3 :(得分:-2)

客户价值传递问题。

/Insert data
$query = "INSERT INTO Customers
(CustomerID, FirstName, LastName, Email, Password, Gender,Age)
VALUES
('$FirstName','$LastName','$Email','$Password','$Gender','$Age')";
mysqli_query($connection,$query);
} else
{
    echo "Error";
}

?>

见上面的代码,如果customerid自动提空'',但你没有提到任何东西,所以它不会插入,如果你提到用户方成功的消息会显示但不会插入数据。

使用类似:

values('','$FirstName','$LastName','$Email','$Password','$Gender','$Age')";