MySQL Database row in a Button - php

I have a database for my ToDo App which has following cloumns:
| ID | ShortDescription | Description | Date | Status |
I already can add a Task to the Datatable and can see it in phphmyadmin.
I have following code till now:
$id = mysql_real_escape_string($_GET['id']);
$out = 'SELECT * FROM ToDo1 WHERE `id` = '.$id.' LIMIT 1';
$result = mysqli_query($link, $out);
$row= mysqli_fetch_array($result);
?>
<div id= "OutShortDescription">
<?php
echo $row['ShortDescription'];
?>
</div>
<div id= "OutDescription">
<?php
echo $row['Description'];
?>
</div>
<div id= "OutDate">
<?php
echo $row['Date'];
?>
</div>
<div id= "OutStatus">
<?php
echo $row['Status'];
?>
</div>
Now I want to put every ID row on a own Site.
For that I want to make a table of Buttons (Buttonnumber=ID).
On this Button should only be shown the ShortDescription and when I click it I want to go to a the Site which matches to the Button.
Can someone help me?
EDIT
okay thanks now I have this code but it wont work:
<?php
$dbname= 'Groups';
$dsn = 'mysql:host=localhost;dbname='.$dbname;
$user = 'root';
$pass = '';
$db = new PDO($dsn, $user,$pass);
$query = "SELECT * FROM groups2 WHERE id = :id LIMIT 1";
$ps = $db->prepare($query);
$ps->bindParam(':id', $id);
$ps->execute();
$row = $ps->fetch(PDO::FETCH_ASSOC);
?>
<div class="searchwindow">
<?php
$data = $link->query('SELECT * FROM Groups2');
foreach($data as $row) {
echo '<p><input type="button" onclick="window.location = All_Groups.php?id=' . $row['ID'] . ' value='.$row['ShortDescription'].' /></p>';
}
I have now following code
<div data-role="page" id="SearchPage" data-title="SearchPage">
<div data-role="header">
<h1>Search</h1>
</div>
<div data-role="content">
<div data-role="header">
<form>
<fieldset data-role="controlgroup" data-type="horizontal" data-mini="true" align="center" id="selectMenu">
<select name="selectStatus" id="selectStatus">
<option value="0">Status</option>
<option value="1">Done</option>
<option value="2">In Progress</option>
</select>
</fieldset>
</form>
</div>
<?php
$dbname= 'Groups';
$dsn = 'mysql:host=localhost;dbname='.$dbname;
$user = 'root';
$pass = '';
$db = new PDO($dsn, $user,$pass);
$query = "SELECT * FROM groups2 WHERE id = :id LIMIT 1";
$ps = $db->prepare($query);
$ps->bindParam(':id', $id);
$ps->execute();
$row = $ps->fetch(PDO::FETCH_ASSOC);
?>
<div class="searchwindow">
<?php
$data = $link->query('SELECT * FROM Groups2');
foreach($data as $row) {
$path = $row['ID'];
$description = $row['ShortDescription'];
echo ("<form action='All_Groups.php?id=$path'><button type='submit' value='$description'/>$description</form>" );
}
?>
</div>
</div>
<div data-role="footer" data-position="fixed">
<div data-role="navbar">
<ul>
<li>Search</li>
<li>New</li>
<li>More</li>
</ul>
</div><!-- Ende navbar -->
</div><!-- Ende footer -->
</div>
And this is my All_groups.php
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Unbenanntes Dokument</title>
</head>
<body>
<?php
$servername ="localhost";
$username = "root";
$password = "";
$dbname = "Groups";
$link = mysqli_connect($servername, $username, $password, $dbname);
if (!$link) {
die('Verbindung nicht möglich : ' . mysqli_error($link) );
}
?>
<?php
$row="";
$Date="";
$Status="";
$ShortDescription="";
$Description="";
mysqli_select_db($link, "groups");
?>
</div>
<?php
$id = mysql_real_escape_string($_GET['id']);
$out = "SELECT * FROM groups2 WHERE ID = '$id' ";
$result = mysqli_query($link, $out);
$id = mysqli_fetch_array($result);
?>
<div id= "OutShortDescription">
<?php
echo $id['ShortDescription'];
?>
</div>
<div id= "OutDescription">
<?php
echo $id['Description'];
?>
</div>
<div id= "OutStatus">
<?php
echo $id['Status'];
?>
</div>
<div id= "OutDate">
<?php
echo $id['Date'];
?>
<div data-role="footer" data-position="fixed">
<div data-role="navbar">
<ul>
<li>Search</li>
<li>New</li>
<li>More</li>
</ul>
</div><!-- Ende navbar -->
</div>
</body>
</body>
</html>

