0
我最近學習PHP和MySQL,也正在開發一個使用PHP和MySQL的在線Web窗體。在我有多個表格,我需要提交數據的形式。我試過這段代碼;插入數據到多個表使用PHP通過網絡形式
$required_fields = array('event_type','accommodation_type','public_user','comments','grand_total');
$required_fields2 = array('first_name','last_name','gender','age_group');
$errors = array_merge($errors, check_required_fields($required_fields, $_POST));
$errors2 = array_merge($errors2, check_required_fields($required_fields2, $_POST));
$fields_with_lengths = array('event_type' => 50, 'accommodation_type' => 50, 'public_user' => 50,'comments' => 10000,'grand_total' => 50);
$fields_with_lengths2 = array('first_name' => 50, 'last_name' => 50, 'gender' => 50,'age_group' => 50);
$errors = array_merge($errors, check_max_field_lengths($fields_with_lengths, $_POST));
$errors2 = array_merge($errors2, check_max_field_lengths($fields_with_lengths2, $_POST));
$event_type = trim(mysql_prep($_POST['event_type']));
$accommodation_type= trim(mysql_prep($_POST['accommodation_type']));
$public_user = trim(mysql_prep($_POST['public_user']));
$comments = trim(mysql_prep($_POST['comments']));
$grand_total = trim(mysql_prep($_POST['grand_total']));
if (empty($errors)){
$query = "INSERT INTO event_registration (event_type, accommodation_type, public_user, comments, grand_total) VALUES ('{$event_type}','{$accommodation_type}','{$public_user}','{$comments}','{$grand_total}')";
$result = mysql_query($query, $connection);
if($result){
$message = "The User was successfully registered";
} else{
$message = "The User could not be registered";
$message .= "<br />" . mysql_error();
}
} else{
if(count($errors)==1){
$message = "There was 1 error in the form.";
} else {
$message = "There were" . count($errors) . " error in the form.";
}
}
} else { // Form has not been submitted
$event_type = "";
$accommodation_type = "";
$public_user = "";
$comments = "";
$grand_total = "";
}
請問您可以編輯您的問題,添加有關什麼是不工作的信息?你有錯誤嗎? – 2010-02-14 21:07:48
是否有任何理由使括號圍繞查詢中的變量值? – malonso 2010-02-14 21:16:31
@malonso>這被稱爲「複雜語法」,因爲{大括號}可以解釋字符串中的變量值和表達式。在PHP語言參考中查找「字符串」。在「引用字符串」中引用對象或數組索引值時,我經常自己使用此語法。 – bdl 2010-02-14 21:27:18