2015-05-13 34 views
0

我使用XAMPP和PHP作为基本网站。PHP超级基本内容管理建议请

我已经完成了大部分内容管理部分。

我有一个基本的形式,您可以上传图片并添加所有必要的详细信息。我需要帮助保持格式一致,因为目前有三间房屋,价格和细节以HTML编码。

如何以类似的格式回显细节?

安全性不是问题,因为没有使用任何敏感信息,也不会公开使用。我只需要知道如何以类似的方式来设置细节和图片的格式?

我附上了照片,以便您可以更好地了解我需要的帮助。下面是代码:

<?php 
    $servername="localhost"; 
    $username="root"; 
    $password=""; 
    $dbname="content_management"; 
    $tbl_name="houses"; 
    $housepic ="housepic"; 
    $houseprice ="houseprice"; 
    $housetype ="housetype"; 
    $houseloc = "houseloc"; 
    $housedesc = "housedesc"; 


$conn = mysqli_connect($servername, $username, $password, $dbname); 
// Check connection 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 

    $sql="INSERT INTO $tbl_name (picture, price, type, location, description) VALUES ('$housepic','$houseprice','$housetype','$houseloc','$housedesc')"; 

    if (mysqli_query($conn, $sql)) { 
    echo ""; 
} else { 
    echo "Error: " . $sql . "<br>" . mysqli_error($conn); 
} 
    mysqli_close($conn); 

?> 
<?php 
    $host="localhost"; 
    $username="root"; 
    $password=""; 
    $db_name="content_management"; 
    $tbl_name="houses"; 
    mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
    mysql_select_db("$db_name")or die("cannot select DB"); 
    $sql = "SELECT * FROM $tbl_name"; 
    $result=mysql_query($sql); 
    while ($row = mysql_fetch_assoc($result)) { 
     echo "<img src='" . $row['picture'] . "'>"; 
      echo $row['price']; 
      echo $row['type']; 
     echo $row['location']; 
     echo $row['description']; 
    } 

?> 

     <div class="housepost"> 
      <img src="img/houses/house_01.jpg"> 
      <h2>£350,000</h2> 
      <p>2 bedroom detached house for sale</p> 
      <p>Deanfield Avenue, Henley-on-Thames</p> 
      <p>Set in the heart of Henley-on-Thames and just a short walk from Henley train station is this rarely available and spacious three bedroom apartment. Offered to the market with no onward chain the property benefits from off road parking.</p> 
     </div> 
     <div class="housepost"> 
      <img src="img/houses/house_02.jpg"> 
      <h2>£475,000</h2> 
      <p>2 bedroom detached bungalow for sale</p> 
      <p>Fair Mile, Henley-on-Thames</p> 
      <p>Set in the heart of the town centre in a quiet backwater this delightful single storey detached home is rarely available and well presented.</p> 
     </div> 
     <div class="housepost"> 
      <img src="img/houses/house_03.jpg"> 
      <h2>£600,000</h2> 
      <p>3 bedroom cottage for sale</p> 
      <p>Remenham Row, Henley-on-Thames</p> 
      <p>The English Courtyard Association and The Beechcroft Trust - synonymous with the very best in retirement housing since the 1980s. An extremely attractive three-bedroom cottage with landscaped riverside gardens in this much sought after location.</p> 
     </div> 
     <?php echo '<div class="housepost"> 
      <img>$row <img> 
      <h2></h2> 
      <p></p> 
      <p></p> 
      <p>$row</p> 
     </div>' ?> 
    </div> 

我不知道我用PHP做什么,我很惊讶,我做了这么远,但我有一个初学者理解。您可以在代码中看到三个硬编码的HTML div,最后一个div是我用PHP尝试失败的实验。如果你看看房子的图片,你可以看到我希望PHP脚本显示信息的格式。 (目前刷新页面自动创建一个记录与表格框标题,我需要修复以及)

请问如果你需要我澄清,如果你能帮助我,那将是非常感激。我相信答案非常简单,我花了一段时间来解释所有这些。

这是我使用的数据库的事情:

enter image description here

这是房子的一个div我需要PHP每个房子被上传时自动创建格式: enter image description here

+1

关于刷新创造了新的记录的页面:它是有重定向回到原来的页面创建一个单独的后处理程序脚本是一个好主意。这确保页面刷新不会重新发布任何内容。 – Ixalmida

+0

