0
我正在开发一个关于注册的PHP代码。 我开始知道自我形式的行为是安全的,而不是转到另一个形式的行动页面。 当我创建表单动作时,值插入到数据库中,但我的问题是PHP代码也在上面的部分中打印。显示php代码的自我形式动作代码
这是我的PHP代码:
<?php
ob_start();
session_start();
if(isset($_SESSION['user'])!=""){
header("Location: index.php");
}
include 'dbconfig.php';
$error = false;
extract($_POST);
error_reporting(0);
## :: Registration :: ##
if(isset($_POST['submit']))
{
$fname = $_POST['fname']; //gets firstname from frontend
$lname = $_POST['lname']; //gets lastname from frontend
$email = $_POST['email']; //gets email from frontend
$password = $_POST['password']; //gets password from frontend
$cpassword = $_POST['cpassword']; //gets Confirm password from frontend
$mobile = $_POST['mobile']; //gets mobile from frontend
$class = $_POST['class']; //gets class from frontend
$remote = $_SERVER['REMOTE_ADDR']; //gets the ip address from user
$reg_query = mysqli_query($conn, "INSERT INTO `at_reg_user`(`fname`, `lname`, `email`, `password`, `cpassword`, `mobile`, `class`, `ipaddress`) VALUES ('$fname','$lname','$email','$password1','$cpassword1','$mobile','$class','$remote')");
if ($reg_query) {
header('Location: index');
$errMSG = "Successfully registered, you may login now";
unset($fname);
unset($lname);
unset($email);
unset($password);
unset($cpassword);
unset($mobile);
unset($class);
} else {
$errMSG = " Not Successfully registered, you may login now";
}
}
}
?>
这是我的表单操作代码
<form method="post" action='<?php echo htmlentities($_SERVER["PHP_SELF"]);?>' >
<div class="form-group"> <span class="text-danger"><?php echo $fnameError; ?></span>
<label class="sr-only" for="form-fname">First name</label>
<input type="text" name="fname" placeholder="First name..." class="form-first-name form-control" id="fname">
</div>
<div class="form-group"><span class="text-danger"> <?php echo $lnameError; ?></span>
<label class="sr-only" for="form-last-name">Last name</label>
<input type="text" name="lname" placeholder="Last name..." class="form-last-name form-control" id="lname">
</div>
<div class="form-group"><span class="text-danger"> <?php echo $emailError; ?></span>
<label class="sr-only" for="form-email">Email</label>
<input type="email" name="email" placeholder="Email..." class="form-email form-control" id="email">
</div>
<div class="form-group"><span class="text-danger"> <?php echo $passwordError; ?></span> <span class="text-danger"> <?php echo $passwordmError; ?></span>
<label class="sr-only" for="form-password">Password</label>
<input type="password" name="password" placeholder="Password..." class="form-password form-control" id="password">
</div>
<div class="form-group"><span class="text-danger"><?php echo $cpasswordError; ?></span> <span class="text-danger"> <?php echo $cpasswordmError; ?></span> <span class="text-danger"> <?php echo $mcpasswordError; ?>
<label class="sr-only" for="form-confirm-password">Confirm Password</label>
<input type="password" name="cpassword" placeholder="Confirm Password..." class="form-cpassword form-control" id="cpassword">
</div>
<div class="form-group"><span class="text-danger"><?php echo $mobileError; ?></span>
<label class="sr-only" for="form-mobile">Mobile Number</label>
<input type="number" maxlength="10" name="mobile" placeholder="Mobile Number..." class="form-mobile form-control" id="mobile">
</div>
<div class="form-group"><span class="text-danger"><?php echo $classError; ?></span>
<select name="class" for="form-class" class="form-class form-control id" id="class">
<option selected="true" disabled="disabled">------Select Class------</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
</div>
<button type="submit" class="btn" name="submit" id="submit">Sign me up!</button>
</form>
的问题是“它的再次重定向到索引页成功,但上面的PHP代码显示
对不起,但它不能理解,你需要什么。所以请用另一种方式解释 –
你应该在你的'header();'后面放一个'exit();'。这将停止脚本的进一步执行,并会阻止显示稍后在代码中发生的任何事情。同样,当你这样做时,你将不得不把你的重定向放在将数据插入数据库的代码下面 – Thakkie
来自html表单的值已经成功地插入到我的数据库中,但上面的php代码在我的html页面 – Karthik