-1

I've got the following mysql table (expensemonthly):

enter image description here

I'm trying to fill inputs text in a form with the account_id (and account name fetch from another mysql table called "accounts"), for doing that from ajax code below I call a php file (CountFetchExpensesArrayDos.php) for creating a two dimention array:

//Count Registers and Fill Inputs
    $('#count').click(function(){ 
     
           $.ajax({  
                url:"CountFetchExpensesArrayDos.php", 
                method: 'POST',
                dataType: 'json',               
                data:{monthyear:"112021"},  
                success:function(data){
                    // array Receive
                        alert(data);
                        AddInputs(data.count);
                        alert("Se detectaron: "+data+" registros");
                        FillExpensesInputs(data.count,data.account_id,data.name);                                           
                    
              }  
           });  
        });

Below the CountFetchExpensesArrayDos.php file for getting data from the month (112021):

$Month = $connect->real_escape_string($_POST['monthyear']);
//Count number of month registers
$sqlc = $connect->query("SELECT * FROM expensemonthly WHERE yearmonth = '$Month'");
$count=$sqlc->num_rows;
    
if(! $connect ) {
    die('Could not connect: ' . mysqli_error());
}
         
//Get account_id and name for the month registers
$sql = "SELECT * FROM expensemonthly,accounts WHERE yearmonth = '$Month' AND accounts.account_id=expensemonthly.account_id";
$result = mysqli_query($connect, $sql) or die(mysqli_error($connect));
$final_array = array();
if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) { 
        $final_array[] = $row['account_id'];           
        $final_array[] = $row['name'];      
    }
    $CountArray[] = $count;
    //Merge Array with number of register for the month, account_id,name
    $Totalarray = array_merge($CountArray,$final_array);
    //Send array to ajax
    exit(json_encode($Totalarray));
} else {
    echo "0 results";
}
mysqli_close($connect);

My problem is that when data is arriving to ajax in an unknown format, If show data "alert(data);" I see it comma separated, but I cannot splitted using javascript "split() Method", I need to split data receive for filling Inputs in the form, this the data as received in ajax:

enter image description here

Please any Ideas? If there another method to get data from month using php and pass it to Ajax?

Barmar
  • 741,623
  • 53
  • 500
  • 612
lexer
  • 77
  • 7
  • It is a very bad idea to use `die(mysqli_error($conn));` in your code, because it could potentially leak sensitive information. See this post for more explanation: [mysqli or die, does it have to die?](https://stackoverflow.com/a/15320411/1839439) – Dharman Nov 10 '21 at 22:27
  • You have an error. [`mysqli_error()`](https://www.php.net/manual/en/mysqli.error.php) needs one argument. Please consider switching error mode on instead. [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Nov 10 '21 at 22:27

1 Answers1

0

Your AJAX code is expecting an associative array with 3 keys: count, account_id, and name. You're not creating an associative array in the PHP code. You're just appending each account ID and name as separate array elements.

You can create and push onto separate arrays for the account IDs and names. Then put them in the associative array when creating the JSON at the end.

//Get account_id and name for the month registers
$sql = "SELECT * FROM expensemonthly,accounts WHERE yearmonth = '$Month' AND accounts.account_id=expensemonthly.account_id";
$result = mysqli_query($connect, $sql) or die(mysqli_error($connect));
$account_ids = array();
$names = array();
if (mysqli_num_rows($result) > 0) {
    while($row = mysqli_fetch_assoc($result)) { 
        $account_ids[] = $row['account_id'];           
        $names[] = $row['name'];    
    }
    //Merge Array with number of register for the month, account_id,name
    $Totalarray = array('count' => $count, 'account_id' => $account_ids, 'name' => $names);
    //Send array to ajax
    exit(json_encode($Totalarray));
} else {
    echo "0 results";
}
mysqli_close($connect);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks for your answer, I changed ,my code as you suggest, but I receive the following erro on lines: $account_ids = []; $names = []; $Totalarray = ['count' => $count, 'account_id' => $account_ids, 'name' => $names]; – lexer Nov 10 '21 at 23:24
  • What error did you receive? – Barmar Nov 10 '21 at 23:30
  • Barmar Thanks for your answer, I changed ,my code as you suggest, but I receive the following error on 3 lines The same for the 3 lines): Parse error: syntax error, unexpected '[' on line 19 $account_ids = []; $names = []; $Totalarray = ['count' => $count, 'account_id' => $account_ids, 'name' => $names]; – lexer Nov 10 '21 at 23:36
  • What version of PHP are you using? It sounds like you're using an old version that doesn't support array literals, so you have to use `array(...)` instead of `[...]`. – Barmar Nov 10 '21 at 23:43
  • This syntax has been supported since PHP 5.4. See https://stackoverflow.com/questions/16439622/php-syntax-for-arrays-different-behaviour-between-php-versions – Barmar Nov 10 '21 at 23:43
  • I've got PHP 5.2.5, Please Do you know the sintax for that PHP version? – lexer Nov 10 '21 at 23:45
  • You really should upgrade. But I already edited the question to show the older syntax. – Barmar Nov 10 '21 at 23:47
  • I also described it in the comment: `[...]` becomes `array(...)`. This should be basic syntax that you already know. – Barmar Nov 10 '21 at 23:47