我是PHP / SQL和通用服务器端脚本的新手。
我有以下代码:
$username = $_POST['username'];
$password = crypt($_POST['password']);
$emailAddress = $_POST['emailAddress'];
$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$con) {
die('Could not connect: ' . mysql_error());
};
if ($stmt = $con->prepare("SELECT * FROM Users WHERE Username=?")) {
$stmt->bind_param("s", $username);
if(!$stmt->execute()) {
echo "This Username isn't taken!";
} else {
echo 'This Username is already taken!';
};
$stmt->close();
};
$con->close();
我运行此代码以检查用户是否在我的数据库中,但我遇到的问题是它产生相同的输出,即使用户名不在数据库中。我该如何解决这个问题?
如果可以修复我的代码,如果执行不成功/如果没有找到用户名,我想运行:
if ($stmt = $con->prepare("INSERT INTO Users (Username, EmailAddress, ePassword) VALUES (?,?,?)")) {
$stmt->bind_param("sss", $username, $emailAddress, $password);
$stmt->execute();
$stmt->close();
};
然后将用户添加到我的数据库。
感谢您的帮助。
答案 0 :(得分:0)
我认为你应该删除<{1}}周围的<{p}}
if-statement
相反,尝试这样的事情:
if ($stmt = $con->prepare("SELECT * FROM Users WHERE Username=?"))`
答案 1 :(得分:0)
我做了一个小项目,从数据库中搜索并给出输出..你可以尝试一下:)
<?php
$con = mysql_connect("localhost","root","");
mysql_select_db("library", $con);
echo "<table border=1 id='tab' width='100%' cellpadding=0 cellspacing=0>";
if(isset($_POST['search'])){
$mysearch=$_POST['search1'];
$locate = mysql_query("select * from book where b_name = '$mysearch'");
if (mysql_num_rows($locate) > 0)
{
echo "<tr>";
echo "<th>Book ID</th>";
echo "<th>Book name</th>";
echo "<th>Author</th>";
echo "<th>Publication</th>";
echo "<th>Price</th>";
echo "<th>Type</th>";
echo "</tr>";
}
else
{
echo "<h1>BOOK NOT FOUND</h1>";
}
while($rows=mysql_fetch_array($locate)){
echo "<tr>";
echo "<td>".$rows['isbn']."</td>";
echo "<td>".$rows['b_name']."</td>";
echo "<td>".$rows['b_author']."</td>";
echo "<td>".$rows['b_publication']."</td>";
echo "<td>".$rows['b_price']."</td>";
echo "<td>".$rows['b_type']."</td>";
/*echo "<td>".$rows['no_copies']."</td>";
if($rows['no_copies']==!0){
echo "<td>".$rows['status']."</td>";
}
else{
echo "<td>Not Available</td>";
}
if($rows['no_copies']==!0){
echo "<td title=Borrow><a href='borrowedform.php?name=".$rows['book_title']."'><center><img src='img/brow.png'></center></a></td>";
echo "<td title=Reserve><a href='reservedform.php?name=".$rows['book_title']."'><center><img src='img/reserved.png'></center></a></td>";
}*/
echo "</tr>";
}
}
答案 2 :(得分:0)
所以我已经解决了我的问题并改变了我正在做的事情,我现在也检查电子邮件地址是否存在(即使它在数据库中设置为唯一)
$username = $_POST['username'];
$password = crypt($_POST['password']);
$emailAddress = $_POST['emailAddress'];
$con = mysqli_connect($db_host, $db_user, $db_pass, $db_name);
if (!$con) {
die('Could not connect: ' . mysql_error());
};
$stmt = $con->prepare("SELECT * FROM Users WHERE EmailAddress=?");
$stmt->bind_param("s", $emailAddress);
$stmt->execute();
$stmt->store_result();
$emailCheck = $stmt->num_rows;
if($emailCheck > 0) {
echo "Email Address in use";
} else {
$stmt = $con->prepare("SELECT * FROM Users WHERE Username=?");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->store_result();
$userCheck = $stmt->num_rows;
if($userCheck > 0) {
echo "Taken";
} else {
$stmt = $con->prepare("INSERT INTO Users (Username, EmailAddress, ePassword) VALUES (?,?,?)");
$stmt->bind_param("sss", $username, $emailAddress, $password);
$stmt->execute();
$stmt->close();
echo "Registered";
};
$stmt->close();
};
$con->close();
感谢您帮助我理解它