This topic is locked

DB::Insert not working in the Import Page Before Insert Record Event

9/3/2025 12:36:30 PM
PHPRunner General questions
J
jacktonghk authorDevClub member

In the import page, after I press the 'Import' button, I found $data has values assigned but the DB::Insert function does not work. The import file inself was imported correct. How can I debug this or view the created SQL statement? I am using phprunner 10.91 with SQL Server database. Thanks

$data = [];
$data["MemberTransactionCode"] = $values["MemberTransactionID"];
$data["MemberTransactionDate"] = $values["MemberTransactionDate"];
$data["MemberCode"] = $member["MemberCode"];
$data["MemberName"] = $values["MemberName"];
$data["MemberMobile"] = $values["MemberMobile"];
$data["MemberTransactionAmount"] = $values["MemberTransactionAmount"];
$data["CommisionRate"] = $company["ComissionRate"];

$data["ComissionAmount"] = round($data["MemberTransactionAmount"] $data["ComissionRate"] / 100, 2);
$data["UL1MemberCode"] = $member["UL1MemberCode"];
$data["UL1ReferralPercentage"] = $company["UL1ReferralPercentage"];
$data["UL1MemberReferralFee"] = round($data["MemberTransactionAmount"]
$data["UL1ReferralPercentage"] / 100, 2);

$data["Redeemed"] = "N";
$data["RedeemTransactionDate"] = "1900-01-01";
$data["UL2MemberCode"] = $member["UL2MemberCode"];;
$data["UL2ReferralPercentage"] = $company["UL2ReferralPercentage"];
$data["UL2MemberReferralFee"] = round($data["MemberTransactionAmount"] * $data["UL2ReferralPercentage"] / 100, 2);
// echo "<script>alert('" . $data["MemberTransactionDate"] . "')</script>";
DB::Insert("dbo.MemberReferralFee", $data);

ChatGPT 9/3/2025

To debug the issue with the $data insertion using the DB::Insert function in PHPRunner, here are several steps you can take:

  1. Check for Errors: PHPRunner should provide some error reporting. Make sure you have error reporting enabled in your PHP configuration. You can add the following lines at the beginning of your script to display errors:

    error_reporting(E_ALL);
    ini_set('display_errors', 1);

  2. Review Database Connection: Ensure that your database connection is properly set up and that you have the necessary permissions to insert data into the dbo.MemberReferralFee table.


  3. Log SQL Queries: PHPRunner does not directly show the SQL statements being executed, but you can manually log the SQL query. You can generate the SQL statement using the values in the $data array. Here’s an example of how to do that:

    // Generate the SQL statement for debugging
    $columns = implode(", ", array_keys($data));
    $values = implode(", ", array_map(function($value) {
    return "'" . addslashes($value) . "'";
    }, array_values($data)));
    $sql = "INSERT INTO dbo.MemberReferralFee ($columns) VALUES ($values)";
    error_log($sql); // Log the SQL query to the error log

    // Now try to execute the insert
    DB::Insert("dbo.MemberReferralFee", $data);

  4. Check Data Types: Make sure the data types of the values in the $data array match the column types in the SQL Server database. Sometimes, the insertion fails due to type mismatches (e.g., trying to insert a string into an integer field).


  5. Check for Constraints: Ensure that there are no constraints (like primary key or foreign key constraints) that are being violated when you try to insert the data.


  6. Test Insert Statement: You can take the generated SQL insert statement from the logging step and test it directly in a SQL Server management tool (like SQL Server Management Studio) to see if it executes without errors.


  7. Use Transactions: If you are executing multiple insertions, consider wrapping your insert statements in a transaction. This can help ensure that all operations are atomic and can help identify issues if something goes wrong.


  8. Debugging Environment: If possible, set up a local or staging environment where you can test changes without affecting production data.



By following these steps, you should be able to identify where the issue lies with the DB::Insert call and take appropriate actions to resolve it.

Sergey Kornilov admin 9/3/2025

Enable SQL debugging and see the actual SQL query being executed:
https://xlinesoft.com/phprunner/docs/debugging_tips.htm

It may point you in the right direction.

J
jacktonghk authorDevClub member 9/5/2025

Will try. Thank you!!