First of all, don't use the mysql_* methods! Use PDO or mysqli_* instead.
Below, I'm pasting your example query, using PDO:
$dsn = 'mysql:host=localhost;dbname='.$dbname;//$dbName is the name of your database
$user = 'root';
$pass = '123';//use your login information here
$db = new PDO($dsn, $user,$pass);
$query = "SELECT * FROM ToDo1 WHERE id = :id LIMIT 1";
$ps = $db->prepare($query);
$ps->bindParam(':id', $id)
$ps->execute();
$row = $ps->fetch(PDO::FETCH_ASSOC);
Now, to get your button, you don't need to use jquery:
<?php
$path = $row['ID'];
$description = $row['ShortDescription'];
echo "<form action='your/site/$path'><button type='submit' value='$description'/>$description</form>"
?>
Another option is use the onclick:
<?php
$path = $row['ID'];
$description = $row['ShortDescription'];
echo "<input type=\"button\" onclick=\"location.href='your/site/$path'\" value=\"$description\" />";
?>
The \ before " is a escape, so PHP will print the character " and not interpret it as the end of your string.
Advice: Try to avoid mix HTML and PHP, in general this is a bad practice.

Related

I have this working php code but it doesn't return the expected resuts

I have PHP code retrieving data from MySQL DB. What I want is to press a button then a popup must display data for that relevant id or display data on that id row. So for the first btn it works well but on other btn's it displays data from btn 1.
Is there any solution out there?
Thanks
$query = "SELECT * FROM users"
$results = mysqli_query ($conn, $query);
$chck_res = mysqli_num_rows($results);
if ($chck_res > 0) {
while($row = mysqli_fetch_array($results)) {
$id = $row['id'];
$name =$row['name'];
$lastName = $row['lName'];
?>
<div class="container">
<button onClick="popWin()">View data at <?php echo $id; ></button>
</div>
<div id="popup">
echo $id = $row['id'];
echo $name =$row['name'];
echo $lastName = $row['lName'];
</div>
<?php
}
}
CSS:
#popup {
display: none;
}
JS:
function popWin() {
document.getElementById('popup').style.display = "Block";
}
DOM elements must have unique ID, you can't get access to the second element with the same ID via getElementById.
Try to replace your divs with next:
<div>
<div class="container">
<button onClick="popWin(this)">View data at <?php echo $id; ></button>
</div>
<div class="popup">
<?= $id = $row['id']; ?>
<?= $name = $row['name']; ?>
<?= $lastName = $row['lName']; ?>
</div>
</div>
JS:
function popWin(btn) {
btn.parentNode.parentNode.getElementsByClassName("popup")[0].style.display = "Block";
console.log(btn.parentNode.parentNode.getElementsByClassName("popup")[0].innerHTML);
}
function popWin(btn) {
btn.parentNode.parentNode.getElementsByClassName("popup")[0].style.display = "Block";
console.log(btn.parentNode.parentNode.getElementsByClassName("popup")[0].innerHTML);
}
<div>
<div class="container">
<button onClick="popWin(this)">View data at 2</button>
</div>
<div class="popup">
name id 2
</div>
</div>
<div>
<div class="container">
<button onClick="popWin(this)">View data at 1</button>
</div>
<div class="popup">
name id 1
</div>
</div>
Your code has errors. you
did not terminate the select query
did not end php tag on echo id on line 14
<?php
$query = "SELECT * FROM users";
$results = mysqli_query ($conn, $query);
$chck_res = mysqli_num_rows($results);
if ($chck_res > 0) {
while($row = mysqli_fetch_array($results)) {
$id = $row['id'];
$name =$row['name'];
$lastName = $row['lName'];
?>
<div class="container">
<button onClick="popWin()">View data at <?php echo $id; ?></button>
</div>
<div id="popup">
<?php echo $id = $row['id'];
echo $name =$row['name'];
echo $lastName = $row['lName'];?>
</div>
<?php }}

