请任何人解决我的问题,我的代码会自动执行。在这里。
$con=mysql_connect("localhost","root","") or die('Not Connected');;
mysql_select_db("reg");
if (isset($_POST['submit'])) {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$username = $_POST['user'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
if($fname=='' or $lname=='' or $email=='' or $username==''
or $password=='' or $cpassword==''){
echo "<script>alert('Please Fill Out Blank Fields')</script>";
exit();
}
}
$query="INSERT INTO `form`(`ID`, `fname`, `lname`, `email`, `username`, `password`, `cpassword`)
VALUES (NULL, '$fname','$lname','$email','$username','$password','$cpassword')";
if (mysql_query($query)) {
echo "<script>alert('Registered Successfully')</script>";
}
“这是我的问题:当我刷新页面时,最后一个代码会自动执行”请检查我的代码。
感谢。
答案 0 :(得分:0)
<?php
function destroy_foo()
{
global $foo;
unset($foo);
}
$foo = 'bar';
destroy_foo();
echo $foo;
?>
答案 1 :(得分:0)
首先,将最后3行放在if
块内。这将确保只有在有POST请求时才执行查询。
要处理刷新问题,请使用Post-Redirect-Get模式确保数据不会重新发布。如果查询成功执行,则重定向到新页面,并在那里显示成功消息。
if (isset($_POST['submit'])) {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$username = $_POST['user'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
if($fname=='' or $lname=='' or $email=='' or $username==''
or $password=='' or $cpassword==''){
echo "<script>alert('Please Fill Out Blank Fields')</script>";
exit();
}
// Run this query only if something was POSTed.
$query="INSERT INTO `form`(`ID`, `fname`, `lname`, `email`, `username`, `password`, `cpassword`)
VALUES (NULL, '$fname','$lname','$email','$username','$password','$cpassword')";
if (mysql_query($query)) {
// Redirect to successpage.php, and print the success message there.
header("Location: successpage.php");
exit();
// echo "<script>alert('Registered Successfully')</script>";
}
}
或者,如果你真的想留在同一个页面上,你可以使用一些查询字符串重定向到这个页面,并在收到相同的消息时显示:
if (isset($_POST['submit'])) {
...
if (mysql_query($query)) {
// Redirect to the same page, with a query string indicating success
header("Location: thesamepage.php?success=1");
exit();
}
}
// Check if registration was successful
if(isset($_GET['success'])) {
// Display the success message.
echo "<script>alert('Registered Successfully')</script>";
}
答案 2 :(得分:0)
我认为你的代码有点混乱。
查找
/
/ connect.php
/ (pagewithform).php
/ (scripttoregister).php
<强> connect.php 强>
<?php
$server = "server";
$user = "user";
$pass = "pass";
$db = "db";
$connect = mysql_connect($server, $user, $pass);
if ( ! $connect ){
echo mysql_error();
exit;
} else{
mysql_select_db("$db", $connect);
}
?>
创建变量,如果不是$ connect,则显示错误并退出。否则,请选择您的数据库。
_
(适用scripttoregister).PHP 强>
include('connect.php');
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$username = $_POST['user'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
if (empty($fname) or empty($lname) or empty($email) or empty($username) or empty($password) or empty($cpassword){
echo "<script>alert('Please Fill Out Blank Fields')</script>";
exit();
} else{
$query= mysql_query("INSERT INTO form(fname, lname, email, username, password, cpassword) VALUES ('$fname','$lname','$email','$username','$password','$cpassword')");
echo "alert('Registered Successfully')";
}
?>
喜欢这个^
你不需要创建另一个if if只显示alert既不将NULL设置为id,也就是auto_increment。 (:现在您可以添加任何操作if(something is empty){}
和else{}
。
_
OOOOR如果是自助页面(您的)
if (isset($_POST['submit'])) {
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$email = $_POST['email'];
$username = $_POST['user'];
$password = $_POST['password'];
$cpassword = $_POST['cpassword'];
if (empty($fname) or empty($lname) or empty($email) or empty($username) or empty($password) or empty($cpassword){
echo "<script>alert('Please Fill Out Blank Fields')</script>";
exit();
} else{
$query= mysql_query("INSERT INTO form(fname, lname, email, username, password, cpassword) VALUES ('$fname','$lname','$email','$username','$password','$cpassword')");
// Redirect to a page, 'n print the success message there. (as said John Bupit)
header("Location: page.php");
exit();
// echo "<script>alert('Registered Successfully')</script>";
}
}