Ciao a tutti!
Ho un problema che spero possiate aiutarmi a risolvere.
Sto realizzando un database.
Ho scritto una pag contenente un form mediante il quale riempire 2 tabelle del mio db.
Il form è costituito da 1 riga nella quale si inserisce una domanda
e 10 righe create con un ciclo while ed il cui nome è UNA VARIABILE!!!!!!!!!!!!![]()
![]()
L'inserimento della domanda va a buon fine, ma quello delle risposte no...il problema è che:
mentre per la domanda mi basta scrivere GetSQLValueString($_POST['testo'] per le risposte non so come fare dal momento che loro hanno un nome dinamicocioè sono variabili
![]()
Vi posto il codice e mi dite che ne pensate...
al posto dei ??????????????? cosa dovrei mettere?
<!doctype html public "-//W3C//DTD HTML 4.0 //EN">
<html>
<head>
<title>Inserimento domande</title>
</head>
<body>
<?php
include ('page.php');
include ('connessione_test.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;
}
//IL CODICE QUI' SOPRA LO INSERISCO SEMPRE QND DEVO CREARE FORM X INSERIRE DATI NEL DB********************************
$query_dati_gruppo = "SELECT * FROM gruppo";
$dati_gruppo = mysql_query($query_dati_gruppo, $conness_test) or die(mysql_error());
$row_dati_gruppo = mysql_fetch_assoc($dati_gruppo);
$totalRows_dati_gruppo = mysql_num_rows($dati_gruppo);
$query_dati_risposte = "SELECT * FROM risposte";
$dati_risposte = mysql_query($query_dati_risposte, $conness_test) or die(mysql_error());
$row_dati_risposte = mysql_fetch_assoc($dati_risposte);
$totalRows_dati_risposte = mysql_num_rows($dati_risposte);
$query_dati_corrispondenza = "SELECT * FROM corrispondenza";
$dati_corrispondenza = mysql_query($query_dati_corrispondenza, $conness_test) or die(mysql_error());
$row_dati_corrispondenza = mysql_fetch_assoc($dati_corrispondenza);
$totalRows_dati_corrispondenza = mysql_num_rows($dati_corrispondenza);
$editFormAction = $_SERVER['PHP_SELF'];
$messaggio_errore = '';
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}
$errore_data=0;
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
// mysql_select_db($database, $conness_test);
// $codice_nuovo = mysql_insert_id();
while ($row_dati_gruppo = mysql_fetch_assoc($dati_gruppo));
mysql_data_seek($dati_gruppo, 0);
?>
<h3 align="center">
<span class="biancobig">INSERIMENTO DOMANDE & RISPOSTE</span><?php echo "
$messaggio_errore" ;?>
</h3>
<form method="post" name="form1" action="<?php
echo $editFormAction; ?>">
<table width="450" border="1" align="center" cellpadding="3" cellspacing="1" bordercolor="#000000" bgcolor="#800040">
<tr valign="baseline">
<td align="right" nowrap bgcolor="#800040">Gruppo</td>
<td>
<select name="idgruppo" id="idgruppo">
<?
while ($row_dati_gruppo = mysql_fetch_assoc($dati_gruppo))
{
echo "<option value=\"".$row_dati_gruppo['idgruppo']."\">".$row_dati_gruppo['nomegruppo']."</option>";
}
?>
</select>
</td></td><td>
</tr>
<tr valign="baseline">
<td align="right" nowrap bgcolor="#800040">Domanda :</td>
<td><input type="text" name="testo" value="" size="48"></td></td><td>
</tr>
<?
$i=0;
while($i<$NUM_MAX_VISUALIZZAZIONE){
$rispostax=sprintf("Risposta %d",$i+1);
echo '
<tr valign="baseline">
<td align="right" nowrap bgcolor="#800040">'.$rispostax.':</td>
<td><input type="text" name="'.$rispostax.'" value="" size="48"></td><td><input type=checkbox name='.$rispostax.' ></td>
</tr>';
$i++;
}
?>
<tr valign="baseline">
<td align="right" nowrap bgcolor="#800040"></td>
<td><input type="submit" value="Inserisci"></td></td><td>
</tr>
</table>
<input name="dom" type="hidden" id="dom" value="">
<input type="hidden" name="MM_insert" value="form1">
</form>
</p>
<?
$i=0;
$fine=0;
while($i<$NUM_MAX_VISUALIZZAZIONE &&!$fine ){
$rispostax=sprintf("Risposta%d",$i+1);
if($rispostax=="") $fine=1;
else{
$N=$i+1;
$i++;
}
}
$array_ind[$N]=0;
$k2 = $_POST["idgruppo"];
// Query per la domanda
$insert2SQL = sprintf("INSERT INTO domande (iddom, idgruppo, testo) VALUES ('', '".$k2."', %s)",GetSQLValueString($_POST['testo'], "text"));
$Result2 = mysql_query($insert2SQL, $conness_test) or die(mysql_error());
$domanda_ind=mysql_insert_id();
echo "$domanda_ind";
// Query per N risposte
while ($i<$N){
$rispostax=sprintf("Risposta %d",$i+1);
echo "$rispostax";
$insertSQL = sprintf("INSERT INTO risposte (idrisp, testo) VALUES ('', %s)",GetSQLValueString($_POST['??????????????'], "text"));
$Result = mysql_query($insertSQL, $conness_test) or die(mysql_error());
$array_ind[$i] = mysql_insert_id();
$i++;
}
$i=0;
while ($i<$N){
$insertCRSP1 = sprintf("INSERT INTO corrispondenza (iddom, idrisp, correttezza) VALUES ('".$domanda_ind."','".$array_ind[$i]."', '1')");
$ResultCRSP1 = mysql_query($insertCRSP1, $conness_test) or die(mysql_error());
$i++;
}
}
?>
<?php
mysql_free_result($dati_gruppo);
mysql_free_result($dati_risposte);
mysql_free_result($dati_corrispondenza);
?>
</body>
</html>
Grazie a tutti innanzitutto per il tempo dedicatomi!!![]()