Sab se phele aap ne aik database bnana hai aur aik table . jis ka name koi v rakhlen aap. fr hum 2 files bnaege php ki aik mein html ka code likhe ge aur aik mein php ki script.
Hum is ko 3 steps mein follow krenge .
1. Create DB And Table
2 Create File Upload Page in page
3 Create Php Upload File Script
1 Create DB And Table
Hum ne ismein aik database bnaya jiska name mlk hai. aur fr aik table bnaya jis ka cads hai .usmein jo entry hai wo aap niche screen short mein dekh skte ho.
Abb hamara table and database bn chuka hai . abb hum second step follow krenge. see
2Create File Upload Page in php
<?php
//session_start();
include 'includes/connection.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Upload Ads Post</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
</head>
<body>
<div class="row">
<div class="menus">
<?php include 'includes/nav.php';?>
</div>
</div>
<div class="container">
<div class="row">
<div class="card" id="cd">
<div class="card-body">
<div class="card-title">
<h3 class="text-center">POST YOUR AD</h3>
</div>
<hr>
<form action="uploadpost.php" method="post" class="form-group" enctype="multipart/form-data">
<div class="form-group">
<label>Ad Title*</label>
<input type="text" name="title" class="form-control" placeholder="enter title">
<span style="font-weight: bold;">Mention the key features of your item (e.g. brand, model, age, type)
0 / 70</span>
</div>
<div class="form-group">
<label>Select Category*</label>
<select class="form-control" name="c_id">
<option value="0">Add as main</option>
<?php
include 'include/connection.php';
$query="select * from adscat";
$res=mysqli_query($conn,$query);
while($data=mysqli_fetch_array($res))
{
?>
<option value="<?php echo $data['id']; ?>">
<?php echo $data['name']; ?></option>
<?php
}
?>
</select>
</div>
<hr>
<div class="form-group">
<label>Ad Description*</label>
<textarea class="form-control" cols="15" rows="10" name="desc"></textarea>
<span style="font-weight: bold;">Include condition, features and reason for selling
0 / 4096</span>
</div>
<hr>
<div class="form-group">
<input type="file" name="fileUpload">
</div>
<div class="form-group">
<label>Set Price*</label>
<input type="number" name="price" class="form-control" placeholder="enter price">
</div>
<hr>
<div class="form-group">
<label> Add State*</label>
<input type="text" name="state" class="form-control" placeholder="enter state">
</div>
<hr>
<label> Add City*</label>
<div class="form-group">
<input type="text" name="city" class="form-control" placeholder="enter city">
</div>
<hr>
<label> Add Pin-Code*</label>
<div class="form-group">
<input type="number" name="pincode" class="form-control" placeholder="enter pincode">
</div>
<hr>
<div class="form-group">
<label> Add Name*</label>
<input type="text" name="name" class="form-control" placeholder="enter name">
</div>
<hr>
<div class="form-group">
<label> Add Phone*</label>
<input type="number" name="phone" class="form-control" placeholder="enter phone">
</div>
<hr>
<div class="form-group">
<button type="submit" name="submit" class="btn btn-primary">POST NOW</button>
</div>
</form>
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"></script>
</body>
</html>
3 Create Php Upload File Script
<?php
include 'includes/connection.php';
if(isset($_POST['submit'])){
// File upload configuration
$title=$_POST["title"];
$cid=$_POST["c_id"];
$desc=$_POST["desc"];
$price=$_POST["price"];
$state=$_POST["state"];
$uname=$_POST["name"];
$phone=$_POST["phone"];
$uploadsDir = "uploads/";
$allowedFileType = array('jpg','png','jpeg');
// Velidate if files exist
if (!empty(array_filter($_FILES['fileUpload']['name']))) {
// Loop through file items
foreach($_FILES['fileUpload']['name'] as $id=>$val){
// Get files upload path
$fileName = $_FILES['fileUpload']['name'][$id];
$tempLocation = $_FILES['fileUpload']['tmp_name'][$id];
$targetFilePath = $uploadsDir . $fileName;
$fileType = strtolower(pathinfo($targetFilePath, PATHINFO_EXTENSION));
$uploadDate = date('Y-m-d H:i:s');
$uploadOk = 1;
if(in_array($fileType, $allowedFileType)){
if(move_uploaded_file($tempLocation, $targetFilePath)){
$sqlVal = "('".$fileName."', '".$uploadDate."')";
} else {
$response = array(
"status" => "alert-danger",
"message" => "File coud not be uploaded."
);
}
} else {
$response = array(
"status" => "alert-danger",
"message" => "Only .jpg, .jpeg and .png file formats allowed."
);
}
// Add into MySQL database
if(!empty($sqlVal)) {
// $insert = $conn->query("INSERT INTO user (images, date_time) VALUES $sqlVal");
$qry="INSERT INTO cads(file_name,title,c_id,description,price,state,username,phone,date_time)VALUES('$fileName','$title','$cid','$desc','$price','$state','$uname','$phone','$uploadDate')";
$result=mysqli_query($conn,$qry);
if($result) {
$response = array(
"status" => "alert-success",
"message" => "Files successfully uploaded."
);
} else {
$response = array(
"status" => "alert-danger",
"message" => "Files coudn't be uploaded due to database error."
);
}
}
}
} else {
// Error
$response = array(
"status" => "alert-danger",
"message" => "Please select a file to upload."
);
}
}
?>
Finally data submit aap niche screenshort mein dekh skte ho.
0 Comments
please enter your comment in message box