我有两个文件:signupcontroller和signupmodel:
signupcontroller.php
<?php
require "../Models/conn.php";
$points=0;
$password=$_POST['password'];
$password2=$_POST['password2'];
$phonenb=$_POST["phonenb"];
require "../Models/signupmodel.php";
?>
signupmodel.php
if ($stmt = $conn->prepare('SELECT password FROM clients WHERE username = ?'))
{
$stmt->bind_param('s', $_POST['username']);
$stmt->execute();
$stmt->store_result();
if ($stmt->num_rows > 0)
{
echo "<script>alert('Username exists');location='../View/signupview.php';</script>";
}
else
{
if ($stmt = $conn->prepare('INSERT INTO clients (username, password, phonenb, nbpoints, address, name) VALUES (?, ?, ?, ?, ?, ?)')) {
$stmt->bind_param('sssiss', $_POST['username'], $_POST['password'], $_POST['phonenb'], $points, $_POST['address'],$_POST['fullname']);
$stmt->execute();
echo "<script>alert('Signup Successfull!'); location='../View/index.html';</script>";
}
}
$stmt->close();
}
else
{
// Something is wrong with the sql statement, check to make sure accounts table exists with all 3 fields.
echo 'Could not prepare statement!';
}
$conn->close();
[当我将代码从signupmodel复制并粘贴到signupcontroller时,它工作得很好,并且用户注册到了数据库,但是当我将require "../Models/signupmodel.php"
并在signupview中输入相同的输入时,它的行为就像是找到并回显该用户名存在,但是没有要求,并且没有将signupmodel中的代码复制到signupcontroller中,它并没有告诉我用户名存在,而是表示用户名成功(这意味着未找到任何行)。我使用的要求是否错误?
[我知道了,我在signupmodel代码之间放置了<?php
和?>
标签,并且它起作用了,现在我不知道为什么,但是如果有人遇到我的问题,请尝试一下。