需要连续检索标题,描述和上传的图像,并使用php显示它们

时间:2015-10-09 18:21:46

标签: php html mysql forms

我有一个名为contact-form-submission.php的文件,其中我将表单数据插入到数据库中。在contact-form-submission.php中,我插入名称,消息并将徽标上传到uploads文件夹中。

以下是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();
?>

这是表单页面编码 - 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>

当提交Index.php中的表单时,它将调用contact-form-submission.php,它将插入标题和消息,并将图像上传到'uploads'文件夹。

在Lists.php中,我分别显示标题,消息和上传的图像。但问题是我需要在一行中显示,标题,消息和图像。

这是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();
?>

在Lists.php中,我需要在一行中显示contact_name,contact_message和上传的徽标。但现在,contact_name和contact_message将单独显示,徽标将单独显示。

非常感谢任何帮助。提前谢谢。

1 个答案:

答案 0 :(得分:1)

使用表格显示行:
以下代码将帮助您 用while循环替换它。

    echo "<table>";
    while($row = $result->fetch_assoc()) {
              echo "<tr><td>id:</td><td>" . $row["id"]. "</td> <td> Name: </td><td>" . $row["name"]. "</td><td> Message </td><td>" . $row["message"]. "</td><td>Logo:</td><td>". "<img src='uploads". $row["logo"] ." />". "</td></tr>";
        }
echo "</table>";

查询应该是这样的

INSERT INTO posts (name, message,logo) VALUES ('$contact_name', '$contact_message','$target_file')