Reputation: 1
I'm trying to save an image in folder and save the path in my database, but I can't do that.
When I insert only name and phone all is ok, but when I add the input type file, it saves my name and phone number but not the image path and not upload the image in folder.
Here is the code:
PED.PHP
<form class="form-horizontal form-label-left input_mask" method="post" id="add" name="add" enctype="multipart/form-data">
<div class="form-group"" >
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">CLIENT</label>
<div class="col-md-9 col-sm-9 col-xs-12" style="float: left; width:70px;">
<input type="text" style="width:200px; float:left;"name="client" class="form-control" placeholder="" >
</div>
</div>
<div class="form-group" style="border: 1px ; height:45px;" >
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">PHONE:
</label>
<div class="col-md-9 col-sm-9 col-xs-12" style="float: left; width:70px;">
<input type="text" style="width:200px; float:left;"name="phone" class="form-control" placeholder="" >
</div>
</div>
<div class="form-group" style="border: 1px ; height:45px;" >
<label class="control-label col-md-3 col-sm-3 col-xs-12" for="first-name">PICTURE:
</label>
<div class="col-md-9 col-sm-9 col-xs-12" style="float: left; width:70px;">
<input type="file" name="uploadImage" id="uploadImage">
</div>
</div>
<div class="ln_solid"></div>
<div class="form-group">
<button id="save_data" type="submit" class="btn btn-success">Guardar</button>
</div>
</div>
</form
and here I receive all data: ADDPED.PHP
<?php
date_default_timezone_set('America/Mexico_City');
session_start();
if (empty($_POST['name'])) {
$errors[] = "Selecciona una tienda";
} else if (empty($_POST['cliente'])){
$errors[] = "Ingrese cliente";
} else if (
!empty($_POST['name']) &&
!empty($_POST['cliente'])
){
include "../config/config.php";
$client = $_POST["cliente"];
$phone = $_POST["telefono"];
("images/fotos/" . $_FILES["uploadImage"]["name"]);
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"images/fotos/" . $_FILES["file"]["name"]);
}
$sql="insert into pedido (client,phone,image) value ('$client','$phone','".$_FILES['uploadImage']['name']."')";
$query_new_insert = mysqli_query($con,$sql);
if ($query_new_insert){
$messages[] = "Tu ticket ha sido ingresado satisfactoriamente.";
} else{
$errors []= "Lo siento algo ha salido mal intenta nuevamente.".mysqli_error($con);
}
} else {
$errors []= "Error desconocido.";
}
if (isset($errors)){
?>
<div class="alert alert-danger" role="alert">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>Error!</strong>
<?php
foreach ($errors as $error) {
echo $error;
}
?>
</div>
<?php
}
if (isset($messages)){
?>
<div class="alert alert-success" role="alert">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong>¡Bien hecho!</strong>
<?php
foreach ($messages as $message) {
echo $message;
}
?>
</div>
<?php
}
?>
Upvotes: 0
Views: 897
Reputation: 361
Though the question is little old, I will answer it with some simple example to help those who come later looking for an answer. In my web app, I have the following information:
The following is the code for html form driver-form.html to collect driver information with driver picture:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Driver Entry Page</title>
</head>
<body>
<header><h3>Please enter driver information:</h3></header>
<form action="http://localhost/car-rental/insert_driver.php" target="_blank" method="post" enctype="multipart/form-data">
Name:<br>
<input type="text" name="name"><br>
<br>
Age:<br>
<input type="text" name="age"><br>
<br>
Picture:<br>
<input type='file' name='imagefile'><br>
<br>
<input type="submit" value="Add driver">
</form>
</body>
</html>
And this is the php code insert_driver.php to insert the record in the database and move the picture file to the uploads folder:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Insert Car Data</title>
</head>
<body>
<?php
//Database connection parameters
$host = 'localhost';
$dbname = 'carsdatabase';
$username = 'root';
$password = '';
//Connecting to the database
try{
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
}
?>
<?php
//Storing values int o variables
$name = $_POST["name"];
$age = $_POST["age"];
// File name
$filename = $_FILES["imagefile"]["name"];
// Location
$target_file = './uploads/'.$filename;
// file extension
$file_extension = pathinfo($target_file, PATHINFO_EXTENSION);
$file_extension = strtolower($file_extension);
// Valid image extension
$valid_extension = array("png","jpeg","jpg");
if(in_array($file_extension, $valid_extension)) {
echo "Filename is $filename <br>";
echo "Target file is $target_file <br>";
if(move_uploaded_file($_FILES['imagefile']['tmp_name'],$target_file)
) {
//Preparing insert array
$task = array('name' => $name,
'age' => $age,
'filename' => $filename,
'fileimage' => $target_file
);
// Execute query
$sql = 'INSERT INTO DRIVER (
NAME,
AGE,
FILENAME,
FILEIMAGE
)
VALUES (
:name,
:age,
:filename,
:fileimage
);';
$q = $pdo->prepare($sql);
$q->execute($task);
}
}
?>
</body>
</html>
And this is the code view-drivers.php that lists the added drivers along with their images from the database:
<?php
$host = 'localhost';
$dbname = 'carsdatabase';
$username = 'root';
$password = '';
try {
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sql = 'SELECT *
FROM DRIVER
ORDER BY DRIVER_ID';
$q = $pdo->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
} catch (PDOException $pe) {
die("Could not connect to the database $dbname :" . $pe->getMessage());
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Drivers Information Page</title>
</head>
<body>
<div id="container">
<h1>Tasks List</h1>
<table border="1">
<thead>
<tr>
<th>Driver ID</th>
<th>Name</th>
<th>Age</th>
<th>Picture</th>
</tr>
</thead>
<tbody>
<?php while ($row = $q->fetch()): ?>
<tr>
<td><?php echo htmlspecialchars($row['DRIVER_ID']) ?></td>
<td><?php echo htmlspecialchars($row['NAME']); ?></td>
<td><?php echo htmlspecialchars($row['AGE']); ?></td>
<td><img src="<?=$row['FILEIMAGE']?>" title="<?=$row['FILENAME']?>" width='100' height='100'></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</body>
</div>
</html>
Finally, below is a screenshot from phpMyAdmin showing the structure of the DRIVER table:
Upvotes: 1
Reputation: 203
The maximum default file size is 2MB for file upload. but you can change it in the configurations and also check the data type you used in the DB and the maximum limit of it. It's better to upload the images in a folder and insert the file name to the DB. it will also increase the performance of your system. check this example for upload image to Database.
Upvotes: -1