php - how to insert image in database using phpmyadmin? -
im develop website using xampp localhost. want add image using phpmyadmin can't add it..the image can display in phpmyadmin can't appear on website..is wrong coding in php?
$host="localhost"; // host name $username="root"; // mysql username $password=""; // mysql password $db_name="mmdb"; // database name $tbl_name="artist"; // table name // create connection $conn = mysqli_connect($host, $username, $password, $db_name); // check connection if (!$conn) { die("connection failed: " . mysqli_connect_error()); } $name=$_post['name']; $hometown=$_post['hometown']; $income=$_post['income']; $image=$_post['image']; //file =$_files['image']['tmp_name']; $video=$_post['video']; $sql = "insert $tbl_name(name, hometown, income, image, video) values ('$name', '$hometown','$income','$image','$video')"; if (mysqli_query($conn, $sql)) { include 'admin.php'; } else { echo "x"; } mysqli_close($conn); ?>
whats code add image in folder?
// should insert code "> $image = $_files["image"]["name"]; $uploadedfile = $_files['image']['tmp_name']; if ($image) { $filename = stripslashes($_files['image']['name']);
/* ensure file jpg/png */ if (($_files['image']['type'] != "image/jpg") && ($_files['image']['type'] != "image/jpeg") && ($_files['image']['type'] != "image/png")) { $error_status = 2; $error_message = "file uploaded not jpg or png file."; } else { /* ensure file less 10mb */ $size = filesize($_files['image']['tmp_name']); if ($size > (max_size * 1024)) { $error_status = 3; $error_message = "file uploaded needs less 10mb."; } else { $extension = "jpg"; if(($_files['image']['type'] == "image/jpg") || ($_files['image']['type'] == "image/jpeg")) { $uploadedfile = $_files['image']['tmp_name']; $src = imagecreatefromjpeg($uploadedfile); } else if (($_files['image']['type'] == "image/png")) { $extension = "png"; $uploadedfile = $_files['image']['tmp_name']; $src = imagecreatefrompng($uploadedfile); } $img_mime_type = $_files['image']['type'];
as others have said, should avoid storing images in mysql, if still want it's pretty simple:
<?php $host="localhost"; // host name $username="root"; // mysql username $password=""; // mysql password $db_name="mmdb"; // database name $tbl_name="artist"; // table name // create connection $conn = mysqli_connect($host, $username, $password, $db_name); // check connection if (!$conn) { die("connection failed: " . mysqli_connect_error()); } //btw: escape data $name=mysqli_real_escape_string($conn,$_post['name']); $hometown=mysqli_real_escape_string($conn,$_post['hometown']); $income=mysqli_real_escape_string($conn,$_post['income']); $image=mysqli_real_escape_string($conn,file_get_contents($_files['image']['tmp_name'])); $video=mysqli_real_escape_string($conn,$_post['video']); $sql = "insert $tbl_name (name, hometown, income, image, video) values ('$name', '$hometown','$income','$image','$video')"; if (mysqli_query($conn, $sql)) { include 'admin.php'; } else { echo "x"; } mysqli_close($conn); ?>
depending on size of data, script may take while execute, not mention warnings others have posted.
to save folder
i believe code posted convert image different format. allows store folder:
$savepath = "/your/path" if($_files['image']['error'] != upload_err_ok) { $error_status = 1; $error_message = "there error uploading file"; } /* ensure file jpg/png */ elseif (($_files['image']['type'] != "image/jpg") && ($_files['image']['type'] != "image/jpeg") && ($_files['image']['type'] != "image/png")) { $error_status = 2; $error_message = "file uploaded not jpg or png file."; } else { /* ensure file less 10mb */ $size = filesize($_files['image']['tmp_name']); if ($size > (max_size * 1024)) { $error_status = 3; $error_message = "file uploaded needs less 10mb."; } else { //save image specified directory $error_status = 0; $error_message = ''; move_uploaded_file($_files['image']['tmp_name'], $savepath.directory_separator.$_files['image']['name']); } } //now file should uploaded, if error status 0, can safely this: $image = $savepath.directory_separator.$_files['image']['name']; //and put database "as text"
Comments
Post a Comment