Dynamic Html pages from Mysql with PHP - php

I'm working on a website with php and mysql and have some problems to generate web pages URL from database rows.
I have only 3 page connection.php (mysql connection) index.php (where show al products/contents thumbnails with button with product details URL) and details.php where i want show info for single product.
from index.php i add a link to redirect to details.php page with this:
<a href="details.php?id=<?php echo $row['ID']; ?>"
it's work but Big problem is in details.php because the script don't show a single products details, but show all products, please someone can help me? Thank you
index.php code
......other html code......
<div class="row">
<?php
require_once 'connection.php';
$query = "SELECT * FROM campi_name";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
?>
<div class="col-sm-4 col-md-3">
<div class="thumbnail">
<img src="<?php echo $row['Thumbnail']; ?>" alt="<?php echo $row['Title']; ? >">
<div class="caption">
<h4><?php echo substr($row['Title'], 0, 30); ?></h4>
<p><?php echo $row['Brand']; ?></p>
<?php echo $row['ID']; ?>
<p>Cofronta Dettagli</p>
</div>
</div>
</div>
<?php
}
?>
......other html code......
connection.php code
$DBhost = "localhost";
$DBuser = "root";
$DBpass = "";
$DBname = "prodotti";
try {
$DBcon = new PDO("mysql:host=$DBhost;dbname=$DBname",$DBuser,$DBpass);
$DBcon->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $ex){
die($ex->getMessage());
}
?>
details.php code
......other html code......
<div class="container">
<div class="row">
<?php
require_once 'connection.php';
$query = "SELECT * FROM campi_name";
$stmt = $DBcon->prepare( $query );
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC)){
?>
<div class="col-sm-4 stylerow">
<a href="<?php echo $row['AffiliateLink']; ?>" class="thumbnail">
<img src="<?php echo $row['Thumbnail']; ?>" alt="<?php echo $row['Title']; ? >">
</a>
</div>
<div class="col-sm-8 stylerow">
<h2><?php echo $row['Title']; ?></h2>
<p><?php echo $row['Brand']; ?></p>
<button type="button" class="btn btn-primary btn-lg">Amazon</button>
</div>
</div>
</div><!-- /.container -->
......other html code......

Add
$id=$_GET['id'];
edit following line in your code
$query = "SELECT * FROM campi_name";
To
$query = "SELECT * FROM campi_name where id="'.$id.'" ";

Related

Generate HTML code with PHP automatically, after data pull in MySql

I have written a really simple php page that populate a database.
I now want to fetch this data in another php page and that is ok.
What I would like to achive is:
when I add another row into the database, I would like the html to create a new card, and not add the information in the same card.
I am not sure I understand how this can be achived.
Do I have to use php templates like smarty or anybody can point me how could I proceed?
This is how it look when I add second raw:
While what i want to achive should look like
Here is the HTML code I use with the PHP code:
<section class="tm-section-3 tm-section-mb" id="tm-section-3">
<div class="row">
<div class="col-md-6 tm-mb-sm-4 tm-2col-l">
<div class="image">
<img src="img/tm-img-1.jpg" class="img-fluid" />
</div>
<div class="tm-box-3">
<h2>
<?php if (mysqli_num_rows($result) > 0) {
?>
<table>
<?php
include_once '../scripts/connection.php';
$result = mysqli_query($link,"SELECT
domain,subdomain,topic,topictitle,topictext FROM newpage");
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["domain"]; ?></td>
</tr>
<tr>
<td><?php echo $row["subdomain"]; ?></td>
</tr>
<tr>
<td><?php echo $row["topic"]; ?></td>
</tr>
<tr>
<td><h4><?php echo $row["topictitle"]; ?></h4></td>
</tr>
<tr>
<td><h5><?php echo $row["topictext"]; ?></h5></td>
</tr>
<?php
$i++;
}
?>
</table>
<?php
}
else{
echo "No result found";
}
?>
</h2>
<p>
</p>
<div class="text-center">
Details
</div>
</div>
</div>
</div>
</section>
This is how i send the code to the db:
<?php
include("connection.php");
$domain = mysqli_real_escape_string($link, $_POST['domain']);
$subdomain = mysqli_real_escape_string($link, $_POST['subdomain']);
$topic = mysqli_real_escape_string($link, $_POST['topic']);
$topictitle = mysqli_real_escape_string($link, $_POST['topictitle']);
$topictext = mysqli_real_escape_string($link, $_POST['topictext']);
$sql = "INSERT INTO newpage (domain,subdomain,topic,topictitle,topictext) VALUES ('$domain','$subdomain','$topic','$topictitle','$topictext')";
$result = mysqli_query($link, $sql);
// if query fails stop script and echo error
if( $result === false)
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
exit;
}
$sql = "INSERT INTO menu (item) VALUES ('$domain')";
$result = mysqli_query($link, $sql);
// if query fails stop script and echo error
if( $result === false)
{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
exit;
}
header("location:../scripts/add-new-page-script-end.php");
exit;
echo "You'll never see this";
?>
Here the code that works even the style is bad. But logically is correct:
<div class="col-md-6 tm-mb-sm-4 tm-2col-l">
<?php
include_once '../scripts/connection.php'; $result = mysqli_query($link,"SELECT domain,subdomain,topic,topictitle,topictext FROM newpage"); foreach($result as $row){
?>
<div class="image">
<img src="img/tm-img-1.jpg" class="img-fluid" />
</div>
<div class="tm-box-3">
<h1><?php echo $row['domain']; ?></h1>
<h2><?php echo $row['subdomain']; ?></h2>
<h3><span><?php echo $row['topic']; ?></span></h3>
<h4> <span><?php echo $row['topictitle']; ?></span></h4>
<p><?php echo $row['topictext']; ?></p>
<div class="text-center">
Details
</div>
</div>
<?php
}
?>
</div>
It currently looks like you have something like this:
<div class="card">
<img src="..." />
<?php
foreach($result as $row){
?>
<h1><?php echo $row['domain-name']; ?></h1>
<h2><?php echo $row['sub-domain-name']; ?></h2>
<span><?php echo $row['topic-text-title']; ?></span>
<p><?php echo $row['text-of-topic']; ?></p>
<?php
}
?>
<button>Details</button>
</div>
If you instead put the foreach loop outside of the card div then it will make a new card for each result, something like this:
<?php
foreach($result as $row){
?>
<div class="card">
<img src="..." />
<h1><?php echo $row['domain-name']; ?></h1>
<h2><?php echo $row['sub-domain-name']; ?></h2>
<span><?php echo $row['topic-text-title']; ?></span>
<p><?php echo $row['text-of-topic']; ?></p>
<button>Details</button>
</div>
<?php
}
?>
Assuming that your are not using any framework.
In raw php context you could do something like this:
<div>
<?php foreach($arr as $item): ?>
<div>
<img src="...">
<h1><?php echo $item->domain_name; ?></h1>
<h2><?php echo $item->subdomain_name; ?></h2>
<h3><?php echo $item->topic; ?></h3>
<h4><?php echo $item->topic_text_title; ?></h4>
<h5><?php echo $item->text_pf_topic; ?></h5>
</div>
<?php endforeach; ?>
</div>

