dongsong73032 2015-04-13 18:46
浏览 37
已采纳

使用isset()动态转换未定义的索引

I'm writing a generic function that will take a large number of fields from $_POST and build an SQL insert into a table. In this case, I have a number of Undefined indexes and from reading other posts on SO, I am using a ternary to test if the variable exists. This works perfectly when I use it in interactive php, especially since there are no $_POST variables defined.

But when I use it in my form, I seem to get a extra quote and a few returns but I cannot see where they are coming from. I've beaten about this in different ways but am hoping someone can help me see what I'm not seeing.

function SaveDonation($form) {
    try {
        $querystr = "INSERT INTO GeneralDonations(donationForm, firstName, startYear)" 
        . "VALUES(" . "'" . $form . "', "
        . ((!isset($_POST['firstName'])) 
            ? "'', " : ("'" . mysql_real_escape_string($_POST['firstName'])."', "))
        . ((isset($_POST['startDate'])) 
            ? ("'" . mysql_real_escape_string($_POST['startDate'])."' ") : "'' ") 
        .")";

        echo "<pre>query = "; var_dump($querystr);die;


        $donation = $this->db->insertRow($querystr);
        $result = true;

    } catch(MysqlException $e) {
        $result = false;
        $this->errorMsg = $e->getMessage();
    }
    return $result;
}

The startDate is the undefined index value. This is the browser output using var_dump. It appears that the x-debug output is showing instead of the variable. But all table, no useful data? Please help me see what's different here?

string 'INSERT INTO GeneralDonations(
  donationForm, firstName, startYear)VALUES('buy-a-foot', 's', 
  '<br />
<font size=\'1\'><table class=\'xdebug-error xe-notice\'
  dir=\'ltr\' border=\'1\' cellspacing=\'0\' cellpadding=\'1\'>

  <tr><th align=\'left\' bgcolor=\'#f57900\' colspan=' )' (length=284)
  • 写回答

1条回答 默认 最新

  • douzhi8488 2015-04-13 20:13
    关注

    Your code has some problems:

    • Please use prepared statements (see below)!

    • The error message (which is not entirely shown) would continue with "Undefined index firstName", since there's an ! too much in (!isset($_POST['firstName'])).

    • The error message is incomplete because your xdebug shortens var_dump output. You can change this behaviour with the settings xdebug.overload_var_dump and xdebug.var_display_max_data. See xdebug documentation.

    • If you can't use prepared statements, consider using some sprintf() construction to improve readability.


    // Prepared statements (untested)
    $stmt = $db->prepare("
        INSERT INTO GeneralDonations(donationForm, firstName, startYear)
        VALUES (?, ?, ?)");
    $stmt->execute(array(
        $form,
        isset($_POST['firstName']) ? $_POST['firstName'] : '',
        isset($_POST['startDate']) ? $_POST['startDate'] : ''
    ));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码