gpt4 book ai didi

php - 注册表的sql问题

转载 作者:行者123 更新时间:2023-11-30 01:12:01 27 4
gpt4 key购买 nike

我已经创建了一个注册表(请参阅下面的代码)。密码有两个字段,第二个字段用作检查,提交表单后我检查字段中的输入是否匹配。如果不这样做,则会成功发送一条消息,表明密码不匹配,但新用户记录仍会插入数据库中。如果密码字段不匹配,如何防止记录插入?

这是代码:

<?php

$username = isset( $_POST["username"] ) ? $_POST["username"] : "";
$password = isset( $_POST["password"] ) ? $_POST["password"] : "";
$confirm = isset( $_POST["confirm"] ) ? $_POST["confirm"] : "";

if( !empty( $username ) && !empty( $password ) ) {

if( $password != $confirm )
header( "location:registration.php?msg = Password does not be match." );

$host = "localhost";
$user = "i have put my username here";
$pass = "i have put my pass here";

$link = mysql_connect( $host,$user,$pass ) or die( mysql_error() );
mysql_select_db( "web_db",$link );
$query = "SELECT * FROM users WHERE username = '".mysql_escape_string( $username )."'";
$result = mysql_query( $query );
$count = mysql_num_rows( $result );

if( $count = = 1 ) {
header( "location:registration.php?msg = username already exists" );
} else {
$qry = "INSERT INTO users( username,password )VALUES( '".mysql_escape_string( $username )."', '".mysql_escape_string( $password )."' )";
mysql_query( $qry );

echo "You are successfully registered.";
}

mysql_close( $link );
} else {
header( "location:registration.php?msg = Username or password cannot be blank." );
}

最佳答案

试试这个:

if($password != $confirm){
header("location:registration.php?msg=Password does not be match.");
exit;
}
else {
// rest of the code goes here
}

关于php - 注册表的sql问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19389509/

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