无法让我的删除按钮适用于3个表

时间:2016-03-09 10:45:53

标签: php mysql

我正在尝试创建一个删除按钮,删除与一个表连接的所有值(关系)。我的表名是person,address和cv,其中address_id与person_address有关系,cv_id与person_cv有关系。单击删除按钮时,应删除连接到特定人员的所有值。在数据库中我使用INNODB和ON DELETE是级联的。我希望你们能帮助我。

我的delete.php

<?php

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password="usbw"; // Mysql password 
$db_name="persons"; // Database name 
$tbl_name="person"; // Table name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar 
$id=$_GET['person_id'];

// Delete data in mysql from row that has this id 
$sql="DELETE FROM $tbl_name WHERE person_id='$id'";
$result=mysql_query($sql);

// if successfully deleted
if($result){
echo "Deleted Successfully";
echo "<BR>";
echo "<a href='admin.php'>Back to main page</a>";
}

else {
echo "ERROR";
}
?> 

<?php
// close connection 
mysql_close();
?>

我的admin.php,其中包含显示三个表的值的表

<?php

$servername = "localhost";
$username = "root";
$password = "usbw";
$dbname = "persons";

// CREATE A CONNECTION WITH THE DATABASE
// CONNECTIE MAKEN MET DATABASE
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

// SELECT TABLE NAMES FROM PERSON, ADDRESS AND CV WHERE address_id IS person_address and cv_id IS person_cv
// SELECTEER VAN TABEL PERSON, ADDRESS AND CV WAAR address_id  GELIJK IS AAN person_address EN cv_id AAN person_cv
$sql = "SELECT person_id, person_firstname, person_lastname, 
                 person_email, person_phonenumber,  
                 address_street,address_housenumber, 
                 address_city,address_state,address_zipcode, cv_path
          FROM person 
            inner join address on address.address_id = person.person_address 
            inner join cv on cv.cv_id = person.person_cv";

// EXECUTE QUERY IF THE RESULT BIGGER IS THAN ZERO
// VOER QUERY UIT ALS RESULTAAT GROTER IS DAN NUL
$result = $conn->query($sql);
if ($result->num_rows > 0) {

// ECHO A TABLE WITH VALUES 
// ECHO EEN TABEL MET ONDERSTAANDE WAARDES
echo "<form action='delete.php' method='post'>";    
echo "<table border=0 align=right>;
<tr>
<th>Voornaam</th>
<th>Achternaam</th>
<th>Straat</th>
<th>Huisnummer</th>
<th>Postcode</th>
<th>Stad</th>
<th>Provincie</th>
<th>Email</th>
<th>Mobiel</th>
<th>cv</th>
<th>delete</th>
</tr>";

// LOOP THROUGH THE RESULTS AND OUTPUT THE RESULTS FOR EACH ROW
// GA DOOR RESULTATEN EN LAAT DE RESULTEN PER RIJ ZIEN
while($row = $result->fetch_assoc()) {
     echo "<tr>";
     echo "<td>" . $row["person_firstname"] . "</td>";
     echo "<td>" . $row["person_lastname"] . "</td>";
     echo "<td>" . $row["address_street"] . "</td>";
     echo "<td>" . $row["address_housenumber"] . "</td>";
     echo "<td>" . $row["address_zipcode"] . "</td>";
     echo "<td>" . $row["address_city"] . "</td>";
     echo "<td>" . $row["address_state"] . "</td>";
     echo "<td>" . $row["person_email"] . "</td>";
     echo "<td>" . $row["person_phonenumber"] . "</td>";
     echo "<td><a href='http://localhost:8080/website/" . $row['cv_path'] . "'>cv file</a></td>";
     echo "<td><a href='delete.php?id=" . $row['person_id'] . "'>delete</a></td>";
     echo "</tr>";
  }
  echo "</form>";
  }

  // IF THERE IS ZERO RESULT ECHO THIS
  // ALS WAARDE NUL IS LAAT DE ONDERSTAADE TEKST ZIEN
  else {
    echo "Er is niks in het database gevonden";
  }
  // CLOSE CONNECTION
  // SLUIT CONNECTIE
  $conn->close();
?>  

编辑:我的delete.php现在

<?php
  $hostname='localhost';
  $username='root';
  $password='usbw';
  $dbname='persons'; 

  // CREATE A CONNECTION WITH THE DATABASE
  // CONNECTIE MAKEN MET DATABASE 
  try {
    $conn = new PDO("mysql:dbname=$dbname; port=3307;", $username, $password);
  echo "Connected to $dbname at successfully.";
  } catch (PDOException $pe) {
  die("Could not connect to the database $dbname :" . $pe->getMessage());
  }

$sql = "DELETE FROM person WHERE person_id =:person_id";
$stmt = $conn->prepare($sql);
$stmt->bindParam(':person_id', $_POST['person_id'], PDO::PARAM_INT);   
$stmt->execute();


// close connection 
$conn = null;
?>

0 个答案:

没有答案