Esempio n. 1
0
function sql_insert($table, $res)
{
    if (count($res) == 0) {
        return 0;
    }
    $res2 = $fields = array();
    $n = 0;
    foreach ($res as $k => $v) {
        $fields[] = '`' . $k . '`';
        if (!is_array($v)) {
            $res2[$k] = array($v);
        } else {
            $res2[$k] = $v;
        }
        $t = count($res2[$k]);
        if ($n > 0 && $n != $t) {
            return false;
        }
        $n = $t;
    }
    $dat = array();
    for ($i = 0; $i < $n; ++$i) {
        $t = array();
        foreach ($res2 as $v) {
            $t[] = '"' . sqlslashes($v[$i]) . '"';
        }
        $dat[] = '(' . implode(',', $t) . ')';
    }
    return sql('INSERT INTO `' . $table . '` (' . implode(',', $fields) . ') VALUES ' . implode(',', $dat));
}
Esempio n. 2
0
         $_POST['f' . $k] = implode("\n", $a);
     }
 }
 #Validation
 foreach ($fields as $k => $v) {
     $func = 'valid_' . $v['valid'];
     if (!$func($_POST['f' . $k])) {
         $_SESSION['msg'][] = $v['caption'] . '格式錯誤';
         redirect('event.php?eventid=' . $eventid, 1);
     }
 }
 #Duplication check
 foreach ($keys as $k => $v) {
     $ct = 0;
     foreach ($v as $fid) {
         if (sql('SELECT 1 FROM `pool` WHERE `eventid`="' . $eventid . '" AND `fieldid`="' . $fid . '" AND `value`="' . sqlslashes($_POST['f' . $fid]) . '"')) {
             ++$ct;
         }
         if ($ct == count($v)) {
             $_SESSION['msg'][] = '資料重複,您可能已經報名過了喔';
             redirect('event.php?eventid=' . $eventid, 1);
         }
     }
 }
 #Commit
 if ($e = sql('SELECT `recordid` FROM `records` WHERE `eventid`="' . $eventid . '" ORDER BY `recordid` DESC LIMIT 0,1')) {
     $rid = $e['recordid'] + 1;
 } else {
     $rid = 1;
 }
 sql_insert('records', array('eventid' => $eventid, 'recordid' => $rid, 'time' => time(), 'ip' => ip()));