插入一个 Table,同时更新另一个并上传文件
Insert into one Table, while updating another & File Upload
我正在尝试更新一个 table 中的数据,同时从 PHP 表单向第二个 table 中插入一个新行。
点击提交时,我收到一条错误消息:
Uncaught Error: Call to undefined method mysqli::exec()
下面是我在 PHP 提交表单上的查询:
$link->exec("SET CHARACTER SET utf8"); // Sets encoding UTF-8
try {
$link->beginTransaction();
$q1 = $link->prepare("INSERT INTO
liquor_licenses_2
( username
, outlet_number
, license_date
, license
, scanned_license
, permit_renewal
, scanned_permit_renewal
, identity_document
, scanned_identity_document
)
VALUES
( :username
, :outlet_number
, :license_date
, :license
, :scanned_license
, :permit_renewal
, :scanned_permit_renewal
, :identity_document
, :scanned_identity_document
");
$q1->bindValue(':username', $username);
$q1->bindValue(':outlet_number', $outlet_number);
$q1->bindValue(':license_date', $license_date);
$q1->bindValue(':liquor_license', $license);
$q1->bindValue(':scanned_license', $scanned_license);
$q1->bindValue(':permit_renewal', $permit_renewal);
$q1->bindValue(':scanned_permit_renewal', $scanned_permit_renewal);
$q1->bindValue(':identity_document', $identity_document);
$q1->bindValue(':scanned_identity_document', $scanned_identity_document);
$q1->execute();
$q2 = $link->prepare("UPDATE
outlet_details
SET
username = :username
, outlet_number = :outlet_number
, mega_region = :mega_region
, outlet_name = :outlet_name
, address_0 = :address_0
, address_1 = :address_1
, address_2 = :address_2
, address_3 = :address_3
, address_4 = :address_4
, contact_number_1 = :contact_number_1
, contact_number_2 = :contact_number_2
WHERE
outlet_number = :outlet_number");
$q1->bindValue(':username', $username);
$q1->bindValue(':outlet_number', $outlet_number);
$q2->bindValue(':mega_region', $mega_region);
$q2->bindValue(':outlet_name', $outlet_name);
$q2->bindValue(':address_0', $address_0);
$q2->bindValue(':address_1', $address_1);
$q2->bindValue(':address_2', $address_2);
$q2->bindValue(':address_3', $address_3);
$q2->bindValue(':address_4', $address_4);
$q2->bindValue(':contact_number_1', $contact_number_1);
$q2->bindValue(':contact_number_2', $contact_number_2);
$q2->execute();
$link->commit();
} catch (Exception $e) {
$link->rollback();
}
if(mysqli_query($link, $q1)){
echo "Records added / updated successfully.";
} else{
echo "ERROR: Could not able to execute $q1. " . mysqli_error($link);
}
header("refresh:2;url=../outlet_capture.php");
// close connection
mysqli_close($link);
?>
我还想添加 3 个文件的上传(扫描的许可证,scanned_permit_renewal,scanned_identity_document)。我找到了下面的代码来处理这个问题,但我不确定如何实现它。
foreach($_FILES['file'] AS $key=>$file) {
$filename = $file['tmp_name'];
$size = $file['size'];
$newfile = $_SERVER['DOCUMENT_ROOT'] . "/uploads/" . date("Ymd_his") . "_" . $filename;
move_uploaded_file($filename, $newfile);
}
我需要将文件名存储在我的数据库中,以便我可以将它引用到已上传到服务器的文件。
$link->set_charset("utf8");
try {
$link->begin_transaction();
$q1 = $link->prepare("INSERT INTO liquor_licenses_2
( username
, outlet_number
, license_date
, license
, scanned_license
, permit_renewal
, scanned_permit_renewal
, identity_document
, scanned_identity_document
)
VALUES
( ?,?,?,?,?,?,?,?,?)");
--^--(you missed closed bracket here)
$q1->bind_param("sdsssssss", $username, $outlet_number, $license_date,$license,$scanned_license,$permit_renewal,$scanned_permit_renewal,$identity_document,$scanned_identity_document);
$res1=$q1->execute();
$q2 = $link->prepare("UPDATE
outlet_details
SET
username = ?
, outlet_number = ?
, mega_region = ?
, outlet_name = ?
, address_0 = ?
, address_1 = ?
, address_2 = ?
, address_3 = ?
, address_4 = ?
, contact_number_1 = ?
, contact_number_2 = ?
WHERE
outlet_number = ?");
$q2->bind_param("sdsssssssssd", $username, $outlet_number, $mega_region,outlet_name,$address_0,$address_1,$address_2,$address_3,$address_4,$contact_number_1,$contact_number_1,$outlet_number);
$q2->execute();
$link->commit();
} catch (Exception $e) {
$link->rollback();
}
if($res1){
echo "Records added / updated successfully.";
} else{
echo "ERROR: Could not able to execute $q1. " . mysqli_error($link);
}
header("refresh:2;url=../outlet_capture.php");
// close connection
$link->close();
?>
Try this code for multiple file uploading:
if (isset($_FILES["file"]["name"])) {
foreach($_FILES['file']['tmp_name'] as $key => $tmp_name)
{
$file_name = $key.$_FILES['file']['name'][$key];
$file_size =$_FILES['file']['size'][$key];
$file_tmp =$_FILES['file']['tmp_name'][$key];
$file_type=$_FILES['file']['type'][$key];
$new_file = $_SERVER['DOCUMENT_ROOT'] . "/uploads/" . date("Ymd_his") . "_" . $file_name;
//echo $new_file;
move_uploaded_file($file_tmp,$new_file);
}
}
我正在尝试更新一个 table 中的数据,同时从 PHP 表单向第二个 table 中插入一个新行。
点击提交时,我收到一条错误消息:
Uncaught Error: Call to undefined method mysqli::exec()
下面是我在 PHP 提交表单上的查询:
$link->exec("SET CHARACTER SET utf8"); // Sets encoding UTF-8
try {
$link->beginTransaction();
$q1 = $link->prepare("INSERT INTO
liquor_licenses_2
( username
, outlet_number
, license_date
, license
, scanned_license
, permit_renewal
, scanned_permit_renewal
, identity_document
, scanned_identity_document
)
VALUES
( :username
, :outlet_number
, :license_date
, :license
, :scanned_license
, :permit_renewal
, :scanned_permit_renewal
, :identity_document
, :scanned_identity_document
");
$q1->bindValue(':username', $username);
$q1->bindValue(':outlet_number', $outlet_number);
$q1->bindValue(':license_date', $license_date);
$q1->bindValue(':liquor_license', $license);
$q1->bindValue(':scanned_license', $scanned_license);
$q1->bindValue(':permit_renewal', $permit_renewal);
$q1->bindValue(':scanned_permit_renewal', $scanned_permit_renewal);
$q1->bindValue(':identity_document', $identity_document);
$q1->bindValue(':scanned_identity_document', $scanned_identity_document);
$q1->execute();
$q2 = $link->prepare("UPDATE
outlet_details
SET
username = :username
, outlet_number = :outlet_number
, mega_region = :mega_region
, outlet_name = :outlet_name
, address_0 = :address_0
, address_1 = :address_1
, address_2 = :address_2
, address_3 = :address_3
, address_4 = :address_4
, contact_number_1 = :contact_number_1
, contact_number_2 = :contact_number_2
WHERE
outlet_number = :outlet_number");
$q1->bindValue(':username', $username);
$q1->bindValue(':outlet_number', $outlet_number);
$q2->bindValue(':mega_region', $mega_region);
$q2->bindValue(':outlet_name', $outlet_name);
$q2->bindValue(':address_0', $address_0);
$q2->bindValue(':address_1', $address_1);
$q2->bindValue(':address_2', $address_2);
$q2->bindValue(':address_3', $address_3);
$q2->bindValue(':address_4', $address_4);
$q2->bindValue(':contact_number_1', $contact_number_1);
$q2->bindValue(':contact_number_2', $contact_number_2);
$q2->execute();
$link->commit();
} catch (Exception $e) {
$link->rollback();
}
if(mysqli_query($link, $q1)){
echo "Records added / updated successfully.";
} else{
echo "ERROR: Could not able to execute $q1. " . mysqli_error($link);
}
header("refresh:2;url=../outlet_capture.php");
// close connection
mysqli_close($link);
?>
我还想添加 3 个文件的上传(扫描的许可证,scanned_permit_renewal,scanned_identity_document)。我找到了下面的代码来处理这个问题,但我不确定如何实现它。
foreach($_FILES['file'] AS $key=>$file) {
$filename = $file['tmp_name'];
$size = $file['size'];
$newfile = $_SERVER['DOCUMENT_ROOT'] . "/uploads/" . date("Ymd_his") . "_" . $filename;
move_uploaded_file($filename, $newfile);
}
我需要将文件名存储在我的数据库中,以便我可以将它引用到已上传到服务器的文件。
$link->set_charset("utf8");
try {
$link->begin_transaction();
$q1 = $link->prepare("INSERT INTO liquor_licenses_2
( username
, outlet_number
, license_date
, license
, scanned_license
, permit_renewal
, scanned_permit_renewal
, identity_document
, scanned_identity_document
)
VALUES
( ?,?,?,?,?,?,?,?,?)");
--^--(you missed closed bracket here)
$q1->bind_param("sdsssssss", $username, $outlet_number, $license_date,$license,$scanned_license,$permit_renewal,$scanned_permit_renewal,$identity_document,$scanned_identity_document);
$res1=$q1->execute();
$q2 = $link->prepare("UPDATE
outlet_details
SET
username = ?
, outlet_number = ?
, mega_region = ?
, outlet_name = ?
, address_0 = ?
, address_1 = ?
, address_2 = ?
, address_3 = ?
, address_4 = ?
, contact_number_1 = ?
, contact_number_2 = ?
WHERE
outlet_number = ?");
$q2->bind_param("sdsssssssssd", $username, $outlet_number, $mega_region,outlet_name,$address_0,$address_1,$address_2,$address_3,$address_4,$contact_number_1,$contact_number_1,$outlet_number);
$q2->execute();
$link->commit();
} catch (Exception $e) {
$link->rollback();
}
if($res1){
echo "Records added / updated successfully.";
} else{
echo "ERROR: Could not able to execute $q1. " . mysqli_error($link);
}
header("refresh:2;url=../outlet_capture.php");
// close connection
$link->close();
?>
Try this code for multiple file uploading:
if (isset($_FILES["file"]["name"])) {
foreach($_FILES['file']['tmp_name'] as $key => $tmp_name)
{
$file_name = $key.$_FILES['file']['name'][$key];
$file_size =$_FILES['file']['size'][$key];
$file_tmp =$_FILES['file']['tmp_name'][$key];
$file_type=$_FILES['file']['type'][$key];
$new_file = $_SERVER['DOCUMENT_ROOT'] . "/uploads/" . date("Ymd_his") . "_" . $file_name;
//echo $new_file;
move_uploaded_file($file_tmp,$new_file);
}
}