向表数据库添加数据时的外键问题

时间:2015-12-02 01:52:09

标签: php database mysqli html-table

我有一个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);?>

而且,这张照片可能更好地解释了我想要实现的目标:

  1. 后台的那个是JOB_Table。
  2. JOB_Table的每一行都有一个按钮" info"比onclick应显示从CUSTOMER_table收集的详细信息。
  3. 例:
    按钮"信息"在第一行,想要得到客户表的第一行..
    按钮"信息"在第二行,想要获得第二行客户表...

    enter image description here

    ---------模式弹出代码,点击-----

    后收集客户表
    <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">&times;</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>";
    
    ?>
    

1 个答案:

答案 0 :(得分:1)

这个脚本似乎有很多错误。

由于首先插入了echo "<table class=box border=1px> <div id=<wrapper> <tr> <th>Zone</th> <th>Rownumber</th> </tr>"; echo "<form action=selectsits.php enctype=multipart/form-data method=post>"; while($record=mysql_fetch_array($availablesitsdata) ) { echo '<tr>'; echo '<select name=cat_id> <option value="a">aa</option> <option value="b">bb</option> </select> </td>'; echo "<td>" . $record['Zone'] . "</td>"; echo"</tr>"; } echo "</form>"; 表行,并且未指定Customer,我认为它正在尝试使用0,这会使外键约束失败。

我无法从您的表中了解哪个实体需要先添加,或者哪个实体依赖于另一个实体。所以我会给你一个适合两者的解决方案。我会说,虽然这个解决方案有效,它允许你忽略FK约束,如果你打算忽略它,那么你最好不要首先设置它。

首先更改job_id表格上的job_id字段以允许Customer值。

第二次更改此行

NULL

到这一行

$sql = "INSERT INTO customer (name, mail, number, device, price, paymenttype,status,date) VALUES ('$name', '$email', '$number', '$device', '$price', '$payment','$status',NOW())";

似乎在执行期间可以访问两个标头重定向。

  1. 您应始终在标题重定向后调用$sql = "INSERT INTO customer (name, mail, number, device, price, paymenttype,status,date,job_id) VALUES ('$name', '$email', '$number', '$device', '$price', '$payment','$status',NOW(),NULL)"; ,否则脚本将继续处理。 Why I have to call 'exit' after redirection through header('Location..') in PHP?

  2. 如果您回显错误消息并且您没有缓冲输出,那么您的重定向将失败,因为您已经发送了输出,它会发送标头。 How to fix "Headers already sent" error in PHP

  3. exit()

    Edit after problem clarification