[PHP] selezionare righe di una tabella con le checkbox e cancellarle

  • Creatore Discussione Creatore Discussione Lalli
  • Data di inizio Data di inizio

Lalli

Nuovo Utente
6 Ott 2016
15
0
1
28
Ciao a tutti sono sempre io :p:D

stavo pensando per il mio programmino di creare una tabella con delle righe selezionabili, e una volta selezionate cancellarle grazie a delle checkbox (fin qui nessun problema).

solo che quando vado a eseguire il programma mi da questo errore "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ID'] ?> '' at line 1" e non capisco come mai..:(:(

vi posto il codice :

PHP:
<html>
<title>Elimina</title>
<head>
<title>Eliminazione</title>
<style>
table, th, td
{
    border: 1px solid black;
   
}
</style>
</head>
<body>
<h1>Eliminazione</h1>

<form action="Eliminazione.php" method="post" >


<input type="submit" name="id" value="Elimina" onClick="return confirm('Sicuro di voler cancellare questo record ?');" />

<a href='Inserimento.php'><input type="submit" name="Inserimento" value="inserisci un record nel database" /></a>
<a href='Modifica.php'><input type="submit" name="Modifica" value="Modifica un record nel database" /></a>
<a href='Ricerca.php'><input type="submit" name="Ricerca" value="Ricerca un record nel database" /></a>



<table>

<?php
$user="xxxx";
$pass="****";
$dbh = mysql_connect("xxxx","xxxx",$pass);
mysql_select_db('xxxx') or die(mysql_error());

?>
<table style="width:100%">
<tr>
    <th>#</th>
    <th>ID</th>
    <th>Filiale</th>
    <th>Ufficio</th>
    <th>Utente</th>
    <th>Tipo</th>
    <th>Marca</th>
    <th>Modello</th>
    <th>Matricola</th>
    <th>Note</th>
    <th>DataDDT</th>
    <th>DDT</th>
    <th>Fornitore</th>
    <th>ACQNOL</th>
    <th>Scadenza</th>
</tr>
<?php

        $user="xxx";
        $pass="****";
        $dbh = mysql_connect("xxxx","xxxx",$pass);
        mysql_select_db('xxxx') or die(mysql_error());
        $stmt = ("Select * from gestione");
        $ris= mysql_query($stmt) or die($stmt."<br/><br/>".mysql_error());
        $count=mysql_num_rows($ris);
        while($rigo=mysql_fetch_array($ris))
        {
           
?>
        <tr>
         <td><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?phpecho $rigo['ID']?>"/></td>
<?php
echo"  <td> " . $rigo['ID'] . " </td> <td> " . $rigo['Filiale'] . " </td> <td> " . $rigo['Ufficio'] . " </td> <td> " . $rigo['Utente'] . " </td> <td> " . $rigo['Tipo'] . " </td> <td> " . $rigo['Marca'] . " </td> <td> " . $rigo['Modello'] . " </td> <td> " . $rigo['Matricola'] . " </td> <td> " . $rigo['Note'] . " </td> <td> " . $rigo['DataDDT'] . " </td> <td> " . $rigo['DDT'] . " </td><td> " . $rigo['Fornitore'] . " </td> <td> " . $rigo['ACQNOL'] . " </td> <td> " . $rigo['Scadenza'] . " </tr>";
}  
    try
    {
       
        if(isset($_POST['id']))
        {
           
               //echo "<meta http-equiv='Refresh' content='3; URL=Eliminazione.php'>";
               if (count($_POST['checkbox']) > 0)
               {
                   foreach ($_POST['checkbox'] as $del_id)
                   {
                       $sql = "DELETE FROM gestione WHERE ID =' " . $del_id . " ' ";
                       $ris = mysql_query($sql) or die(mysql_error());
                      
                   }
             }      
        }
   
    }
catch (PDOException $e) {
    print "Error!: " . $e->getMessage() . "<br/>";
    die();
}  
?>  

</table>
</form>
</body>
</html>
Grazie mille anticipatamente per l'interessamento e per eventuali risposte :)
 
Ciao Lalli.

Il codice incriminato dovrebbe essere questo.

$sql = "DELETE FROM gestione WHERE ID =' " . $del_id . " ' ";

Quando fai il debug per essere sicuro della sintassi della query metti un echo $sql; e vedi effettivamente com'è formata la query.

L'errore potrebbe essere:
1) ID è un campo numerico e tu ci metti gli apici (quindi lo consideri un campo stringa)
2) Hai messo degli spazi tra gli apici e le virgolette quindi il risultato è ad esempio (il meno per delimitare il risultato) - 1 - e non -1-
3) per qualche ragione $del_id non è valorizzata
 
Ok quando provo ti faccio sapere, grazie mille !


Aggiornamento: risolto così
PHP:
if(isset($_POST['el_id']))
        {
            
               echo "<meta http-equiv='Refresh' content='0'; URL=Eliminazione.php'>";
               if (count($_POST['checkbox']) > 0)
               {
                   foreach ($_POST['checkbox'] as $del_id)
                   {
                       $sql = "DELETE FROM gestione WHERE id ='" . $del_id . " '";
                       $ris = mysql_query($sql) or die(mysql_error());
                      
                   }
             }       
        }
 
Ultima modifica:

Discussioni simili