Display the value on to the label below the applicant pending

I have to pull out the value from the database table call count.php which I already got the value 49. The problem is how to insert into the html
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hopeplace";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT COUNT(*) AS TOTAL_APPLICANT FROM applicant";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo $row["TOTAL_APPLICANT"];
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
code for the container for the applicant pending
<div class="content">
<div class="row">
<div class="col-xs-5">
<div class="icon-big icon-danger text-center">
<i class="ti-user"></i>
</div>
</div>
<div class="col-xs-7">
<div class="numbers">
<p>Applicant Pending</p>
<p>*this is the place where value need to be put*</p>
</div>
</div>
</div>
<div class="footer">
<hr />
</div>
</div>
You can assign value to one variable and echo that variable inside html
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "hopeplace";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$count = 0;
$sql = "SELECT COUNT(*) AS TOTAL_APPLICANT FROM applicant";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$count = $row["TOTAL_APPLICANT"] ;
}
}
mysqli_close($conn);
?>
code for the container for the applicant pending will be like
<div class="content">
<div class="row">
<div class="col-xs-5">
<div class="icon-big icon-danger text-center">
<i class="ti-user"></i>
</div>
</div>
<div class="col-xs-7">
<div class="numbers">
<p>Applicant Pending</p>
<p>
<?php echo $count; ?>
</p>
</div>
</div>
</div>
<div class="footer">
<hr />
</div>
</div>
save both html & php in same file or ensure that your html code is saved as a .php file. So, your script will be - <p><?php echo $total; ?></p> or <p><?= $total ?></p>

Putting the WHERE statement in SQL STATEMENT

