sql >> Base de Datos >  >> RDS >> Mysql

Marque para insertar o actualizar la tabla

Use REPLACE INTO de mysql o INSERT... ON DUPLICATE KEY UPDATE . Por ejemplo:

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";
        }
    }
}