我有一個PHP頁面,它填充了我數據庫的2個不同表格。將數據添加到表數據庫時出現外鍵問題
在我添加一個新列然後創建一個外鍵後,數據不能再被插入到該表中。如果我刪除外鍵,那麼它再次運作...任何人都經歷過這樣的事情?
這是我如何填充表都來自同一個頁面:
include("../includes/connection.php");
$name = mysqli_real_escape_string($link, $_POST['name']);
$email = mysqli_real_escape_string($link, $_POST['email']);
$number = mysqli_real_escape_string($link, $_POST['number']);
$device = mysqli_real_escape_string($link, $_POST['device']);
$price = mysqli_real_escape_string($link, $_POST['price']);
$payment = mysqli_real_escape_string($link, $_POST['payment']);
$status = mysqli_real_escape_string($link, $_POST['status']);
$model = mysqli_real_escape_string($link, $_POST['model']);
$problem = mysqli_real_escape_string($link, $_POST['problem']);
// attempt insert query execution
$sql = "INSERT INTO customer (name, mail, number, device, price, paymenttype,status,date) VALUES ('$name', '$email', '$number', '$device', '$price', '$payment','$status',NOW())";
if(mysqli_query($link, $sql)){
// echo "Records added successfully.";
header("location:add-customer.php?message=The customer has been added to the database1");
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
$sql = "INSERT INTO job (device, model, problem, status) VALUES ('$device', '$model', '$problem', '$status')";
if(mysqli_query($link, $sql)){
// echo "Records added successfully.";
header("location:add-customer.php?message=The customer has been added to the database2");
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
} mysqli_close($link);?>
而且,這是一個照片可能更好地解釋我想達到的目標:
- 的一箇中背景是JOB_Table。
- JOB_Table的每一行都有一個「info」按鈕,而onclick應顯示從CUSTOMER_table收集的詳細信息。
舉例:第一行中
按鈕「信息」,希望得到客戶表的第一行。第二行中
按鈕「信息」,希望得到客戶表的第二行...
---------模式彈出代碼,收集客戶的表後點擊-----
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-lg">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Customer Information</h4>
</div>
<div class="modal-body">
<?php
include("../includes/connection.php");
if ($link->connect_errno > 0) {
die('Unable to connect to database [' . $link->connect_error . ']');
}
$sql = "SELECT id,name,mail,number,price,paymenttype,faktura,date from customer WHERE id = '[job_id]' ";
if (!$result = $link->query($sql)) {
die('There was an error running the query [' . $link->error . ']');
}
echo "
<table class='table'>
<thead>
<tr>";
/* Get field information for all columns */
while ($finfo = $result->fetch_field()) {
echo "
<th>" . $finfo->name . "</th>";
}
echo "
</tr>
</thead>
<tbody>";
while ($row = $result->fetch_assoc()) {
echo "<tr class='info'>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['mail'] . "</td>
<td>" . $row['number'] . "</td>
<td>" . $row['price'] . "</td>
<td>" . $row['paymenttype'] . "</td>
<td>" . $row['faktura'] . "</td>
<td>" . $row['date'] . "</td>
</tr>";
}
echo "
</tbody>
</table>";
?>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
---------用這段代碼收集作業表----
<?php
include("../includes/connection.php");
if ($link->connect_errno > 0) {
die('Unable to connect to database [' . $link->connect_error . ']');
}
if (isset($_POST['update'])) {
$results = $link->query("UPDATE job SET status='$_POST[status]', priority='$_POST[priority]' WHERE id='$_POST[hidden]'");
$results = $link->query("UPDATE customer SET status='$_POST[status]' WHERE id='$_POST[hidden]'");
}
$sql = "SELECT * from job";
if (!$result = $link->query($sql)) {
die('There was an error running the query [' . $link->error . ']');
}
echo "
<table class='table'>
<thead>
<tr>";
/* Get field information for all columns */
while ($finfo = $result->fetch_field()) {
echo "
<th>" . $finfo->name . "</th>";
}
echo "
</tr>
</thead>
<tbody>";
while ($row = $result->fetch_assoc()) {
$job_id = $row['id'];
echo "<form action='' method=post>";
echo "<tr class='info'>
<input type=hidden name=hidden value=" . $row['id'] . ">
<td>" . $row['id'] . "</td>
<td>" . $row['device'] . "</td>
<td>" . $row['model'] . "</td>
<td>" . $row['problem'] . "</td>
<td>
<select class='form-control col-sm-10' id='status' name='status'>
<option value='new' ". ($row['status'] == 'new'? 'selected ': '') .">New</option>
<option value='progress' ". ($row['status'] == 'progress'? 'selected ': '') .">Progress</option>
<option value='wait' ". ($row['status'] == 'wait'? 'selected ': '') .">Wait</option>
<option value='done' ". ($row['status'] == 'done'? 'selected ': '') .">Done</option>
<option value='close' ". ($row['status'] == 'close'? 'selected ': '') .">Close</option>
</select>
</td>
<td><select class='form-control col-sm-10' id='priority' name='priority'>
<option style='background-color:green;'value='low' ". ($row['priority'] == 'Low'? 'selected ': '') .">Low</option>
<option style='background-color:yellow; value='Medium' ". ($row['priority'] == 'Medium'? 'selected ': '') .">Medium</option>
<option style='background-color:red; value='High' ". ($row['priority'] == 'High'? 'selected ': '') .">High</option>
</select></td>
<td> <button type='submit' class='btn btn-primary btn-sm' name='update'>Update</button></td>
<td> <a class='btn btn-primary btn-sm' data-toggle='modal' data-target='#myModal' name='job_id' value='[$job_id]' > Info</a></td>
</tr>";
echo "</form>";
}
echo "
</tbody>
</table>";
?>
我們需要看到的表。你是否也按正確的順序插入?如果你嘗試在pk之前插入fk表,那麼你將遇到問題。你在使用innoDB嗎? – bassxzero
ubuntu ??或窗戶? –
@bassxzero我更新了我的問題 – gigi