View data to another page in PHP - 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']);

Related

How can I change the id of an SQL query automatically via PHP?

I'm creating a news website, and when I create a new article, the title/text are registered on the database. The articles are in a dynamic PHP page with header/footer, with the text itself being called from the database . On the index there is a list with links to every article, and I want those links to change the id automatically.
index.php
<?php include "header.php"; session_start();?>
<?php include "slideshow.php"; ?>
<?php include "db.php"; ?>
<br>
<table class="table_index">
<?php
while ($line = mysqli_fetch_array($query)) {
echo '<tr><td class="td_tit"><a href="example_article.php" >'.$line["title_article"].'</a></td></tr>';
echo '<tr><td class="td_subt"><a href="example_article.php" >'.$line["subtitle_article"].'</a></td></tr>';
$_SESSION['id_article'] = $line["id_tb_article"];
}
?>
</table>
<?php include "footer.php"; ?>
example_article.php
<?php
session_start();
include "header.php";
$id_article = $_SESSION['id_article'];
$query = "SELECT title_article, subtitle_article, content_article FROM tb_article WHERE id_tb_article = $id_article";
$conn = mysqli_connect('127.0.0.1:3307', 'root', '', 'article') or die("error");
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "<div class='titlediv'><h1 class='title'>" . $row["title_article"]. "</h1></div><div class='titlediv'><h3 class='title'>". $row["subtitle_article"]. "</h3></div><div class='textdiv'><p class='text'>" . $row["content_article"]. "</p></div><br>";
}
} else {
echo "0 results";
}
include "footer.php";
?>
You shouldn't use session parameters for this task. Instead use get parameters (or post) here is a little example. Also your code is vulnerable to SQL-Injection, but thats your problem :D
Index.php rewrite your while loop like this
while ($line = mysqli_fetch_array($query)) {
echo '<tr><td class="td_tit">'.$line["title_article"].'</td></tr>';
echo '<tr><td class="td_subt">'.$line["subtitle_article"].'</td></tr>';
}
then fetch the id in example_article.php like this:
$id_article = $_GET['id_article'];

Generate title from database and show related post

I have a DB name askadoc , where people can ask about their problem. I want to show all question(or you can say the titles) together in a page. And then when user click on a question, the question will show in a different page with it's comments/details. To do this i have tried the below code and its working perfectly. but how can i do it without using button ?
<?php
$comment = "SELECT * FROM `askadoc` ";
$result = mysqli_query($conn, $comment);
$question = "";
$id="";
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
$question = $row["question"];
?>
<form action="post.php" method="post">
<?php
echo $id;
echo "<input type='hidden' name = 'id' value = ".$id.">";
echo "<button>".$question."</button>";
echo "<br>";
?>
</form>
<?php
}
}
?>
I think you don't need form this. You can user anchor like this.
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
$question = $row["question"];
?>
<?php echo $question ?>
<?php
}
}
Now when you click this anchor, you get question id inside post.php, so easily you can display a particular question comments.
You can use anchor tag then pass the question id to post.php
if(mysqli_num_rows($result)>0){
while($row = mysqli_fetch_assoc($result)) {
$id = $row["id"];
$question = $row["question"];
?>
<?php
echo "<a href='post.php?question_id=".$id."' target='_blank'>".$question."</a>";
echo "<br>";
?>
<?php
}
}
Then to get the value of question_id use $_GET['question_id'];
$questionId = $_GET['question_id']
and its better to check if question_id does exist.
$questionID = isset($_GET['question_id'])? $_GET['question_id'] : '';
if(!empty($questionID)){
//Use $questionID here to query some data from database
}

Echo inside the html tag

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>";
}
?>

PHP MySQL display data by id from database - freedom placement

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>

Delete item from mysql database then go back to last page

I have a page where I list items ( in this case businesses) that are pulled from a mysql database with php.
<?
include('config.php');
echo "<h3>Saved Businesses</h3>";
echo "<ul style='list-style-type:none;'>";
$result = mysql_query("SELECT * FROM `saved_biz` WHERE user_id = '$id'") or trigger_error(mysql_error());
while($row = mysql_fetch_array($result)){
foreach($row AS $key => $value) { $row[$key] = stripslashes($value); }
$business_id = $row['business_id'];
$result = mysql_query("SELECT * FROM `company` WHERE id = '$business_id'") or trigger_error(mysql_error());
while($row = mysql_fetch_array($result)){
$business_name = $row['name'];
}
echo "<li>" . nl2br( $business_name);
echo "<a href=deletesavedbiz.php?id={$row['id']}>Delete</a></li>";
echo "</tr>";
}
echo "</ul>";
?>
If the user decides to delete one of the businesses ( by clicking the "Delete" link ) he/she is then taken to deletesavedbiz.php and then, if successful, presented with a link to get back to the profile.php page that he/she was just on.
<?
include('config.php');
$id = (int) $_GET['id'];
mysql_query("DELETE FROM `saved_biz` WHERE `id` = '$id' ") ;
echo (mysql_affected_rows()) ? "Row deleted.<br /> " : "Nothing deleted.<br /> ";
?>
<a href='profile.php'>Back To Listing</a>
Now, what I want to do is have the php delete and then do like a php header redirect to profile.php without ever making the user click a link back. How can I accomplish this? Also, I'm fine with having the answer being javascript if it's not possible or not very clean in PHP.
Thanks for all help!
Do redirect in case your query returned TRUE.
function redirect($page = 'profile.php'){
header("Location: $page");
exit;
}
function your_query(){
include('config.php');
$id = (int) $_GET['id'];
//returns TRUE on success, FALSE otherwise
return mysql_query("DELETE FROM `saved_biz` WHERE `id` = '$id' ") ;
}
if ( your_query() ){
redirerct();
} else {
redirect('some_error_page.php');
}
header("Location: profile.php");
Put that at the bottom of your script that deletes the record.

Categories