• User Newbie

    controllo dati esistenti prima di insert

    <?php require_once('Connections/camere.php'); ?>
    <?php
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

    switch ($theType) {
    case "text":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "long":
    case "int":
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
    break;
    case "double":
    $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
    break;
    case "date":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "defined":
    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
    break;
    }
    return $theValue;
    }

    $editFormAction = $_SERVER['PHP_SELF'];
    if (isset($_SERVER['QUERY_STRING'])) {
    $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
    }
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
    $insertSQL = sprintf("INSERT INTO stato_pren (numero_camere, cognome, nome, indirizzo, telefono, fax, email, data_prenotazione, data_arrivo, data_partenza, stato) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
    GetSQLValueString($_POST['numero_camere'], "text"),
    GetSQLValueString($_POST['cognome'], "text"),
    GetSQLValueString($_POST['nome'], "text"),
    GetSQLValueString($_POST['indirizzo'], "text"),
    GetSQLValueString($_POST['telefono'], "text"),
    GetSQLValueString($_POST['fax'], "text"),
    GetSQLValueString($_POST['email'], "text"),
    GetSQLValueString($_POST['data_prenotazione'], "date"),
    GetSQLValueString($_POST['data_arrivo'], "date"),
    GetSQLValueString($_POST['data_partenza'], "date"),
    GetSQLValueString($_POST['stato'], "text"));

    mysql_select_db($database_camere, $camere);
    $Result1 = mysql_query($insertSQL, $camere) or die(mysql_error());
    $insertGoTo = "cercastatopren.php";
    if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
    }
    header(sprintf("Location: %s", $insertGoTo));
    }
    mysql_select_db($database_camere, $camere);
    $query_Recordset1 = "SELECT * FROM stato_pren WHERE stato_pren.data_arrivo<='$data_partenza' AND stato_pren.data_partenza>='$data_arrivo'";
    $Recordset1 = mysql_query($query_Recordset1, $camere) or die(mysql_error());
    $row_Recordset1 = mysql_fetch_assoc($Recordset1);
    $totalRows_Recordset1 = mysql_num_rows($Recordset1);
    ?>

    Vorrei aggiungere un cotrollo di inserimento,se la camera 1 con la data_arrivo e la data_partenza gia esiste mi deve dare camera gia prenotata,ma non riesco a inserire questo controllo.
    grazie
    ciao
    :arrabbiato:


  • Super User

    ho dato un'occhiata veloce al tuo codice e mi pare che il controllo non funzioni molto bene.

    per capire bene come costruire questa query devi prendere un pezzo di carta e lavorare con le stanghette.

    disegna la linea temporale e fai due stanghette che indicano il periodo di camera prenotata e poi prova tutte le combinazioni possibili e vedi se il tuo controllo le include o meno (attualmente no).

    le combinazioni possibili sono che la data di inizio sia precedente al periodo già prenotato ma che la data di fine cada all'interno.

    che tutte due le date cadano all'interno del periodo già prenotato

    e che solo la data di inizio cada all'interno mentre quella di fine sia dopo il perido.

    una volta fatte tutte le possibilità cerchi di ricostruire la condizione where 🙂


  • User Newbie

    risolto,
    Il codice che ho postato va bene, ho solo aggiunto il controllo all'inizio
    ciao e grazie
    😎