我正在尝试将图像上传到我的数据库,所以我为此创建了一个表格。
使用以下PHP代码在同一页面上传和显示图像。
<?php
// Connection To Database
$host = "localhost";
$username = "root";
$password = "";
$dbname = "my_db";
$connection = mysqli_connect($host,$username,$password,$dbname);
if (!$connection)
{
die('Could Not Connect To Database: ' . mysqli_connect_error());
}
mysqli_query($connection, "SET NAMES utf8");
// Garb User Account Details
$garb = mysqli_query($connection, "SELECT * FROM my_table");
if(!$garb){
$error = "<div class='error'>There's Little Problem: ".mysql_error()."</div>";
} else {
//Showing The User Data
while($row = mysqli_fetch_array($garb)) {
$A_Id = $row['A_Id'];
$A_ProfilePic = $row['A_ProfilePic'];
}
// Getting Image
$A_ProfilePic = addslashes(file_get_contents($_FILES['A_ProfilePic']['tmp_name'])); //SQL Injection defence!
$A_ProfilePic = mysqli_real_escape_string($connection, $A_ProfilePic);
//Everything Is Okay So Let's Register This User
$update = mysqli_query($connection, "UPDATE accounts SET A_ProfilePic='$A_ProfilePic' WHERE A_Id='999999'");
if(!$update){
$updateerror = "<div class='error'>There's Little Problem: ".mysql_error()."</div>";
} else {
//Confirming Message To User
$error = "<div class='success'>Your Pic Is Updated.</div><br/>";
}
?>
/* Getting Image */
<?php echo '<img class="profileAvatar" alt="" title="" src="data:image/jpeg;base64,'.base64_encode($A_ProfilePic).'"/>';?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="A_ProfilePic" id="A_ProfilePic"/>
<input type="submit" name="submit" value="Update Your Setting"></input>
<input type="reset" name="reset" value="Reset Form"></input>
</form>
但是我收到错误,因为插入图像时我的Base64代码并没有完美插入,因为在检索图像时无法看到它。我从在线Base64转换器转换了相同的图像,然后从我的数据库代码获得了不同的代码。那么错误是什么...... ???
答案 0 :(得分:0)
最后,我通过使用与上述几乎相同但有一些变化的以下代码来实现它。现在从这段代码中我可以在一个页面上更改和查看运行时的图像。
<?php
// Connection To Database
$host = "localhost";
$username = "root";
$password = "";
$dbname = "my_db";
$connection = mysqli_connect($host,$username,$password,$dbname);
if (!$connection)
{
die('Could Not Connect To Database: ' . mysqli_connect_error());
}
mysqli_query($connection, "SET NAMES utf8");
// Garb User Account Details
$garb = mysqli_query($connection, "SELECT * FROM my_table");
if(!$garb){
$error = "<div class='error'>There's Little Problem: ".mysql_error()."</div>";
} else {
//Showing The User Data
while($row = mysqli_fetch_array($garb)) {
$A_Id = $row['A_Id'];
$A_ProfilePic = $row['A_ProfilePic'];
}
// Update Image In Database
if (isset($_POST['A_ProfilePic'])){
// Getting Image
$A_ProfilePic_Add = mysqli_real_escape_string($connection, $A_ProfilePic);
//Everything Is Okay So Let's Register This User
$update = mysqli_query($connection, "UPDATE accounts SET A_ProfilePic='$A_ProfilePic_Add' WHERE A_Id='999999'");
if(!$update){
$updateerror = "<div class='error'>There's Little Problem: ".mysql_error()."</div>";
} else {
//Confirming Message To User
$error = "<div class='success'>Your Pic Is Updated.</div><br/>";
}
header("Refresh:0");
}
?>
/* Getting Image */
<?php echo '<img class="profileAvatar" alt="" title="" src="data:image/jpeg;base64,'.base64_encode($A_ProfilePic).'"/>';?>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="A_ProfilePic" id="A_ProfilePic"/>
<input type="submit" name="submit" value="Update Your Setting"></input>
<input type="reset" name="reset" value="Reset Form"></input>
</form>