2016-01-21 104 views
-2

我正在做一個小小的php + mysql用於刪除數據庫中的行,當我按下按鈕時什麼也沒有發生。我很感謝你的幫助。用php刪除mysql中的一行

這個PHP顯示了一類員工必須做的課程列表,刪除按鈕應該刪除您選擇列表的過程。 ?

例如

cargocurso.php ID = 1

顯示員工的類型,例如:

銷售經理

,並在表中與該類型相關的課程empleyee:

1 |銷售| (刪除按鈕)

<?php 

$link = mysql_connect("localhost", "root","123456"); 
mysql_select_db("ecl", $link); 

if (isset($_GET['id'])) { 

    $id=$_GET['id'];; 

    $sql = mysql_query("select k.idcargo, c.fullname, k.nombre_cargo, d.cursoid, d.id from mdl_course as c 
         inner join mdl_user_cursos_asoc as d on c.id = d.cursoid 
         inner join mdl_user_cargo as k on d.cargoid = k.idcargo where k.idcargo=$id", $link); 

    if (isset($_POST['delete'])) { 
    $idasoc = $_POST['idasoc']; 
    $sql2 = "DELETE FROM mdl_user_cursos_asoc WHERE id = '.$idasoc.'"; 
    $result = mysql_query($sql2); 

     echo "has been deleted."; 
    } 

    if(mysql_num_rows($sql) == 0) { 

     echo "No hay cursos asociados"; 

    } else { 

    $cargnombre = mysql_fetch_array($sql); 

     echo $cargnombre[2]."</br></br>"; 

    echo "<form action='".$_SERVER['PHP_SELF']."' method='post'>"; 
    echo "<table border='1px'>"; 

    while ($row = mysql_fetch_array($sql)) { 
       echo "<tr>"; 
       echo "<input type='hidden' name='idasoc' value = '.$row[4].'>"; 
       echo "<td>$row[1]</td>"; 
       echo "<td><input type='submit' name='delete' value='Eliminar'></td>"; 
       echo "</tr>"; 
      } 
    echo "</table>"; 
    echo "</form>"; 


    } 

    } 
?> 

感謝您的幫助!

+1

您不應該使用不推薦使用的'mysql_ *'函數。代之以使用mysqli或PDO,並且您的代碼可廣泛應用於SQL注入。總是逃避你的投入! (如果你使用mysqli或者PDO,你應該使用這個準備好的語句)。 –

+0

您是否檢查過POST變量$ idasoc是否有使用isset的任何值($ _ POST ['idasoc'])? –

+0

請查閱以下鏈接http://php.net/manual/en/function.mysql-error.php和http://php.net/manual/en/function.error-reporting.php 並將其應用於您的代碼,你會看到你犯的錯誤。 –

回答

3

在刪除記錄的sql語句中有一個小錯誤 - 裏面有虛假的時間段.。更改爲:

$sql2 = "DELETE FROM mdl_user_cursos_asoc WHERE id = '{$idasoc}'"; 

另外,其他人可能會注意到LSO - 停止使用現在已經過時mysql_*功能,因爲它們通常現在被認爲是不安全的。

除了在sql中引用和句點的誤用,循環中還有另一個輸出表的內容。

<?php 
    $link = mysql_connect("localhost", "root","123456"); 
    mysql_select_db("ecl", $link); 

    if(isset($_GET['id'])) { 

     $id=$_GET['id']; 

     $sql = mysql_query("select k.idcargo, c.fullname, k.nombre_cargo, d.cursoid, d.id from mdl_course as c 
          inner join mdl_user_cursos_asoc as d on c.id = d.cursoid 
          inner join mdl_user_cargo as k on d.cargoid = k.idcargo where k.idcargo=$id", $link); 

     if (isset($_POST['delete'], $_POST['idasoc'])) {/* Combine `isset` to test multiple POST vars are set */ 
      $idasoc = $_POST['idasoc']; 
      $sql2 = "DELETE FROM mdl_user_cursos_asoc WHERE id = '{$idasoc}'";/* changed here */ 
      $result = mysql_query($sql2); 

      echo "has been deleted."; 
     } 

     if(mysql_num_rows($sql) == 0) { 
      echo "No hay cursos asociados"; 
     } else { 
      $cargnombre = mysql_fetch_array($sql); 
      echo $cargnombre[2]."</br></br>"; 


     echo " 
     <form action='".$_SERVER['PHP_SELF']."' method='post'> 
      <table border='1px'>"; 

      while($row = mysql_fetch_array($sql)) { 
        echo " 
        <tr> 
         <td> 
          <input type='hidden' name='idasoc' value = '{$row[4]}'><!-- changed here --> 
          {$row[1]} 
         </td> 
         <td><input type='submit' name='delete' value='Eliminar'></td> 
        </tr>"; 
       } 
     echo " 
      </table> 
     </form>"; 
     } 
    } 
?> 

如果你不打算改變的mysqli或PDO預處理語句(其減輕對SQL注入),那麼至少可以保證您篩選從用戶接收到的數據 - 至少例如mysql_real_escape_string提供了一些保護。

+0

你是對的那些「。」,謝謝你的幫助! – rfcabal

+0

我有一種其他問題,當你按下刪除按鈕時,它刪除了最後一行,而不是你選擇的那一行。任何想法可能是錯誤的? – rfcabal

+1

是的。一般來說 - 將按鈕類型從'submit'更改爲'button',添加'data-id = $ id'屬性並使用javascript通過附加到按鈕的eventlistener提交表單 – RamRaider

0
<?php 

$cDatabase['Server'] = '127.0.0.1'; // Or Localhost // Or Remote Server IP 
$cDatabase['User'] = 'Root'; 
$cDatabase['Password'] = '123456'; 
$cDatabase['DBName'] = 'ecl'; 

$DBConnect = mysql_connect($cDatabase['Server'],$cDatabase['User'],$cDatabase['Password']); 
if($DBConnect) 
{ 
    $DBSelect = mysql_select_db($cDatabase['DBName'],$DBConnect); 
    if($DBSelect == false) 
     echo 'DB Select Failed !'; 

} 
else 
    echo 'DB Server Connect Failed !'; 

if(extract($_GET['id'])) 
{ 

    $SQL = "SELECT k.idcargo, c.fullname, k.nombre_cargo, d.cursoid, d.id FROM mdl_course AS 
      c INNER JOIN mdl_user_cursos_asoc AS 
      d ON c.id = d.cursoid INNER JOIN mdl_user_cargo AS 
      k ON d.cargoid = k.idcargo 
      WHERE k.idcargo=".$_GET['id']; 

    $SQL = mysql_query($SQL,$DBConnect); 

    if (extract($_POST['delete'])) 
    { 
     if(empty($_POST['idasoc'])) 
      echo 'Whooops ! IDASOC is a empty.'; 
     else 
     { 
      $DELETESQL = "DELETE FROM mdl_user_cursos_asoc WHERE id = '".$_POST['idasoc']."'"; 
      $DELETESQL = mysql_query($DELETESQL,$DBConnect); 

      if($DELETESQL) 
       echo 'Success. Has been deleted.'; 
     } 
    } 
    else 
    { 

     if(mysql_num_rows($SQL) == 0) 
      echo "No hay cursos asociados"; 
     else 
     { 
      $cargnombre = mysql_fetch_array($SQL); 
      echo $cargnombre[2]."</br></br>"; 

      echo "<form action='".$_SERVER['PHP_SELF']."' method='POST'>"; 
      echo '<table border="1px">'; 
      while ($Rows = mysql_fetch_array($SQL)) 
      { 
        echo "<tr>"; 
        echo "<input type='hidden' name='idasoc' value =".$Rows[4].">"; 
        echo "<td>".$Rows[1]."</td>"; 
        echo "<td><input type='submit' name='delete' value='Eliminar'></td>"; 
        echo "</tr>"; 
      } 
     echo "</table>"; 
     echo "</form>"; 
     } 
    } 
} 
?>