0

SQL 列のデータを更新するフォームを作成しましたが、これまでのところ問題ありません。私が抱えている問題は、チェックボックスを使用して、更新する列を選択したいということです。2 つのファイルを動画として分類したいので、チェックボックスをオンにしてフォームに動画を書き込みます。

しかし、それだけではありません。すべての列は既にチェックボックスに接続されていますが、このチェックボックスを削除クエリで使用しているため、複数の行を削除できます。

したがって、私の質問は、この2つの機能を「1つの」チェックボックスとどのように組み合わせることができるかということです。

これは私のコードの一部です。

これは、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']);
    };

これら2つの機能をどのように組み合わせることができますか?

4

1 に答える 1

1

同じフォームに複数の送信ボタンを配置できます。したがって、次のようなことができます。

<?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];
}
于 2012-07-13T22:13:13.000 に答える