我有一個表單,我需要在其中附加圖像文件并上傳到數(shù)據(jù)庫,我可以將其插入到數(shù)據(jù)庫中。但是我無法從數(shù)據(jù)庫中檢索圖像來顯示它。如何從數(shù)據(jù)庫中獲取圖像并顯示?form2.php: <form action="insert2.php" method="GET" enctype="multipart/form-data"> <div class="container"> <div class="row"> <h2>3. Description of Item(s) </h2> </div> <div class="col-xs-12"> <div class="styled-input wide"> <textarea name="description" required /></textarea> </div> // this is the file attachment where it allows to select file from computer. <div> <label>Attachment:</label><input type='file' name='img' ><br> </div> </div> </form>插入2.php: $con= mysqli_connect('127.0.0.1','root',''); if(!$con) { echo 'Not Connected To Server'; } if(!mysqli_select_db($con,'satsform1')) { echo 'Database Not Selected'; } $description = $_GET['description']; $image = $_GET['img'];//insert image to database. $sql = "INSERT INTO handover (description,image) VALUES ('$description','$image')"; if(!mysqli_query($con,$sql)) { echo 'Not Submitted'; } else { echo 'Submitted'; } header("refresh:2; url=selection.php")?>
2 回答

楊__羊羊
TA貢獻(xiàn)1943條經(jīng)驗 獲得超7個贊
這是您可以使用的示例腳本:
<?php
$con= mysqli_connect('127.0.0.1','root','');
$query = "SELECT * FROM handover ORDER BY ID";
$stmt = $con->prepare($query);
$stmt->execute();
$result = $stmt->get_result();
$stmt->close();
while($obj = $result->fetch_object()) {
echo($obj->ID.": <img src='".$obj->image."' alt='IMG-".$obj->ID."' height='150' width='150'><br>");
echo($obj->description);
}
?>
將此代碼綁定到您的form2.php或media.php應(yīng)該工作。如果您想要此代碼中的其他內(nèi)容,請隨時對其進(jìn)行編輯。
- 2 回答
- 0 關(guān)注
- 121 瀏覽
添加回答
舉報
0/150
提交
取消