我使用XAMPP和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脚本显示信息的格式。 (目前刷新页面会自动创建一个记录,其中包含我需要修复的表单框标题)
如果您需要我澄清,请提出问题,如果您能帮助我,我们将非常感激。我确定答案非常简单,我花了一些时间来解释所有这些。
这是我正在使用的数据库:
这是div中房子的格式,我需要PHP每次上传房屋时自动创建:
答案 0 :(得分: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之类的网址传递变量,或者您可以从引荐网页面创建表单并从隐藏字段传递变量。
你可以制作两个按钮。一个只显示房屋记录,另一个将插入,然后显示新插入的记录。 因此,第一个按钮将链接到http://mypage/inserthouse.php?doinsert=true 以及秒http://mypage/inserthouse.php?doinsert=false或http://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框架它非常好用且易于使用。
我希望我帮助
答案 1 :(得分:0)
这是一种轻松快捷的方式来实现您的目标。
我认为你的问题不是理解PHP,数据库和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);