第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試將圖像存儲到數(shù)據(jù)庫時出現(xiàn) SQL 錯誤(已更新)

嘗試將圖像存儲到數(shù)據(jù)庫時出現(xiàn) SQL 錯誤(已更新)

PHP
森林海 2023-08-11 17:43:41
UPDATED:正如評論部分所建議的,我已經(jīng)更新了我的代碼并現(xiàn)在使用 PDO。我仍然有同樣的錯誤。我正在嘗試將多個圖像存儲到數(shù)據(jù)庫(phpmyadmin)。當(dāng)我嘗試上傳時出現(xiàn)錯誤Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'file.png' in 'field list'我的數(shù)據(jù)庫:我有一個名為 image_upload 的數(shù)據(jù)庫,其中有一個名為 car_detailss 的表,其中包含 id (int11)、car_name(varchar 255) 和 imageOfcar(longblob)。這是我的圖像詳細(xì)信息:Array(    [0] => Array        (            [name] => file.png            [type] => image/png            [tmp_name] => /opt/lampp/temp/phpJYyrQn            [error] => 0            [size] => 77776        )    [1] => Array        (            [name] => files.png            [type] => image/png            [tmp_name] => /opt/lampp/temp/phpXOLvzL            [error] => 0            [size] => 84710        ))如何修復(fù)我提到的它顯示的錯誤(在頂部)?
查看完整描述

1 回答

?
繁花如伊

TA貢獻(xiàn)2012條經(jīng)驗 獲得超12個贊

我首先將代碼更改為 PDO,并且沒有在沒有任何反引號或“”的情況下插入數(shù)據(jù)庫,而是按照注釋部分中的建議,在將圖像插入數(shù)據(jù)庫時添加了反引號和“”。


$conn->exec( "INSERT INTO car_detailss? (car_name , imageOfcar) VALUES ('$carname', '$imagename')");


現(xiàn)在我可以將圖像插入到我的數(shù)據(jù)庫中。我也在這里更新了我的代碼。


我的代碼:


<!doctype html>

<html>

? ? <head>

? ? ? ? <title>Test</title>

? ? </head>

? ? <body>

? ? <form? ?method="post" enctype="multipart/form-data">

? ? ? ? ? ?<input type="file" name="userfile[]" multiple="" />?

? ? ? ? ? ?<input type="submit" name="submit" value="upload" />

? ? </form>


? ? <?php

? ? ? $servername="localhost";

? ? ? $username="root";

? ? ? $password = "";

? ? ? $dbname="image_upload";

? ? ? try {

? ? ? $conn = new PDO("mysql:host=$servername;dbname=$dbname",$username, $password);

? ? ? $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


? ? ? // begin the transaction

? ? ? $conn->beginTransaction();



? ? ??

? ? ??


$phpFileUploadErrors = array(

? ? 0 => "There is no error, the file uploaded with success",

? ? 1 => "The uploaded file exceeds the upload_max_filesize directive in php.ini",


? ? 2 => "The upload file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form",


? ? 3 => "The upload file was only partially uploaded",



? ? 4 => "No file was uploaded",


? ? 6 => "Missing a temporary folder",

? ? 7 => "Failed to write file to disk.",

? ? 8 => "A php extension stopped the file upload"

);




? ? if(isset($_FILES['userfile'])) {

? ? ??

? ? ? ?$file_array = reArrayFiles($_FILES['userfile']);

? ? ? ?pre_r($file_array);

? ? ? ? for($i=0;$i<count($file_array);$i++) {


? ? $carname = $file_array[$i]["size"];

? ? $tablename = "car_detailss";


? ? ? ? ? ? $imagename = $file_array[$i]["name"];

? ? ? ? ? ?

? ? ? ? ? ? $conn->exec( "INSERT INTO `car_detailss` (`car_name`, `imageOfcar`) VALUES ('$carname', '$imagename')");


? ? ? ? ??

? ? ? ? ??

? ? ? ? ??

? ? ? ? ? ? }

? ? ? ? ? ?

? ? ? ? ?

? ? ? ? ?


? ? ? ? }

? ? ? ? $conn->commit();

? ? ? ? echo "new records created succesfully";

? ? } catch(PDOException $e) {

? ? ? ? // roll back the transaction if something failed

? ? ? ? $conn->rollback();

? ? ? ? echo "Error: " . $e->getMessage();

? ? ? }

? ? ??

? ? ? $conn = null;

? ? ? ?

? ??


? ? ??

?

? ??


?function reArrayFiles($file_post) {

? ? ?$file_ary = array();

? ? ?$file_count = count($file_post['name']);

? ? ?$file_keys = array_keys($file_post);


? ? ?for ($i = 0; $i < $file_count; $i++){

? ? ?foreach($file_keys as $key) {

? ? ? ? ?$file_ary[$i][$key] = $file_post[$key][$i];

? ? ?

? ? ?}

?}

?return $file_ary;

}


? ? function pre_r($array) {

? ? ? ? echo '<pre>';

? ? ? ? print_r($array);

? ? ? ? echo '</pre>';

? ? }


? ??

? ? ?>

? ??

? ? </body>

</html>



查看完整回答
反對 回復(fù) 2023-08-11
  • 1 回答
  • 0 關(guān)注
  • 130 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號