gpt4 book ai didi

php - 使用发布按钮更新表行值

转载 作者:行者123 更新时间:2023-11-29 16:08:56 25 4
gpt4 key购买 nike

我的 mysql 数据库中有一些数据,我使用 PHP 在表中显示这些数据,如下所示

enter image description here

$requirement_qry="SELECT t1.*, t2.id as unit_id, t2.name as unit_name FROM `tbl_requirements` 
t1 INNER JOIN tbl_units AS t2 on unit_type = t2.id AND project_id='".$_GET['project_id']."' AND user_id='".$userid."'";
$requirement_result=mysqli_query($mysqli,$requirement_qry);

<form action="" name="addeditcategory" method="post" class="form form-horizontal" enctype="multipart/form-data">
<input type="hidden" name="project_id" value="<?php echo $_GET['project_id'];?>" />

<div class="section">
<div class="section-body">
<div class="form-group">
<label class="col-md-3 control-label" >Project Name :-</label>
<div class="col-md-6">
<input type="text" name="name" id="name" value="<?php if(isset($_GET['project_id'])){echo $row['name'];}?> " class="form-control" disabled>
</div>
</div>

<div class="form-group">
<label class="col-md-3 control-label" >Location :-</label>
<div class="col-md-6">
<input type="text" name="location" id="location" value="<?php if(isset($_GET['project_id'])){echo $row['location'];}?> " class="form-control" disabled>
</div>
</div>


<div class="form-group">
<label class="col-md-3 control-label">Project Status :-</label>

<div class="col-md-6">

<select name="status" id="status" class="select2" disabled>
<?php if (!isset($_GET['project_id'])) { ?>
<option value="1">--Project Status--</option>
<?php } ?>
<option value="1" <?php echo $row['status'] == '1' ? 'selected' : ''; ?> >Open</option>
<option value="2" <?php echo $row['status'] == '2' ? 'selected' : ''; ?> >Closed</option>
</select>

</div>
</div>



<div class="form-group">
<div class="col-md-3">
<label class="control-label">Project Details :-</label>
</div>
<div class="col-md-6">
<textarea name="details" id="details" rows="4" class="form-control" disabled><?php echo stripslashes($row['details']);?></textarea>

</div>
</div>


<div class="form-group">
<div class="col-md-3">
<label class="control-label">Project Requirements :-</label>
</div>
<div class="col-md-6">
<table id="t01">
<thead>
<tr>
<th>#</th>
<th>Requirements</th>
<th>Required</th>
<th>Sent</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
while ($row1 = mysqli_fetch_array($requirement_result))
{
$id = $row1['id'];
$unit_name = $row1['unit_name'];
echo '<input type="hidden" name="reqId" id= "reqId" value="'.$id.'" />';
echo '<tr>
<td>'.$no.'</td>
<td>'.$row1['name'].'</td>
<td>'.$row1['unit_required']." ".$unit_name.'</td>
<td><input type="number" id = "received" name = "received" value ="'.$row1['unit_received'].'"/></td>
<td><button type="submit" name="submit" class="btn btn-primary" style="padding:5px 10px;">Submit</button></td>
</tr>';
$no++;
}?>
</tbody>
</table>

</div>
</div>
</div>
</form>

上面的代码正确显示了我的数据。我需要使用提交按钮更新行中的一个字段值。如果只有一行,它工作正常......如果有多行,它不会更新除最后一行之外的数据。我的提交代码如下

if(isset($_POST['submit']) and isset($_POST['project_id']))
{

$projectId = $_GET['project_id'];

$data = array(
'unit_received' => $_POST['received']
);
$unit_edit=Update('tbl_requirements', $data, " WHERE id = '".$_POST['reqId']."'");
print_r($unit_edit);
echo $unit_edit;


if ($unit_edit > 0)
{

$_SESSION['msg']="11";
header( "Location:view_open_project.php?project_id=".$_POST['project_id']);
exit;

}
}

我对 PHP 不太熟悉,请告诉我是否有人可以帮助我解决该错误。非常感谢:)

最佳答案

正如@Saji已经说过的,你正在通过循环复制你的名字。你应该使用

echo '<input  type="hidden" name="reqId[]" id= "reqId" value="'.$id.'" />';

请注意附加的 [] 括号,以便您创建名称数组。要更新,您需要一个像

这样的循环
for($i=0;$i<count($_POST['reqId']);$i++){
$unit_edit=Update('tbl_requirements', $data, " WHERE id = '".$_POST['reqId'][$i]."'");
}

关于php - 使用发布按钮更新表行值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55456277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com