php - my delete query won't delete anything from mysql -
i'm building website .. , in page, want show data in mysql , select 1 row , delete mysql, when testing (row_affect) gives nothing has changed.. shows info database, , ok, when want delete, won't delete. code..
<?php //connect db //include_once("project/cieconn.php"); $mysqlcon= mysqli_connect("localhost", "root", "","cie") or die(mysqli_connect_error()); mysqli_select_db($mysqlcon,'cie') or die ("no database"); $idid = isset($_post['id']); // 1 2 if( isset($_post['delete']) ){ if( empty($idid) || $idid == 0 ){ echo"<h4> please choose delete </h4>"; }else{ echo $idid ; echo "<br>" ; $impid = implode(" , " , $_post['id']); $sqldelete = ("delete shopping id in ('".$impid."') ") ; $deletequery = mysqli_query($mysqlcon,$sqldelete) or die ("error : ".mysqli_error($mysqlcon)); // test ////////----------------------------------------//////////// if (mysqli_affected_rows($mysqlcon) > 0) { echo "you have updated data.<br><br>"; } else { echo "the data submitted matched current data nothing changed.<br><br>"; } if (!$deletequery) { die("invalid query:" ); } //var_dump($deletequery ); ////////----------------------------------------//////////// if(isset($deletequery )){ echo"<h3> done deleting .... !!</h3>"; }else {echo "error while deleting...";} } } $sqlcommand = "select * shopping "; $result = mysqli_query($mysqlcon,$sqlcommand) or die(mysqli_error($mysqlcon)); echo ' <form action= "shop_sup.php" method = "post"> <table width ="100%" cellpadding ="4" border="1" > <tr> <th>check </th> <th>id</th> <th>jobs name</th> <th>description</th> <th> no students needed</th> <th>due date</th> </tr>'; while ($row = mysqli_fetch_array($result) ){ // name = 'id[]' echo "<tr> <td> <input type='checkbox' name='id[]' vlaue='". $row['id'] ."' /> </td> <td>". $row['id'] ." </td> <td> ". $row['jobname'] ." </td> <td> ". $row['description'] ." </td> <td> ". $row['nostudent'] . "</td> <td>". $row['date'] ." </td> </tr>"; } echo ' </table> <br/> <div align="center"> <input type="submit" name="delete" value="delete" /> <input type="reset" value="clear marks" /> </div> </form> '; ?> <html> <head><title> shopping.. </title></head> <br> <body> </body> </html>
the problem because of incorrect single quotes in delete query,
$sqldelete = ("delete shopping id in ('".$impid."') ") ; ^ ^ so delete query should this:
$sqldelete = "delete shopping id in (" . $impid . ")"; edited:
if $_post['id'] contains array of strings, this:
// code $impid = implode("' , '" , $_post['id']); $sqldelete = "delete shopping id in ('" . $impid . "')"; // execute $sqldelete query also there's problem in html code. @ statement here,
<td> <input type='checkbox' name='id[]' vlaue='". $row['id'] ."' /> </td> ^ here it should be,
<td> <input type='checkbox' name='id[]' value='". $row['id'] ."' /> </td>
Comments
Post a Comment