簡體   English   中英

從表單和復選框更新SQL數據

[英]Update sql data from form and checkboxes

我創建了一個更新sql列中數據的表單,到目前為止一切正常。 我的問題是,我想使用復選框選擇要更新的列。 因此,我想將兩個文件歸類為電影,我只需要選中復選框並以表格形式編寫電影即可。

不僅如此。 每列都已連接到一個復選框,但是我將此復選框用於刪除查詢,因此可以刪除多行。

所以我的問題更像是,如何將這兩個功能與“一個”復選框組合在一起。

這是我的一些代碼。

這是我的表與sql輸出和刪除按鈕

<?php  
$files = mysql_query("SELECT * FROM files WHERE `user_name` = '{$_SESSION['username']}' ORDER BY `file_time` DESC LIMIT $start, $per_page")
    or die(mysql_error()); 
    ?>
<table id="table-3">
 <thead>
        <tr>
            <th scope="col">Upload User</th>
            <th scope="col">Filename</th>
            <th scope="col">Upload date</th>
             <th scope="col">IP adress</th>
             <th scope="col">Category</th>
            <th scope="col">Delete</th>
        </tr>
    </thead>
<?php
while (($row = mysql_fetch_assoc( $files )) !==false)
{ 
echo "<tr>"; 
echo "<td>".$row['user_name'] . "</td> ";
?>
<td class="download"> <a href="download.php?file_id=<?php echo $row ['file_id']; ?>"> <?php echo $row['file_name']; ?></a></td>
<?php echo "<td>".$row['file_time'] . "</td> "; ?>
<?php echo "<td>".$row['file_ip'] . "</td> "; ?>
<?php echo "<td>".$row['category'] . "</td> "; ?>
<td>

<form action="delete.php" method="post">
<input type="checkbox" name="done[]" id="<?php echo $row['file_id'] ?>" value="<?php echo $row['file_id'] ?>">

</td>

<?php 
}
    echo "</tr>";
    echo "</table>"; 
 ?>
<input type ="submit" value ="Delete">
</form> 

這種形式是具有更新功能的形式

 <form action="function.php" method="post">
category: <input type="text" name="category" />
<input type="submit" />
</form>

和function.php

include('core/inc/init.inc.php');

    if(isset($_POST['category'])){
    $category = $_POST['category'];
    mysql_query("UPDATE files SET category='$category'
    /*WHERE category='genom' */ ");

    header("Location: profile.php?uid=" . $_SESSION['uid']);
    }else{
    header("Location: profile.php?uid=" . $_SESSION['uid']);
    };

如何結合這兩個功能?

您可以在同一表單中具有多個提交按鈕。 因此,您可以執行以下操作:

<?php  
    $files = mysql_query("SELECT * FROM files WHERE `user_name` = '{$_SESSION['username']}' ORDER BY `file_time` DESC LIMIT $start, $per_page")
    or die(mysql_error()); 
?>
<form action="updateordelete.php" method="post">
    <table id="table-3">
        <thead>
            <tr>
                <th scope="col">Upload User</th>
                <th scope="col">Filename</th>
                <th scope="col">Upload date</th>
                <th scope="col">IP adress</th>
                <th scope="col">Category</th>
                <th scope="col">Delete</th>
            </tr>
        </thead>
        <?php
        while (($row = mysql_fetch_assoc( $files )) !==false)
        {
        ?>
        <tr>
            <td> <?php echo $row['user_name']; ?></td>
            <td class="download"> <a href="download.php?file_id=<?php echo $row ['file_id']; ?>"> <?php echo $row['file_name']; ?></a></td>
            <td><input type="text" name="file_time[<?php echo $row ['file_id']; ?>]" value="<?php echo $row['file_time']; ?>" /></td>
            <td><input type="text" name="file_ip[<?php echo $row ['file_id']; ?>]" value="<?php echo $row['file_ip']; ?>" /></td>
            <td><input type="text" name="file_category[<?php echo $row ['file_id']; ?>]" value="<?php echo $row['category']; ?>" /></td>
            <td><input type="checkbox" name="done[]" id="<?php echo $row['file_id'] ?>" value="<?php echo $row['file_id'] ?>"></td>
        </tr>
        <?php } ?>
    </table>
    <input type ="submit" name="submittype" value = "Update">
    <input type ="submit" name="submittype" value = "Delete">
</form> 

然后,在updateordelete.php中:

if($_POST['submittype']=="Delete"){
   (do delete code...)
}elseif($_POST['submittype']=="Update"){
   (do update code...)
}

對於每個值,都可以使用$ _POST ['file_category']訪問它們。 例如:

$file_categories=$_POST['file_category'];
foreach($_POST['done'] as $file_id){
    echo $file_categories[$file_id];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM