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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?