|
 |
|
ต้องการเพิ่มข้อมูล ลงตารางที่ 2 มี foreign key จากตารางที่ 1 ต้องเขียนอย่างไรค่ะ |
|
 |
|
|
 |
 |
|
ช่วยด้วยค่ะ จะเก็บฐานข้อมูลประวัติบุคคล
มี 2 tableที่เชื่อมกัน คือ
- reviewers เก็บประวัติบุคคล
- education เก็บประวัติการศึกษา สามารถเพิ่มได้ไม่จำกัด

Submit Form สำเร็จ แต่ตาราง education ไม่บันทึก FK ของ reviewers ค่ะ ได้แต่ค่า 0
รบกวนช่วยที (มือใหม่หัดเขียน php and sql)
Code (PHP)
require 'connectdb.php';
if (empty($_POST['firstname'])){
echo 'Could you please fill in this form?';
header("Location:index.php");
exit();
} else {
$firstname = mysqli_real_escape_string($dbcon,$_POST['firstname']);
}
$aca_id = $_POST['aca_id'];
$lastname = $_POST['lastname'];
$department = $_POST['department'];
$faculty = $_POST['faculty'];
$university =$_POST['university'];
$email =$_POST['email'];
$mobile =$_POST['mobile'];
$expertise =$_POST['expertise'];
$review_preference =$_POST['review_preference'];
$ext = pathinfo(basename($_FILES['cv']['name']),PATHINFO_EXTENSION);
$new_ab_name = 'cv_'.uniqid().".".$ext;
$file_path = "cv/";
$upload_path = $file_path.$new_ab_name;
// uploading CV
$success = move_uploaded_file($_FILES['cv']['tmp_name'], $upload_path);
if ($success==FALSE){
echo 'ไม่สามารถ upload CV'. mysqli_error($dbcon);
exit();
}
$cv = $new_ab_name;
$q = "INSERT INTO reviewers (aca_id,firstname,lastname,department,faculty,university,email,mobile,cv,expertise,review_preference) VALUES ('$aca_id','$firstname','$lastname','$department','$faculty','$university','$email','$mobile','$cv','$expertise','$review_preference')";
$result = mysqli_query($dbcon, $q);
if(isset($_POST['submit'])){
$gradArr = $_POST['grad'];
$grad_fieldArr = $_POST['grad_field'];
$grad_yearArr = $_POST['grad_year'];
$grad_instArr = $_POST['grad_inst'];
if(!empty($gradArr)){
$rev_id = mysqli_insert_id();
for($i = 0; $i < count($gradArr); $i++){
if(!empty($gradArr[$i])){
$grad = $gradArr[$i];
$grad_field = $grad_fieldArr[$i];
$grad_year = $grad_yearArr[$i];
$grad_inst = $grad_instArr[$i];
//database insert query goes here
$q = "INSERT INTO education (rev_id,grad,grad_field,grad_year,grad_inst) VALUES ('$rev_id','$grad','$grad_field','$grad_year','$grad_inst')";
$result = mysqli_query($dbcon, $q);
}
}
}
}
Tag : PHP, MySQL

|
ประวัติการแก้ไข 2018-05-24 16:48:27
|
 |
 |
 |
 |
Date :
2018-05-24 16:46:21 |
By :
oporzaa111 |
View :
1105 |
Reply :
3 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตรวจเช็คการคิวรี่บันทัดที่ 35 ก่อน
Code (PHP)
$result = mysqli_query($dbcon, $q) or die($dbcon->error. '<br>', $q);
ปล. เช็ค flow control ให้ดีครับ
if(isset($_POST['submit'])){ บันทัดนี้ควรทำแต่ต้นๆ ไฟล์แล้วครับ
|
ประวัติการแก้ไข 2018-05-24 16:55:23
 |
 |
 |
 |
Date :
2018-05-24 16:52:05 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
$rev_id = mysqli_insert_id($dbcon);
ใส่ตัว connect database เข้าไปด้วยครับ
|
 |
 |
 |
 |
Date :
2018-05-24 21:16:56 |
By :
kikeeza01 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ตอบความคิดเห็นที่ : 1 เขียนโดย : Chaidhanan เมื่อวันที่ 2018-05-24 16:52:05
รายละเอียดของการตอบ ::
ทำได้แล้วค่ะปรับ flow control ใหม่
Code (PHP)
<?php
require 'connectdb.php';
if(isset($_POST['submit'])){
if (empty($_POST['firstname'])){
echo 'Could you please fill in this form?';
header("Location:index.php");
exit();
} else {
$firstname = mysqli_real_escape_string($dbcon,$_POST['firstname']);
}
$aca_id = $_POST['aca_id'];
$lastname = $_POST['lastname'];
$department = $_POST['department'];
$faculty = $_POST['faculty'];
$university =$_POST['university'];
$email =$_POST['email'];
$mobile =$_POST['mobile'];
$expertise =$_POST['expertise'];
$review_preference =$_POST['review_preference'];
//upload CV
$ext = pathinfo(basename($_FILES['cv']['name']),PATHINFO_EXTENSION);
$new_ab_name = 'cv_'.uniqid().".".$ext;
$file_path = "cv/";
$upload_path = $file_path.$new_ab_name;
$success = move_uploaded_file($_FILES['cv']['tmp_name'], $upload_path);
if ($success==FALSE){
echo 'ไม่สามารถ upload CV'. mysqli_error($dbcon);
exit();
}
$cv = $new_ab_name;
$q = "INSERT INTO reviewers (aca_id,firstname,lastname,department,faculty,university,email,mobile,cv,expertise,review_preference) VALUES ('$aca_id','$firstname','$lastname','$department','$faculty','$university','$email','$mobile','$cv','$expertise','$review_preference')";
$result = mysqli_query($dbcon, $q) or die($dbcon->error.'<br>'. $q);
$gradArr = $_POST['grad'];
$grad_fieldArr = $_POST['grad_field'];
$grad_yearArr = $_POST['grad_year'];
$grad_instArr = $_POST['grad_inst'];
if(!empty($gradArr)){
$rev_id = mysqli_insert_id($dbcon);
for($i = 0; $i < count($gradArr); $i++){
if(!empty($gradArr[$i])){
$grad = $gradArr[$i];
$grad_field = $grad_fieldArr[$i];
$grad_year = $grad_yearArr[$i];
$grad_inst = $grad_instArr[$i];
//database insert query goes here
$q = "INSERT INTO education (rev_id,grad,grad_field,grad_year,grad_inst) VALUES ('$rev_id','$grad','$grad_field','$grad_year','$grad_inst')";
$result = mysqli_query($dbcon, $q);
}
}
}
}
if($result){
echo 'เพิ่มข้อมูลเรียบร้อยแล้ว';
header("Location:index.php");
}else {
echo 'เกิดข้อผิดพลาดในการเพิ่มข้อมูล'. mysqli_error($dbcon);
}
mysqli_close($dbcon);
|
ประวัติการแก้ไข 2018-05-25 09:18:56
 |
 |
 |
 |
Date :
2018-05-25 09:18:12 |
By :
oporzaa111 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|