View Full Version : [php - mysql]estrarre valori dalle celle ed effettuare un eliminazione dal database
Salve a tutti,ho il seguente problema: praticamente,dopo aver visualizzato tutte le prenotazioni nel mio sito,accanto ad ognuna di esse c'è una check box e sotto ci sono i pulsanti elimina ed elimina tutte le prenotazioni visualzzate...devo eliminare dal database le prenotazioni che l' utente dopo aver selezionato vuole eliminare spuntando le check delle relative prenotazioni..adesso,la query dell' elimianzione la so fare(<?php,crea connessione,seleziona database ed esegui la query "delete * from prenotazioni where nome=...) ma :
come faccio a prendere i valori della tabella che stanno negli elementi <td> che stanno negli elementi <tr> i quali rappresentano una riga?
e poi come faccio a passare questi valori al file php che contiene la query per effettuare l' eliminazione dal database?
nell' inserimento avevo il form che passava i paramentri al file .php tramite post o get ma adesso come faccio?
anonimizzato
17-09-2009, 19:06
Posta il codice altrimenti diventa difficile dare una risposta corretta.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
function visua(){
for (var i=0;i<11;i++)
if (prova.check[i]==true)alert("la check " + i +" è selezionata");
return false;
}
</script>
</head>
<body bgcolor="#640035">
<?php
extract($_POST);
$datainizio="";
$datafine="";
if (($anno!="") && ($mese!="") && ($giorno!=""))
$datainizio=$anno."/".($mese+1)."/".$giorno;
if (($annofine!="") && ($mesefine!="") && ($giornofine!=""))
$datafine=$annofine."/".($mesefine+1)."/".$giornofine;
//print("$data");
$creadatabase="CREATE DATABASE IF NOT EXISTS bancadati;";
$usadatabase="use bancadati;";
$creatabella="CREATE TABLE IF NOT EXISTS prenotazioni(nome char(20) not null,cognome char(20) not null,email char(30) not null,telefono char(10) not null,data date not null,tipo char(6) not null,nospiti integer not null,richieste text);";
$primo=0;
if ($nome!=""){
$primo=1;
$stringawhere="nome='".$nome."'";}
if ($cognome!=""){
if ($primo==0) $primo=1;
else $stringawhere= $stringawhere . " and ";
$stringawhere=$stringawhere . "cognome='".$cognome."'";}
if ($email!=""){
if ($primo==0) $primo=1;
else $stringawhere= $stringawhere . " and ";
$stringawhere=$stringawhere . "email='".$email."'";}
if ($telefono!=""){
if ($primo==0) $primo=1;
else $stringawhere= $stringawhere . " and ";
$stringawhere=$stringawhere . "telefono='".$telefono."'";}
if ($datainizio!=""){
if ($primo==0) $primo=1;
else $stringawhere= $stringawhere . " and ";
$stringawhere=$stringawhere . "data>='".$datainizio."'";}
if ($datafine!=""){
if ($primo==0) $primo=1;
else $stringawhere= $stringawhere . " and ";
$stringawhere=$stringawhere . "data<='".$datafine."'";}
if ($radiobutton!=""){
if ($primo==0) $primo=1;
else $stringawhere= $stringawhere . " and ";
$stringawhere=$stringawhere . "tipo='".$radiobutton."'";}
if ($numospiti!=""){
if ($primo==0) $primo=1;
else $stringawhere= $stringawhere . " and ";
$stringawhere=$stringawhere . "nospiti=".$numospiti;}
$stringawhere=$stringawhere . ";";
$query="select * from prenotazioni where ".$stringawhere;
//echo "$query";
if (!($database = mysql_connect("localhost","root","ppw")))
die("Impossibile connettersi al databse");
mysql_query($creadatabase,$database); // print("Database creato");
mysql_query($usadatabase,$database); //print("Database impostato");
mysql_query($creatabella,$database); //print("Tabella creata");;
if (!mysql_select_db("bancadati",$database))
die("Impossibile aprire la tabella del database");
if ( !($result = mysql_query( $query,$database))){
print("Impossibile eseguire la query");
die (mysql_error());
}
?>
<?php
//print("<script type=\"text/javascript\">");
//print("window.open(\"risultati.html\",\"Elenco prenotazioni\",\"resizable=false,toolbar=false,menubar=false,status=false,location=false,directories=false,scrollbars=true\");");
//print("</script>");
print("<table bgcolor=\"#640035\" border =\"1\" cellpadding=\"0\" width=\"100%\">");
print("<thead>");
print("<tr>");
print("<td></td>");
print("<td><font color=\"#FFFFFF\" >NOME</font></td>");
print("<td><font color=\"#FFFFFF\" >COGNOME</font></td>");
print("<td><font color=\"#FFFFFF\" >E-MAIL</font></td>");
print("<td><font color=\"#FFFFFF\" >TELEFONO</font></td>");
print("<td><font color=\"#FFFFFF\" >DATA PRENOTAZIONE</font></td>");
print("<td><font color=\"#FFFFFF\" >TIPO</font></td>");
print("<td><font color=\"#FFFFFF\" >NUMERO OSPITI</font></td>");
print("<td><font color=\"#FFFFFF\" >RICHIESTE</font></td>");
print("</tr></thead><tbody>");
for ($counter = 0;$row=mysql_fetch_row( $result);$counter++){
print("<tr>");
print("<td><input type =\"checkbox\" name =\"check\" value=\"\" /></td>");
foreach ($row as $key =>$value){
/*print("<td> <font color=\"#FFFFFF\" >$value</font></td>");
print("</tr>");*/
/*echo "<script type = \"text/javascript\" language=\"javascript\">\n";
echo "alert($key);";
echo "</script>\n";}*/
if (($key==7) && (strlen($value)>56))
print("<td><div style = \"width:350px; height:80px; overflow:scroll\"> <font color=\"#FFFFFF\" >$value</font></div></td>");
else
print("<td> <font color=\"#FFFFFF\" >$value</font></td>");
}
print("</tr>");
}
print("</tbody></table>");
mysql_close($database);
?>
<!--</table>-->
<form id="prova" action="JavaScript::visua()">
<!--<input type="button" name="elimina" value="Elimina selezionati" onclick="visua()" />-->
<input type="submit" name="eliminatutti" value="Elimina tutti" />
</form>
</body>
</html>
dunque spiego meglio ciò che voglio:all' inizio questo file mostra i risultati in base ai parametri passati dal form del file .html(il cui codice non ho postato e che comunque non serve e che non ha niente a che vedere con il form con id prova del codice postato)...all' inizio vengono estratti i dati,vengono accoppiati i campi delle date,viene creata la stringa di selezione dei risultati in base al valore dei campi(cioè se il campo non è vuoto viene inserita la ricerca nella query di selezione)..al termine viene creata la stringa di selezione,viene creato il database se non esiste, il dataabse appena creato viene settato impostato come in uso e poi viene creata la tabella se non esiste;dunque ogni record della tabella prenotazioni è costituito dai campi nome,cognome,email,telefono,data,tipo,numero ospiti e richieste..e tutti questi campi vengono visualizzati nella tabella a video(viene fatta una select dal database)...in più però,per ogni riga della tabella,accanto aggiungo una checkbox che serve per selezionare le prenotazioni da eliminare con i pulsanti che stanno sotto..il funzionamento è semplice:seleziono tutte le prenotazioni che voglio eliminare e poi clicco sul pulsante elimina oppure se voglio eliminare tutte le prenotazioni ricercate clicco sul pulsante elimina tutte le prenotazioni..adesso: per eliminare devo creare la stringa sql "delete * from prenotazioni where nome = (campo tabella nome),cognome=.... ed ecco perchè mi serve accedere ai valori dei <td> della tabella:quindi devo scorrere tutte le check e per ogni check selezionata devo eliminare dal database quella prenotazione tramite il delete che ho detto prima...se invece devo eliminare tutti chiaramente non faccio il controllo sulle check ma estraggo ed elimino direttamente...
spero di essere stato chiaro
in internet ho torvato più o meno una soluzione la problema e quindi ho cambiato il codice:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body bgcolor="#640035">
<?php
extract($_POST);
if ( isset ( $pulsricerca ) )
{
$datainizio="";
$datafine="";
if (($anno!="") && ($mese!="") && ($giorno!=""))
$datainizio=$anno."/".($mese+1)."/".$giorno;
if (($annofine!="") && ($mesefine!="") && ($giornofine!=""))
$datafine=$annofine."/".($mesefine+1)."/".$giornofine;
//print("$data");
$creadatabase="CREATE DATABASE IF NOT EXISTS bancadati;";
$usadatabase="use bancadati;";
$creatabella="CREATE TABLE IF NOT EXISTS prenotazioni(id int(11) NOT NULL auto_increment,nome char(20) not null,cognome char(20) not null,email char(30) not null,telefono char(10) not null,data date not null,tipo char(6) not null,nospiti integer not null,richieste text,PRIMARY KEY (id));";
$primo=0;
if ($nome!=""){
$primo=1;
$stringawhere="nome='".$nome."'";}
if ($cognome!=""){
if ($primo==0) $primo=1;
else $stringawhere= $stringawhere . " and ";
$stringawhere=$stringawhere . "cognome='".$cognome."'";}
if ($email!=""){
if ($primo==0) $primo=1;
else $stringawhere= $stringawhere . " and ";
$stringawhere=$stringawhere . "email='".$email."'";}
if ($telefono!=""){
if ($primo==0) $primo=1;
else $stringawhere= $stringawhere . " and ";
$stringawhere=$stringawhere . "telefono='".$telefono."'";}
if ($datainizio!=""){
if ($primo==0) $primo=1;
else $stringawhere= $stringawhere . " and ";
$stringawhere=$stringawhere . "data>='".$datainizio."'";}
if ($datafine!=""){
if ($primo==0) $primo=1;
else $stringawhere= $stringawhere . " and ";
$stringawhere=$stringawhere . "data<='".$datafine."'";}
if ($radiobutton!=""){
if ($primo==0) $primo=1;
else $stringawhere= $stringawhere . " and ";
$stringawhere=$stringawhere . "tipo='".$radiobutton."'";}
if ($numospiti!=""){
if ($primo==0) $primo=1;
else $stringawhere= $stringawhere . " and ";
$stringawhere=$stringawhere . "nospiti=".$numospiti;}
$stringawhere=$stringawhere . ";";
//nome,cognome,email,telefono,data,tipo,nospiti,richieste
$query="select * from prenotazioni where ".$stringawhere;
echo "$query";
if (!($database = mysql_connect("localhost","root","ppw")))
die("Impossibile connettersi al databse");
mysql_query($creadatabase,$database); // print("Database creato");
mysql_query($usadatabase,$database); //print("Database impostato");
mysql_query($creatabella,$database); //print("Tabella creata");;
if (!mysql_select_db("bancadati",$database))
die("Impossibile aprire la tabella del database");
if ( !($result = mysql_query( $query,$database))){
print("Impossibile eseguire la query");
die (mysql_error());
}
//print("<script type=\"text/javascript\">");
//print("window.open(\"risultati.html\",\"Elenco prenotazioni\",\"resizable=false,toolbar=false,menubar=false,status=false,location=false,directories=false,scrollbars=true\");");
//print("</script>");
echo '<form name="risultati" action="">';
echo '<table bgcolor="#640035" border ="1" cellpadding="0" width="100%">
<thead>
<tr>
<th> </th>
<th><font color="#FFFFFF" >NOME</font></th>
<th><font color="#FFFFFF" >COGNOME</font></th>
<th><font color="#FFFFFF" >EMAIL</font></th>
<th><font color="#FFFFFF" >TELEFONO</font></th>
<th><font color="#FFFFFF" >DATA PRENOTAZIONE</font></th>
<th><font color="#FFFFFF" >TIPO</font></th>
<th><font color="#FFFFFF" >NUMERO OSPITI</font></th>
<th><font color="#FFFFFF" >RICHIESTE</font></th>
</tr></thead><tbody> ';
for ($counter = 0;$row=mysql_fetch_row( $result);$counter++){
echo '<tr>';
foreach ($row as $key =>$value){
if ($key==0) echo '<td><input type ="checkbox" name ="vettore[]" value="'.$value.'" /></td>';
elseif (($key==7) && (strlen($value)>56))
echo '<td><div style = "width:350px; height:80px; overflow:scroll"> <font color="#FFFFFF">'.$value.'</font></div></td>';
else
echo '<td> <font color="#FFFFFF">'.$value.'</font></td>';
}
echo '</tr>';
}
echo '</tbody></table>';
echo '<input type="submit" name="elimina" value="Elimina selezionati" />';
echo '<input type="submit" name="eliminatutti" value="Elimina tutti" />';
echo '</form>';
mysql_free_result($result);
mysql_close($database);
}
//parte relativa all' eliminazione
elseif ( isset ($elimina))
{if ($_POST)
$idelim=isset($_POST['vettore']) ? $_POST['vettore'] : array();
if (count($idelim)>0){
$creadatabase="CREATE DATABASE IF NOT EXISTS bancadati;";
$usadatabase="use bancadati;";
$creatabella="CREATE TABLE IF NOT EXISTS prenotazioni(id int(11) NOT NULL auto_increment,nome char(20) not null,cognome char(20) not null,email char(30) not null,telefono char(10) not null,data date not null,tipo char(6) not null,nospiti integer not null,richieste text,PRIMARY KEY (id));";
$idelim=array_map('intval',$idelim);
$idelim=implode(',',$idelim);
//$query="DELETE FROM prenotazioni WHERE id =".$idelim.";";
$query="DELETE FROM prenotazioni WHERE id = 2;";
if (!($database = mysql_connect("localhost","root","ppw")))
die("Impossibile connettersi al databse");
mysql_query($creadatabase,$database); // print("Database creato");
mysql_query($usadatabase,$database); //print("Database impostato");
mysql_query($creatabella,$database); //print("Tabella creata");;
if (!mysql_select_db("bancadati",$database))
die("Impossibile aprire la tabella del database");
if ( !($result = mysql_query( $query,$database))){
print("Impossibile eseguire la query");
die (mysql_error());
}
}}
?>
</body>
</html>
però mi funziona solo dal blocco pulsricerca..quando elimino e clicco sul pulsante di nome elimina dovrebbe funzionare anche il blocco di sotto (da " //parte relativa all' eliminazione")....
vBulletin® v3.6.4, Copyright ©2000-2025, Jelsoft Enterprises Ltd.