插入mysql数据库时出错

时间:2018-06-30 13:57:48

标签: php mysql

我在使用用户名和密码进行简单注册以插入mysql数据库时遇到了简单错误。

  

注意:未定义的变量:第42行的C:\ xampp \ htdocs \ x \ index.php中的用户名

     

注意:未定义的变量:第42行的C:\ xampp \ htdocs \ x \ index.php中的密码   注册成功

<!DOCTYPE html>
<html>
	<head>
		<title>THIS IS MY FIRST LOGIN PAGE</title>
		<link rel="stylesheet" type="text/css" href="style.css"></link>
		<link rel="stylesheet" type="text/css" href="">
	</head>

	<body>
		<div class="container">
			<img src="men.png" alt="missing">
			<form method="post" >
				<div class="font-input">
					<input type="text" name="name" id="name"  placeholder="Enter Name">
				</div class="font-input">
				<div>
					<input type="password" name="pass" id="pass"  placeholder="Enter Password">
				</div>
				<div>
					<input type="button" name="login" value="Login">
				</div>
			</form>
		</div>
	</body>
</html>

  <?php

$conn = mysqli_connect('localhost','root','','y');

if(!$conn)
{
    die('Connection failed!'.mysqli_error($conn));
}

if (isset($_POST['name'], $_POST['pass']))
{
        $username = $_POST['name'];
        $password = $_POST['pass'];
} 
 $sql = "INSERT INTO hack(myusername,mypassword) VALUES('$username', '$password')";



if(mysqli_query($conn,$sql))
{
    echo "Registerd Successfully";
}
else
{
    echo mysqli_error($conn);
}

?>

1 个答案:

答案 0 :(得分:0)

尝试一下: 我们检查用户名和密码是否与表单一起发送,如果是,则在没有错误的情况下将它们插入数据库。
代码:

if((isset($_POST['name'])) && (isset($_POST['pass'])))
{
    $username = $_POST['name'];
    $password = $_POST['pass'];
    $sql = "INSERT INTO hack(myusername, mypassword) VALUES('$username', '$password')";
    if(mysqli_query($conn,$sql))
    {
    echo "Registerd Successfully";
    }
    else
    {
    echo mysqli_error($conn);
    }
}