dqluw20882 2015-10-09 18:21
浏览 29
已采纳

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

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.

  • 写回答

1条回答 默认 最新

  • douse8732 2015-10-09 18:41
    关注

    Use table to display in the rows:
    below code will help you
    Replace it with your while loop.

        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>";
    

    query should be like this

    INSERT INTO posts (name, message,logo) VALUES ('$contact_name', '$contact_message','$target_file')
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题