我正在使用MySQL和PHP编写注册表,在MySQL中我有2个表:users和a_codes。 在我定义并将一些用户名插入a_codes表之前。 当用户在注册表单中输入用户名时,将使用a_codes表中的记录进行检查(如果存在)则完成注册。 我的问题是我想要在用户成功注册后,必须删除a_codes表中提到的用户名。 我下面的代码中的所有内容均正常运行。唯一的问题是如何删除a_codes表中使用的用户名? 。我是PHP新手,感谢您的帮助。
<?php
require_once 'DbConnect.php';
//an array to display response
$response = array();
if(isset($_GET['apicall'])){
switch($_GET['apicall']){
case 'signup':
//checking the parameters required are available or not
if(isTheseParametersAvailable(array('username','email','password','gender'))){
//getting the values
$username = $_POST['username'];
$email = $_POST['email'];
$password = md5($_POST['password']);
$gender = $_POST['gender'];
//checking if the user is already exist in a_codes table with this username
$stmt = $conn->prepare("SELECT id FROM a_codes WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();
$stmt->store_result();
//if the user already exist in the database
if($stmt->num_rows > 0){
//if user is already exist creating an insert query
$stmt = $conn->prepare("INSERT INTO users (username, email, password, gender) VALUES (?, ?, ?, ?)");
$stmt->bind_param("ssss", $username, $email, $password, $gender);
//if the user is successfully added to the database
if($stmt->execute()){
//fetching the user back
$stmt = $conn->prepare("SELECT id, id, username, email, gender FROM users WHERE username = ?");
$stmt->bind_param("s",$username);
$stmt->execute();
$stmt->bind_result($userid, $id, $username, $email, $gender);
$stmt->fetch();
$user = array(
'id'=>$id,
'username'=>$username,
'email'=>$email,
'gender'=>$gender
);
$stmt->close();
//adding the user data in response
$response['error'] = false;
$response['message'] = 'done';
$response['user'] = $user;
}
} else {
$response['error'] = true;
$response['message'] = 'already registered';
$stmt->close();
}
}else{
$response['error'] = true;
$response['message'] = 'error eccoured';
}
break;
答案 0 :(得分:0)
mysql删除的正确语法是;-
DELETE FROM table_name WHERE column = value;
根据您的情况将是
DELETE FROM `a_codes` WHERE `username` = "insert the username here"
或
$stmt = $conn->prepare("DELETE FROM a_codes WHERE username = ?");
$stmt->bind_param("s", $username);
$stmt->execute();