带复选框的UPDATE表

时间:2015-06-29 09:37:26

标签: php mysql

我有能力在我的cms上更新我称之为'文档'(类似于创建帖子)的内容,但是我已经引入了与文档关联的类别。现在我设法在从new创建doc时绑定它们但是在尝试更新它们时我有点卡住了。我使用复选框来显示类别列表,选中它后会更新使用doc_id和cat_id的连接表。

以下是更新文档的脚本:

<?php

include ('includes/header.php'); 
require ('../../db_con.php'); 

echo '<h1>Document Edit</h1>';

// Check for a valid document ID, through GET or POST:
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // From view_docs.php
    $id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { // Form submission.
    $id = $_POST['id'];
} else { // No valid ID, kill the script.
    echo '<p class="error">This page has been accessed in error.</p>';
    exit();
}


// Check if the form has been submitted:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $errors = array();

    // Check for a document name:
    if (empty($_POST['doc_name'])) {
        $errors[] = 'You forgot to enter your document name.';
    } else {
        $dn = mysqli_real_escape_string($dbc, trim($_POST['doc_name']));
    }

    // Check for a document content:
    if (empty($_POST['doc_content'])) {
        $errors[] = 'You forgot to enter your last name.';
    } else {
        $dc = mysqli_real_escape_string($dbc, trim($_POST['doc_content']));
    }



    if (empty($errors)) { // If everything's OK.

        //  Test for unique doc title:
        $q = "SELECT doc_id FROM docs WHERE doc_name='$dn' AND doc_id != $id";

        $r = mysqli_query($dbc, $q);
        if (mysqli_num_rows($r) == 0) {

            // Make the query:
            $q = "UPDATE docs SET doc_name='$dn', doc_content='$dc', doc_name='$dn' WHERE doc_id=$id LIMIT 1";

            $r = mysqli_query ($dbc, $q);

            if (mysqli_affected_rows($dbc) == 1) { // If it ran OK.




            $doc_id = mysqli_insert_id($dbc); 

            $query = "UPDATE doc_cat_join (cat_id,doc_id) VALUES "; 
            $cat_ids = $_POST['cat_id']; 
            $length = count($cat_ids); 
            for ($i = 0; $i < count($cat_ids); $i++) { 
            $query.='(' . $cat_ids[$i] . ',' . $doc_id . ')'; 

            if ($i < $length - 1) 
            $query.=','; 
            } 




                // Print a message:
                echo '<p>The document has been edited.</p>';    

            } else { // If it did not run OK.
                echo '<p class="error">The document could not be edited due to a system error. We apologize for any inconvenience.</p>'; // Public message.
                echo '<p>' . mysqli_error($dbc) . '<br />Query: ' . $q . '</p>'; // Debugging message.
            }

        } else { // Already used.
            echo '<p class="error">The document name has already been used.</p>';
        }

    } else { // Report the errors.

        echo '<p class="error">The following error(s) occurred:<br />';
        foreach ($errors as $msg) { // Print each error.
            echo " - $msg<br />\n";
        }
        echo '</p><p>Please try again.</p>';

    } // End of if (empty($errors)) IF.

} // End of submit conditional.

// Always show the form...

// Retrieve the document's information:
$q = "SELECT * FROM docs WHERE doc_id=$id"; 

$r = mysqli_query ($dbc, $q);

if (mysqli_num_rows($r) == 1) { // Valid document ID, show the form.

    // Get the document's information:
    $row = mysqli_fetch_array ($r, MYSQLI_NUM);


    // Create the form:
    echo '<form action="edit_doc.php" method="post">

    <p>Document Name: <input type="text" name="doc_name" size="15" maxlength="15" value="' . $row[1] . '" /></p>

    <textarea name="doc_content" id="doc_content" placeholder="Document Content" style="display: none;"></textarea>
    <iframe name="editor" id="editor" ></iframe>'
        ?>


    <div class="row">
        <div class="col-group-1"> 
            <?php

            $q = "SELECT * FROM cats";  

            $r = mysqli_query ($dbc, $q); // Run the query.

            echo '<div class="view_body">';

            // FETCH AND PRINT ALL THE RECORDS
            while ($row = mysqli_fetch_array($r)) {
            echo '<br><label><input type="checkbox" name="cat_id[]" value="' . $row['cat_id'] . '">' . $row["cat_name"] . '</label>';
            }
            echo '</div>'; 

            ?>
        </div>
    </div>       


    <br><br> 
    <input onclick="formsubmit()" type="submit" value="Update Document" name="submit"/>

    <?php echo'

<input type="hidden" name="id" value="' . $id . '" />
</form>
<br><br><a href="list_doc.php">Back to docs list</a>';

} else { // Not a valid document ID.
    echo '<p class="error">This page has been accessed in error.</p>';
}

?>

<?php
    mysqli_close($dbc);
?>

所以我有三张桌子:

docs
  doc_id
  doc_name
  doc_content

cats
  cat_id
  cat_name

doc_cat_join
  doc_id
  cat_id

连接表与doc_id和cat_id相关,然后将它们关联在一起。我在我的脚本中猜测更新文档时需要删除行然后重新插入它们?我只是需要知道更新连接表的方法或最简单的方法,因为我有点卡住......

1 个答案:

答案 0 :(得分:3)

如果要更改复选框,您需要删除之前存储的具有相应ID的复选框并插入新的复选框,因为我们无法预测用户将选择多少个复选框,因此无法更新复选框...

区分:

用户可能会在更新时删除一个复选框,因此您永远不会知道要删除哪一个.......

在您的代码中......

docs
   doc_id
   doc_name
   doc_content

cats
   cat_id
   cat_name

doc_cat_join
   id
   doc_id
   cat_id

在这里你必须删除更新文档的旧复选框,

DELETE FROM doc_cat_join WHERE cat_id = some_id

接下来,您可以在第一次插入时插入所选复选框...