Php 将行上列的值设置回NULL
我有一张有行和列的小桌子Php 将行上列的值设置回NULL,php,mysql,Php,Mysql,我有一张有行和列的小桌子 <tr> <td>Warranty</td> <td><?php echo ($admrmadetrow['rd_warranty'] == 1 ? "YES" : ' '); ?></td> <td><select name="rd_warranty" data-placeholder="rd_warranty" class="form-control"&
<tr>
<td>Warranty</td>
<td><?php echo ($admrmadetrow['rd_warranty'] == 1 ? "YES" : ' '); ?></td>
<td><select name="rd_warranty" data-placeholder="rd_warranty" class="form-control">
<option></option>
<option value="1">YES</option>
<option value="0">NO</option>
</select></td>
</tr>
更新中的用例,因为当选择0时,基本上需要NULL来更新。类似于
$update_rma_detail_stmt = $dbh->prepare("UPDATE rma_detail SET $key = CASE WHEN ? = 0 then NULL else ? END WHERE r_nr = ? AND rd_artikel_code = ?");
注意:这里的答案是使用CASE语句,我不确定上面的代码是否100%,因为您使用占位符(?)作为值,我对此不太熟悉
有关案例的更多详细信息,请访问:如果HTML代码中的字段值为空,您的POST请求将使用空字符串值生成(如“”)。 要在准备好的语句中设置NULL,必须使用bindValue函数,代码如下:
foreach ($_POST as $key => $value){
$update_rma_detail_stmt = $dbh->prepare("UPDATE rma_detail SET $key = ? WHERE r_nr = ? AND rd_artikel_code = ?");
/*you can use "empty($value)" or "$value==''" condition*/
if(empty($value)){
$update_rma_detail_stmt->bindValue(1, null);
}else{
$update_rma_detail_stmt->bindParam(1, $value);
}
$update_rma_detail_stmt->bindParam(2, $getadmrmaid);
$update_rma_detail_stmt->bindParam(3, $artcode);
}
$update\u rma\u detail\u stmt->execute() 在
上选择进入mysql,使用IFNULL(rd\U保修,0)
最佳实践 需要查看更新dbSee my update Dragon thx的代码!0与null不同。没有什么是“与null相同的”,包括null本身。你是对的,但是我仍然认为这个mysql更新正在删除它,因为它不能更新一个空字段(0),因此它什么都不做。例如,如果我将0改为9,它就可以工作,但我只想将mysql中的列的值恢复为NULL,就像一开始没有值时一样。换句话说,只需清除my Table中特定行中的特定列警告:不允许来自$\u POST
的任意用户变量进入查询。你在这里用$key
做的事非常危险。总是正确地逃避这些事情。工作,但为了最好,我只是使用值9为否,所以它更新为。这将帮助我在我的项目中更进一步。
foreach ($_POST as $key => $value){
$update_rma_detail_stmt = $dbh->prepare("UPDATE rma_detail SET $key = ? WHERE r_nr = ? AND rd_artikel_code = ?");
/*you can use "empty($value)" or "$value==''" condition*/
if(empty($value)){
$update_rma_detail_stmt->bindValue(1, null);
}else{
$update_rma_detail_stmt->bindParam(1, $value);
}
$update_rma_detail_stmt->bindParam(2, $getadmrmaid);
$update_rma_detail_stmt->bindParam(3, $artcode);