Page templates in PHP and MySQL - php

I have a blog written in PHP which uses MySQL as its' back-end. There is a template for blog posts stored in the database and the script pulls this content when it is needed. How can I make it so I do not need to store the template in my database and in my PHP file instead?
My PHP code is:
<?php
$dbhost="localhost";
$dbname="database";
$dbuser="username";
$dbpass="password";
$mysql=#mysql_select_db($dbname, #mysql_connect($dbhost, $dbuser, $dbpass)) or die (mysql_error());
if (($_GET['act'] == "blog") && (is_numeric($_GET['id']))) {
$temp = mysql_fetch_row(mysql_query("SELECT template FROM templates WHERE name = 'blog'"));
$sql = mysql_query("SELECT * FROM posts WHERE id = '".$_GET['id']."'");
while($r = mysql_fetch_array($sql)) {
$pab[0] = "/{subject}/";
$pab[1] = "/{date}/";
$pab[2] = "/{blog}/";
$rab[0] = "<a href='blog-".$r[id].".html'>".stripslashes($r[subject])."</a>";
$rab[1] = $r[date];
$rab[2] = stripslashes($r[blog])."<br /><br />";
eval (" ?>" . preg_replace($pab, $rab, stripslashes($temp[0])) . " <?php ");
}
}
?>
Here is the code stored in the database:
<table cellpadding='0' cellspacing='0' border='0' align='center' width='394'>
<tr>
<td><font face='Tahoma' size='4' color='#3A3A3A'><b>{subject}</b></font></td>
</tr>
<tr>
<td>{blog}</td>
</tr>
</table>
My question is; How can I make it so I do not need to store the template in my database and in my PHP file instead?

You mean like this?
<?php
$dbhost="localhost";
$dbname="database";
$dbuser="username";
$dbpass="password";
$mysql=#mysql_select_db($dbname, #mysql_connect($dbhost, $dbuser, $dbpass)) or die (mysql_error());
if (($_GET['act'] == "blog") && (is_numeric($_GET['id']))) {
$temp = "<table cellpadding='0' cellspacing='0' border='0' align='center' width='394'>
<tr>
<td><font face='Tahoma' size='4' color='#3A3A3A'><b>{subject}</b></font></td>
</tr>
<tr>
<td>{blog}</td>
</tr>
</table>";
$sql = mysql_query("SELECT * FROM posts WHERE id = '".$_GET['id']."'");
while($r = mysql_fetch_array($sql)) {
$pab[0] = "/{subject}/";
$pab[1] = "/{date}/";
$pab[2] = "/{blog}/";
$rab[0] = "<a href='blog-".$r[id].".html'>".stripslashes($r[subject])."</a>";
$rab[1] = $r[date];
$rab[2] = stripslashes($r[blog])."<br /><br />";
eval (" ?>" . preg_replace($pab, $rab, stripslashes($temp)) . " <?php ");
}
}
?>

Related

showing contents of a table using PHP in HTML

