Call PHP function from a button to delete row from a table - php

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

Related

Create tabe from mysql data with hyperlink to point to report page

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

How to display new rows without refresh using AJAX

I'm actually a little bit confused with all these AJAX techniques. The aim is to display the new rows on my site when new rows are inserted into my database, but i dont know how. The following code has been inserted into my Joomla site threw an extension:
<!-- You can place html anywhere within the source tags -->
<script language="javascript" type="text/javascript">
// You can place JavaScript like this
</script>
<?php
$today = date("d/m/Y");
$sql = "SELECT * FROM queue WHERE";
$sql =$sql . " Date>='$today' ORDER BY Qnumber Desc LIMIT 3";
// echo $sql;
$con=mysqli_connect("localhost","root","db","pass");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Αριθμός Εξυπηρέτησης</th>
<th>Πελάτες σε Αναμονή</th>
<th>Ώρα</th>
<th>Ημερομηνία</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Qnumber'] . "</td>";
echo "<td>" . $row['WaitingCustomers'] . "</td>";
echo "<td>" . $row['Time'] . "</td>";
echo "<td>" . $row['Date'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>

MySQL query displays no results in HTML table with PHP

I'm trying to throw together a simple inventory database for a small customer of mine (I normally don't do WebDev stuff) but I'm a little stumped. I have what I think should work, but I get no results in my table. I know the query is good since I get the expected results when querying directly to the database, unless PHP expects different formatting of my SQL statement. here is my page:
<html>
<head>
<title>Inventory</title>
</head>
<body>
<?php
$con=mysqli_connect("localhost","user","pass","db_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = "SELECT
products.name,
products.sku,
inventory.quantityfry,
inventory.quantityjuv,
inventory.quantityadult,
inventory.notes,
inventory.location,
inventory.owner
FROM
products
INNER JOIN
inventory
ON
products.sku=inventory.sku";
$result = mysqli_query($query);
echo "<table border='1'>
<tr>
<th>Species</th>
<th>SKU</th>
<th>Fry Count</th>
<th>Juvie Count</th>
<th>Adult Count</th>
<th>Notes</th>
<th>Location</th>
<th>Owner</th>
</tr>";
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['sku'] . "</td>";
echo "<td>" . $row['quantityfry'] . "</td>";
echo "<td>" . $row['quantityjuv'] . "</td>";
echo "<td>" . $row['quantityadult'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['owner'] . "</td>";
echo "</tr>";
}
mysqli_free_result($result);
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
When I load the page, all I see is my HTML table headers, but no data. No error messages, either. What am I missing?
you don't see error messages because you don't bother checking for them. You're calling mysqli_query incorrectly, and since you don't check for errors, never see them:
$result = mysqli_query($con, $query) or die(mysqli_error($con));
^^^^---required
Since you used it incorrectly, the query call returns false. You then blindly try to fetch result rows from that boolean FALSE, which would lead to further errors and your while() loop never executing at all.

Filtering mysql results via select dropdown

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.

php issue with output of user information

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

Categories