About the implementation of login function in php

novice, I am writing a registration function.
realize the registration function through ajax, php and mysql.
I checked it with chrome"s debugging tool. When I entered an existing user name, I successfully returned "registered successfully!" , when I enter the new username and password, the value of echo $res_insert is 0. Return "the system is busy, please wait a moment" ,
ask God for help, thank you!
js section:

$(function(){
            $("-sharpregbutton").click(function(){ 
                    $.ajax({
                          type:"post",
                          url:"register.php",
                          dataType:"json",
                          data:{username:$("-sharpusernamereg").val(),password:$("-sharppasswordreg").val()},
                          success:function(feedbackdata){
                            $("-sharpregisterstatus").html(feedbackdata);
                            }
                    })
            })
         });

php section:

<?php
    $username = $_POST["username"]; 
     $password = $_POST["password"]; 
     $servername = "localhost";
    $sqlusername = "root";
    $sqlpassword = "root";
    $dbname = "guitartabs";
     
    // 
    $conn = new mysqli($servername, $sqlusername, $sqlpassword, $dbname);

    if ($conn->connect_error) {
        die(": " . $conn->connect_error);
    } 
    
    $sql = "SELECT username FROM users where username = "$username"";
    $result = $conn->query($sql);
    $num = mysqli_num_rows($result); 

    if($num){ 
         echo ""; 
     } 
    else // 
     { 
         $sql_insert = "insert into users (`username`,`password`) values ("".$username."","".$password."")"; 
         $res_insert = $conn->query($sql_insert); 

         if($res_insert) 
             { 
             echo ""; 
             } 
         else 
             { 
             echo ""; 
             } 
    }
?>
Php
Mar.07,2021

do not mix the two modes
instead of

$num = $conn->num_rows;

$sql_insert = "insert into users (`username`,`password`) values ('".$username."','".$password."')"; 
         $res_insert = $conn->query($sql_insert); 

         if($res_insert) 

$res_insert the return value is not id, is it
you should use this method mysql_insert_id

Menu