I would like to have the freedom to place a row entry from my database wherever i' prefer in the page. Right now, the php code that I use is as follows (it is clean working code):
<html><head></head>
<body>
<?php
$db = mysql_connect("xxx","xxx","xxx") or die("Database Error");
mysql_select_db("caisafety",$db);
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `cert_rr` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
echo $row['id']; while($row = mysql_fetch_array( $result )) {
echo "<br><br>";
echo $row['basic3'];
echo $row['basic2'];
echo $row['basic1'];
}
?>
</body>
</html>
I call id through the browser Eg. http://site.com/getid.php?id=10 . But I do not have the freedom to place my row entry within my html. For eg. like this:
<table><tr>
<td align="center">BASIC INFO 1: <?php echo $row['basic1']; ?></td>
<td align="center">BASIC INFO 2: <?php echo $row['basic2']; ?></td>
</tr></table>
I can place html within echo PHP tags but then I have to clean up my html and thats a lot of work. Retaining HTML formatting would be preferred. Any help or guidelines on this would be much appreciated.
<?php
$db = mysql_connect("xxx","xxx","xxx") or die("Database Error");
mysql_select_db("caisafety",$db);
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `cert_rr` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
//you need to retrieve every row and save to an array for later access
for($rows = array(); $tmp = mysql_fetch_array($result);)
{
$rows[] = $tmp;
}
//now you can use the $rows array where every you want e.g. with the code from Zhube
?>
....
<table><?php foreach($rows as $r):
<td><?php echo $r['id'] ?></td><?php endforeach ?>
</table>
By
while($row = mysql_fetch_array( $result )) {
echo "<br><br>";
echo $row['basic3'];
echo $row['basic2'];
echo $row['basic1'];
}
you save only the last row
Instead of having your HTML tags as part of the PHP variable, do something like this instead:
<table><?php foreach($row as $r):?>
<td><?php echo $r['id'] ?></td><?php endforeach ?>
</table>
Related
I'm making a page which needs to display all the rows stored in a table named 'events' in a database named 'school'
The problem is that even though I've multiple entries/rows in the table, for some reason only a single row is displayed when I run the page.
Here's my code-
<?php
require("includes/common.php");
$query = "SELECT * FROM school.events";
$result = mysqli_query($con, $query)or die(mysqli_error($con));
$row = mysqli_fetch_array($result);
$ename = $row['name'];
$place = $row['place'];
$date = $row['date'];
?>
.
.
.
<?php
while ($row) {
?>
<tr>
<td><?php echo $date; ?></td>
<td><?php echo $ename; ?></td>
<td><?php echo $place; ?></td>
</tr>
<?php
$row = mysqli_fetch_array($result);
$name = $row['name'];
$place = $row['place'];
$date = $row['date'];
}
?>
Try instead while($row = mysqli_fetch_assoc($result)){ ... } , you're just assigning the array to $row, you can't loop an array saying while($array)
Do it this way:
while($row = mysqli_fetch_assoc($result)){
echo $row['id'];
echo $row['name'];
}
That is because $row = mysqli_fetch_array($result); will only pull one row and then move the pointer. In order to get all the rows, use a while loop
while($row = mysqli_fetch_array($result)) {
//code to populate table one row at a time
}
With the code i have i can succesfully read and display data from my database,
"agencies_validation" does this. I also manage to make it so it display Update Link for each row. But the Update link doesen't work. So what i want it to do is when i click Update link it calls "agencies_admin_update.php" which is supposed to Update that row. But it doesn't work.
//agencies_validation
<table border="2">
<tr>
<td>title</td>
</tr>
<?php
include_once 'database.php';
echo '<form action= "agencies_admin_update.php" method="get">';
$valid_query = "SELECT * FROM agencies ";
$valid_result = mysqli_query($link, $valid_query);
while ($row = mysqli_fetch_array($valid_result)) {
echo '<tr>';
$id = $row['id'];
echo '<td>'.$row['title'].'</td>';
echo "<td><a href='agencies_admin_update.php?'>UPDATE</a></td>";
echo '<tr>';
}
echo '</form>';
?>
</table>
and
//agencies_admin_update.php
<?php
include_once 'database.php';
$id = $_GET['id'];
$query = "UPDATE agencies SET admin=2 WHERE id = $id";
header("Location: agencies_validation.php");
?>
The first you have to add the ID to link URL:
...
echo "<td><a href='agencies_admin_update.php?id=" . $id . "'>UPDATE</a></td>";
...
The second you must not only to generate the SQL query, but and execute it:
...
$query = "UPDATE agencies SET admin=2 WHERE id = $id";
mysqli_query($link, $query)
...
If this is really all code, then I think, you are just specifying your query in agencies_admin_update.php and not executing it.
See here:
$query = "UPDATE agencies SET admin=2 WHERE id = $id";
The $query variable is set, but not executed.
After specifying the variable you are redirecting the user immidiately with this code:
header("Location: agencies_validation.php");
But I'm wondering, how this should work, because you have no $id specified, which you want to update in agencies_admin_update.php.
Try this!!!
<td><a href='agencies_admin_update.php?id=<?php echo $row['id']; ?>'>UPDATE</a></td>
You need to execute the update query in the agencies_admin_update.php file and send id with the url in agencies_validation file
//agencies_validation
<table border="2">
<tr>
<td>title</td>
</tr>
<?php
include_once 'database.php';
echo '<form action= "agencies_admin_update.php" method="get">';
$valid_query = "SELECT * FROM agencies ";
$valid_result = mysqli_query($link, $valid_query);
while ($row = mysqli_fetch_array($valid_result)) {
echo '<tr>';
$id = $row['id'];
echo '<td>'.$row['title'].'</td>';
echo "<td><a href='agencies_admin_update.php?id=".$id."'>UPDATE</a></td>";
echo '<tr>';
}
echo '</form>';
?>
</table>
Execute query in this file
// agencies_validation
<?php
include_once 'database.php';
$id = $_GET['id'];
$query = "UPDATE agencies SET admin=2 WHERE id = $id";
$result = mysqli_query($link, $query);
if($result) {
// Show success message
header("Location: agencies_validation.php");
} else {
// Show error message
header("Location: agencies_validation.php");
}
?>
I have a problem for display data from page to another page.
This is index.php:
<?php
include '../php/connect.php';
$query = mysql_query("SELECT * FROM user
ORDER BY user.id_user DESC") or die(mysql_error());
if(mysql_num_rows($query) == 0){
echo '<tr><td colspan="6">Tidak ada data!</td></tr>';
}else{
while($data = mysql_fetch_assoc($query)){
echo '<tr>';
echo '<td>'.$data['id_user'].'</td>';
echo '<td>'.$data['name'].'</td>';
echo '<td>'.$data['email'].'</td>';
echo '<td>View File</td>';
echo '<td>Konfirmasi</td>';
echo '</tr>';
?>
<?php
}}
?>
This is confirm_pembayaran.php
<?php
include '../php/connect.php';
$query = mysql_query("SELECT * FROM user
WHERE id_user=$id_user") or die(mysql_error());
if(mysql_fetch_array($query) == 0){
echo '<tr><td colspan="6">Tidak ada data!</td></tr>';
}else{
while($data = mysql_fetch_assoc($query)){
echo '<tr>';
echo '<td>'.$data['id_user'].'</td>';
echo '<td>'.$data['name'].'</td>';
echo '<td>'.$data['email'].'</td>';
echo '</tr>';
?>
<?php
}}
?>
The problem is in confirm_pembayaran.php for id_user that i was click from index.php not display in confirm_pembayaran.php. What should i do for confirm_pembayaran.php?
On the index page change the link to something like this ( unless you have set your .htaccess file up to accept links as they were generated )
You should, however, not be using the mysql_ family of functions as they have been deprecated. The code, as it is now is vulnerable to sql injection - I merely posted this to show how to pass the user_id parameter from one page to another which was what ( I think ) you wanted
/* index.php */
Konfirmasi
/* confirm_pembayaran.php */
$user_id=isset( $_GET['user_id'] ) ? filter_input( INPUT_GET,'user_id', FILTER_SANITIZE_STRING ) : false;
$query = mysql_query("SELECT * FROM user
WHERE id_user='{$user_id}'") or die(mysql_error());
First you need to pass your user id in query string correctly like below:
index.php
Changes this line
echo '<td>Konfirmasi</td>';
To
echo '<td>Konfirmasi</td>';
Then you need to change confirm_pembayaran.php
<?php
include '../php/connect.php';
$id_user = $_GET['id_user']; // Add this line for get your user id
$query = mysql_query("SELECT * FROM user
WHERE id_user=$id_user") or die(mysql_error());
if(mysql_fetch_array($query) == 0){
echo '<tr><td colspan="6">Tidak ada data!</td></tr>';
}else{
while($data = mysql_fetch_assoc($query)){
echo '<tr>';
echo '<td>'.$data['id_user'].'</td>';
echo '<td>'.$data['name'].'</td>';
echo '<td>'.$data['email'].'</td>';
echo '</tr>';
?>
<?php
}}
?>
Simplest way is:
In index.php
Konfirmasi
in confirm_pembayaran.php
$id_user = intval($_GET['userId']);
The PHP only works on echoing the content when I echo it directly with the HTML tag (echo is outside the tag), as follows
include('db.php');
$blogurl="http://www.com/view/";
$results = mysql_query("SELECT * FROM product ORDER BY `id` DESC LIMIT 1");
while ($row = mysql_fetch_array($results)) {
echo "<h2>" . $row['title'] . "</h2>";
}
But it doesn't work when I try with this style:
<?php
include('db.php');
$results = mysql_query("SELECT * FROM product ORDER BY `id` ASC");
while ($row = mysql_fetch_array($results)) {
$blogurl="http://www.com/view";
$url=$row['url'];
$title=$row['title'];
?>
<td>
<?php echo $title;?>
</td>
<?php
}
?>
What I want is to change the way I echo the data from the database. But what is wrong with that second style?
I just solved it. I think the problem is because we can't get the content which has an extension of html from database. So the solution is I have to create a string or a var or (I dont know what we call it in php) by as follows:
<?php echo $blogurl;?>/<?php echo $title.".html"?>
The solution is that I don't need the row of url in my database. I just need to echo the title and give the ".html" behind it.
Thanks for anyone who has tried to helped me.
Cheers
Code should be:
<?php
include('db.php');
$results = mysql_query("SELECT * FROM product ORDER BY `id` ASC");
$blogurl="http://www.com/view";
while ($row = mysql_fetch_array($results)) {
$url=$row['url'];
?>
<td><?php echo $title; ?></td>
<?php
}
?>
Try this code
<?php
include('db.php');
$results = mysql_query("SELECT * FROM product ORDER BY `id` ASC");
$blogurl="http://www.com/view";
while ($row = mysql_fetch_array($results)) {
$url=$row['url'];
$title=$row['title'];
?>
<td><?= $title;?></td>
<?php
}
?>
Try this
<?php
include('db.php');
$blogurl="http://www.com/view";
$results = mysql_query("SELECT * FROM product ORDER BY `id` DESC LIMIT 1");
while ($row = mysql_fetch_array($results)) {
echo "<h2><a href='" . $blogurl."/".$row['url'] . "'>" . $row['title'] . "</a></h2>";
}
?>
The following code retrieves and displays the correct data from the database however it gets all the data. I need a way to assign each value it retrieves from the database to a PHP variable. For example, if it gets "Joe", "Henry", and "Robert" from the database, I'd like one variable for each of those and right now it returns and array with all the values.
<?php
dbCon();
$query1 = mysql_query("SELECT * FROM hosts WHERE name!=''");
while($row1 = mysql_fetch_assoc($query1)) {
$res = $row1['name'] . '<br />';
echo $res;
}
?>
<?php
function echoName($id) {
dbCon();
$query1 = mysql_query("SELECT * FROM hosts WHERE id='$id'");
while($row1 = mysql_fetch_assoc($query1)) {
$res = $row1['name'] . '<br />';
echo $res;
}
}
?>
<div id="joe_div">
<?
echoName("1");
?>
</div>
.....
<div id="henry_div">
<?
echoName("2");
?>
</div>
Declare variable variables as such:
while($row1 = mysql_fetch_assoc($query1)) {
$$row1['name'] = $row1['name']
}
echo $Joe; //returns Joe
Though I don't know why you would ever need that
if it gets "Joe", "Henry", and "Robert" from the database, I'd like one variable for each of those
So try this:
<?php
dbCon();
$query1 = mysql_query("SELECT * FROM hosts WHERE name!=''");
while($row1 = mysql_fetch_assoc($query1)) {
$$row1['name'] = $row1['name'];
}
echo $Joe;
echo $Henry;
echo $Robert;
?>
P.S: I don't know why you want to do this, but i am sure you have a better approach to solve your problem.