Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    update multiplo su tabella mysql

    Ho un menu dinamico dal quale posso selezionare più username contemporaneamente.
    Ho necessità di salvare nel database mysql una riga per ogni username.
    In questa riga i campi 'presente' ed 'evento' hanno il medesimo valore per tutti gli usernames selezionati.
    Come si puo' fare?
    Così com'è salva una sola scelta tra tutte quelle selezionate e la funzione implode applicata sul campo username non funziona.

    Codice PHP:
    $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 partecipanti (evento, username, presente) VALUES (%s, %s, %s)", 
    GetSQLValueString($_POST['evento'], "text"), 
    GetSQLValueString($_POST['username'], "text"),
    GetSQLValueString($_POST['presente'], "int"));  
    mysql_select_db($database_server, $server); 
    $Result1 = mysql_query($insertSQL, $server) or die(mysql_error());  
    $insertGoTo = "modificapresenti.php"; 
    if (isset($_SERVER['QUERY_STRING'])) 
    { $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; 
    $insertGoTo .= $_SERVER['QUERY_STRING']; } 
    header(sprintf("Location: %s", $insertGoTo)); }  
    <select name="username" size="12" multiple="multiple"> 
    <?php do { ?> 
    <option value="<?php echo $row_elencosenzarisposta['hash']?>">
    <?php echo $row_elencosenzarisposta['fname']." ".$row_elencosenzarisposta['lname']?></option> 
    <?php } while ($row_elencosenzarisposta mysql_fetch_assoc($elencosenzarisposta));
    $rows mysql_num_rows($elencosenzarisposta); 
    if(
    $rows 0) { mysql_data_seek($elencosenzarisposta0); 
    $row_elencosenzarisposta mysql_fetch_assoc($elencosenzarisposta); } ?> 
    </select>

  2. #2
    Codice PHP:
    <select name="username[]" size="12" multiple="multiple"
    in questo modo ricevi un array dei valori selezionati, poi puoi usare le prepared statement di mysqli

  3. #3
    ah ok grazie...ma sul lato pratico come diventa il codice?

  4. #4
    in pratica prima c'è questo codice che inserisce i record

    Codice PHP:
    $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 partecipanti (evento, username, presente) VALUES (%s, %s, %s)",
                           
    GetSQLValueString($_POST['evento'], "text"),
                           
    GetSQLValueString($_POST['username'], "text"),
                           
    GetSQLValueString($_POST['presente'], "int"));

      
    mysql_select_db($database_server$server);
      
    $Result1 mysql_query($insertSQL$server) or die(mysql_error());

      
    $insertGoTo "modificapresenti.php";
      if (isset(
    $_SERVER['QUERY_STRING'])) {
        
    $insertGoTo .= (strpos($insertGoTo'?')) ? "&" "?";
        
    $insertGoTo .= $_SERVER['QUERY_STRING'];
      }
      
    header(sprintf("Location: %s"$insertGoTo));

    e invece questo è il codice del form intero

    Codice PHP:
    <form action="<?php echo $editFormAction?>" method="post" name="form1" id="form1">
     
    <table align="center">
        <tr valign="baseline">
          <td align="left" valign="top" nowrap="nowrap">Nome socio:</td>
          <td> <select name="username[]" size="12" multiple="multiple">  
            <?php
    do {  
    ?>
            <option value="<?php echo $row_elencosenzarisposta['hash']?>"><?php echo $row_elencosenzarisposta['fname']." ".$row_elencosenzarisposta['lname']?></option>
            <?php
    } while ($row_elencosenzarisposta mysql_fetch_assoc($elencosenzarisposta));
      
    $rows mysql_num_rows($elencosenzarisposta);
      if(
    $rows 0) {
          
    mysql_data_seek($elencosenzarisposta0);
          
    $row_elencosenzarisposta mysql_fetch_assoc($elencosenzarisposta);
      }
    ?>
          </select></td>
        </tr>
        <tr> </tr>
        <tr valign="baseline">
          <td nowrap="nowrap" align="right"></td>
          <td valign="baseline"><table>
            <tr>
              <td><input type="radio" name="presente" value="1" />
                Presente</td>
            </tr>
            <tr>
              <td><input type="radio" name="presente" value="0" />
                Assente</td>
            </tr>
          </table></td>
        </tr>
        <tr valign="baseline">
          <td nowrap="nowrap" align="right"></td>
          <td><input type="submit" value="Conferma" /></td>
        </tr>
      </table>
        <input type="hidden" name="evento" value="<?php echo $_GET['id']; ?>" />
      <input type="hidden" name="MM_insert" value="form1" />
    </form>

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 © 2025 vBulletin Solutions, Inc. All rights reserved.