I have this code snippet:
try
{
$conn = new PDO("mysql:host=$host;dbname=judges", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
echo "Connection failed: " . $e->getMessage();
}
and
$query = "SELECT * FROM feedback";
echo '<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td> <font face="Arial">Judge Name</font> </td>
<td> <font face="Arial">PC</font> </td>
<td> <font face="Arial">TD</font> </td>
<td> <font face="Arial">EX</font> </td>
<td> <font face="Arial">ID</font> </td>
<td> <font face="Arial">comment</font> </td>
</tr>';
if ($result = $conn->query($query)) {
echo "got result";
echo $result["PC"];
//while ($row = $result->fetch_assoc()) {
while ($row = $result) {
$field1name = $row["judgeName"];
$field2name = $row["PC"];
$field3name = $row["TD"];
$field4name = $row["EX"];
$field5name = $row["ID"];
$field6name = $row["comment"];
echo '<tr>
<td>'.$field1name.'</td>
<td>'.$field2name.'</td>
<td>'.$field3name.'</td>
<td>'.$field4name.'</td>
<td>'.$field5name.'</td>
<td>'.$field6name.'</td>
</tr>';
}
}
both inside submit.php, however, while the feedback table inside judges database is not empty, I don't see any of the rows shown in the HTML page only the column name shows up in the Response tab of Network tab in Firefox Inspect tool. What do you suggest to populate the existing database into the html page even before I enter the information for the new user?
Here's my feedback table from the judges database:
Trying the following from the comments also didn't work and nothing got printed:
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
There's no point in the if statement. You have PDO::ERRMODE_EXCEPTION enabled, so if there's an error in the query it will throw an exception, not return false.
The syntax to fetch a row in PDO is $result->fetch(PDO::FETCH_ASSOC).
$result = $conn->query($query);
echo "<td><tr>got result</tr></td>";
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$field1name = $row["judgeName"];
$field2name = $row["PC"];
$field3name = $row["TD"];
$field4name = $row["EX"];
$field5name = $row["ID"];
$field6name = $row["comment"];
echo '<tr>
<td>'.$field1name.'</td>
<td>'.$field2name.'</td>
<td>'.$field3name.'</td>
<td>'.$field4name.'</td>
<td>'.$field5name.'</td>
<td>'.$field6name.'</td>
</tr>';
}
You were mixing PDO and MYSQLI_
Also you had some debug code that would have damaged your table HTML.
Also there is no real point in moving data from a perfectly good array into scalar variables just to include those values in some HTML.
if ($result = $conn->query($query)) {
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo "<tr>
<td>$row[judgeName]</td>
<td>$row[PC]</td>
<td>$row[TD]</td>
<td>$row[EX]</td>
<td>$row[ID]</td>
<td>$row[comment]</td>
</tr>";
}
}

$result & $search in same PHP file

I want to search with PHP in mysql Database, and search results to edit or delete, but until now i can do search and everything works or Edit/Delete and search dosen't work anymore..
I want to combine both in same php file, but dosen't work. Know someone how to combine ?
How can i add both in same php.. ?
Search PHP
<?php
include_once("db_W3.php");
$result = mysqli_query($mysqli, "SELECT * FROM centralhub_lagerbestand");
//load database connection
include_once("db_W3.php");
$host = "$servername";
$user = "$username";
$password = "$password";
$database_name = "$db";
$pdo = new PDO("mysql:host=$host;dbname=$database_name", $user, $password, array(
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
));
// Search from MySQL database table
$search=$_POST['search'];
$query = $pdo->prepare("select * from centralhub_lagerbestand where KundenNr LIKE '%$search%' LIMIT 0 , 50");
$query->bindValue(1, "%$search%", PDO::PARAM_STR);
$query->execute();
// Display search result
if (!$query->rowCount() == 0) {
echo "<table style=\"font-family:arial;color:#333333;\">";
echo "<tr>
<td align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;background:#FFFFFF;\">Kunden Nr.:</td>
<td align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;background:#FFFFFF;\">Regal:</td>
<td align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;background:#FFFFFF;\">Regal Ebene:</td>
<td align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;background:#FFFFFF;\"> Edit </td>
</tr>";
while ($results = $query->fetch()) {
echo "<tr> <td width='500px' align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;\">";
echo $results['KundenNr'];
echo "</td><td width='230px' align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;\">";
echo $results['Regal'];
echo "</td><td width='230px' align='center' style=\"border-style:solid;border-width:1px;border-color:#A4A4A4;\">";
echo "".$results['RegalEbene'];
echo "</td> </tr>";
}
echo "</table>";
} else {
echo 'Für die aktuellen Filter wurden keine Ergebnisse gefunden';
}
?>
Edit/Delete PHP
<?php
//including the database connection file
include_once("db_W3.php");
//fetching data in descending order (lastest entry first)
//$result = mysql_query("SELECT * FROM users ORDER BY id DESC"); // mysql_query is deprecated
$result = mysqli_query($mysqli, "SELECT * FROM centralhub_lagerbestand ORDER BY id DESC"); // using mysqli_query instead
?>
<html>
<head>
<title>Homepage</title>
</head>
<body>
<table width='100%' border=0>
<tr bgcolor='#CCCCCC'>
<td>KundenNr</td>
<td>Regal</td>
<td>RegalEbene</td>
<td>Update</td>
</tr>
<?php
//while($res = mysql_fetch_array($result)) { // mysql_fetch_array is deprecated, we need to use mysqli_fetch_array
while($res = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['KundenNr']."</td>";
echo "<td>".$res['Regal']."</td>";
echo "<td>".$res['RegalEbene']."</td>";
}
?>
</table>
</body>
</html>

