2014-11-06 23 views
0

首先,我在这个论坛上发现了一些关于此问题的主题,但似乎无法在我的页面上找到它......所以这就是为什么我问。如何从mysql检索html制动器

我正在尝试为网页制作图片上传脚本,允许某人上传带有描述信息的网页。到目前为止,我得到了上传好,它被存储到数据库并显示,但我遇到的问题是,从文本从textarea不会显示刹车时,从mysql的数据retreiving ...

怎么可以我解决这个问题?

这是upload.php的

<?php 
 
// Start a session for error reporting 
 
session_start(); 
 
    
 
// Call our connection file 
 
require("includes/conn.php"); 
 
    
 
// Check to see if the type of file uploaded is a valid image type 
 
function is_valid_type($file) 
 
{ 
 
    // This is an array that holds all the valid image MIME types 
 
    $valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif"); 
 
    
 
    if (in_array($file['type'], $valid_types)) 
 
     return 1; 
 
    return 0; 
 
} 
 
    
 
// Just a short function that prints out the contents of an array in a manner that's easy to read 
 
// I used this function during debugging but it serves no purpose at run time for this example 
 
function showContents($array) 
 
{ 
 
    echo "<pre>"; 
 
    print_r($array); 
 
    echo "</pre>"; 
 
} 
 
    
 
// Set some constants 
 
    
 
// This variable is the path to the image folder where all the images are going to be stored 
 
// Note that there is a trailing forward slash 
 
$TARGET_PATH = "content/uploads/"; 
 
    
 
// Get our POSTed variables 
 
$fname = $_POST['fname']; 
 
$lname = $_POST['lname']; 
 
$image = $_FILES['image']; 
 
    
 
// Sanitize our inputs 
 
$fname = mysql_real_escape_string($fname); 
 
$lname = mysql_real_escape_string($lname); 
 
$image['name'] = mysql_real_escape_string($image['name']); 
 
    
 
// Build our target path full string. This is where the file will be moved do 
 
// i.e. images/picture.jpg 
 
$TARGET_PATH .= $image['name']; 
 

 
// Make sure all the fields from the form have inputs 
 
if ($fname == "" || $lname == "" || $image['name'] == "") 
 
{ 
 
    $_SESSION['error'] = "All fields are required"; 
 
    header("Location: index.php"); 
 
    exit; 
 
} 
 

 
// Check to make sure that our file is actually an image 
 
// You check the file type instead of the extension because the extension can easily be faked 
 
if (!is_valid_type($image)) 
 
{ 
 
    $_SESSION['error'] = "You must upload a jpeg, gif, or bmp"; 
 
    header("Location: index.php"); 
 
    exit; 
 
} 
 

 
// Here we check to see if a file with that name already exists 
 
// You could get past filename problems by appending a timestamp to the filename and then continuing 
 
if (file_exists($TARGET_PATH)) 
 
{ 
 
    $_SESSION['error'] = "A file with that name already exists"; 
 
    header("Location: index.php"); 
 
    exit; 
 
} 
 
    
 
// Lets attempt to move the file from its temporary directory to its new home 
 
if (move_uploaded_file($image['tmp_name'], $TARGET_PATH)) 
 
{ 
 
    // NOTE: This is where a lot of people make mistakes. 
 
    // We are *not* putting the image into the database; we are putting a reference to the file's location on the server 
 
    $sql = "insert into people (fname, lname, filename) values ('$fname', '$lname', '" . $image['name'] . "')"; 
 
    $result = mysql_query($sql) or die ("Could not insert data into DB: " . mysql_error()); 
 
    header("Location: images.php"); 
 
    exit; 
 
} 
 
else 
 
{ 
 
    // A common cause of file moving failures is because of bad permissions on the directory attempting to be written to 
 
    // Make sure you chmod the directory to be writeable 
 
    $_SESSION['error'] = "Could not upload file. Check read/write persmissions on the directory"; 
 
    header("Location: index.php"); 
 
    exit; 
 
} 
 
?>

,这是一个需要时进入textarea的显示与详细描述和刹车的图像的PHP:

<?php 
 
// Get our database connector 
 
require("includes/conn.php"); 
 
?> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
 
    
 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
    <head> 
 
     <title>Dream in code tutorial - List of Images</title> 
 
    </head> 
 
    
 
    <body> 
 
     
 
     <div> 
 
    
 
      <?php 
 
       // Grab the data from our people table 
 

 
       $sql = "select * from people"; 
 
       $result = mysql_query($sql) or die ("Could not access DB: " . mysql_error()); 
 
    
 
       while ($row = mysql_fetch_assoc($result)) 
 
       { 
 
        echo "<div class=\"picture\">"; 
 
        echo "<p>"; 
 
    
 
        // Note that we are building our src string using the filename from the database 
 
        echo "<img src=\"content/uploads/" . $row['filename'] . "\" alt=\"\" /><br />"; 
 
        echo $row['fname'] . " " . "<br />" . "<br />" . $row['lname'] . "<br />"; 
 
        echo "</p>"; 
 
        echo "</div>"; 
 
\t \t \t \t } 
 
    
 
    
 
      ?> 
 
     
 
     </div> 
 
    </body> 
 
</html>

和certenty,这是一个让你输入的详细描述,并上传的图片中的index.php:

<?php 
 

 
// Start a session for displaying any form errors 
 

 
session_start(); 
 
?> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
 
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
 

 
<html xmlns="http://www.w3.org/1999/xhtml"> 
 
    <head> 
 
     <title>Dream in code tutorial</title> 
 

 
     <style type="text/css"> 
 
      label 
 
      { 
 
       float: left; 
 
       text-align: right; 
 
       margin-right: 10px; 
 
       width: 100px; 
 
       color: black; 
 
      } 
 

 
      #submit 
 
      { 
 
       float: left; 
 
       margin-top: 5px; 
 
       position: relative; 
 
       left: 110px; 
 
      } 
 
      #error 
 
      { 
 
       color: red; 
 
       font-weight: bold; 
 
       font-size: 16pt; 
 
      } 
 

 
     </style> 
 
    </head> 
 

 
    <body> 
 

 
     <div> 
 

 
       <?php 
 
       if (isset($_SESSION['error'])) 
 
       { 
 
        echo "<span id=\"error\"><p>" . $_SESSION['error'] . "</p></span>"; 
 
        unset($_SESSION['error']); 
 
       } 
 

 
       ?> 
 

 
       <form action="upload.php" method="post" enctype="multipart/form-data"> 
 
       <p> 
 
        <label>Merk</label> 
 
        <input type="text" name="fname" size="50"/><br /> 
 

 
        <label>beschrijving</label> 
 
        <textarea name="lname" style="width:250px;height:150px;"></textarea><br /> 
 
        
 
        <label>Upload afbeelding</label> 
 
        <input type="file" name="image" /><br /> 
 
        
 
        <input type="hidden" name="MAX_FILE_SIZE" value="5000000" /> 
 
        <input type="submit" id="submit" value="Upload" /> 
 
       </p> 
 
       </form> 
 
     </div> 
 
    </body> 
 
</html>

我试着像使用和nl2br但是没有什么几件事情似乎工作(可能是因为我错误地使用它...)

回答

0

我认为这是一个非常简单的解决方案。第一个变化是:

$lname = mysql_real_escape_string($lname); 

要这样:

$lname = mysql_real_escape_string(nl2br($lname)); 

这将采取任何新行字符,并把它们变成<BR>字符,然后逃脱它为MySQL插入。

您还必须将文本字段更改为textarea。因此,改变这种:

<input type="text" name="lname" size="50"/> 

要这样:

<textarea name="lname" cols="50" rows="3"></textarea> 

希望的作品!如果您有任何后续问题,请告知我。

+0

完美地工作,所以这一定是为什么我没有得到它的工作,我认为它需要改变在images.php因为这是我的输出,从来没有想过我上传错了...谢谢很多! – FGOD 2014-11-06 08:04:40

+0

我确实有一些跟进问题,但这些与制动器的东西无关:所以我想我最好提出一个新的问题;)因为我仍然需要在我的布局中实现这一点,出于某种原因,它拧紧了布局我的页面 – FGOD 2014-11-06 08:06:03

+0

没关系,刚发现问题,忘记上传css对布局没有多大帮助:p – FGOD 2014-11-06 08:13:15