Visualizzazione dei risultati da 1 a 3 su 3

Discussione: php e sql

  1. #1
    Utente di HTML.it
    Registrato dal
    Apr 2010
    Messaggi
    216

    php e sql

    in pratica devo inserire una data tramite una text nel db.
    il punto è che, come saprete, mysql inserire le date nel formato YYYY/MM/GG; a me servirebbe GG/MM/YYYY.

    vi posto il codice:
    codice:
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
    {
      if (PHP_VERSION < 6) {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
      }
    
      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($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 partite (`data`, orario, Scasa, Sospite, serie, risCasa, risOspite, descrizione) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                           GetSQLValueString($_POST['data'], "date"),
                           GetSQLValueString($_POST['orario'], "text"),
                           GetSQLValueString($_POST['Scasa'], "int"),
                           GetSQLValueString($_POST['Sospite'], "int"),
                           GetSQLValueString($_POST['serie'], "text"),
                           GetSQLValueString($_POST['risCasa'], "int"),
                           GetSQLValueString($_POST['risOspite'], "int"),
                           GetSQLValueString($_POST['descrizione'], "text"));
    
      mysql_select_db($database_calcio, $calcio);
      $Result1 = mysql_query($insertSQL, $calcio) or die(mysql_error());
    }
    
    mysql_select_db($database_calcio, $calcio);
    $query_ADDpartita = "SELECT * FROM partite";
    $ADDpartita = mysql_query($query_ADDpartita, $calcio) or die(mysql_error());
    $row_ADDpartita = mysql_fetch_assoc($ADDpartita);
    $totalRows_ADDpartita = mysql_num_rows($ADDpartita);
    
    mysql_select_db($database_calcio, $calcio);
    $query_extract_squadra = "SELECT * FROM squadre";
    $extract_squadra = mysql_query($query_extract_squadra, $calcio) or die(mysql_error());
    $row_extract_squadra = mysql_fetch_assoc($extract_squadra);
    $totalRows_extract_squadra = mysql_num_rows($extract_squadra);
    ?>
    come posso fare???
    Server utilizzato: MySQL


    (cit.)
    Un misto tra pazzia ed intelligenza

  2. #2
    Utente di HTML.it L'avatar di mrseo88
    Registrato dal
    Jan 2012
    residenza
    Italia
    Messaggi
    75
    prova ad usare queste due funzioni per convertire le date in formato che ti serve

    Codice PHP:
    function data_it($data) {   
    // Creo una array dividendo la data YYYY-MM-DD sulla base del trattino  
     
    $array explode("-"$data);   
      
    // Riorganizzo gli elementi in stile DD/MM/YYYY  
     
    $data_it $array[2]."/".$array[1]."/".$array[0];    
     
    // Restituisco il valore della data in formato italiano   
    return $data_it;  } 

    // Funzione per trasformazione inversa della data(Y-m-d)
     
    function dataformat($data){  
      
    $gg=substr($data02);    
     
    $mm=substr($data32);    
     
    $aa=substr($data610);     
    $dataformat="$aa"."$mm"."$gg";    
     return 
    $dataformat;} 
    ovviamente prima di inserire nel db la date devi fare conversione

    Codice PHP:
    dataformat($dat
    o
    Codice PHP:
    data_it($data
    prima di stamparla a video

  3. #3
    Utente di HTML.it
    Registrato dal
    Apr 2010
    Messaggi
    216
    no...
    ovviamente nel db il tipop di campo sara data, giust?

    cmq ho messo la funzione al click del pulsante prima del insert

    codice:
    $editFormAction = $_SERVER['PHP_SELF'];
    if (isset($_SERVER['QUERY_STRING'])) {
      $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
    }
    
    function data_it($data) {    
    // Creo una array dividendo la data YYYY-MM-DD sulla base del trattino   
     $array = explode("-", $data);    
      // Riorganizzo gli elementi in stile DD/MM/YYYY   
     $data_it = $array[2]."/".$array[1]."/".$array[0];     
     // Restituisco il valore della data in formato italiano    
    return $data_it;  }  
    
    // Funzione per trasformazione inversa della data(Y-m-d) 
     function dataformat($data){   
      $gg=substr($data, 0, 2);     
     $mm=substr($data, 3, 2);     
     $aa=substr($data, 6, 10);      
    $dataformat="$aa"."$mm"."$gg";     
     return $dataformat;}
    data_it($data);
    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
      $insertSQL = sprintf("INSERT INTO partite (`data`, orario, Scasa, Sospite, serie, risCasa, risOspite, descrizione) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)",
                           GetSQLValueString($_POST['data'], "date"),
                           GetSQLValueString($_POST['orario'], "text"),
                           GetSQLValueString($_POST['Scasa'], "int"),
                           GetSQLValueString($_POST['Sospite'], "int"),
                           GetSQLValueString($_POST['serie'], "text"),
                           GetSQLValueString($_POST['risCasa'], "int"),
                           GetSQLValueString($_POST['risOspite'], "int"),
                           GetSQLValueString($_POST['descrizione'], "text"));
    
      mysql_select_db($database_calcio, $calcio);
      $Result1 = mysql_query($insertSQL, $calcio) or die(mysql_error());
    }
    
    mysql_select_db($database_calcio, $calcio);
    $query_ADDpartita = "SELECT * FROM partite";
    $ADDpartita = mysql_query($query_ADDpartita, $calcio) or die(mysql_error());
    $row_ADDpartita = mysql_fetch_assoc($ADDpartita);
    $totalRows_ADDpartita = mysql_num_rows($ADDpartita);
    
    mysql_select_db($database_calcio, $calcio);
    $query_extract_squadra = "SELECT * FROM squadre";
    $extract_squadra = mysql_query($query_extract_squadra, $calcio) or die(mysql_error());
    $row_extract_squadra = mysql_fetch_assoc($extract_squadra);
    $totalRows_extract_squadra = mysql_num_rows($extract_squadra);
    ?>
    Server utilizzato: MySQL


    (cit.)
    Un misto tra pazzia ed intelligenza

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.