Add class to PHP if SQL field is 'Yes'

I'm creating a site that lists custom userbars in a forum, and am using both PHP and SQL to achieve this easily.
My question is:
If a field is listed 'active' for the group, how do I go about adding a class specifically for that, to enable a glow around the Bootstrap card?
Here's my current code (forgive me if it's terrible)
<div class="cards">
<?php
include_once("assets/php/db.php");
$sql = "SELECT ubFilename, ubGroupName, ubGroupOwner, ubOwnerLink, isOfficial, isActiveGroup FROM UBSUserbars ORDER BY ubGroupName";
$resultset = mysqli_query($conn, $sql) or die("database error:". mysqli_error($conn));
while( $record = mysqli_fetch_assoc($resultset) ) {
?>
<div class="col-sm-4 col-card">
<img src="i/OGUsers/<?php echo $record['ubFilename']; ?>">
<hr>
<h4 class="group"><?php echo $record['ubGroupName']; ?></h4>
<span>Owner: <a class="owner" href="<?php echo $record['ubOwnerLink']; ?>"><?php echo $record['ubGroupOwner']; ?></a></span>
</div>
<?php } ?>
</div>
You can use a ternary operator like this:
<div class="cards">
<?php
include_once("assets/php/db.php");
$sql = "SELECT ubFilename, ubGroupName, ubGroupOwner, ubOwnerLink, isOfficial, isActiveGroup FROM UBSUserbars ORDER BY ubGroupName";
$resultset = mysqli_query($conn, $sql);
while( $record = mysqli_fetch_assoc($resultset) ) {
?>
<div class="col-sm-4 col-card <?php $record['isActiveGroup'] === 'yes' ? 'ADD-CLASS-HERE' : ''; ?>">
<img src="i/OGUsers/<?php echo $record['ubFilename']; ?>">
<hr>
<h4 class="group"><?php echo $record['ubGroupName']; ?></h4>
<span>Owner: <a class="owner" href="<?php echo $record['ubOwnerLink']; ?>"><?php echo $record['ubGroupOwner']; ?></a></span>
</div>
<?php } ?>
</div>
You can change the value of 'yes' to the value of the active group that is saved on your database.

NO error but not working while retrieving rows from mysql database through php

