为什么每次加载网站时都会运行我的PHP代码?

时间:2015-09-11 06:28:53

标签: php mysql

我有一个userregistration.php页面,由于某种原因,每次重新加载页面时都会在我的MYSQL数据库中添加NULL用户名/密码。就像在按下提交按钮之前运行脚本一样。这是代码:

<?php 

include("dbconnect.php"); 


$firstname=$_POST["firstname"];
$lastname=$_POST["lastname"];
$email=$_POST["email"];
$username=$_POST["username"];
$password=$_POST["password"];

  $personcon=$conn;


  $bbsSQL = "INSERT INTO TBLUSERS (FIRSTNAME, LASTNAME, EMAIL, USERNAME, PASSWORD) VALUES ('$firstname', '$lastname', '$email', '$username', '$password')"; 

  $personinfo=oci_parse($personcon,$bbsSQL); 
  oci_execute($personinfo); 


  oci_free_statement($personinfo);
  oci_close($personcon);

 echo "Thank you for registration";

?>


<html>
<head>
<title>Register</title>
</head>

<body>
<form name="myForm" action="RegisterUser.php" method="POST" onsubmit="return validate()">
<table>
<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">User name:</td>
<td align="left"><input type="text" name="username" /></td>
</tr>

<tr>
<td align="right">Password:</td>
<td align="left"><input type="password" name="password" /></td>
</tr>

</table>
<input type="submit" name="Submit"/>
<input type="reset" name="Reset"/></form>
</body>
</html>

任何人都可以帮我解决这个问题吗?显然,我只想在&#34;提交&#34;时创建一个新用户。按下按钮。谢谢!

1 个答案:

答案 0 :(得分:2)

在if()语句中包装创建帐户的代码,该语句检查$ _POST中是否有内容。像这样:

if ( isset($_POST['Submit']) ) {
    $firstname=$_POST["firstname"];
    $lastname=$_POST["lastname"];
    $email=$_POST["email"];
    $username=$_POST["username"];
    $password=$_POST["password"];        

    $personcon=$conn;    


    $bbsSQL = "INSERT INTO TBLUSERS (FIRSTNAME, LASTNAME, EMAIL, USERNAME, PASSWORD) VALUES ('$firstname', '$lastname', '$email', '$username', '$password')";         

    $personinfo=oci_parse($personcon,$bbsSQL); 
    oci_execute($personinfo);     


    oci_free_statement($personinfo);
    oci_close($personcon);        

    echo "Thank you for registration";
}