Trouble arises when attempting AJAX call to PHP file resulting in failed MySQL execution accompanied by errors

Having some trouble identifying the issue here. It seems quite simple, but I'll highlight it here anyway. On a basic website, I have a form for user registration where I'm using AJAX to send data to register.php. The form and AJAX code are as follows:

        <form method="GET">
        <h4>Username:</h4>
        <input type="text" class="username" placeholder="Username" name="reg-user">
        <h4>Choose password:</h4>
        <input type="text" class="password" placeholder="Password" name="reg-pass">
        <h4>First Name</h4>
        <input type="text" class="firstname" placeholder="First Name" name="reg-fn">
        <h4>Last Name</h4>
        <input type="text" class="lastname" placeholder="Last Name" name="reg-en"><br><br>
        <div class="btn btn-success reg-btn">Register</div>
    </form>
</div>

<script type="text/javascript">
    $(".reg-btn").click(function(){
        var username  = $(".username").val();
        var password  = $(".password").val();
        var firstname = $(".firstname").val();
        var lastname  = $(".lastname").val();

    //Ajax call
    $.ajax({
        method:"GET",
        url: "php/register.php",
        data:{ username: username, password: password, firstname: firstname, lastname: lastname },
        success: function(){
        $(".username").val("");
        $(".password").val("");
        $(".firstname").val("");
        $(".lastname").val("");
        $("#slide-register").prepend("<p>User registered.</p>");
}});
})

The information is sent to the PHP file for user registration without any error messages displayed currently:

<?php
   mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
   include('connect.php');

   if(isset($_GET['username']) && isset($_GET['password'])){
    $uname  = mysqli_real_escape_string($conn, $_GET['username']);
    $pword  = password_hash(mysqli_real_escape_string($conn, $_GET['password']), PASSWORD_DEFAULT);
    $fname = mysqli_real_escape_string($conn,$_GET['firstname']);
    $lname  = mysqli_real_escape_string($conn,$_GET['lastname']);

    $username  = trim($uname);
    $password  = trim($pword);
    $firstname = trim($fname);
    $lastname  = trim($lname);
    $admin = "0";

        $query  = "SELECT username FROM users WHERE username ='$username'";
        $result = mysqli_query($conn, $query);

        $count  = mysqli_num_rows($result);

        if($count == 0){
            $stmt = mysqli_prepare($conn, "INSERT INTO users (username, password, firstname, lastname, admin) VALUES (?, ?, ?, ?, ?)");
            mysqli_stmt_bind_param($stmt, "sss", $username, $password, $firstname, $lastname, $admin);
            mysqli_stmt_execute($stmt);
        }
        else {
            $msg  = "Username already taken.. ";
        }
   }
        mysqli_close($conn);
?>

However, even though there are no syntax errors visible in the AJAX call execution, nothing seems to happen. Let me know if you see something I've missed. Thank you in advance.

I'm encountering the following errors:

Warning: mysqli_stmt_bind_param(): Number of elements in type definition string doesn't match number of bind variables in /customers/5/0/5/xxxxxx/php/register.php on line 24

Fatal error: Uncaught mysqli_sql_exception: No data supplied for parameters in prepared statement in /customers/5/0/5/xxxxx/php/register.php:25
Stack trace:
#0 /customers/5/0/5/xxxxx/php/register.php(25):    mysqli_stmt_execute(Object(mysqli_stmt))
#1 {main}
thrown in /customers/5/0/5/xxxxxxx/php/register.php on line 25

Answer №1

Resolved:

An issue was discovered where the character count for binding values was insufficient.

mysqli_stmt_bind_param($stmt, **"sss"**, $username, $password, $firstname, $lastname, $admin);

The correct format should be:

mysqli_stmt_bind_param($stmt, **"sssss"**, $username, $password, $firstname, $lastname, $admin);

ALSO

In the code snippet:

 mysqli_real_escape_string(-->$conn<--, $_GET['password']);

the inclusion of $conn was absent.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The antithesis of jQuery's .parents() selector

I am currently developing a userscript for a webpage with an old-fashioned design consisting mainly of tables. My goal is to access a long table of fields so that they can be filled in automatically by the script. To simplify, the structure is as follows: ...

Using jQuery, implement a functionality where a line break is added when the Enter key is pressed. Furthermore, the added

I have a text box where users can add cars to a list. Each car is entered on a new line, and when the user presses enter, jQuery adds a \n to move to the next line. However, when I collect this string, the \n characters are "hidden" and cannot b ...

Utilizing AJAX and jQuery to dynamically load a div instantly, followed by automatic refreshing at set intervals