我不知道如何完成这个任务,我是PHP的一个极端的基本用户。我不能只写脚本。我的老师甚至没有教过我们,他只是给了我们练习来复制代码,所以我只能通过我在那些非常有限的知识中学到的知识。 –

+0

....在“the_post_script.php”中: header('Location:the_original_page.php'); – Ixalmida

回答

0

这是一种简单快捷的方式来完成您的要求。

我想你的问题是不理解PHP,DB和HTML基础知识,但是把他们放在一起 - 这样:

阅读代码中的注释仔细了解所有这些三件事情之间的所有步骤和关系。

当你得到它后,你可以按照这个链接:php_file_upload学习如何上传图片(这有点复杂,但是对你的学习来说是一个很好的测试)。

这是代码(全部在同一页!):

所有的
<!-- HTML form to add a new house --> 
<form method="post"> 
    <!-- this is only giving and existing url, notice you should change this 
     if you want to upload an actual pic --> 
    <input type="text" name="housepic"> 
    <br> 
    <input type="text" name="houseprice"> 
    <br> 
    <input type="text" name="housetype"> 
    <br> 
    <input type="text" name="houseloc"> 
    <br> 
    <input type="text" name="housedesc"> 
    <br> 
    <input type="submit" name="submit" value="Submit"> 
</form> 

<?php 
//init DB vars 
$servername="localhost"; 
$username="root"; 
$password=""; 
$dbname="content_management"; 
$tbl_name="houses"; 

//conect to db 
$conn = mysqli_connect($servername, $username, $password, $dbname); 
// Check connection 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 

/* if request method is: "post", a form was submitted. 
    get the submitted form params, and update DB */ 
if ($_SERVER["REQUEST_METHOD"] == "POST") 
{ 
    /* notice you should add here error handling, case user didn't fill 
     all params */ 
    $housepic = (isset($_POST["housepic"])) ? $_POST["housepic"] : ""; 
    $housepic = (isset($_POST["houseprice"])) ? $_POST["houseprice"] : ""; 
    $housepic = (isset($_POST["housetype"])) ? $_POST["housetype"] : ""; 
    $housepic = (isset($_POST["houseloc"])) ? $_POST["houseloc"] : ""; 
    $housepic = (isset($_POST["housedesc"])) ? $_POST["housedesc"] : ""; 

    // update DB 
    $sql="INSERT INTO $tbl_name (picture, price, type, location, description) VALUES ('$housepic','$houseprice','$housetype','$houseloc','$housedesc')"; 

    if (mysqli_query($conn, $sql)) { 
    echo ""; 
    } else { 
     echo "Error: " . $sql . "<br>" . mysqli_error($conn); 
    } 
} 

/* we'll always get to this part. 
    notice, if a form was already submitted, we've just updated our DB 
    accordingly, so the houses list is already updated */ 
while ($row = mysql_fetch_assoc($result)) { 
    /* good practice: grab your params before, 
     for a nicer and reusable code */ 
    $picture = $row['picture']; 
    $price = $row['price']; 
    $type = $row['type']; 
    $location = $row['location']; 
    $description = $row['description']; 

    /******************************************************************** 
     this is the part you are asking about. 
     notice that echoing the HTML tags content is done 
     inside the WHILE loop, 
     which of course will be always in the same format... 
    *********************************************************************/ 
    echo " 
      <div class=\"housepost\"> 
     <img src=\"{$picture}\"> 
     <h2>{$price}</h2> 
     <p>{$type}</p> 
     <p>{$location}</p> 
     <p>{$description}</p> 
    </div>"; 

} 

/* don't forget to close your DB connection */ 
mysqli_close($conn); 
+1

OP为什么要“尝试这个”?请添加一个解释,你做了什么,为什么你这样做,不仅是为了OP,而且是为了未来的访问者。 –

+0

goof idea @Jay,iv'e添加了一个解释 –

+0

你的东西只是摆脱了一切,并增加了5个连续的文本框,甚至不能上传图像,不幸的是:c –

1

首先,我不明白你通过类似的格式是什么意思。但我猜你需要做这样的事情

<?php 

    //Remove these variables and place them in a file like config.php 

    $servername="localhost"; 
    $username="root"; 
    $password=""; 
    $dbname="content_management"; 
    $tbl_name="houses"; 
    $housepic ="housepic"; 
    $houseprice ="houseprice"; 
    $housetype ="housetype"; 
    $houseloc = "houseloc"; 
    $housedesc = "housedesc"; 

