When I run the following code I get this error:
Fatal error: Uncaught PDOException: SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens in C:\xampp\htdocs\pizda\aids\admin\addnew.php:73 Stack trace: #0
on line 73
<?php
error_reporting( ~E_NOTICE ); // avoid notice
require_once 'dbconfig.php';
if(isset($_POST['btnsave']))
{
$NaslovSrpski = $_POST['NaslovSrpski'];
$NaslovEngleski = $_POST['NaslovEngleski'];
$TekstSrpski = $_POST['TekstSrpski'];
$TekstEngleski = $_POST['TekstEngleski'];
$imgFile = $_FILES['user_image']['name'];
$tmp_dir = $_FILES['user_image']['tmp_name'];
$imgSize = $_FILES['user_image']['size'];
if(empty($NaslovSrpski)){
$errMSG = "Please Enter Username.";
}
else if(empty($NaslovEngleski)){
$errMSG = "Please Enter Your Job Work.";
}
else if(empty($TekstSrpski)){
$errMSG = "Please Enter Your Job Work.";
}
else if(empty($TekstEngleski)){
$errMSG = "Please Enter Your Job Work.";
}
else if(empty($imgFile)){
$errMSG = "Please Select Image File.";
}
else
{
$upload_dir = '../uploads/'; // upload directory
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
// valid image extensions
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
// rename uploading image
$userpic = rand(1000,1000000).".".$imgExt;
// allow valid image file formats
if(in_array($imgExt, $valid_extensions)){
// Check file size '5MB'
if($imgSize < 5000000) {
move_uploaded_file($tmp_dir,$upload_dir.$userpic);
}
else{
$errMSG = "Sorry, your file is too large.";
}
}
else{
$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
}
// if no error occured, continue ....
if(!isset($errMSG))
{
$stmt = $DB_con->prepare('INSERT INTO Obavestenja(NaslovSrpski,NaslovEngleski,TekstSrpski,TekstEngleski, Slika) VALUES(:naslovs, :naslove, :teksts, tekste, :upic)');
$stmt->bindParam(':naslovs',$NaslovSrpski);
$stmt->bindParam(':naslove',$NaslovEngleski);
$stmt->bindParam(':teksts',$TekstSrpski);
$stmt->bindParam(':tekste',$TekstEngleski);
$stmt->bindParam(':upic',$userpic);
if($stmt->execute())
{
$successMSG = "new record succesfully inserted ...";
header("refresh:5;index.php"); // redirects image view page after 5 seconds.
}
else
{
$errMSG = "error while inserting....";
}
}
}
include 'header.php';
?>
<div class="container">
<div class="page-header">
<h1 class="h2">add news. <a class="btn btn-default" href="index.php"> <span class="glyphicon glyphicon-eye-open"></span> view all </a></h1>
</div>
<?php
if(isset($errMSG)){
?>
<div class="alert alert-danger">
<span class="glyphicon glyphicon-info-sign"></span> <strong><?php echo $errMSG; ?></strong>
</div>
<?php
}
else if(isset($successMSG)){
?>
<div class="alert alert-success">
<strong><span class="glyphicon glyphicon-info-sign"></span> <?php echo $successMSG; ?></strong>
</div>
<?php
}
?>
<form method="post" enctype="multipart/form-data" class="form-horizontal">
<table class="table table-bordered table-responsive">
<tr>
<td><label class="control-label">NaslovSrpski</label></td>
<td><input class="form-control" type="text" name="NaslovSrpski" placeholder="NaslovSrpski" value="<?php echo $NaslovSrpski; ?>" /></td>
</tr>
<tr>
<td><label class="control-label">NaslovEngleski</label></td>
<td><input class="form-control" type="text" name="NaslovEngleski" placeholder="NaslovEngleski" value="<?php echo $NaslovEngleski; ?>" /></td>
</tr>
<tr>
<td><label class="control-label">TekstSrpski</label></td>
<td><textarea class="form-control" type="text" name="TekstSrpski" placeholder="TekstSrpski" value="<?php echo $TekstSrpski; ?>" /></textarea></td>
</tr>
<tr>
<td><label class="control-label">TekstEngleski</label></td>
<td><textarea class="form-control" type="text" name="TekstEngleski" placeholder="TekstEngleski" value="<?php echo $TekstEngleski; ?>" /></textarea></td>
</tr>
<tr>
<td><label class="control-label">Profile Img.</label></td>
<td><input class="input-group" type="file" name="user_image" accept="image/*" /></td>
</tr>
<tr>
<td colspan="2"><button type="submit" name="btnsave" class="btn btn-default">
<span class="glyphicon glyphicon-save"></span> save
</button>
</td>
</tr>
</table>
</form>
You're missing a ":" in tekste's placeholder:
$stmt = $DB_con->prepare
('INSERT INTO Obavestenja' .
' (NaslovSrpski,NaslovEngleski,TekstSrpski,TekstEngleski, Slika)' .
' VALUES (:naslovs, :naslove, :teksts, :tekste, :upic)');
# ":" was missing here ----------------^
Related
I'm very new to any web development issues and I couldn't find a solution to this problem. I have a form for uploading files so I use the enctype="multipart/form-data". However, I was also trying to take the fields from the form and encode them in a JSON file and read those out in a table in my html on my webpage. With the default enctype, the JSON encoding works great but obviously the file upload functionality doesn't work and vice versa. I followed a lot of W3Schools tutorials but I'm still lost. Is it possible to use two enctypes or what is the work around to this problem? I'm only uploading my files to localhost, no databases used. I apologize if I give too much info here but my form html looks like:
<form class="modal-content animate" enctype="multipart/form-data" method="post">
<div class="imgcontainer">
<span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">×</span>
</div>
<div class="container">
<label for="artist"><b>Artist Name</b></label>
<input class="form-control" type="text" placeholder="Enter name of artist" name="artist" required>
<label for="song"><b>Song Name</b></label>
<input class="form-control" type="text" placeholder="Enter name of song." name="song" required>
<label for="instrument"><b>Instrument Type</b></label>
<input class="form-control" type="text" placeholder="Enter type of instrument for tab." name="instrument" required>
<label for="myfile"><b>Tablature</b></label>
<input class="form-control" type="file" placeholder="Select your file." name="myfile" id="myfile" required>
<input type="submit" name="submit" value="Upload Tab PDF" class="w3-hover-purple" style="color:black; background-color:#A9A9A9"></input>
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" onclick="document.getElementById('id01').style.display='none'" class="cancelbtn">Cancel</button>
</div>
</form>
My php code is structured as such:
<?php
$message = '';
$error = '';
if(isset($_POST["submit"])) {
if(empty($_POST["artist"])) {
$error = "<label class='text-danger'>Enter artist</label>";
}
else if(empty($_POST["song"])) {
$error = "<label class='text-danger'>Enter song name</label>";
}
else if(empty($_POST["instrument"])) {
$error = "<label class='text-danger'>Enter instrument</label>";
}
else if(empty($_POST["myfile"])) {
$error = "<label class='text-danger'>Enter file to upload</label>";
}
else {
if(file_exists('tablature.json')) {
$current_data = file_get_contents('tablature.json');
$array_data = json_decode($current_data, true);
$extra = array(
'artist' => $_POST['artist'],
'song' => $_POST['song'],
'instrument' => $_POST['instrument'],
'myfile' => $_POST['myfile']
);
$array_data[] = $extra;
$final_data = json_encode($array_data);
if(file_put_contents('tablature.json', $final_data)) {
$message = "<label class='text-success'>File Appended Successfully.</label>";
}
else {
$error = 'JSON File does not exist';
}
}
}
$target_dir = "Tablature/";
$target_file = $target_dir . basename($_FILES["myfile"]["name"]);
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if file already exists
if (file_exists($target_file)) {
$error = "Sorry, file already exists.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
$error = "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["myfile"]["tmp_name"], $target_file)) {
$message = "The file has been uploaded.";
} else {
$error = "Sorry, there was an error uploading your file.";
}
}
}
?>
Finally, on my page I print out my JSON contents on my page like so:
<table style="width:100%">
<tr>
<th>Artist</th>
<td>Song Name</td>
<td>Instrument Type</td>
<td style="float:right; margin-right:100px;">File</td>
</tr>
<?php
$read_data = file_get_contents("tablature.json");
$read_data = json_decode($read_data, true);
print("Working<br>");
foreach($read_data as $row) {
print('<tr><th>'.$row["artist"].'</th><td>'.$row["song"].'</td><td>'.$row["instrument"].'</td><td style="float:right">'.$row["myfile"].'</td></tr>');
}
?>
</table><br>
Any help would be greatly appreciated. Thank you in advance.
I have a code which works fine if I want to upload one image into my MySQL and server.
PHP code:
if(isset($_POST['btnsave']))
{ foreach($_FILES['user_image']['tmp_name'] as $key => $tmp_name ){
$username = $_POST['user_name'];
$userjob = $_POST['user_job'];
$imgFile = $key.$_FILES['user_image']['name'][$key];
$tmp_dir = $_FILES['user_image']['tmp_name'][$key];
$imgSize = $_FILES['user_image']['size'][$key];
}
if(empty($username)){
$errMSG = "Please Enter Username.";
}
else if(empty($userjob)){
$errMSG = "Please Enter Your Job Work.";
}
else if(empty($imgFile)){
$errMSG = "Please Select Image File.";
}
else
{
$upload_dir = 'user_images/';
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
$userpic = rand(1000,1000000).".".$imgExt;
if(in_array($imgExt, $valid_extensions)){
if($imgSize < 5000000) {
move_uploaded_file($tmp_dir,$upload_dir.$userpic);
}
else{
$errMSG = "Sorry, your file is too large.";
}
}
else{
$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
}
if(!isset($errMSG))
{
$stmt = $DB_con->prepare('INSERT INTO tbl_users(userName,userProfession,userPic) VALUES(:uname, :ujob, :upic)');
$stmt->bindParam(':uname',$username);
$stmt->bindParam(':ujob',$userjob);
$stmt->bindParam(':upic',$userpic);
if($stmt->execute())
{
$successMSG = "new record successfully inserted ...";
header("refresh:5;index.php");
}
else
{
$errMSG = "error while inserting....";
}
}
}
Html code:
<?php
if(isset($errMSG)){
?>
<div class="alert alert-danger">
<span class="glyphicon glyphicon-info-sign"></span> <strong><?php echo $errMSG; ?></strong>
</div>
<?php
}
else if(isset($successMSG)){
?>
<div class="alert alert-success">
<strong><span class="glyphicon glyphicon-info-sign"></span> <?php echo $successMSG; ?></strong>
</div>
<?php
}
?>
<form method="post" enctype="multipart/form-data" class="form-horizontal">
<table class="table table-bordered table-responsive">
<tr>
<td><label class="control-label">Username.</label></td>
<td><input class="form-control" type="text" name="user_name" placeholder="Enter Username" value="<?php echo $username; ?>" /></td>
</tr>
<tr>
<td><label class="control-label">Profession(Job).</label></td>
<td><input class="form-control" type="text" name="user_job" placeholder="Your Profession" value="<?php echo $userjob; ?>" /></td>
</tr>
<tr>
<td><label class="control-label">Profile Img.</label></td>
<td><input class="input-group" type="file" name="user_image[]" accept="image/*" multiple /></td>
</tr>
<tr>
<td colspan="2"><button type="submit" name="btnsave" class="btn btn-default">
<span class="glyphicon glyphicon-save"></span> save
</button>
</td>
</tr>
</table>
</form>
How can I make it code to use for multiple upload? For all images which I would upload I would make the same values except 'userPic'- the same name for a file at my server. Can you help me out please?
I found one more problem! Maybe you guys can help me?
If I'm trying to upload for example 6 images, one of theme is bigger than maxsize its popups the error but every other files which was uploaded just before it went to upload folder. How to delete this files if i get an error?
second questions is how to resize images? any code?
Put everything inside the foreach block.
if(isset($_POST['btnsave']))
{ foreach($_FILES['user_image']['tmp_name'] as $key => $tmp_name ){
$username = $_POST['user_name'];
$userjob = $_POST['user_job'];
$imgFile = $key.$_FILES['user_image']['name'][$key];
$tmp_dir = $_FILES['user_image']['tmp_name'][$key];
$imgSize = $_FILES['user_image']['size'][$key];
if(empty($username)){
$errMSG = "Please Enter Username.";
}
else if(empty($userjob)){
$errMSG = "Please Enter Your Job Work.";
}
else if(empty($imgFile)){
$errMSG = "Please Select Image File.";
}
else
{
$upload_dir = 'user_images/';
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif');
$userpic = rand(1000,1000000).".".$imgExt;
if(in_array($imgExt, $valid_extensions)){
if($imgSize < 5000000) {
move_uploaded_file($tmp_dir,$upload_dir.$userpic);
}
else{
$errMSG = "Sorry, your file is too large.";
}
}
else{
$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
}
if(!isset($errMSG))
{
$stmt = $DB_con->prepare('INSERT INTO tbl_users(userName,userProfession,userPic) VALUES(:uname, :ujob, :upic)');
$stmt->bindParam(':uname',$username);
$stmt->bindParam(':ujob',$userjob);
$stmt->bindParam(':upic',$userpic);
if($stmt->execute())
{
$successMSG = "new record succesfully inserted ...";
header("refresh:5;index.php");
}
else
{
$errMSG = "error while inserting....";
}
}
}}
So I have a form that stores username, profession, image and image1.
Now, username, profession and image are stored in the database. But image2's name is stored without its extension and I cant figure out why its doing this.
<?php
error_reporting( ~E_NOTICE ); // avoid notice
require_once 'dbconfig.php';
if(isset($_POST['btnsave']))
{
$username = $_POST['user_name'];// user name
$userjob = $_POST['user_job'];// user email
$imgFile = $_FILES['user_image']['name'];
$tmp_dir = $_FILES['user_image']['tmp_name'];
$imgSize = $_FILES['user_image']['size'];
$imgFile1 = $_FILES['user_image1']['name'];
$tmp_dir1 = $_FILES['user_image1']['tmp_name'];
$imgSize1 = $_FILES['user_image1']['size'];
if(empty($username)){
$errMSG = "Please Enter Username.";
}
else if(empty($userjob)){
$errMSG = "Please Enter Your Job Work.";
}
else if(empty($imgFile or $imgFile1)){
$errMSG = "Please Select Image File.";
}
else
{
$upload_dir = 'user_images/'; // upload directory
$imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension
$imgExt1 = strtolower(pathinfo($imgFile1,PATHINFO_EXTENSION)); // get image extension
// valid image extensions
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif'); // valid extensions
// rename uploading image
$userpic = rand(1000,1000000).".".$imgExt;
$userpic1 = rand(1000,1000000).".".$imgExt1;
// allow valid image file formats
if(in_array($imgExt, $valid_extensions)){
// Check file size '5MB'
if($imgSize < 5000000) {
move_uploaded_file($tmp_dir,$upload_dir.$userpic);
}
else{
$errMSG = "Sorry, your file is too large.";
}
}
else if(in_array($imgExt1, $valid_extensions)){
// Check file size '5MB'
if($imgSize1 < 5000000) {
move_uploaded_file($tmp_dir1,$upload_dir.$userpic1);
}
else{
$errMSG = "Sorry, your file is too large.";
}
}
else{
$errMSG = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
}
}
// if no error occured, continue ....
if(!isset($errMSG))
{
$stmt = $DB_con->prepare('INSERT INTO tbl_users(userName,userProfession,userPic,userPic1) VALUES(:uname, :ujob, :upic, :upic1)');
$stmt->bindParam(':uname',$username);
$stmt->bindParam(':ujob',$userjob);
$stmt->bindParam(':upic',$userpic);
$stmt->bindParam(':upic1',$userpic1);
if($stmt->execute())
{
$successMSG = "new record succesfully inserted ...";
header("refresh:5;index.php"); // redirects image view page after 5 seconds.
}
else
{
$errMSG = "error while inserting....";
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Upload, Insert, Update, Delete an Image using PHP MySQL - Coding Cage</title>
<link rel="stylesheet" href="bootstrap/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="bootstrap/css/bootstrap-theme.min.css">
</head>
<body>
<div class="navbar navbar-default navbar-static-top" role="navigation">
<div class="container">
<div class="navbar-header">
<a class="navbar-brand" href="http://www.codingcage.com" title='Programming Blog'>Coding Cage</a>
<a class="navbar-brand" href="http://www.codingcage.com/search/label/CRUD">CRUD</a>
<a class="navbar-brand" href="http://www.codingcage.com/search/label/PDO">PDO</a>
<a class="navbar-brand" href="http://www.codingcage.com/search/label/jQuery">jQuery</a>
</div>
</div>
</div>
<div class="container">
<div class="page-header">
<h1 class="h2">add new user. <a class="btn btn-default" href="index.php"> <span class="glyphicon glyphicon-eye-open"></span> view all </a></h1>
</div>
<?php
if(isset($errMSG)){
?>
<div class="alert alert-danger">
<span class="glyphicon glyphicon-info-sign"></span> <strong><?php echo $errMSG; ?></strong>
</div>
<?php
}
else if(isset($successMSG)){
?>
<div class="alert alert-success">
<strong><span class="glyphicon glyphicon-info-sign"></span> <?php echo $successMSG, $imgExt1; ?></strong>
</div>
<?php
}
?>
<form method="post" enctype="multipart/form-data" class="form-horizontal">
<table class="table table-bordered table-responsive">
<tr>
<td><label class="control-label">Username.</label></td>
<td><input class="form-control" type="text" name="user_name" placeholder="Enter Username" value="<?php echo $username; ?>" /></td>
</tr>
<tr>
<td><label class="control-label">Profession(Job).</label></td>
<td><input class="form-control" type="text" name="user_job" placeholder="Your Profession" value="<?php echo $userjob; ?>" /></td>
</tr>
<tr>
<td><label class="control-label">Profile Img.</label></td>
<td><input class="input-group" type="file" name="user_image" accept="image/*" /></td>
</tr>
<tr>
<td><label class="control-label">Profile Img2.</label></td>
<td><input class="input-group" type="file" name="user_image1" accept="image/*" /></td>
</tr>
<tr>
<td colspan="2"><button type="submit" name="btnsave" class="btn btn-default">
<span class="glyphicon glyphicon-save"></span> save
</button>
</td>
</tr>
</table>
</form>
<div class="alert alert-info">
<strong>tutorial link !</strong> Coding Cage!
</div>
</div>
<!-- Latest compiled and minified JavaScript -->
<script src="bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
Your help would be greatly appreciated.
I have a problem with 1 part of upload section, how to make the code continue the process if the upload section is empty? I already make the code, but everytime i skip the file button, it show echo 'ERROR: Ekstensi file tidak di izinkan!'
Here is my code:
insert_form
<form name="pegawai" action="insert-proses.php" method="post" enctype="multipart/form-data">
<table width="700px" align="left" cellpadding="2" cellspacing="0">
<tr>
<td><b>NOKOM</b></td>
<td><b>:</b></td>
<td><input type="text" name="nokom" size="40" required /></td>
</tr>
<tr>
<td><b>NIP</b></td>
<td><b>:</b></td>
<td><input type="text" name="nip" size="40" required /></td>
</tr>
<tr>
<td><b>Nama</b></td>
<td><b>:</b></td>
<td><input type="text" name="nama" size="40" required /></td>
</tr>
<tr>
<td><b>Foto</b></td>
<td><b>:</b></td>
<td><input name="file" type="file" id="file" /></td>
</tr>
<tr>
<td></td>
<td></td>
<td><hr/><input type="submit" name="upload" value="Upload" /></td>
</tr>
</table>
</form>
insert-proses
<?php session_start();
include "config.php";
if(ISSET($_SESSION['superadmin'])) {
if($_POST['upload']) {
$nokom = $_POST['nokom'];
$nip = $_POST['nip'];
$nama = $_POST['nama'];
// Proses upload file
$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
$file_name = $_FILES['file']['name']; // Nama File
$file_ext = strtolower(end(explode('.', $file_name))); // Merubah nama file
$file_size = $_FILES['file']['size']; // Size File
$file_tmp = $_FILES['file']['tmp_name']; // Temp File
// Proses pengecekan upload file
if(in_array($file_ext, $allowed_ext) === true) {
if($file_size < 5220350) // Max upload file 5 MB / 1MB = 1044070 {
$lokasi = 'files/'.$nama.'.'.$file_ext;
move_uploaded_file($file_tmp, $lokasi);
$sql = "INSERT INTO pegawai (nokom,nip,nama,fileext,filegambar)
VALUES('$nokom','$nip','$nama','$file_ext','$lokasi')";
if (mysqli_query($conn, $sql)) {
echo "<script>alert('Insert data berhasil! Klik ok untuk melanjutkan');location.replace('pegawai-list.php')</script>";
}
else {
echo "Error updating record: " . mysqli_error($conn);
}
} else
{
echo '<div class="error">ERROR: Besar ukuran file (file size) maksimal 1 Mb!</div>';
}
} else
{
echo '<div class="error">ERROR: Ekstensi file tidak di izinkan!</div>';
}
}
} else
{
echo '<script>alert("Anda bukan Superadmin! Silahkan login menjadi Superadminterlebih dahulu");location.replace("index.php")</script>';
}
?>
Any help will be so thankful. Thanks
Jump over the complete upload part if there is no file uploaded, or better, run the upload part in your script only IF a file is uploaded:
if(isset($_FILES['file']) && is_array($_FILES['file'])) {
// your code to upload and check the file
}
<form method="POST" action="crud.php" enctype="multipart/form-data" >
<div style="background-color:#252323; color:#FFFFFF; "><b>Image Gallery</b></div>
<table>
<tr>
<td valign="top">
<label for="description">Big Image</label>
</td>
<td valign="top" style="text-align:right">
<input type="file" name="image" /></br>
</td>
</tr>
<tr>
<td valign="top">
<label for="description">Thumbnail Image</label>
</td>
<td valign="top" style="text-align:right">
<input type="file" name="image" /></br>
</td>
</tr>
</table>
<input id="button" type="submit" value="Add" name="imagegalleryadd"/>
</form>
This is my form it has two upload images with one button i want to upload two images at the same time and put them in two separate column in the datase because i will use these two images in a slider one for big image and other for thumbnail i tried upload only one images and it was good but how do you upload two images at the same time this is my code for adding images. This code only uploads one images how can it be changed to upload two?
function AddImageGallery(){
global $dbh;
if(is_uploaded_file($_FILES["image"]['tmp_name'])){
$folder = "images/imagegallery/";
$file = basename( $_FILES['image']['name']);
$full_path = $folder.$file;
if(move_uploaded_file($_FILES['image']['tmp_name'], $full_path)) {
echo "succesful upload, we have an image!";
$stmt = $dbh->prepare("INSERT INTO imagegallery (imggall_imageurl) VALUES (?)");
$stmt->bindValue(1,$full_path,PDO::PARAM_STR);
if($stmt->execute()){
echo "Image Recorded";
}else{
echo "Image was not Recorded";
}
} else {
echo "upload received! but process failed";
}
}else{
echo "upload failure ! Nothing was uploaded";
}
}
database will look like this
$sql ="CREATE TABLE IF NOT EXISTS $imagegallery (
imggall_id int(40) NOT NULL AUTO_INCREMENT PRIMARY KEY,
imggall_imageurl VARCHAR(1000) NOT NULL,
imggall_thumnailurl VARCHAR(1000) NOT NULL);" ;
$dbh->exec($sql);
UPDATE
new code i tried
function AddImageGallery(){
global $dbh;
if(is_uploaded_file($_FILES["bigimage"]['tmp_name'] && $_FILES["thumbnailimage"]['tmp_name'] )){
$folder = "images/imagegallery/";
$filebi = basename( $_FILES['bigimage']['name']);
$fileti = basename( $_FILES['thumbnailimage']['name']);
$fileti = basename( $_FILES['thumbnailimage']['name']);
$fullbi_path = $folder.$filebi;
$fullti_path = $folder.$fileti;
if(move_uploaded_file($_FILES['bigimage']['tmp_name'], $fullbi_path && $_FILES['thumbnailimage']['tmp_name'], $fullti_path )) {
echo "succesful upload, we have an image!";
$stmt = $dbh->prepare("INSERT INTO imagegallery (imggall_imageurl,imggall_thumnailurl) VALUES (?,?)");
$stmt->bindValue(1,$fullbi_path,PDO::PARAM_STR);
$stmt->bindValue(2,$fullti_path,PDO::PARAM_STR);
if($stmt->execute()){
header("Location: dashboard.php");
exit;
echo "Image Recorded";
}else{
echo "Image was not Recorded";
}
} else {
echo "upload received! but process failed";
}
}else{
echo "upload failure ! Nothing was uploaded";
}
}
<form method="POST" action="crud.php" enctype="multipart/form-data" >
<div style="background-color:#252323; color:#FFFFFF; "><b>Image Gallery</b></div>
<table>
<tr>
<td valign="top">
<label for="description">Big Image</label>
</td>
<td valign="top" style="text-align:right">
<input type="file" name="bigimage" /></br>
</td>
</tr>
<tr>
<td valign="top">
<label for="description">Thumbnail Image</label>
</td>
<td valign="top" style="text-align:right">
<input type="file" name="thumbnailimage" /></br>
</td>
</tr>
</table>
<input id="button" type="submit" value="Add" name="imagegalleryadd"/>
</form>
Quick Google:
http://php.net/manual/en/features.file-upload.multiple.php#53240
Shows a clear and simple method for multiple image uploads.
Quote:
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;
}
if ($_FILES['upload']) {
$file_ary = reArrayFiles($_FILES['ufile']);
foreach ($file_ary as $file) {
print 'File Name: ' . $file['name'];
print 'File Type: ' . $file['type'];
print 'File Size: ' . $file['size'];
}
}
For you:
<form method="POST" action="crud.php" enctype="multipart/form-data" >
<input type="file" name="image[]" />
<input type="file" name="image[]" />
<input id="button" type="submit" value="Add" name="imagegalleryadd"/>
</form>
Then using the earlier function linked
if ($_FILES['upload']) {
$file_ary = reArrayFiles($_FILES['ufile']);
$main = $file_ary[0]['name'];
$thumb = $file_ary[1]['name'];
// Upload to DB
}
<form method="POST" action="crud.php" enctype="multipart/form-data" >
<div style="background-color:#252323; color:#FFFFFF; "><b>Image Gallery</b></div>
<table>
<tr>
<td valign="top">
<label for="description">Big Image </label>
</td>
<td valign="top">
<input type="file" name="bigimage" /></br>
</td>
</tr>
<tr>
<td valign="top">
<label for="description">Thumbnail Image </label>
</td>
<td valign="top">
<input type="file" name="thumbnailimage" /></br>
</td>
</tr>
</table>
<input id="button" type="submit" value="Add" name="imagegalleryadd"/>
</form>
function AddImageGallery(){
global $dbh;
if(is_uploaded_file($_FILES["bigimage"]['tmp_name'] )){
$folder = "images/imagegallery/";
$filebi = basename( $_FILES['bigimage']['name']);
$fileti = basename( $_FILES['thumbnailimage']['name']);
$fileti = basename( $_FILES['thumbnailimage']['name']);
$fullbi_path = $folder.$filebi;
$fullti_path = $folder.$fileti;
if(move_uploaded_file($_FILES['bigimage']['tmp_name'], $fullbi_path)) {
if(move_uploaded_file($_FILES['thumbnailimage']['tmp_name'], $fullti_path )) {
echo "succesful upload, we have an image!";
$stmt = $dbh->prepare("INSERT INTO imagegallery (imggall_imageurl,imggall_thumnailurl) VALUES (?,?)");
$stmt->bindValue(1,$fullbi_path,PDO::PARAM_STR);
$stmt->bindValue(2,$fullti_path,PDO::PARAM_STR);
if($stmt->execute()){
header("Location: dashboard.php");
exit;
echo "Image Recorded";
}else{
echo "Image was not Recorded";
}
}
} else {
echo "upload received! but process failed";
}
}else{
echo "upload failure ! Nothing was uploaded";
}
}
anyone can try this out i was able to get what
If one is having difficulties with sending more than 1 image to .php it is crucial to put [] in the end of your name of input.
Thought I should mention that sine I spend last few hours wondering why PHP is not detecting more than 1 file.