javascript - Error 500 When Updating MySql Database from WordPress - Same Code Works Fine Outside of WordPress

I need to update a MySql database from inside a JS function within a WordPress Woocommerce page. I'm using Ajax to do this. The following code works fine as a stand-alone code but when I put it into the WordPress page (on my localhost) it throws an error 500. Also I put the required data (which will eventually be a variable) onto the end of the url (?test=14230) because I couldn't get it to send the data when using the data: line in the Ajax.

Here's the Ajax:

function db()
{
   
         $.ajax({
      url: 'update_db.php?test=14230',
      type: 'post',
      data: 0,
      success: function(output) 
      {
          alert('Success, server says '+output);
      }, error: function()
      {
          alert('Something went wrong.');
      }
   });
   
}

Here's the update_db.php:

<?php
if(isset($_GET['test']) ){
$id = $_GET['test'];
}
        
    include 'database-handler.php';
    
    $sql = "UPDATE db_name SET column = 'Some Value' WHERE id = $id";

    if(mysqli_query($conn, $sql)){
        //echo ('<p>'."Success.".'</p>');
    } else {
        //echo ('<p>'."Something went wrong. $sql. " . mysqli_error($conn).'</p>');
    }
    
    mysqli_close($conn);  
?>

So I'm just wondering why this works as a stand-alone code but not when it's inside WordPress?

Edit: Here is the error log:

[Wed Nov 09 15:16:47.543162 2022] [php:error] [pid 4564:tid 1828] [client ::1:5888] PHP Fatal error: Uncaught TypeError: mysqli_query(): Argument #1 ($mysql) must be of type mysqli, null given in C:\xampp\htdocs\my-sites\wowcard\wp-content\themes\blocksy-child\woocommerce\single-product\save-card-size.php:17\nStack trace:\n#0 C:\xampp\htdocs\my-sites\wowcard\wp-content\themes\blocksy-child\woocommerce\single-product\save-card-size.php(17): mysqli_query(NULL, 'UPDATE new_card...')\n#1 {main}\n thrown in C:\xampp\htdocs\my-sites\wowcard\wp-content\themes\blocksy-child\woocommerce\single-product\save-card-size.php on line 17, referer: http://localhost/my-sites/wowcard/product/polka-dot-brush-strokes-two-photo-birthday-card-purple/?card=complete&id=14230

Answer

Solution:

Edit:

I believe the issue I had was due to the Ajax being embedded within more than one WordPress function and also a Woocommerce action. The variable I needed was not being passed from my PHP to the Ajax like it normally would be. I used a session variable instead, which fixed that particular issue.

The Ajax now uses the "data:" line to pass the data over to save-card-size.php rather than passing it inside the url. I would show the code but the editor is too glitchy on this site, it wouldn't allow me to copy and paste my code properly.

I also had to replace the include in save-card-size.php with the contents of database-handler.php.

Everything is now working.

Answer

Solution:

WordPress has its own database handler which is automatically loaded, there are most likely conflicts between your code and WordPress, but without seeing the actual error, I can not give more information.

Check this out https://developer.wordpress.org/reference/classes/wpdb/

Example:

global $wpdb;
$results = $wpdb->query($wpdb->prepare( 'UPDATE db_name SET column = 'Some Value' WHERE id = %d' , $id ));

Source