Mysql

一次從多個表中刪除行

  • February 27, 2020

我對我剛剛編寫的這段程式碼有疑問。我的語法有問題,第一個查詢執行其目的,問題在於第二個查詢沒有。

if(isset($_POST['deletecontr'])){
 $dcquery = $_POST['dni']; // This get the ID of the person who I want to delete.
 $catchempr = ("SELECT `empresa` FROM `contratistas` WHERE dni='$dcquery'"); //This catch the name of the company of that person
 $query = ("DELETE FROM `contratistas` WHERE dni='$dcquery'"); // This works fine
 $query2 = ("DELETE FROM `empleados` WHERE empresa='$catchempr'"); This does not.
 mysqli_query($link,$query);
 mysqli_query($link,$query2); 
 echo "La empresa a sido eliminada de la base de datos";

我希望你們能看到我沒有看到的東西。

你的程式必須是完整的。

像這樣:

if(isset($_POST['deletecontr'])){
     $dcquery = $_POST['dni']; // This get the ID of the person who I want to delete.
     if (isset($dcquery)) {          
            if ($stmt = mysqli_prepare($link, "SELECT `empresa` FROM `contratistas` WHERE dni=?")) {    
               /* bind parameters for markers */
               mysqli_stmt_bind_param($stmt, "s", $dcquery);

               /* execute query */
               mysqli_stmt_execute($stmt);

               $affected_rows = mysqli_stmt_num_rows($stmt);
               if($affected_rows > 0 ){
                   /* bind result variables */
                  mysqli_stmt_bind_result($stmt, $empresa);
                  /* fetch value */
                  mysqli_stmt_fetch($stmt);
                  mysqli_stmt_affected_rows($stmt);
                  if ($stmt = mysqli_prepare($link, "DELETE FROM `contratistas` WHERE dni=?")) {
                       mysqli_stmt_bind_param($stmt, "s", $dcquery);
                       mysqli_stmt_execute($stmt);
                       $affected_rows = mysqli_stmt_affected_rows($stmt)
                       if($affected_rows > 0 ){ 
                           if ($stmt = mysqli_prepare($link, "DELETE FROM `empleados` WHERE empresa=?")) {
                               mysqli_stmt_bind_param($stmt, "s", $empresa);
                               mysqli_stmt_execute($stmt);
                               $affected_rows = mysqli_stmt_affected_rows($stmt)
                               if($affected_rows > 0 ){ 
                                   echo "La empresa a sido eliminada de la base de datos";
                               } else {
                                  echo 'Error Delete empleados<br />';
                                  echo mysqli_error();
                                  mysqli_stmt_close($stmt);
                                  mysqli_close($dbc);
                               }
                           } else {
                              echo 'Error Delete Empresas<br />';
                              echo mysqli_error();
                              mysqli_stmt_close($stmt);
                              mysqli_close($dbc);
                           }
                       } else {
                         echo 'Error Occurred<br />';
                         echo mysqli_error();
                         mysqli_stmt_close($stmt);
                         mysqli_close($dbc);
                       }
                    } else {
                         echo 'Error Occurred<br />';
                         echo mysqli_error();
                         mysqli_stmt_close($stmt);
                         mysqli_close($dbc);
                    }
           } else {
                 echo 'Error Occurred<br />';
                 echo mysqli_error();
                 mysqli_stmt_close($stmt);
                 mysqli_close($dbc);
            }
       } else {
             echo 'Error Occurred<br />';
             echo mysqli_error();
             mysqli_stmt_close($stmt);
             mysqli_close($dbc);
        }
   } else {
   echo 'NO DNI<br />';
   }
}

通常我使用 PDO 和更多的物件導向程式。

即使準備好的語句不是真正必要的,您也應該使用它們來練習和理解它的工作原理。

如您所見,檢查了每個查詢,如果它完成了它的工作並且您知道有多少行受到影響,如果您需要檢查一些東西。

當您必須在一兩年內進行工作時,或者如果更新 php 或 mysql 或其他東西出現問題時,清晰的程式也會有所幫助。

引用自:https://dba.stackexchange.com/questions/260359