php/mysql basic search not returing results

I have a test query I am performing and it seems to be running OK, however not returning any data within the rows of the tables when using a varible passed or a direct set year for the table
<?
$curyr = date("Y");
isset($_GET['year']) ? $year = $_GET['year'] : $year = "";
$server = $_SERVER['PHP_SELF'];
$dbhost = "localhost";
$dbuname = "";
$dbpass = "";
$dbname = "";
$conn = new mysqli($dbhost,$dbuname,$dbpass,$dbname);
if($conn->connect_errno):
die("$conn->connect_error \n");
endif;
echo ("<title>$year</title>");
if (($year >= 2001) && ($year <=$curyr)) {
echo ("
<table border='0' align='center' width='100%'>
<tr>
<td>
<table border='1' align='center' width='60%' style='border:1 solid #000000; border-collapse: collapse'>
<tr>
<th align='center' colspan='5'>
DETAIL
</th>
</tr>");
$query = "SELECT c.* , p.* FROM $year c, dataset p WHERE c.id = p.id ORDER BY p.name ASC";
$r = $conn->query($query);
while ($row = $r->fetch_assoc()) {
echo ("
<tr>
<td>
$row[namepre]$row[name]
</td>
<td>
</td>
<td>
TEST
</td>
<td>
</td>
</tr>");
}
echo ("
</table>
</td>
</tr>
</table>");
} else {
echo ("ERROR NO ROWS");
}
$conn->close();
?>
I'm returning the "detail" portion of the top of the sample table but nothing else. This is just basic but wondering where I'm possibly missing something.

search data and will choose to display in static table. like an add to cart

I want to display data after in static table just like an "Add to Cart".
<table>
<th>Item</th> <th>Description</th>
<tr>
<td>Monitor<td>
<td>Monitor Blue<td>
</tr>
<tr>
<td>Mouse<td>
<td>Mouse Wireless<td>
</tr>
<tr>
<td>Keyboard<td>
<td>Keyboard<td>
</tr>
My search form : This is no problem is searching item. Only in the add button every row. So when you search for item you have choice if you want to add in static table.
Search:
Mysql when search
<?
$sql = "select * from item_tb where name='".$search."'";
$result=$conn->query($sql);
echo "
<table cellspacing='20' border='0' ><tr> <th>Item</th><th>Description</th>
</tr>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "
<tr> <td>
".$row["name"]."</td> <td>
".$row["description"]."</td>";
echo" <form name='add' method='POST'>";
echo" <td><a href='item.php?id=".$row['Iid']."'><input type='submit' value='Add'></td></a>";
echo" </form>";
}
}
?>
Here is my main problem. i don't how to make table where i'll get the data i have search and will display as many as item i add. I'm really down in this.
<?
if($id != "")
{
$sql = "SELECT * FROM item_tb WHERE Iid='".$id."'";
$result=$conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo" ".$row["name"]." ";
}
}
}
?>
<?php
if($id != "") {
$sql = "SELECT * FROM item_tb WHERE Iid='" . $id . "'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?= $row['name'] ?></td>
</tr>
<?php
}
}
}
?>

