Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following code which for some reason does not seem to insert the data into the database. Please help:

Index.php <SCRIPT>
$("#insert").click(function(e) {
    $.ajax({
        url: ["./required/orderaction.php"],
        type: "POST",
        data: $("#add-form-data").serialize() + "&action=insert",
        success: function(response) {
            console.log(amodate);
            Swal.fire({
                title: 'Order added successfully!',
                showConfirmButton: false,
                type: 'success',
                icon: 'success',
                timer: 500,
                timerProgressBar: true,
            })

            $("#addOrderModal").modal("hide");
            
            ShowAllOrders();
        }
    });
});

orderaction.php
PHP
if (isset($_POST['action']) && $_POST['action'] == "insert") {
    $odate = $_POST['amodate'];
    $oclient = $_POST['amoclient'];
    $ocode = $_POST['amoprojectcodes'];
    $oname = $_POST['amoprojectname'];
    $otype = $_POST['amotype'];
    $oamount = $_POST['amoamount'];

    $db->insert($odate, $oclient, $ocode, $oname, $otype, $oamount);
}

db.php
PHP
// private $dsn = "sqlsrv:Server=localhost;Database=test";    // Conect with SQLServer
private $dsn = "mysql:host=localhost;dbname=limardfeng";   // Conect with MySQL
private $username = "root";
private $pass = "";
public $conn;

public function __construct()
{
    try {
        $this->conn = new PDO($this->dsn, $this->username, $this->pass);
        // echo "Succesfully Conected!";
    } catch (PDOException $e) {
        echo $e->getMessage();
    }
}

public function insert($odate, $oclient, $ocode, $oname, $otype, $oamount)
{
    $sql = "INSERT INTO tbl_order (odate, oclient, ocode, oname, otype, oamount) VALUES (:amodate,:amoclient,:amoprojectcodes,:amoprojectname,:amotype,:amoamount)";
    $stmt = $this->conn->prepare($sql);
    $stmt->execute(['amodate' => $odate, 'amoclient' => $oclient, 'amoprojectcodes' => $ocode, 'amoprojectname' => $oname, 'amotype' => $otype, 'amoamount' => $oamount]);
    return true;       
}


What I have tried:

I have similar code to the above which works on others however this does not work, the script code shows that it works in the inspection when I add "console.log("Works");" to it but other than that I am stumpt. Please help.
Posted
Updated 17-Jan-22 2:56am
v2
Comments
Luc Pattyn 17-Jan-22 9:26am    
1. you did not show how $db got initialized in orderaction.php
2. you don't have any error handling in function insert
Richard MacCutchan 17-Jan-22 9:55am    
You call $stmt->execute and immediately return true to the caller. So every time the insert command fails you just ignore it.
Luc Pattyn 18-Jan-22 8:16am    
An insert (or any other) database operation can fail for many reasons.
The execute() function returns a bool indicating success/failure.
When it fails, one should call mysqli_error() to find out the reason of the failure.

And yes I know, most examples google finds don't do error handling on inserts, but that is a big mistake as your experience proves...
Luc Pattyn 18-Jan-22 8:49am    
Your code needs error handling, as explained before. Then act on what you get as error message. That is all I can help you with.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900