i have this specific question to do to you,
i have a database from which i extract the information and my output is the name of the user and the photo of the user.
What i need to do is that when i click the user name i am redirected to another php file which which holds all information for this particular user.
The user is extracted like this:
$sql =" select * from hostess";
$query = mysql_query($sql);
echo "<table border='1'>
<tr>
<th>FIRSTNAME</th>
<th>PHOTO</th>
</tr>";
while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['first_name_en'] . "</td>";
"<td> <img src=upproject/foto/photo1".$row['photo'] . "
></td>";
"</tr>";
}
echo"</table>";
You could change this line
echo "<td>" . $row['first_name_en'] . "</td>";
with one like this
echo '<td><a href="yournewpage.php?userid='.$row['userid'].'">'.
$row['first_name_en']."</a></td>";
where userid is database-associated user id.
Naturally in the page you are redirected you need to get $_GET['userid'] and retrieve database data associated to this.
Be careful: sanitize user input, always, to avoid sql-injection!
try something like:
while ($row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td> <a href='infopage.php?id=".$row['id']."'>" . $row['first_name_en'] . "</a></td>";
echo "<td> <img src=upproject/foto/photo1".$row['photo'] . "></td>";
echo "</tr>";
}
Add a link/href around the photo that the next php (or the same one with some conditional checking) page can use to display information using an ID to query the database like this:
while ($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td><a href='somephpfile.php?ID=".$row['ID']."'>" . $row['first_name_en'] . "</a></td>";
"<td><img src=upproject/foto/photo1".$row['photo'] . "></td>";
"</tr>";
}
echo"</table>";
Related
Ok i am playing around with this idea for a system i am working on. I want to display data from mysql into a table. I can do this easy. What i would like to do is this. For the ID i would like to make it a link where i pass the id to the next page.
<?php
$con=mysqli_connect("example.com","username","password","my_db");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM Persons");
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Address</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
What i want to do is in the (echo "" . $row['ID'] . "";) I would like to convert it to a link so it would be something like this: example.com/report.php=24 or something like that. What i need to do is find out how to pass the ID and also how to convert the echo to a hyperlink.
Also on the report page it will display all data for id=24 or what ever the id is into a form that i have setup.
Can anyone help me with this.
Try This
echo '
<td>'.$row["ID"].' </td>';
To make a clickable link you use an anchor tag <a></a> In the href attribute you add the form name report.php and follow that with a ? to seperate that from the parameter list. You then add name=value pairs after the ? for the data you wish to pass eg href="report.php?id=24"
So to make the first page contain a clickable link you do something like this
echo '<td><?php echo $row['ID']; ?></td>';
Now in the report.php script you access the passed data by looking at the $_GET array in PHP
<?php
// check incoming params exist
if ( ! isset($_GET['id'] ) {
// missing param, go to an error page for example
header('Location: error.php');
exit;
}
// You can now use $_GET['id'] which will be the id number passed
// any way you want.
// For example using a PDO connection called $pdo
$table = "table1";
$sql = "SELECT * FROM $table WHERE id = :id";
try {
$stmt = $pdo->prepare($sql);
$stmt->bindParam(':id', $_GET['id'], PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->FetchAll(); // $rows now contains all the results of the query
}
catch( PDOException $e) {
echo $e-getMessage();
}
foreach ( $rows as $row ) {
// do things with the $row['column_name'] data
}
I always prefer using direct HTML for something like this.
<td>Link Name</td>
Try this -
while($row = mysqli_fetch_array($result))
{
$link = "example.com/report.php?id=".$row['ID'];
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Address'] . "</td>";
echo "</tr>";
}
echo "</table>";
I have this code which takes a user submitted USERNAME, their username is written to a SQL database and is echoed on the index. It works, but I can't figure out how to concatenate the username into a anchor tag.
I have this code so far:
<?php $result = mysqli_query($con, "SELECT * FROM Tumblr");
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . "www." . $row['BlogName'] . ".tumblr.com" . "</td>";
echo "<td>" . $row['Promo'] . "</td>";
echo "</tr>";
}
?>
I want it to echo in the format of:
example.tumblr.com
With an anchor tag of:
http://username.tumblr.com
The username subdomain is dynamic, of course.
You just need to create the relevant markup, which in your case is the anchor tag itself!
<?php $result = mysqli_query($con, "SELECT * FROM Tumblr");
while ($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><a href='http://www.{$row['BlogName']}.tumblr.com' target='_blank'>" . $row['BlogName'] . ".tumblr.com" . "</a></td>";
echo "<td>" . $row['Promo'] . "</td>";
echo "</tr>";
}
?>
Try this
echo '<td>http://www.'.$row['BlogName'].'.tumblr.com</td>';
I would like to know how I can use the GET-method in my .php file: forumdocument.php to get the ID of the href that is clicked in table.php
I hope this is the right way to begin it:
'<a class="formtitellink" href="forumdocument.php" id="$row['ID']">' . $row['Titel'] . '</a>' . "</td>";
table.php:
$query = "Select *
from forum";
$resultaat = mysql_query($query, $connectie);
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Tijd</th>
<th>Gebruikersnaam</th>
<th>Titel</th>
</tr>";
while($row = mysql_fetch_array($resultaat))
{
$_SESSION['row'] = $row;
echo "<tr>";
echo "<td>" . $row['Tijd'] . "</td>";
echo "<td>" . $row['Gebruikersnaam'] . "</td>";
echo "<td>" . '<a class="formtitellink" href="forumdocument.php" id="$row['ID']">' . $row['Titel'] . '</a>' . "</td>";
echo "</tr>";
}
echo "</table>";
A GET method is used for name value pairs in a url.
Example: www.test.com?var1=var1&var2=var2
Here if we use $_GET['var1'] the expected value would be var1.
Just change your links to reflect the desired variable
href="forumdocument.php?rowid=".$row['ID']."
Then you can use retrieve the id using something like this on forumdocument.php
$rowid = $_GET['rowid'];
Change this part
a class="formtitellink" href="forumdocument.php" id="$row['ID']"
a class="formtitellink" href="forumdocument.php"?id="'.$row['ID'].'"
I have the following code inside PHP page that displays data from a table. In the last column I have a delete button that calls a function to delete the corresponding row from the table:
// Print data from db
$result = mysql_query("SELECT * FROM questions");
echo "<table border='1' align='center'>
<tr>
<th>ID</th>
<th>Multiple Choice Question</th>
<th>Correct answer</th>
<th>The Tip You Give</th>
<th>Edit Question</th>
<th>Delete Question</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td align='center'>" . $row['ID'] . "</td>";
echo "<td>" . $row['Question'] . "</td>";
echo "<td align='center'>" . $row['Answer'] . "</td>";
echo "<td>" . $row['Help'] . "</td>";
echo "<td align='center'><button type='button' onclick='edit_question()'><img src='images/Edit.png'></button></td>";
echo "<td align='center'><button type='button' onclick='delete_question($row)'><img src='images/delete.png'></button></td>";
echo "</tr>";
}
echo "</table>";
the function is the following:
<script>
function delete_question(int $row_id)
{
var r=confirm("Are you sure you want to delete this question?");
if (r==true)
{
alert('<?php
$con=mysql_connect("localhost","stesia","stesia","stesia");
// Check connection
if (mysql_errno())
{
echo "Failed to connect to MySQL: " . mysql_error();
}
mysql_query($con,"DELETE FROM questions WHERE ID='".$row_id."'");
echo "You want to delete the " . $row_id['ID'] . " question!";
echo "Question deleted!";
mysql_close($con);
?>');
}
else
{
alert("Question not deleted!");
}
}
</script>
The problem is that the function is called and displays the messages, but the row is not deleted (checked that in mysql also). I have tried some things but no luck. What am I doing wrong here?
You cannot do this on client side. You should send an AJAX request to the server (PHP-) side to handle the deletion.
You can find related information here (though these are not showing the best practices, but it helps understanding the concepts):
PHP - AJAX and MySQL
As far as i know onclick only executes JavaScript Code. You could try to put your PHP function into a JavaScript function and execute this from the onclick tag
I'm a new member of StackOverflow, and although I've been using the website for a long time, it's my first time posting a question, in a hope that someone will be able to help me. I'll start by saying that my knowledge of PHP and MySQL is basic, but what I'm trying to do isn't too complex in my opinion, so hopefully I won't be asking for much. I've done a lot of prior research, but I just couldn't find the right answer.
In short, this is what I'm trying to do:
I've got an html form, which upon submission writes data to a database, and then publishes a table on a separate html page. With each successful submission a new table gets generated and published, while the old one gets pushed underneath. This all works fine, and I've also implemented pagination so that only 5 tables are visible per page.
What I'd like to be able to do is allow people to ONLY view/display results (tables) based on a specific criteria, in this case "rating", by selecting a rating from a drop-down on the page where tables are published. Rating is one of the fields in my form which gets submitted to a database and then published in one of the rows in a table.
Below is the code which publishes tables. Thanks in advance for your help!
<?php
include('dbconnect.php');
mysql_select_db("vtracker", $con);
$result = mysql_query("SELECT * FROM userdata");
$age = "Age:";
$rating = "Rating:";
$country = "From:";
$name = "Name:";
while($row = mysql_fetch_array($result))
{
echo "<table id='mft_table' cellspacing='0'>";
echo "<tbody>";
echo "<tr>";
echo "<td class='row1'>" .$name . " " . $row['personsname'] . "</td>";
echo "<td rowspan='4'>";
echo "<div class='mft_column'>" . $row['mft'] . "</div>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='row2'>" . $country . " " . $row['nationality'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='row3'>" . $age . " " . $row['personsage'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='row4'>" . $rating . " " . $row['rating'] . "</td>";
echo "</tr>";
echo "</tbody>";
echo "<br>";
echo "</table>";
}
?>
for both true and false use can add thid in your code:
if($_POST['rating_dropdown']!='')
{
$temp_rating = $_POST['rating_dropdown'];
$query=mysql_query("SELECT * FROM userdata WHERE rating = '$temp_rating'");
}
else
{
$query=mysql_query("SELECT * FROM userdata");
}
Dunno if this works, it's just a hinch. haha.
It will see if the rating is true(not null), if it's true it will echo the results.
while($row = mysql_fetch_array($result))
{
if ($rating)
echo "<table id='mft_table' cellspacing='0'>";
echo "<tbody>";
echo "<tr>";
echo "<td class='row1'>" .$name . " " . $row['personsname'] . "</td>";
echo "<td rowspan='4'>";
echo "<div class='mft_column'>" . $row['mft'] . "</div>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='row2'>" . $country . " " . $row['nationality'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='row3'>" . $age . " " . $row['personsage'] . "</td>";
echo "</tr>";
echo "<tr>";
echo "<td class='row4'>" . $rating . " " . $row['rating'] . "</td>";
echo "</tr>";
echo "</tbody>";
echo "<br>";
echo "</table>";
}
}
Once the dropdown gets selected and posted to your display page, use this code:
$temp_rating = $_POST['rating_dropdown'];
mysql_query("SELECT * FROM userdata WHERE rating = '$temp_rating'");
Keep in mind, however, that you should be using PDO or mysqli extension, not the mysql extension. According to PHP's website:
This extension is deprecated as of PHP 5.5.0, and will be removed in
the future. Instead, the MySQLi or PDO_MySQL extension should be used.
See also MySQL: choosing an API guide and related FAQ for more
information.