2014-03-19 39 views
0

我被困在我的一個應用程序模塊中。我必須在我的應用程序中設置多個刪除選項。 我已經使用了下面的代碼。設計非常好。所有我想在其中添加多個刪除選項。
以下是我已經嘗試了代碼:通過結帳多次刪除

<html> 
<head> 
<title>Strad Hosting Limited -Web Hosting</title> 

<script language="javascript"> 
function validate() 
{ 
    var chks = document.getElementsByName('checkbox[]'); 
    var hasChecked = false; 
    for (var i = 0; i < chks.length; i++) 
    { 
     if (chks[i].checked) 
     { 
      hasChecked = true; 
      break; 
     } 
    } 
    if (hasChecked == false) 
    { 
     alert("Please select at least one."); 
     return false; 
    } 
    return true; 
} 
</script> 

</head> 
<body > 

<?php 
    $con=mysqli_connect("localhost","stradsol","D#v,b5TnQ!D!","stradsol_sales"); 
    if (mysqli_connect_errno()) 
    { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
    $result = mysqli_query($con,"SELECT * FROM Persons"); 

    echo "<form name='form1' method='post' action='' onSubmit='return validate();'>"; 
    echo "<table border='1' style=' 
      background-color: white;'> 
      <tr> 
       <th></th> 
       <th>id</th> 
       <th style='padding-left: 11px;'>Lead Generated Date </th> 
       <th>name</th> 
       <th>email</th> 
       <th>contacts</th> 
       <th>requirement</th> 
       <th style='display:none;'>Company name</th> 
       <th style='display:none;'>Address</th> 
       <th>Next Follow-Up date</th> 
       <th>Final_Details</th> 
       <th style='display:none;'>Status</th> 
       <th style='padding-left: 12px; display:none;'>Lead Closed Date</th> 
       <th>EDIT</th> 
       <th>Follow-up History</th> 
       <th>Add Follow-up</th> 
       <th>Delete Record</th> 
      </tr>"; 

    while($row = mysqli_fetch_array($result)) 
    { 
     echo "<tr>"; 
     echo "<td><input name='checkbox[]' type='checkbox' id='checkbox[]' value='".$rows['id']."'></td>"; 
     echo "<td>" . $row['id'] . "</td>"; 
     echo "<td>" . $row['date'] . "</td>"; 
     echo "<td><a href='config_info.php?id=" . $row['id'] . "'>".$row['name']."</a></td>"; 
     echo "<td>" . $row['email'] . "</td>"; 
     echo "<td>" . $row['contacts'] . "</td>"; 
     echo "<td>" . $row['requirement'] . "</td>"; 

     echo "<td style='display:none;'>" . $row['company_name'] . "</td>"; 
     echo "<td style='display:none;'>" . $row['address'] . "</td>"; 
     echo "<td>" . $row['startdate'] . "</td>"; 
     echo "<td>" . $row['final_details'] . "</td>"; 
     echo "<td style='display:none;'>" . $row['status'] . "</td>"; 
     echo "<td style='display:none;'>" . $row['lead_close'] . "</td>"; 
     echo "<td><a href='edit_eg1.php?id=" . $row['id'] . "'>Edit</a></td>"; 
     echo "<td><a href='Follow_history.php?id=" . $row['id'] . "'>Follow_history</a></td>"; 
     echo "<td><a href='add_follow.php?id=" . $row['id'] . "'>Followup</a></td>"; 
     echo "<td><a href='delete.php?id=" . $row['id'] . "'>Delete</a></td>"; 
     echo "</tr>"; 
    } 
    echo "<tr><td><input name='delete' type='submit' id='delete' value='Delete'></td></tr>"; 

    $count=mysqli_num_rows($result); 
    if(isset($_POST['delete'])){ 
     for($i=0;$i<count($_POST['checkbox']);$i++){ 
      $del_id=$_POST['checkbox'][$i]; 
      $sql = "DELETE FROM Persons WHERE id='$del_id'"; 
      echo $sql; 
      $result2 = mysqli_query($con,$sql); 
     } 

     // if successful redirect to delete_multiple.php 
     if($result2) 
     { 
      echo "<meta http-equiv=\"refresh\" content=\"0;URL=edit_table_del.php\">"; 
     } 
    } 

    mysqli_close($con); 
    echo "</table>"; 
    echo "</form>"; 
?> 

<br><br> 
<a href="index2.php">[ Back To Home ]</a> 

</body> 
</html> 

我堅持,查詢不工作,我認爲。我不知道我錯過了什麼。

+0

你可以只添加你的錯誤發生的代碼嗎?如果您不確定問題的具體位置,可以使用'var_dump()'來打印變量並檢查它給出的意外結果。 –

回答

0

嘗試更換

$sql = "DELETE FROM Persons WHERE id='$del_id'"; 

隨着

$sql = "DELETE FROM Persons WHERE id=$del_id"; 

我假設的ID是那麼不需要單引號的數字。