我有一个php函数
function insertIntoDb($db, $table) {
mysql_select_db($db) or die("Could not select database. " . mysql_error());
$resultInsert = mysql_query("SHOW COLUMNS FROM " . $table);
$fieldnames=array();
if (mysql_num_rows($resultInsert) > 0) {
while ($row = mysql_fetch_array($resultInsert)) {
$fieldnames[] = $row['Field'];
$values = array_intersect_key( $_POST, array_flip($fieldnames) ); #check if value is null otherwise do not INSERT
}
}
$sql = sprintf('INSERT INTO %s (%s) VALUES ("%s")', $table,
implode(', ', array_map('mysql_escape_string', array_keys($values))), implode('", "',array_map('mysql_escape_string', $values)));
mysql_query($sql);
echo '<div class="success">Data was entered into the database successfully!<br><a href="view.php?type=recent">View listing?</a></div>';
}
基本上,我使用表列为用户表单生成字段,然后使用相同的方法插入表中。目前它正在工作,但是如何在将其插入表之前检查输入的值是否为空(例如用户尚未填写字段)?