gpt4 book ai didi

php - PHP 连接 MYSQL 出错

转载 作者:行者123 更新时间:2023-11-29 18:26:30 25 4
gpt4 key购买 nike

美好的一天!我正在尝试将我的代码连接到数据库 localhost/phpmyadmin。

这是我的代码:

在register.php文件中:

<?php
session_start();
require_once('dbconfig/config.php');
//phpinfo();
?>

config.php是数据库名称所在的文件

<!DOCTYPE html>
<html>
<head>
<title>Registration Page</title>
<link rel="stylesheet" href="css/style.css">
</head>
<body style="background-color:#bdc3c7">
<div id="main-wrapper">
<center><h2>Registration Form</h2></center>
<form action="register.php" method="post">
<div class="imgcontainer">
<img src="imgs/avatar.png" alt="Avatar" class="avatar">
</div>
<div class="inner_container">
<label><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="username" required>
<label><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="password" required>
<label><b>Confirm Password</b></label>
<input type="password" placeholder="Enter Password" name="cpassword" required>
<button name="register" class="sign_up_btn" type="submit">Sign Up</button>

<a href="index.php"><button type="button" class="back_btn"><< Back to Login</button></a>
</div>
</form>

<?php
if(isset($_POST['register']))
{
@$username=$_POST['username'];
@$password=$_POST['password'];
@$cpassword=$_POST['cpassword'];

if($password==$cpassword)
{
$query = "select * from userinfotbl where username='$username'";
//echo $query;
$query_run = mysqli_query($con,$query);
//echo mysql_num_rows($query_run);
if($query_run)
{
if(mysqli_num_rows($query_run)>0)
{
echo '<script type="text/javascript">alert("This Username Already exists.. Please try another username!")</script>';
}
else
{
$query = "insert into userinfotbl values('$username','$password')";
$query_run = mysqli_query($con,$query);
if($query_run)
{
echo '<script type="text/javascript">alert("User Registered.. Welcome")</script>';
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
header( "Location: homepage.php");
}
else
{
echo '<p class="bg-danger msg-block">Registration Unsuccessful due to server error. Please try later</p>';
}
}
}
else
{
echo '<script type="text/javascript">alert("DB error")</script>';
}
}

这一行发生错误。但我不知道到底发生了什么。

                else
{
echo '<script type="text/javascript">alert("Password and Confirm Password do not match")</script>';
}

}
else
{
}
?>
</div>
</body>
</html>

但是在本地主机页面中我收到错误消息“本地主机说:数据库错误”我不知道这是怎么回事。

最佳答案

这意味着“select * from userinfotbl where username='$username'”查询未找到任何与条件匹配的实体或出现错误。

Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE.

http://php.net/manual/en/mysqli.query.php

使用mysqli_error看看你遇到了什么错误。

示例:

if($password==$cpassword)
{
$query = "select * from userinfotbl where username='$username'";
//echo $query;
$query_run = mysqli_query($con,$query);
//echo mysql_num_rows($query_run);
if(!empty($query_run)) {
...
} elseif ($query_run === false)
echo mysqli_error($con);
else
echo '<script type="text/javascript">alert("DB error")</script>';
}

关于php - PHP 连接 MYSQL 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46149027/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com