php if(isset($_POST['Submit'])){ 不工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27825953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:32:45  来源:igfitidea点击:

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

phpformsif-statement

提问by TheKaiser4

Im trying to POST data entered by a user into my databse however my if statement to make sure this only happens once the submit button has been pressed is not working. Please help!

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

code below..

下面的代码..

<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"); ?>

my regstersql.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";
}

?>

采纳答案by NeiL

You need to change from

你需要从

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

to

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

回答by chakeda

isset()tells you whether the variable has been defined, not whether it's been populated.

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

Try something like this:

尝试这样的事情:

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

回答by Youssef TOUNOUSSI

You did two things incorrectly :

你做错了两件事:

1) you used tag twice:

1)您使用了两次标签:

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

with one closing tag

有一个结束标签

2) don't specify the file location you can use require or require_once ('yourfile.php') and into this file you can handle your POST submission with if(isset($_POST['submit']))then have what you want.

2) 不要指定你可以使用 require 或 require_once ('yourfile.php') 的文件位置,在这个文件中你可以处理你的 POST 提交,if(isset($_POST['submit']))然后有你想要的。

回答by jagadish

Problem in customerid value passing.

customerid 值传递问题。

/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";
}

?>

see above code if customerid auto mention empty ' ', but your not mentioned anything so it will not insert, if you mention user side success message will display but not insert data.

如果customerid 自动提及空' ',请参阅上面的代码,但您没有提及任何内容,因此不会插入,如果您提及用户端成功消息将显示但不插入数据。

Use like:

像这样使用:

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