//Make a function with all this block like function inserhome 
$conn = mysqli_connect($servername, $username, $password, $dbname); 
// Check connection 
if (!$conn) { 
    die("Connection failed: " . mysqli_connect_error()); 
} 

//Send a variable named $_REQUEST["doinsert"] from the referer page to check if to insert or not 

if($_REQUEST["doinsert"] == true){ 
    $conn = mysqli_connect($servername, $username, $password, $dbname); 
    // Check connection 
    if (!$conn) { 
     die("Connection failed: " . mysqli_connect_error()); 
    } 

     $sql="INSERT INTO $tbl_name (picture, price, type, location, description) VALUES ('$housepic','$houseprice','$housetype','$houseloc','$housedesc')"; 

     if (mysqli_query($conn, $sql)) { 
     echo ""; 
    } else { 
     echo "Error: " . $sql . "<br>" . mysqli_error($conn); 
    } 
     mysqli_close($conn);  
} 


//Make a function like function selecthomes 
    mysql_connect($servername, $username, $password)or die("cannot connect"); 
    mysql_select_db($db_name)or die("cannot select DB"); 

    $sql = "SELECT * FROM $tbl_name"; 

    $result=mysql_query($sql); 


/*Change made to print all tables using mysql_fetch_assoc. I would 
recommend using pdo instedof mysql_fetch_assoc functions since they are 
deprecated. check http://php.net/manual/en/book.pdo.php 
check the deprecation http://php.net/manual/en/function.mysql-fetch-assoc.php*/ 

    while ($row = mysql_fetch_assoc($result)) { ?> 
     <div class="housepost"> 
      <img src="<?php echo $row['picture'] ?>"> 
      <h2><?php echo $row['price'] ?></h2> 
      <p><?php echo $row['type'] ?></p> 
      <p><?php echo $row['location'] ?></p> 
      <p><?php echo $row['description'] ?></p> 
     </div> 
<?php  } ?> 

其次,你也说:

目前刷新页面会自动创建与 形式框标题,我需要修复以及

记录

很明显,这个脚本总是会插入一条新记录,因为每次你在php文件的开始处插入,所以不可避免地会插入一条新记录。

你需要检查的一件事是如果记录已经插入。

我添加了一些功能来演示如何检查插入记录与否。最好你应该传递一个像http://mypage/inserthouse.php?doinsert=true这样的url变量,或者你可以从引用页面创建一个表单并从隐藏字段传递该变量。

您可以制作两个按钮。一个将仅显示房屋记录,另一个将插入并随后显示新插入的记录。 所以第一个按钮将链接到http://mypage/inserthouse.php?doinsert=true 和秒http://mypage/inserthouse.php?doinsert=falsehttp://mypage/inserthouse.php

我建议你应该为每个动作的功能,并检查枯萎你需要执行插入或没有。提出问题,我应该何时插入新记录?

此外,您不需要变量名称$ tbl_name。在你的代码中并不重要,它会延长脚本。

你还需要使用一个包含文件的变量,

$servername="localhost"; 
    $username="root"; 
    $password=""; 
    $dbname="content_management"; 
    $tbl_name="houses"; 
    $housepic ="housepic"; 
    $houseprice ="houseprice"; 
    $housetype ="housetype"; 
    $houseloc = "houseloc"; 
    $housedesc = "housedesc"; 

你的代码看起来像你需要阅读一些用PHP的面向对象编程。给这个镜头,http://www.codecademy.com/courses/web-beginner-en-bH5s3/0/1

我也建议使用框架,而不是使它成为你自己。 你的技能还没有制作你自己的cms。使用MVC和面向对象的模式。

检查zend框架,它是非常好的,易于使用。

我希望我帮助

+0

你是什么意思,每次我做插入PHP文件的开始?对不起,我在PHP上太糟糕了。有了这个解决方案,它确实有一些工作。格式是正确的,因为他们周围有白色框等,但我得到的是'0'和'houseloc'等,如下所示:http://i57.tinypic.com/rr0euf.png –

+0

我的意思是,每次该文件加载,您正在插入一条记录。没有什么可以阻止它这样做。检查我的答案。 – themis

+0

我想你会得到0,因为数据库中存在流氓记录。你需要清理它。它可能被插入,因为你加载你的PHP文件和变量没有设置。尽管如此,记录插入的是空值 – themis