[英]for each loop PHP, insert into SQL not inserting data
$student_info = array(
'student_number'=>$_POST['student_number'],
'student_first_name'=>$_POST['student_first_name'],
'student_middle_name'=>$_POST['student_middle_name'],
'student_last_name'=>$_POST['student_last_name']);
foreach($student_info as $table_row=>$information){
$sql = "INSERT INTO student_info_db (`$table_row`) VALUES(`$information`)";
echo $table_row . " " . $information;
}
林不是很確定為什么它不會在數據庫中插入任何數據。 echo $ table_row $ information僅用於獲取值並成功,但仍不插入任何數據。 問題是,怎么了? 我很確定我在做正確的SQL ..還是我不是?
看來您的SQL查詢字串不正確。 您正在為每個元素運行查詢! 它將每次插入數據到每一列! 您的表中將有4個條目供一個學生信息使用!
您也不會在循環中運行查詢。
您應該在循環內創建查詢,然后在循環后執行查詢
您需要首先從數組中查詢字符串。
首先使您的查詢是這樣的:
嘗試這樣:
$student_info = array(
'student_number'=>mysql_real_escape_string($_POST['student_number']),
'student_first_name'=>mysql_real_escape_string($_POST['student_first_name']),
'student_middle_name'=>mysql_real_escape_string($_POST['student_middle_name']),
'student_last_name'=>mysql_real_escape_string($_POST['student_last_name']));
foreach($student_info as $table_row=>$information){
$cols .= "`".$table_row."` ,";
$vals .= "'".$information . "' ,";
}
$cols = rtrim($cols,",");
$vals = rtrim($vals,",");
$sql = "INSERT INTO student_info_db (".$cols . ") VALUES(".$vals .")";
帶有示例數據的現場演示: https : //eval.in/104428
然后您需要運行此$sql
查詢
像這樣:
if(mysqli_query($con, $sql)
echo "successfully inserted";
else
echo "something is wrong!";
您沒有執行查詢! 首先建立與數據庫的連接。 然后添加mysql_query($ sql)來執行查詢。
$student_info = array(
'student_number'=>mysql_real_escape_string(htmlspecialchars($_POST['student_number'])),
'student_first_name'=>mysql_real_escape_string(htmlspecialchars($_POST['student_first_name'])),
'student_middle_name'=>mysql_real_escape_string(htmlspecialchars($_POST['student_middle_name'])),
'student_last_name'=>mysql_real_escape_string(htmlspecialchars($_POST['student_last_name'])));
//First we need to make a connection with the database
$host='localhost'; // Host Name.
$db_user= 'root'; //User Name
$db_password= 'nopass';
$db= 'product_record'; // Database Name.
$conn=mysql_connect($host,$db_user,$db_password) or die (mysql_error());
mysql_select_db($db) or die (mysql_error());
$column = "";
$value = "";
foreach($student_info as $table_row=>$information){
if($column != ""){
$column .= ",";
$value .= ",";
}
$column .= $table_row;
$value .= "'".$information."'";
}
$sql = "INSERT INTO student_info_db (".$column.") VALUES(".$value.")";
mysql_query($sql);
mysql_close($conn);
在您的foreach循環中,運行查詢。 像這樣:
$student_info = array(
'student_number'=>$student_number,
'student_first_name'=>$student_first_name,
'student_middle_name'=>$student_middle_name,
'student_last_name'=>$student_last_name);
foreach($student_info as $table_row=>$information)
{
$sql = "INSERT INTO student_info_db (`$table_row`) VALUES('".mysqli_real_escape_string($connection, $information)."')";
mysqli_run($connection, $sql);
echo $table_row . " " . $information;
}
此處有關mysqli_query的更多信息
$sql = <<<'END'
INSERT INTO student_info_db (
student_number,
student_first_name,
student_middle_name,
student_last_name
) VALUES (?, ?, ?, ?)
END;
$stmt = $dbConnection->prepare( $sql )
$stmt->bind_param( 's', $_POST['student_number'] );
$stmt->bind_param( 's', $_POST['student_first_name'] );
$stmt->bind_param( 's', $_POST['student_middle_name'] );
$stmt->bind_param( 's', $_POST['student_last_name'] );
$stmt->execute();
或者,如果您堅持使用數組作為中間階段:
$student_info = array(
'student_number' => $_POST['student_number'],
'student_first_name' => $_POST['student_first_name'],
'student_middle_name' => $_POST['student_middle_name'],
'student_last_name' => $_POST['student_last_name']
);
$keys = array_keys( $student_info );
$columns = implode( ',', $keys );
$holders = implode( ',', array_fill( 0, count($keys), '?' ) );
$sql = "INSERT INTO student_info_db ($columns) VALUES ($holders)";
$stmt = $dbConnection->prepare( $sql )
foreach ( $keys as $key ) {
$stmt->bind_param( 's', $student_info[$key] );
}
$stmt->execute();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.