This is php code for retrieving query from mysql database. This has no error as far as I know. Unfortunately this is not working. I cannot find the mistake.
<?php
if(isset($_POST['submit'])) // submit is submit button name as POST method
{
$username = $_POST['in'];
$Timein= date("Y-m-d H:i:s",strtotime(str_replace('/','-',$username))); // converting into mysql datetime format
$username = $_POST['out'];
$Timeout = date("Y-m-d H:i:s",strtotime(str_replace('/','-',$username)));
$dbc = mysqli_connect('localhost','dev','dev#123','Database') or die('Error while connecting Database'.mysql_connect_error);
// connection to database
$q="select * from camera where NOT chkin > '$Timein' AND NOT chkout < '$Timeout'";
$r = mysqli_query($dbc,$q);
// query
while ($data = mysqli_fetch_assoc($r)){ ?>
// printing a bootstrap grid
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-6 col-xxs-12">
<div class="tm-tours-box-2">
<img src="img/index-0 <?php echo $data['id'];?>.jpg" alt="image" class="img-responsive">
<div class="tm-tours-box-2-info">
<h3 class="margin-bottom-15"><?php echo $data['name']; ?></h3>
<p>Rs <?php echo $data['price']; ?>/day</p>
</div>
<a class="tm-tours-box-2-link" onclick="POP()">Book Now</a>
</div>
</div>
<?php
}
}
?>

How can I include an image that is tied to a user's database information?