MySQL/PHP News System

I have a PHP/MySQL News system which displayes the newest news article on the home page and a full list on a news page.
The newest article bit works but, my problem is that whenever i try to echo all the news article on the news page it either repeats the same or outputs one and nothing else.
**MySQL Information**
id INT AUTO_INCREMENT,
author VARCHAR(xxx),
title VARCHAR(xxx),
message TEXT,
date TEXT,
time TEXT,
PRIMARY KEY(id)
This is the insertion page (news_center.php)
<form action='/?module=admin&n=news_center_ac' method='post'>
<table align="center" width="68%">
<tr>
<td>Title</td>
<td><input style="width:100%;" type='text' name='news_title' /></td>
</tr>
<tr>
<td height="57">Message</td>
<td><input style="width:100%; height:100%;" type='text' name='news_message' /></td>
</tr>
<tr>
<td colspan='2'><input type='submit' /></td>
</tr>
</table>
This is news_center_ac.php
<?php
$conn = mysql_connect(*Connection Information*) or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$author == $_SESSION['name'];
if(!empty($_POST['news_title']) || !empty($_POST['news_message']))
{
if(!empty($_POST['news_title']) && !empty($_POST['news_message']))
{
$date = date( 'jS F Y' );
$time = date( 'H:i' );
$query = "INSERT INTO news (id, author, title, content, date, time) VALUES('', '".$author."', '".$_POST['news_title']."', '".$_POST['news_message']."', '".$date."', '".$time."')" or die(mysql_error());
$insert = mysql_query($query) or die(mysql_error());
echo '<p>Successful News Update “'.$_POST['news_title'].'”';
}
else
{
echo '<p>Please fill in all fields</p>';
}
}
?>
This is the Output on the news page (/news/index.php)
<?php
$conn = mysql_connect("*CONNECTION INFORMATION*") or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$news = mysql_query("SELECT * FROM news ORDER BY date DESC,id DESC");
$output = mysql_fetch_array($news);
?>
*CONTENT*
<?php
foreach ($output as $value) {
echo "<p> “" .$output['content'];
echo "”";
echo "Posted:" .$output['date'];
echo " " .$output['time'];
}
?>
I just want it to output each news article in turn i can sort out the formatting later once it works.
You are misusing mysql_fetch_array(). It needs to be called in a loop, as it only returns one row at a time.
$conn = mysql_connect("*CONNECTION INFORMATION*") or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$news = mysql_query("SELECT * FROM news ORDER BY date DESC,id DESC");
EDIT Added htmlentities() calls to convert html special characters
while ($row = mysql_fetch_array($news)) {
echo "<p> “" . htmlentities($row['content']);
echo "”";
echo "Posted:" . htmlentities($row['date']);
echo " " . htmlentities($row['time']);
}
Rewrite it this way:
<?php
while($output = mysql_fetch_array($news)) {
echo "<p> “" .$output['content'];
echo "”";
echo "Posted:" .$output['date'];
echo " " .$output['time'];
}
?>
When you call output first, it is only returning one value, this will loop through all.
try /news/index.php
<?php
$conn = mysql_connect("*CONNECTION INFORMATION*") or die(mysql_error());
$db = mysql_select_db( "db372357229")or die(mysql_error());
$news = mysql_query("SELECT * FROM news ORDER BY date DESC,id DESC");
while($output = mysql_fetch_assoc($news)) {
echo "<p> “" .$output['content'];
echo "”";
echo "Posted:" .$output['date'];
echo " " .$output['time'];
}
?>
Shouldn't the last foreach loop use $value, ie
<?php
foreach ($output as $value) {
echo "<p> “" .$value['content'];
echo "”";
echo "Posted:" .$value['date'];
echo " " .$value['time'];
}
?>

Categories