基本上,我想更新每個複選框對應的MySql表。這裏是我的要求的架構:如何在php中檢查和取消選中複選框來更新mysql表?
我已經在sql中上傳了文件,並在從數據庫[顯示在網絡上](讀取,寫入,下載,共享)中拾取的每個文件旁邊放置了四個複選框。另外,我在數據庫中有四個額外的列(readFlag,WriteFlag,DownloadFlag,ShareFlag)。我想要的是,假設我檢查特定文件旁邊的「讀取」複選框,我想更新該特定文件名旁邊的readFlag,而不是其他任何文件。對於其他複選框和文件也是如此。
我已經編寫了一個手動更新readflag的代碼,該代碼工作正常,但在使用複選框時不起作用。任何建議都會有很大的幫助。謝謝。
這是我迄今爲止寫的代碼:我已經手動將readFlag設置爲1,只是爲了在assignMainPage.php中檢查incase。
PHP:
<?php
$host = 'localhost';
$user = '';
$password= '';
$db = 'user_information';
@$conn = mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db($db, $conn);
if(!$conn) {
echo "Connection to the database failed";
}
$result = mysql_query("SELECT * FROM user_files ORDER BY id ASC");
?>
的Jquery:
<script>
$(function() {
$(".assign").click(function() {
var check = $(this).closest('tr').find('input:checkbox');
var fileID = $(this).closest('tr').find('.file_id').val();
var element = $(this);
var assign_id = element.attr("id");
var assigninfo = 'id=' +assign_id;
if(confirm("assign")) {
$.ajax({
type: "POST",
url: "assignMainPage.php",
data: assigninfo,
success: function(){
}
});
alert("file id : " + fileID);
}
});
});
assignMainPage.php
<?php
$host = 'localhost';
$user = '';
$password= '';
$db = 'user_information';
@$conn = mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db($db, $conn);
if(!$conn) {
echo "Connection to the database failed";
}
if($_POST['id']) {
$id = mysql_real_escape_string($_POST['id']);
$assign = "UPDATE user_files SET readFlag = 1 where id = '$id'";
mysql_query($assign);
}
?>
HTML:
<div class="container">
<?php
while($row = mysql_fetch_array($result)){
$id1 = $row['id'];
$name = $row['user_file_name'];
?>
<?php
if(isset($_POST['assign']) && $_POST['assign'] == '$id1') {
echo("success");
}
?>
<div class="show">
<table width = "80%" align = "center">
<tr class="trclass">
<td width= "15%"><span class="name"><?php echo $name; ?> <> <?php echo $id1;?> </span></td>
<input type="hidden" value="<?php echo $id1; ?>" class="file_id" />
<td width="15%">
<span class="action"><input type="button" id="<?php echo $id1; ?>" class="delete" title="Delete" value="Delete <> <?php echo $id1;?>"></span>
</td>
<td width= "10%"><span class="action">
<input type="checkbox" id="<?php echo $id1; ?>" class="read" title="read">Read <> <?php echo $id1;?></span>
</td>
<td width= "15%"><span class="action"><input type="checkbox" id="<?php echo $id1; ?>" class="write" title="write">Write <> <?php echo $id1;?></span></td>
<td width= "15%"><span class="action"><input type="checkbox" id="<?php echo $id1; ?>" class="download" title="download">Download <> <?php echo $id1;?></span></td>
<td width= "15%"><span class="action"><input type="checkbox" id="<?php echo $id1; ?>" class="share" title="share">Share <> <?php echo $id1;?></span></td>
<td width= "15%">
<span class="action">
<input type="submit" id="<?php echo $id1; ?>" name="assign" class="assign" title="assign" value="Assign <> <?php echo $id1; ?>">
</span>
</td>
</tr>
<table>
</div>
<?php
}
?>