I've been coding PHP for 2 weeks (it's not pretty) and I have not been able to find the answer to my question. I want an admin type user to be able to fill a form and post it to a page where base level users can view the content. I've gotten all of this to work like a charm, but my dilemma is to allow the admin user to include an image as well. Maybe I just don't know what to search for.
Here is the php code and the form for the admin user page:
<?php
error_reporting(E_ALL & ~E_NOTICE);
session_start();
include_once("connection.php");
if (isset($_SESSION['adminid'])) {
$adminid = $_SESSION['adminid'];
$adminemail = $_SESSION['adminemail'];
if ($_POST['submit']) {
$title = $_POST['title'];
$deadline = $_POST['deadline'];
$content = $_POST['content'];
$sql_blog = "INSERT INTO blog (title, deadline, content, logoname) VALUES ('$title', '$deadline', '$content', '$logoname')";
$query_blog = mysqli_query($dbcon, $sql_blog);
echo "<script>alert('Your inquiry has been posted')</script>";
}
} else {
header('Location: index.php');
die();
}
$sql = "SELECT adminid, adminemail, adminpassword, adminname FROM admin WHERE adminemail = '$adminemail' LIMIT 1";
$query = mysqli_query($dbcon, $sql);
if ($query) {
$row = mysqli_fetch_row($query);
$adminname = $row[3];
}
?>
and here is the code for the base level user page: (i commented out the image block where I want the admin's image to be shown.
<main>
<div class="container">
<div class="row topbuffpost">
<h1>business inquiries</h1>
<hr>
<?php
include_once('connection.php');
$sql = "SELECT * FROM blog ORDER BY id DESC";
$result = mysqli_query($dbcon, $sql);
while ($row = mysqli_fetch_array($result)) {
$title = $row['title'];
$content = $row['content'];
$date = strtotime($row['deadline']);
?>
<div class="col-md-4 col-lg-3">
<div class="card hoverable">
<!-- <div class="card-image">
<div class="view overlay hm-white-slight z-depth-1">
<img src="">
<a href="#">
<div class="mask waves-effect">
</div>
</a>
</div>
</div> -->
<div class="card-content">
<h5> <?php echo $title; ?> <br/> <h6>Deadline |<small> <?php echo date("j M, Y", $date); ?> </small> </h6></h5> <br/>
<p> <?php echo $content; ?> </p>
<div class="card-btn text-center">
Read more
<i class="fa fa-lightbulb-o"></i>&nbsp propose a plan
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
</main>
All of this works perfectly, I just can't figure out how to have an image display in the same way as the title, deadline, and content. Youtube wont help either, too much outdated php + I haven't been coding long enough to really work things out on my own.
You can save all user images under a folder (let's call /images/user) and record the file name into database.
if ($_POST['submit']) {
$title = $_POST['title'];
$deadline = $_POST['deadline'];
$content = $_POST['content'];
$logoname = basename($_FILES["fileToUpload"]["logoname"]; // <-- Make sure your form is ready to submit an file
// Update below as per your need.
$target = 'images/users/' . $logoname;
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target);
$sql_blog = "INSERT INTO blog (title, deadline, content, logoname) VALUES ('$title', '$deadline', '$content', '$logoname')";
$query_blog = mysqli_query($dbcon, $sql_blog);
echo "<script>alert('Your inquiry has been posted')</script>";
}
You can then display the image your page
<main>
<div class="container">
<div class="row topbuffpost">
<h1>business inquiries</h1>
<hr>
<?php
include_once('connection.php');
$sql = "SELECT * FROM blog ORDER BY id DESC";
$result = mysqli_query($dbcon, $sql);
while ($row = mysqli_fetch_array($result)) {
$title = $row['title'];
$content = $row['content'];
$date = strtotime($row['deadline']);
$logoname = 'images/user/' . $row['logoname'];
?>
<div class="col-md-4 col-lg-3">
<div class="card hoverable">
<div class="card-image">
<div class="view overlay hm-white-slight z-depth-1">
<img src="<?php echo $logoname; ?>">
<a href="#">
<div class="mask waves-effect">
</div>
</a>
</div>
</div>
<div class="card-content">
<h5> <?php echo $title; ?> <br/> <h6>Deadline |<small> <?php echo date("j M, Y", $date); ?> </small> </h6></h5> <br/>
<p> <?php echo $content; ?> </p>
<div class="card-btn text-center">
Read more
<i class="fa fa-lightbulb-o"></i>&nbsp propose a plan
</div>
</div>
</div>
</div>
<?php
}
?>
</div>
</div>
</main>

php form submitting info twice

I have a form that's supposed to enter a reply to a forum topic into the database and redirect the user back to the same topic. After much trial and error I have finally got the form to work, only it is putting two identical entries into the db every time. I cannot figure out why. I have looked up this same problem and most of the other people were not redirecting after the form submission or they were using AJAX or jquery or something. Here is my page info:
<?php
session_start();
include_once('includes/config.php');
include_once('classes/topic.php');
include_once('classes/post.php');
include('includes/header.php');
?>
<link rel="stylesheet" href="css/dd.css">
<?php
$topic = new Topic;
if (isset($_GET['id']))
{
$topic_id = $_GET['id'];
$data = $topic->fetch_data($topic_id);
if (isset($_POST['content']))
{
// someone posted a reply
$date = date('Y-m-d H:i:s');
$by = $_SESSION['user_id'];
$query = $pdo->prepare("INSERT INTO dd_posts (post_content, post_date, post_by, post_topic) VALUES (? ,? ,?, ?)");
$query->bindParam(1, $_POST['content']);
$query->bindParam(2, $date);
$query->bindParam(3, $by);
$query->bindParam(4, $_GET['id']);
$query->execute();
$result = $query->execute();
header("location:topic.php?id=".$_GET['id']);
exit;
}
?>
<div id ="wrapper">
<div class="drop-section">
<div id="menu">
<a class="item" href="drop_index.php">Dead Drop</a>
<a class="item" href="add_topic.php">New Post</a>
<a class="item" href="admin/add_cat.php">New Category</a>
<div id="userbar">
<?php
if( $user->is_logged_in() ) {
echo 'Hello ' . $_SESSION['user_name'] . '. How are you?';
} else {
echo '<a class="item" href="login.php">Sign in</a> or <a class="item" href="index.php">Create an account</a>';
}
?>
</div>
</div>
<table>
<tr class = "header-row">
<div id = "sans">
<?php echo $data['topic_subject']; ?>
- <small>started by <?php echo $data['user_name']; ?> </small><br />
<?php echo $data['topic_content']; ?>
</div>
</tr>
<?php
// retrieve all the replies to the original topic
$post = new Post;
$topic_id = $_GET['id'];
$posts = $post->fetch_all_posts_by_topic($topic_id);
?>
<tr>
<td class="first-column">
<?php foreach ($posts as $post) { ?>
<div class="drop-content-box">
<li><?php echo $post['post_content']; ?><br />
<div class = "forum-user-info">
<a href="player.php?id=<?php echo $post['user_id']; ?>">
<?php echo $post['user_name']; ?></a> - level:
<?php echo $post['user_level']; ?>
</div>
</li>
</div>
<?php } ?>
</td>
</tr>
</table>
<?php
if( $user->is_logged_in() )
{
?>
<div id = "header-section">Reply</div>
<?php if (isset($error)) { ?>
<small><?php echo $error; ?></small>
<?php } ?>
<form action="<?php echo "topic.php?id=".$_GET['id']?>" method="post" autocomplete="off">
<small><i>Do not post the actual answer to any level.</i></small><br />
<textarea rows="15" cols="50" name="content" placeholder="Give us your thoughts..."></textarea><br />
<input type="submit" value="Post" />
</form>
</div>
</div>
<?php
} else {
echo '<div id = "errors"><small>You must be signed in to reply.</div></small>';
}
}
include_once('includes/footer.php');
?>
You're executing the query twice.
$query->execute();
$result = $query->execute();

Categories