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
}
Related
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']);
I need a table that have condition that the fullyy = $ddate how will I get the value of the ddate textbox and put it to the variable $ddate I have. I need this code in the same page:
//php
$ddate = "";
if (isset($_POST['ddate']))
{ $ddate = $_POST['ddate'];
$result = mysql_query("SELECT * FROM tblnpatient WHERE fullyy = '$ddate'");
while($row = mysql_fetch_array($result)){
echo '<tr>';
echo '<td>'.$row['pnum'].'</td>';
echo '<td>'.$row['pname'].'</td>';
echo '<td>'.$row['addr'].'</td>';
}}
This is my html, the value of ddate is from a script:
Diff:<input name="ddate" id="ddate" />
There is no result in my page, what is wrong in my code?
You have to use a form. It should look something like this:
<?php
$ddate = "";
if (isset($_POST['ddate'])){
$ddate = $_POST['ddate'];
}
?>
and you should use mysqli_ instead of mysql_:
<?php
$results = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($results)){
echo "<br/>";
echo $row["name"];
}
mysqli_free_result($result);
?>
and your form should look something like this:
<form action="samefile.php" method="post">
Diff:<input type="text" name="ddate" id="ddate" onchange"this.form.submit()"/>
</form>
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>
The users of my website can subscribe some of their animals for a competition.
My code works perfectly but there is a big issue. When a user presses subscribe the page reloads but because the isset is after the echo of the button itself the page needs to be refreshed before the buttontext changes into "subscribed".
Change the order gives issues as you probably can see.
Who can help me out? I'm out of options myself. (I translated the variables etc.)
<form action="" method="post" name="frmSubscribe">
<?php
$Counter = 0;
$sql = "Select * from Animals where username='".$_SESSION['User']."' ";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result)){
echo $row['Animalname'];
$Duiven[] = $row['AnimalID'];
$Username[] = $row['username'];
?>
<input type='submit' <?php echo "name= ".$Buttons[$Counter].""; ?> value='<?php
$sqlSubscribed = "SELECT * FROM Competitionresults WHERE AnimalID='".$Animals[$Counter]."'";
$resultSubscribed = mysql_query($sqlSubscribed);
if(mysql_num_rows($resultSubscribed) == 0){ echo "Subscribe";}
else {echo "deregister";}
?>'><br/><?php
$Teller++;
}
if (isset($_POST['btnSubscribe1'])){
$sqlCheck = "SELECT * FROM Competitionresults WHERE AnimalID='".$Animals[0]."'";
$resultCheck = mysql_query($sqlCheck);
if (mysql_num_rows($resultCheck) == 0){
$sql1 = "INSERT INTO Competitionresuls (AnimalID, username) VALUES ('".$Animals[0]."','".$Username[0]."')";
$result1 = mysql_query($sql1);
$row=mysql_fetch_array($result1);
?><?php
}
}
... and so on for the next animals.
You have to use output buffering I think. Put ob_start() at the beginning of your page, now output any placeholder instead of real input code:
[SUMBIT]
instead of:
<input type='submit' ....
At the end of page you get your buffered content:
$content = ob_get_clean()
And replace submit button with correct code:
$content = str_replace('[SUBMIT]', 'Your actual submit button code here...', $content);
And now output content:
echo $content;
quick fix can be javascript
echo '<span id="someid" >Subscribe</span>';
and when changing
echo "<script>document.getElementById('someid').innerHTML = 'Subscribed';</script>";
in future I STRONGLY suggest you to use PHP MVC Framework
You can use your post code above the page then loaded your page....
Please use like below code:
<?php
if (isset($_POST['btnSubscribe1'])){
$sqlCheck = "SELECT * FROM Competitionresults WHERE AnimalID='".$Animals[0]."'";
$resultCheck = mysql_query($sqlCheck);
if (mysql_num_rows($resultCheck) == 0){
$sql1 = "INSERT INTO Competitionresuls (AnimalID, username) VALUES ('".$Animals[0]."','".$Username[0]."')";
mysql_query($sql1);
header('Location: samefilename.php');
die;
}
}
?>
<form action="" method="post" name="frmSubscribe">
<?php
$Counter = 0;
$sql = "Select * from Animals where username='".$_SESSION['User']."' ";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result)){
echo $row['Animalname'];
$Duiven[] = $row['AnimalID'];
$Username[] = $row['username'];
?>
<input type='submit' <?php echo "name= ".$Buttons[$Counter].""; ?> value='<?php
$sqlSubscribed = "SELECT * FROM Competitionresults WHERE AnimalID='".$Animals[$Counter]."'";
$resultSubscribed = mysql_query($sqlSubscribed);
if(mysql_num_rows($resultSubscribed) == 0){ echo "Subscribe";}
else {echo "deregister";}
?>'><br/><?php
$Teller++;
}
}
most easy solution move the isset up to the top. just so you know actions should always be the first things you handle after that you are gonna work on the view
<form action="" method="post" name="frmSubscribe">
<?php
if (isset($_POST['btnSubscribe1'])){
$sqlCheck = "SELECT * FROM Competitionresults WHERE AnimalID='".key($_GET)."'";
$resultCheck = mysql_query($sqlCheck);
if (mysql_num_rows($resultCheck) == 0){
$sql1 = "INSERT INTO Competitionresuls (AnimalID, username) VALUES ('".key($_GET)."','".$_SESSION['User']."')";
$result1 = mysql_query($sql1);
$row=mysql_fetch_array($result1);
}
}
$Counter = 0;
$sql = "Select * from Animals where username='".$_SESSION['User']."' ";
$result = mysql_query($sql);
while($row=mysql_fetch_array($result)){
echo $row['Animalname'];
$Duiven[] = $row['AnimalID'];
$Username[] = $row['username'];
?>
<input type='submit' <?php echo "name= ".$Buttons[$Counter].""; ?> value='<?php
$sqlSubscribed = "SELECT * FROM Competitionresults WHERE AnimalID='".$Animals[$Counter]."'";
$resultSubscribed = mysql_query($sqlSubscribed);
if(mysql_num_rows($resultSubscribed) == 0){ echo "Subscribe";}
else {echo "deregister";}
?>'><br/><?php
$Teller++;
}
I have retrieved the database details from a database to a
php page. i have actually retrieved a specific column of a query.
but i am not able to add the radio buttons to the retrieved values.
Following is my coding:
<?php
$query = "SELECT url FROM measurementurl";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{
$url = $row[0];
echo "url :$url <br>" ;
}
?>
Try this:
<form action="">
<?php
$query = "SELECT url FROM measurementurl";
$result = mysql_query($query);
while($row = mysql_fetch_row($result))
{
//$url = $row[0]; removed cause not used in code
echo "<input type=\"radio\" name=\"url\" value=\"$row[0]\" />$row[0]<br />";
}
?>
</form>