<div class = "col-md-9 text-left">
<?php
$host = 'localhost';
$dbname = 'project';
$username = 'root';
$password = '1234';
$charset = 'utf8';
try
{
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sql = "SELECT subject,description,time,date FROM status";
$q = $pdo->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
$usid = ($row['userID']);
$sql1 = 'SELECT status.subject, status.description, status.time , status.date , status.stno , status.userID , tbl_users.userID , tbl_users.Fname , tbl_users.Lname
FROM status , tbl_users
WHERE status.userID=tbl_users.userID ORDER BY status.time DESC';
$q1 = $pdo->prepare($sql1);
$q1->execute([$usid]);
$q1->setFetchMode(PDO::FETCH_ASSOC);
}
catch (PDOException $e)
{
die("Could not connect to the database $dbname :" . $e->getMessage());
}
?>
<?php while ($row = $q->fetch()): ?>
<?php while ($row1 = $q1->fetch()): ?>
<div class="col-md-9">
<div class="box box-widget">
<div class="box-header with-border">
<div class="user-block">
<img class="img-circle" src="<?php echo $row10['des']; ?><?php echo $row9['userPic']; ?>" alt="User Image">
<span class="username"><?php echo htmlspecialchars($row1['Fname']); ?> <?php echo htmlspecialchars($row1['Lname']); ?></span>
<span class="description">Shared publicly - <?php echo htmlspecialchars($row['time']) ?> <?php echo htmlspecialchars($row['date']) ?></span>
</div>
<!-- /.user-block -->
<div class="box-tools">
<button type="button" class="btn btn-box-tool" data-toggle="tooltip" title="Mark as read">
<i class="fa fa-circle-o"></i>
</button>
<button type="button" class="btn btn-box-tool" data-widget="collapse">
<i class="fa fa-minus"></i>
</button>
<button type="button" class="btn btn-box-tool" data-widget="remove">
<i class="fa fa-times"></i>
</button>
</div>
<!-- /.box-tools -->
</div>
<!-- /.box-header -->
<div class="box-body">
<p><b><?php echo htmlspecialchars($row1['subject']) ?></b></p>
<p><i><?php echo htmlspecialchars($row1['description']) ?></i></p>
<?php
// Check connection
$servername = "localhost";
$username = "root";
$password = "1234";
$dbname = "project";
htmlspecialchars($a = $row1['stno']);
$d1 = $row7['userID'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM likes WHERE rec = $a";
$result = $conn->query($sql);
if ($result->num_rows > 0)
{
// output data of each row
while($row = $result->fetch_assoc())
{
$GLOBALS['a'] = $row['do'];
}
}
$z4 = $GLOBALS['a'];
if ($d1==$z4)
{
include ("unlikee.php");
}
else
{
include ("likee.php");
}
$conn->close();
?>
<span class="pull-right text-muted"><?php
$con=mysqli_connect("localhost","root","1234","project");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
htmlspecialchars($a = $row1['stno']);
$sql="SELECT * FROM likes WHERE rec = $a";
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
printf("%d\n",$rowcount);
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?>
</h5>
<span class="description-text"><?php
$con=mysqli_connect("localhost","root","1234","project");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
htmlspecialchars($a = $row1['stno']);
$sql="SELECT * FROM likes WHERE rec = $a";
if ($result=mysqli_query($con,$sql))
{
// Return the number of rows in result set
$rowcount=mysqli_num_rows($result);
if ($rowcount==1)
echo 'Like';
else
echo 'Likes';
// Free result set
mysqli_free_result($result);
}
mysqli_close($con);
?> - 3 comments</span>
</div>
<!-- /.box-body -->
<!-- /.box-footer -->
<div class="box-footer">
<form action="#" method="post">
<img class="img-responsive img-circle img-sm" src="../dist/img/user4-128x128.jpg" alt="Alt Text">
<!-- .img-push is used to add margin to elements next to floating images -->
</form>
</div>
<!-- /.box-footer -->
</div>
<!-- /.box -->
</div>
<?php endwhile; ?> <?php endwhile; ?>
</div>
I want to retrieve the Data of only one USER but I don't know how to give a condition for it in SQL Statement. Where and how I can put WHERE userID = $user_Session?
$sql = "SELECT subject,description,time,date FROM status";
In the two code statements above where should I put the first?
$sql1 = 'SELECT status.subject, status.description, status.time , status.date , status.stno , status.userID , tbl_users.userID , tbl_users.Fname , tbl_users.Lname
FROM status , tbl_users
WHERE status.userID=tbl_users.userID ORDER BY status.time DESC';
$sql1 =
'SELECT
status.subject, status.description, status.time , status.date , status.stno , status.userID , tbl_users.userID , tbl_users.Fname , tbl_users.Lname
FROM
status , tbl_users
WHERE
status.userID=tbl_users.userID
AND [correct_table_name].userID = $user_Session # here with AND instead WHERE
ORDER BY
status.time DESC';
Here is the code I added a parameter UID
try
{
$pdo = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
$sql = "SELECT subject,description,time,date FROM status";
$q = $pdo->query($sql);
$q->setFetchMode(PDO::FETCH_ASSOC);
$usid = ($row['userID']);
$sql1 = 'SELECT status.subject, status.description, status.time , status.date , status.stno , status.userID , tbl_users.userID , tbl_users.Fname , tbl_users.Lname
FROM status , tbl_users
WHERE status.userID=tbl_users.userID and tbl_users.userID = :UID ORDER BY status.time DESC';
$q1 = $pdo->prepare($sql1);
$q1->bindParam(':UID', $usid, PDO::PARAM_INT); //call with param
$q1->execute();
$q1->setFetchMode(PDO::FETCH_ASSOC);
}

Repeat div width with Different Database Table ID

I am trying to repeat a div the same div multiple times with as many rows as i have in the database table. a non working example i made of what i want is below.
<?php for ($i=0; $i < 3; $i++) {
?><!-- switch the three with the number of rows in the table-->
I would want to loop the container and switch the database query with the value of i.
<div id = "bodyContainer">
<form class="review">
<div class="reviewContainer">
<div class = "images">
<?php
$link = mysqli_connect("localhost", "root", "root","DJP");
if (mysqli_connect_error()) {
die("Could not connect to database");
}
$query = "SELECT * FROM reviews WHERE id = '<?php echo $i ?><!--switch the value of i here-->'";
$result=mysqli_query($link, $query);
$row = mysqli_fetch_array($result);
echo '<img height = 90% width = 100% src="data:image/jpeg;base64,'.$row[3].' " />';
?>
</div>
<!--<div class="title">
<?php
//echo $row[1];
?>
</div>-->
<div class = "comment">
<?php
echo $row[2];
?>
</div>
<div class = "rating" style = "float: right;">
<?php
echo $row[4];
?>
<br/>
<br/>
stars
</div>
</div><!--end review-->
</form>
</div><!--end body container-->
<?php;}?><!-- end the for loop so all the divs can be re-done with a new entry for the query. I would move the $link out of loop also.-->
Just fetch all the rows once and then go through them as you print the data:
$link = mysqli_connect("localhost", "root", "root","DJP");
if (mysqli_connect_error()) {
die("Could not connect to database");
}
$query = "SELECT * FROM reviews";
$result=mysqli_query($link, $query);
while($row = mysqli_fetch_array($result)){ ?>
<div id = "bodyContainer">
<form class="review">
<div class="reviewContainer">
<div class = "images">
<?php
echo '<img height = 90% width = 100% src="data:image/jpeg;base64,'.$row[3].' " />';
?>
</div>
<!--<div class="title">
<?php
//echo $row[1];
?>
</div>-->
<div class = "comment">
<?php
echo $row[2];
?>
</div>
<div class = "rating" style = "float: right;">
<?php
echo $row[4];
?>
<br/>
<br/>
stars
</div>
</div><!--end review-->
</form>
</div><!--end body container-->
<?php } ?>

Data not displaying from database

I am new to PHP and trying to display data from the database.
However it only displays data from one row but I want to show the data from multiple rows where the condition match. Here is my code I am using:
<?PHP
session_start();
if (!(isset($_SESSION['username']) && $_SESSION['username'] != '')) {
header ("Location: checklogin.php");
}
$con = mysqli_connect("localhost", "root", "", "map_my_way");
$fetch_row = $_SESSION['username'];
$result = mysqli_query($con,"SELECT * FROM members where username='$fetch_row'");
while($row = mysqli_fetch_array($result)) {
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$email = $row['email'];
$m_no = $row['m_no'];
$v_name = $row['v_name'];
$capacity = $row['capacity'];
$fuel_type = $row['fuel_type'];
}
$result2 = mysqli_query($con,"SELECT s_a_name FROM locations where username='$fetch_row'");
$row2 = mysqli_fetch_array($result2);
?>
<!DOCTYPE HTML>
<html>
<head>
<title>Profile Page</title>
<link rel="stylesheet" type="text/css" href="profile.css">
</head>
<body id="body">
<div id="mmw"> <span> MAP MY WAY </span></div>
<div id="title_box">
<button id="lvbutton">My Profile</button>
<button id="lvbutton">Maps</button>
<button id="lvbutton">Edit Profile</button>
<button id="lvbutton" style="float:right; margin- right:10px;">Sign-Out</button>
</div>
<div id="box">
<div id="name_box"><span>Welcome <?php echo($_SESSION['username']); ?></span></div>
<div id="box1">
<div> <p id="link">Your Information </p>
<ul style="margin-top:20px; padding-left:0;">
<li><span>First Name : <?php echo $first_name; ?></span></li><br>
<li><span>Last Name : <?php echo $last_name; ?></span></li><br>
<li><span>Email : <?php echo $email; ?></span></li><br>
<li><span>Age : <?php echo $m_no; ?></span></li><br>
<li><span>Current Vehical : <?php echo $v_name; ?></span></li><br>
<li><span>Fuel Type: <?php echo $fuel_type; ?> </span></li><br>
<li><span>Seating Capacity : <?php echo $capacity; ?></span></li><br>
</ul>
</div>
</div>
<div id="box2"> <p id="link">Saved Routes </p>
<ul style="margin-top:20px;">
<span> <?php foreach($row2 as $data)
{echo "route Name : $data <br>" ; };
?>
</span>
</ul>
</div>
</div>
</div>
</body>
</html>
Your query on the members table is only going to return one row, so you don't really need to have $row = mysqli_fetch_array($result) in a loop (that shouldn't break your script, though). Your query on the locations table will potentially return more than one row, so you do need to loop on that one. Something like:
$result2 = mysqli_query($con,"SELECT s_a_name FROM locations where username='$fetch_row'");
$savedRoutes = "";
while($row2 = mysqli_fetch_array($result2))
{
$savedRoutes .= "route Name : " . $row2['s_a_name'] . "<br/>";
}
will put all the data together in a string ($savedRoutes) that can be echoed down in the "Saved Routes" section of your html code.
Alternatively, you can put the loop down in the "Saved Routes" section and just echo out each line as you loop through. Personally I think putting the loop in the top and concatenating the data into a string is a bit cleaner and easier to read.

Categories