dongxing4643 2010-06-23 11:07
浏览 87
已采纳

很多'if语句',还是冗余的mysql查询?

$url = mysql_real_escape_string($_POST['url']); $shoutcast_url = mysql_real_escape_string($_POST['shoutcast_url']); $site_name = mysql_real_escape_string($_POST['site_name']); $site_subtitle = mysql_real_escape_string($_POST['site_subtitle']); $email_suffix = mysql_real_escape_string($_POST['email_suffix']); $logo_name = mysql_real_escape_string($_POST['logo_name']); $twitter_username = mysql_real_escape_string($_POST['twitter_username']);

with all those options in a form, they are pre-filled in (by the database), however users can choose to change them, which updates the original database. Would it be better for me to update all the columns despite the chance that some of the rows have not been updated, or just do an if ($original_db_entry = $possible_new_entry) on each (which would be a query in itself)?

Thanks

  • 写回答

4条回答 默认 最新

  • dongshuan8722 2010-06-23 11:10
    关注

    I'd say it doesn't really matter either way - the size of the query you send to the server is hardly relevant here, and there is no "last updated" information for columns that would be updated unjustly, so...

    By the way, what I like to do when working with such loads of data is create a temporary array.

    $fields = array("url", "shoutcast_url", "site_name", "site_subtitle" , ....); 
    
    foreach ($fields as $field)
     $$field = mysql_real_escape_string($_POST[$field]);
    

    the only thing to be aware of here is that you have to be careful not to put variable names into $fields that would overwrite existing variables.

    Update: Col. Shrapnel makes the correct and valid point that using variable variables is not a good practice. While I think it is perfectly acceptable to use variable variables within the scope of a function, it is indeed better not use them at all. The better way to sanitize all incoming fields and have them in a usable form would be:

    $sanitized_data = array();
    $fields = array("url", "shoutcast_url", "site_name", "site_subtitle" , ....); 
    
    foreach ($fields as $field)
     $sanizited_data[$field] = mysql_real_escape_string($_POST[$field]);
    

    this will leave you with an array you can work with:

    $sanitized_data["url"] = ....
    $sanitized_data["shoutcast_url"] = ....
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c