I have a file named contact-form-submission.php in which I insert the form data into Database. In contact-form-submission.php I insert the Name, Message and upload the logo into uploads folder.
Here is the code of contact-form-submission.php
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$connection = mysql_connect("localhost", "root", ""); // Establishing Connection with Server
$db = mysql_select_db("posts", $connection); // Selecting Database from Server
if(isset($_POST['submit'])){ // Fetching variables of the form which travels in URL
$contact_name = $_POST['contact_name'];
$contact_message = $_POST['contact_message'];
if($contact_name !='' && $contact_message !='' && move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)){
$query = mysql_query("INSERT INTO posts (name, message) VALUES ('$contact_name', '$contact_message')");
echo "<br/><h2>Data Inserted successfully...!! Redirecting to Offer Add page in 5 seconds</h2>";
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
}
else{
echo "<h2>Insertion Failed <br/> Offer Add Fields are Blank....!! Redirecting to Offer Add page in 5 seconds</h2>";
echo "Sorry, there was an error uploading your file.";
}
}
//$conn->close();
?>
This is the form page coding - index.php
<div class="col-sm-12">
<?php
// check for a successful form post
if (isset($_GET['s']))
echo "<div class=\"alert alert-success\">" . $_GET['s'] . "</div>";
// check for a form error
elseif (isset($_GET['e']))
echo "<div class=\"alert alert-danger\">" . $_GET['e'] . "</div>";
?>
<form role="form" method="POST" action="contact-form-submission.php" enctype="multipart/form-data">
<div class="row">
<div class="form-group col-lg-4">
<label for="input1">Name</label>
<input type="text" name="contact_name" class="form-control" id="input1">
</div>
<div class="clearfix"></div>
<div class="form-group col-lg-12">
<label for="input4">Message</label>
<textarea name="contact_message" class="form-control" rows="6" id="input4"></textarea>
</div>
<div class="form-group col-lg-4">
<label for="input1">Logo</label>
<input name="fileToUpload" type="file" class="btn btn-lg btn-primary" value="Upload" id="fileToUpload">
</div>
<div class="form-group col-lg-12">
<input type="hidden" name="save" value="contact">
<button name="submit" type="submit" class="btn btn-primary">Submit</button>
</div>
</div>
</form>
</div>
When the form in Index.php is submitted it will call the contact-form-submission.php and it will insert title and message and also upload the image to 'uploads' folder.
In Lists.php I display the title, message and the uploaded image separately. But the issue is I need to display, title, message and image in a single row.
Here is the code of Lists.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "posts";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, message, logo FROM posts";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//echo "id: " . $row["id"]. " - Name: " . $row["name"]. "- Message " . $row["message"]. "<br>";
echo "id: " . $row["id"]. " - Name: " . $row["name"]. "- Message " . $row["message"]. "Logo: ". "<img src='uploads". $row["logo"] ." />". "<br>";
}
} else {
echo "0 results";
}
//scan "uploads" folder and display them accordingly
$folder = "uploads";
$results = scandir('uploads');
foreach ($results as $result) {
if ($result === '.' or $result === '..') continue;
if (is_file($folder . '/' . $result)) {
echo '
<div class="col-md-3">
<div class="thumbnail">
<img src="'.$folder . '/' . $result.'" alt="...">
<div class="caption">
<p><a href="remove.php?name='.$result.'" class="btn btn-danger btn-xs" role="button">Remove</a></p>
</div>
</div>
</div>';
}
}
$conn->close();
?>
In Lists.php I need to show the contact_name, contact_message and uploaded logo in a single row. But now the contact_name and contact_message is displayed separately and the logos are shown separately.
Any help is greatly appreciated. Thanks in advance.