尝试构建一个可以插入到 SQL 数据库中的函数,而不管变量有多少或有多少。目前我的功能不起作用。
function insert($table, $columns, $results){
global $dbh;
$results = explode(',',$results);
$val= '';
$ex = '';
foreach($results as $result){
$val .= ":" . $result . ",";
$ex = "':" . $result . "' => $result,";
}
$val = rtrim($val, ',');
$ex = rtrim($ex, ',');
$sql = "INSERT INTO $table ($columns) VALUES ($val)";
$q = $dbh->prepare($sql);
$q->execute(array($ex));
}
像这样调用: insert('users','email,pwd,forename,surname,level,status',$vals);
当我设法让它为我的 Select 功能工作时。
function check($table,$columns,$results){
global $dbh;
$columns = explode(',',$columns);
$results = explode(',',$results);
$whr= '';
$int = 0;
foreach($columns as $column){
$whr.= " AND " . $column . " = '{$results[$int]}'";
$int += 1;
}
$sql = "SELECT * FROM $table WHERE status != 'D' $whr";
$return = 0;
foreach($dbh->query($sql) as $check){
$return = 1;
}
return $return;
}