I have a problem to save the data into the database. My case is when I save the data into the database table (user
), the new username must cannot same with exist username in the table.
Below is my code to check whether insert username is the same for the existing username if not the same then can insert to the table, but my code doesn't work, anyone can guide me which part I get wrong.
save
$arr_val = $_POST;
$arr_val = $_POST;
$loc = $arr_val['loc'];
$action = $arr_val['action'];
$id = $arr_val['id'];
$type = $arr_val['type'];
unset($arr_val['loc']);
unset($arr_val['action']);
unset($arr_val['filter_id']);
unset($arr_val['type']);
$table = 'user';
if ($action == 'save') {
$query_user = mysqli_query($mysql_con, 'SELECT * FROM user WHERE username="' . $arr_val['username'] . '"');
$num_user = mysqli_num_rows($query_user);
if(count($num_user)>0){
echo "Duplicate username";
}elseif($num_user == 0) {
$key_relationship_1[] = 'is_active';
$val_relationship_1[] = convert_db_value('1');
$key_relationship_1[] = 'user_type';
$val_relationship_1[] = convert_db_value('1');
$key_relationship_1[] = 'created';
$val_relationship_1[] = convert_db_value($cur_dt);
$key_relationship_1[] = 'createdby';
$val_relationship_1[] = convert_db_value($user_name);
$key_relationship_1[] = 'modified';
$val_relationship_1[] = convert_db_value($cur_dt);
$key_relationship_1[] = 'modifiedby';
$val_relationship_1[] = convert_db_value($user_name);
$key_relationship_1[] = 'username';
$val_relationship_1[] = convert_db_value($_POST['username']);
$key_relationship_1[] = 'name';
$val_relationship_1[] = convert_db_value($_POST['name']);
$key_relationship_1[] = 'email';
$val_relationship_1[] = convert_db_value($_POST['email']);
$row_insert = db_conn_insert('user', $key_relationship_1, $val_relationship_1);
$sql_insert = $row_insert['sql'];
$error_insert = $row_insert['error'];
$record_id = $row_insert['record_id'];
unset($key_relationship_1);
unset($val_relationship_1);
}
elseif ($row_insert) {
echo 'Saved successfully';
}
The output shows me like the below:
This output I try to insert the same username with the table "user".
If remove the "count", the output show like below: