sql数据库查询问题

时间:2017-09-06 12:58:14

标签: php mysql sql

上周,我的网站的一部分是注册脚本。我已经让它在数据库中创建新用户,但是当有人输入我遇到问题的重复用户名时,这很好。

数据库设置为不允许重复,因此如果您尝试在网页上打印出一个可爱的错误,虽然功能看起来不太好。

我一直在努力做的事情并且已经看过许多如何做的例子,但它对我来说无效。我会喜欢一些帮助,请不要成为一个混蛋,并说有答案/它是重复的,因为我已经尝试过。如果您不想提供帮助,请继续:)。

这是代码:

<?php
include 'pdo_connect.php';

if(!empty($_POST)) {
    $uname = $_POST['uname'];
    $upassword = password_hash($_POST['upassword'], PASSWORD_DEFAULT);
	
    //here i want to search for the duplicate username and if none then carry on ar if match echo "alredy taken"
	
    $query = 'INSERT INTO `users` ( `uname`, `password`) VALUES (?,?)'; //if duplicate exists returns a duplicate error.
	
    $params = array($uname, $upassword);
    $results = dataQuery($query, $params);
	
	

}

?>

更新1

<?php
include 'pdo_connect.php';

if(!empty($_POST)) {
    $uname = $_POST['uname'];
    $upassword = password_hash($_POST['upassword'], PASSWORD_DEFAULT);
	
    //here i want to search for the duplicate username and if none then carry on ar if match echo "alredy taken"
	try
	{
       $query = 'INSERT INTO `users` ( `uname`, `password`) VALUES (?,?)'; //if duplicate exists returns a duplicate error.
	}
	catch (Exception $e)
	{
		echo "username taken";
	}
    $params = array($uname, $upassword);
    $results = dataQuery($query, $params);
	
	

}

?>

按照建议尝试了try catch但同样的问题是服务器错误显示在屏幕上我认为它因为它仍然执行而且它没有“崩溃”。

这是我得到的错误:(当我尝试注册为已存在的管理员时)

error

更新2

同样的结果:(

<?php
include 'pdo_connect.php';

if(!empty($_POST)) {
    $uname = $_POST['uname'];
    $upassword = password_hash($_POST['upassword'], PASSWORD_DEFAULT);
	
    //here i want to search for the duplicate username and if none then carry on ar if match echo "alredy taken"
	try
	{
       $query = 'INSERT INTO `users` ( `uname`, `password`) VALUES (?,?)'; //if duplicate exists returns a duplicate error.
	   $params = array($uname, $upassword);
       $results = dataQuery($query, $params);
	}
	catch (Exception $e)
	{
		echo "username taken";
	}

	
	

}

?>

更新2

<?php
include 'pdo_connect.php';

if(!empty($_POST)) {
    $uname = $_POST['uname'];
    $upassword = password_hash($_POST['upassword'], PASSWORD_DEFAULT);
	
    //here i want to search for the duplicate username and if none then carry on ar if match echo "alredy taken"
	try
	{
       $query = 'INSERT INTO `users` ( `uname`, `password`) VALUES (?,?)'; //if duplicate exists returns a duplicate error.
	   $params = array($uname, $upassword);
       $results = dataQuery($query, $params);
	}
	catch (PDOException $e)
	{
		echo "username taken";
	}

	
	

}

?>

仍然是这样:(

更新3

<?php
include 'pdo_connect.php';
if (!empty($_POST)) {
    $uname     = $_POST['uname'];
    $upassword = password_hash($_POST['upassword'], PASSWORD_DEFAULT);

    //here i want to search for the duplicate username and if none then carry on ar if match echo "alredy taken"
    try {
        $query   = $ConString->prepare("SELECT * from users where uname = $uname ");
        $query->execute([$uname]);
        $results = $query->fetchall();
        if (count($results) > 0) {

            echo "username taken";

        } else {
            $query   = 'INSERT INTO `users` ( `uname`, `password`) VALUES (?,?)';
            $params  = array($uname,$upassword);
            $results = dataQuery($query, $params);
        }    
    }
    catch (Exception $e) {
        echo "username taken";
    }    
}
?>

这两个错误:

enter image description here

pdo_connect代码:

<?php



error_reporting(E_ALL);
ini_set('display_errors', 1);

define('USER', 'root');
define('PASS', 'pass');


function dataQuery($query, $params) {
    $queryType = explode(' ', $query);

    // establish database connection
    try {
        $dbh = new PDO('mysql:host=localhost;dbname=test', USER, PASS);
        $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch(PDOException $e) {
        echo $e->getMessage();
        $errorCode = $e->getCode();
    }

    // run query
    try {
        $queryResults = $dbh->prepare($query);
        $queryResults->execute($params);
        if($queryResults != null && 'SELECT' == $queryType[0]) {
            $results = $queryResults->fetchAll(PDO::FETCH_ASSOC);
            return $results;
        } else {
            return $queryResults->rowCount();
        }
        $queryResults = null; // first of the two steps to properly close
        $dbh = null; // second step tp close the connection
    }
    catch(PDOException $e) {
        $errorMsg = $e->getMessage();
        echo $errorMsg;
    }
}

?>

2 个答案:

答案 0 :(得分:1)

在插入之前,你需要运行一个select语句,选择id或者你们用户表中哪些内容与寄存器上提供的用户名相匹配,如果select语句返回结果,则输入用户名,否则运行insert。

<?php
include 'pdo_connect.php';
if (!empty($_POST)) {
    $uname     = $_POST['uname'];
    $upassword = password_hash($_POST['upassword'], PASSWORD_DEFAULT);

    //here i want to search for the duplicate username and if none then carry on ar if match echo "alredy taken"
    try {
        $query   = $ConString->prepare("SELECT * from users where uname = ? ");
        $query->execute([$uname]);
        $results = $query->fetchall();
        if (count($results) > 0) {

            echo "username taken";

        } else {
            $query   = 'INSERT INTO `users` ( `uname`, `password`) VALUES (?,?)';
            $params  = array($uname,$upassword);
            $results = dataQuery($query, $params);
        }    
    }
    catch (Exception $e) {
        echo "username taken";
    }    
}
?>

您需要修改我的代码以与您的方法匹配,因为您已经完成了自己的sql函数。

答案 1 :(得分:0)

找到它了!

error_reporting(E_ALL);
ini_set('display_errors', 1);

define('USER', 'root');
define('PASS', 'Unhackable');


function dataQuery($query, $params) {
    // what kind of query is this?
    $queryType = explode(' ', $query);

    // establish database connection
    try {
        $dbh = new PDO('mysql:host=localhost;dbname=test', USER, PASS);
        $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
        $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    }
    catch(PDOException $e) {
        echo $e->getMessage();
        $errorCode = $e->getCode();
    }

    // run query
    try {
        $queryResults = $dbh->prepare($query);
        $queryResults->execute($params);
        if($queryResults != null && 'SELECT' == $queryType[0]) {
            $results = $queryResults->fetchAll(PDO::FETCH_ASSOC);
            return $results;
        } else {
            return $queryResults->rowCount();
        }
        $queryResults = null; // first of the two steps to properly close
        $dbh = null; // second step tp close the connection
    }
    catch(PDOException $e) {
        $errorMsg = $e->getMessage();
        echo "too slow, username already taken";
		//echo $errorMsg;
		
    }
}

?>

注释掉了回显错误消息并回显了一条自定义消息,我知道它不是很好但是它可以完成这项工作。