Mysql
 sql >> Base de données >  >> RDS >> Mysql

Cocher pour insérer ou mettre à jour le tableau

Utilisez mysql REPLACE INTO ou INSERT... ON DUPLICATE KEY UPDATE . Par exemple :

foreach ($sheet as $data) {
    // Get Phone ID
    $dataPhoneID = mysql_escape_string($data['handset']['phone_id']);

    if (isset($stocks[$dataPhoneID])) {
        $stockPhone = $stocks[$dataPhoneID ];
        $phoneName = mysql_escape_string($stockPhone['description']);
        $stock = mysql_escape_string($stockPhone['stock']);

        $SQLInsert = "INSERT INTO phone_affiliate (affiliate_id, affiliate_phone_id, stock) ";
        $SQLInsert .= "VALUES ('1', '$dataPhoneID', '$stock') ";
        $SQLInsert .= "ON DUPLICATE KEY UPDATE stock = '$stock'";
        mysql_query($SQLInsert);
        if (mysql_insert_id()) {
            $SQLInsert = "INSERT INTO phone (name) VALUE('$phoneName')";
            mysql_query($SQLInsert);
            $phone_id = mysql_insert_id();
            $SQLUpdate = "UPDATE phone_affiliate set phone_id = $phone_id WHERE affiliate_id = 1 AND affiliate_phone_id = $dataPhoneID_id";
        }
    }
}