dongma7796 2013-08-15 02:11
浏览 13
已采纳

仅为!empty $ _POST值更新行

This is what my input form looks like:

<tr>
    <td class="standard_text_bold" width="25%">First name:</td> 
    <td class="standard_text_bold" width="25%"><input type="text" name="FirstName" value="<?echo $Name;?>" <?php if ($Status != "New customer") { echo "disabled"; } ?>></td>
</tr>
<tr>
    <td class="standard_text_bold" width="25%">Last name:</td>  
    <td class="standard_text_bold" width="25%"><input type="text" name="LastName" value="<?echo $Lastname;?>" <?php if ($Status != "New customer") { echo "disabled"; } ?>></td>
</tr>
<tr>
    <td class="standard_text_bold" width="25%">Email:</td>  
    <td class="standard_text_bold" width="25%"><input type="text" name="Email" value="<?echo $Email;?>" <?php if ($Status != "New customer") { echo "disabled"; } ?>></td>
</tr>

It's very general, and in a specific case, almost all inputs will be disabled.

I define all my inputs as

$LastName = $_POST['LastName'];
$FirstName = $_POST['FirstName'];

And I run an update query

mysql_query("
    UPDATE data SET
        FirstName = '$FirstName',
        LastName = '$LastName',
        ProductName = '$Productname',
        ProductPriceUSD = '$Productpriceusd',
        ProductPriceEUR = '$Productpriceeur',
        ProductLink = '$Productlink',
        Status = '$Newstatus',
        Modified = now()
    WHERE ID = '$id'
") or die(mysql_error());   

Now I'd like to update ONLY those values that are not empty. Since this is a general query, is it possible to validate variables within the query itself?

I've tried stuff like:

if (empty($_POST['LastName'])) {
    $LastName = $row['LastName'];
}
if (!empty($_POST['LastName'])) {
    $LastName = $_POST['LastName'];
}

But it doesn't work for some reason. Any suggestions?

  • 写回答

3条回答 默认 最新

  • douchen2011 2013-08-15 02:15
    关注

    You can do this by constructing the query in php to have only the right set statements.

    You can also do this in SQL:

    UPDATE data
        SET FirstName = (case when '$FirstName' = '' then FirstName else '$FirstName' end),
            LastName = (case when '$LastName' = '' then LastName else '$LastName' end),
            ProductName = (case when '$Productname' = '' then ProductName else '$ProductName' end),
            ProductPriceUSD = (case when '$Productpriceusd' = '' then Productpriceusd else '$Productpriceusd' end),
            ProductPriceEUR = (case when '$Productpriceeur' = '' then Productpriceeur else '$Productpriceeur' end),
            ProductLink = (case when '$Productlink' = '' then Productlink else '$Productlink' end),
            Status = (case when '$Newstatus' = '' then Status  else '$NewStatus' end),
            Modified = now()
        WHERE ID = '$id';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等