I am utilizing jQuery and AJAX to refresh a few divs every X seconds. I am interested in finding out how to load these divs immediately upon the page loading for the first time, and then waiting (for example, 30 seconds) before each subsequent refresh. I h ...

Having trouble with jQuery hover and AJAX loaded content not functioning properly?

When I receive content through AJAX, it looks like this: <div class="message" id="1"> blah <div class="details" id="1" style="float: left; display: none;">hidden information</div> <div class="spacer" style="clear: both;"&g ...

`Adjusting function calls based on the specific situation`

On my webpage, I have implemented a tab system where clicking on a tab displays the corresponding content below. This functionality is controlled by a JavaScript/jQuery function called 'changeTab'. Now, I want to set up individual JavaScript fun ...

Tips for transferring a control's value between pages using AJAX in ASP.NET

How can I pass the value of a control, such as a textbox, from one page to another using AJAX in ASP.NET with C#? Any recommendations or advice would be greatly appreciated! ...

Storing a two-dimensional array in Laravel session

My task involves converting a pure PHP code to Laravel, particularly focusing on the session usage. Below is a simplified snippet of the original code: $_SESSION['gamedata']=[ 'game'=> 23 , 'n'=>0, ...

Encountering an application error when switching from APP_ENV=local to APP_ENV=production in the .env file of a

After attempting to deploy my app to production, I made changes from: APP_ENV=local APP_DEBUG=true to APP_ENV=production APP_DEBUG=false However, upon making this switch, the API I created and its corresponding route suddenly became inaccessible, resulti ...

After the MariaDB 10.6 update and transitioning to nd_mysqli, saving emojis in utf8mb3 tables has become impossible overnight

Previously, when I was using MariaDB 10.5 and mysqli, I had numerous older WordPress websites with tables in utf8mb3 where I could easily store a variety of Emojis in the database. However, after switching to MariaDB 10.6 and nd_mysqli, if the database an ...

jQuery: What's the Difference Between Triggering a Click and Calling a Function?

Imagine having certain actions assigned to a link using .click or .live, and wanting to repeat the same action later without duplicating code. What would be the right approach and why? Option 1: $('#link').click(function(){ //do stuff }); //c ...

Wait for the page to finish loading using C# and .NET Webclient

I'm attempting to load the HTML of a page using Ajax, but there is an issue with Webclient.Downloadstring() returning too quickly. This results in the Ajax page not loading completely, thus I am not receiving the correct HTML content. Is there a way ...

What is the method for retrieving child tags using the each statement?

How can I retrieve child tags using an each statement? <svg id="svgcontent" > <g id="layer" > <g> <g id="test1" > <g> <g id="test2"></g> <g id="test2"> </g> <svg> I am looking for the fo ...

Scaling Images When Uploaded

Are you aware of any method, using PHP or otherwise, that allows images to be resized proportionally during an upload process? ...

Error in jQuery when element is not found

On my website, I use multiple jQuery functions, but not all of them are necessary on every page. These functions are all located in one XXX.js file, such as: jQuery(function() { $(".title").slug({ slug:'slug', hide: false }); }); ...

Completely Erase a Row from JSON

Does anyone know how to completely remove a specific line from a JSON when using a deletion method? I have a button that determines which line should be removed, but even though the correct index is passed in, the row does not get deleted entirely and just ...

Restore the button to its original color when the dropdown menu is devoid of options

Is it possible to change the button colors back to their original state automatically when a user deselects all options from my dropdown menu? The user can either uncheck each option box individually or click on the "clear" button to clear all selections. ...

Harnessing the power of Shopify's Predictive Search API

I am currently working on implementing a custom solution using the predictive search API for when a product is out of stock. Our goal is to display an alternate product that we have chosen, rather than relying on automated recommendations. We have tagged a ...

How to properly utilize $.ajaxComplete and $.ajaxError functions?

I have been utilizing the code snippet below for my ajax form submissions: $.ajax({ url: "", data: ilmoittautumisdata, type: "POST", success:function(){ }); error:function(){ }); }); However, I recall being advised to discontinue using success and erro ...

Troubleshooting the Issue of Selecting a Menu Item with Jquery and AJAX

How can I trigger the AJAX Javascript function (Code-1) when selecting or clicking on the "Log Report" menu item (Code-2) and display the content of test.txt in an HTML text box on the page? I have a JavaScript function code-1 that reads the contents of t ...

Tips for saving HTML data locally

Is there a way to persist my HTML element tag data even after the user closes the browser? For example: <div class="classOne" data-random="50"> If I use jQuery to change the data attribute like this: $(".classOne").attr("data-random","40") How ca ...