如何添加注册验证,以便用户无法使用与其他用户相同的用户ID?我被困在这里,因为我已经尝试了所有代码而没有任何工作。它会崩溃我的应用程序。
我正在使用Eclipse来完成我的应用。
<?php
/*
* Following code will create a new product row
* All product details are read from HTTP Post Request
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['user_name']) && isset($_POST['user_pwd'])){
$user_name = $_POST['user_name'];
$user_pwd = $_POST['user_pwd'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO doc_user (user_name, user_pwd) VALUES('$user_name', '$user_pwd')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
答案 0 :(得分:0)
在代码中更改此部分
// connecting to db
$db = new DB_CONNECT();
// chack database
$result = mysql_query("select user_name from doc_user where user_name = '$user_name'");
if(mysql_num_rows($result)){
$response["success"] = 0;
$response["message"] = "username not available.";
// echoing JSON response
echo json_encode($response);
exit;
}
// mysql inserting a new row
$result = mysql_query("INSERT INTO doc_user (user_name, user_pwd) VALUES('$user_name', '$user_pwd')");