How to make a button in PHP that deletes the result? - php

I'm a novice at this and I just lately dug into the code... I'm having hard time understanding why this doesn't work:
I added a button in the search results so it would delete the certain data field but it doesn't do it.
What am I missing out here?
<?php //deleting
isset($_GET['action']) ? $action=$_GET['action'] : $action="";
if($action=='delete'){
$id=$_REQUEST['id'];
$query=mysql_query("set names 'utf8'");
$query = mysql_query("DELETE FROM norse5_proov WHERE id='$id'") or die(mysql_error());
if($query){
echo "<div id='dain'><br><br><br><br><br><br><br><br><br><br>Andmed kustutatud.</div>";
}
}
?>
<?php //searching
mysql_query ("set character_set_results='utf8'");
if($_SERVER['REQUEST_METHOD'] == "POST"){
$query = "SELECT * FROM norse5_proov WHERE 1=1 ";
if(!empty($_POST["osakond"])){
$query .= " AND osakond LIKE '%".mysql_real_escape_string($_POST["osakond"])."%'";
}
if(!empty($_POST["soetusaasta"])){
$query .= " AND soetusaasta LIKE '%".mysql_real_escape_string($_POST["soetusaasta"])."%'";
}
if(!empty($_POST["it_number"])){
$query .= " AND it_number LIKE '%".mysql_real_escape_string($_POST["it_number"])."%'";
}
if(!empty($_POST["tooteruhm"])){
$query .= " AND tooteruhm LIKE '%".mysql_real_escape_string($_POST["tooteruhm"])."%'";
}
if(!empty($_POST["mudeli_nimetus"])){
$query .= " AND mudeli_nimetus LIKE '%".mysql_real_escape_string($_POST["mudeli_nimetus"])."%'";
}
if(!empty($_POST["sn"])){
$query .= " AND sn LIKE '%".mysql_real_escape_string($_POST["sn"])."%'";
}
if(!empty($_POST["riigivara_nr"])){
$query .= " AND riigivara_nr LIKE '%".mysql_real_escape_string($_POST["riigivara_nr"])."%'";
}
if(!empty($_POST["inventaari_nr"])){
$query .= " AND inventaari_nr LIKE '%".mysql_real_escape_string($_POST["inventaari_nr"])."%'";
}
if(!empty($_POST["maja"])){
$query .= " AND maja LIKE '%".mysql_real_escape_string($_POST["maja"])."%'";
}
if(!empty($_POST["ruum"])){
$query .= " AND ruum LIKE '%".mysql_real_escape_string($_POST["ruum"])."%'";
}
if(!empty($_POST["vastutaja"])){
$query .= " AND vastutaja LIKE '%".mysql_real_escape_string($_POST["vastutaja"])."%'";
}
if(!empty($_POST["markus"])){
$query .= " AND markus LIKE '%".mysql_real_escape_string($_POST["markus"])."%'";
}
if(!empty($_POST["id"])){
$query .= " AND id LIKE '%".mysql_real_escape_string($_POST["id"])."%'";
}
?>
<table id="tablk">
<tr>
<th width="80">Osakond</th>
<th width="80">Soetusaasta</th>
<th width="80">IT Number</th>
<th width="80">Tooterühm</th>
<th width="80">Mudeli nimetus</th>
<th width="80">SN</th>
<th width="80">Riigivara nr</th>
<th width="80">Inventaari nr</th>
<th width="80">Maja</th>
<th width="80">Ruum</th>
<th width="80">Vastutaja</th>
<th width="80">Märkus</th>
<th width="80">ID</th>
<th width="80">Tegevus</th>
</tr>
<?php
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
echo "<tr>";
echo "<td>".$row["osakond"]."</td>";
echo "<td>".$row["soetusaasta"]."</td>";
echo "<td>".$row["it_number"]."</td>";
echo "<td>".$row["tooteruhm"]."</td>";
echo "<td>".$row["mudeli_nimetus"]."</td>";
echo "<td>".$row["sn"]."</td>";
echo "<td>".$row["riigivara_nr"]."</td>";
echo "<td>".$row["inventaari_nr"]."</td>";
echo "<td>".$row["maja"]."</td>";
echo "<td>".$row["ruum"]."</td>";
echo "<td>".$row["vastutaja"]."</td>";
echo "<td>".$row["markus"]."</td>";
echo "<td>".$row["id"]."</td>";
echo "<td>";
echo "<button onclick='delete_user( {id} )'>Kustuta</button>";
echo "<form method='post' action='edit.php?id=<?php echo ".$row["id"]."; ?>'><input type='submit' value='Muuda'>";
echo "</td>";
echo "</tr>";
}
mysql_free_result($result);
?>
</table>
<?php
}
?>

This line:
echo "<button onclick='delete_user( {id} )'>Kustuta</button>";
creates a button and calls delete_user function, but it is not PHP function but Javascript. You probably did not define this function. Secondly the problem might be in {id}, perhaps you should have written delete_user(".$row["id"].").
I suppose you have to create a button that will submit form that is for deletion. You may want to have two forms, one that is for editing (you have it in the next line), the other for deletion.
Instead of the line I cited use:
echo "<form method='post' action='edit.php?action=delete&id=".$row["id"]."'><input type='submit' value='Muuda'>";

echo "<form method='post' action='edit.php?id=<?php echo ".$row["id"]."; ?>
The above line is opening a PHP tag within a PHP string.

try
echo "<button onclick='delete_user(".$row[id].")'>Kustuta</button>";
echo "<form method='post' action='edit.php?action=delete&id=<?php echo ".$row["id"]."; ?>'><input type='submit' value='Muuda'>";

Related

How to Disable Specific Button rows in php

I want to disable the two buttons in a single row if the button will be clicked and won't affect the buttons of another row.
I dont know how to disable an echo button of the table. I want to disable the "Accept" and "Reject" button if one of those was been clicked.I provide a screenshot so that you can easily understand what I mean. Thank you in advance.
Here's my php code. It names as app.php
<?php
//connect to database
$con = mysqli_connect('127.0.0.1','root','');
//select database
mysqli_select_db($con, 'appointment');
//select query
$sql = "SELECT * FROM service";
//Execute the query
$records = mysqli_query($con,$sql)
?>
<html>
<head>
<title>Appointment Schedule</title>
</head>
<body>
<table width = "100%" border = "5px" height = "20%">
<tr align = "left">
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
<th>Address</th>
<th>Date</th>
<th>Time</th>
<th>Office</th>
<th>Service</th>
<th>Contact No.</th>
<th>Remarks</th>
</tr>
<?php
while($row = mysqli_fetch_array($records))
{
echo "<tr><form action = 'display.php' method = post>";
echo "<input type=hidden name=id value='".$row['ID']."'>";
echo "<td>".$row['fname']."</td>";
echo "<td>".$row['mname']."</td>";
echo "<td>".$row['lname']."</td>";
echo "<td>".$row['address']."</td>";
echo "<td>".$row['date']."</td>";
echo "<td>".$row['time']."</td>";
echo "<td>".$row['office']."</td>";
echo "<td>".$row['services']."</td>";
echo "<td><name = number>".$row['contactno']."</td>";
echo "<td>".$row['remarks']."</td>";
echo "<td><input type =submit value='Accepted' name=accept>";
echo "<td><input type =submit value='Rejected' name=reject>";
echo "</form></tr>";
}
?>
</table>
</body>
</html>
here's my another one php code. It names display.php
<?php
//connect to database
$con = mysqli_connect('127.0.0.1','root','');
//select database
mysqli_select_db($con, 'appointment');
if($_POST['accept'])
{
$sql = "UPDATE service SET remarks = 'Accepted' WHERE ID=$_POST[id]";
}
else if($_POST['reject'])
{
$sql = "UPDATE service SET remarks = 'Rejected' WHERE ID=$_POST[id]";
}
//Execute Query
if(mysqli_query($con,$sql))
header("refresh:1; url=app.php");
else
echo "Unsuccessful";
?>
here's the screenshot of my work
Sample of my database table using php
Kindly try the below code:
Provide the condition to display the buttuon
echo "<td><input type =submit value='Accepted' name=accept>";
echo "<td><input type =submit value='Rejected' name=reject>";
if($row['remarks'] == 'Accepted' || $row['remarks'] == 'Rejected')
{
echo "<td><input type =submit disabled value='Accepted' name=accept>";
echo "<td><input type =submit disabled value='Rejected' name=reject>";
}

How can get the value of the selected cell from a table created by php?

After I printed those values by using PHP, I got confused how to get the value of a selected cell.
<div>
<php
$query = mysql_query(select * from items, $conn);
echo "<table border='1'>
<th width ='120'>itemID</th>
<th width ='120'>itemName</th>";
while(row = mysql_fetch_array($query))
{
echo "<tr>";
echo "<td>".$row['itemID']."</td>";
echo "<td>".$row['itemName']."</td>";
}
echo "</table>";
?>
</div>
seems you doing fine, iv'e just edited some typos, added tags,and changed to: "mysql_fetch_assoc" function. try this:
<div>
<?php
$query = mysql_query("select * from items", $conn);
echo "<table border='1'>
<tr>
<th width ='120'>itemID</th>
<th width ='120'>itemName</th>
</tr>";
while($row = mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>".$row['itemID']."</td>";
echo "<td>".$row['itemName']."</td>";
echo "</tr>";
}
echo "</table>";
?>
</div>

Pass a dynamic variable through URL php

I'm not sure about the title, I tried my best.
I have a table displayed with information from a database using this file
display.php
<?php
mysql_connect("localhost", "root", "root") or die(mysql_error());
mysql_select_db("tournaments") or die(mysql_error());
$result = mysql_query("SELECT * FROM tournies")
or die(mysql_error());
echo '<table id="bets" class="tablesorter" cellspacing="0" summary="Datapass">
<thead>
<tr>
<th>Tournament <br> Name</th>
<th>Pot</th>
<th>Maximum <br> Players</th>
<th>Minimum <br> Players</th>
<th>Host</th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>';
while($row = mysql_fetch_array( $result )) {
$i=0; if( $i % 2 == 0 ) {
$class = "";
} else {
$class = "";
}
echo "<tr" . $class . "><td>";
echo $row['tour_name'];
$tour_id = $row['tour_name'];
echo "</td><td>";
echo $row['pot']," Tokens";
echo "</td><td class=\"BR\">";
echo $row['max_players']," Players";
echo "</td><td class=\"BR\">";
echo $row['min_players']," Players";
echo "</td><td class=\"BR\">";
echo $row['host'];
echo "</td><td>";
echo "<input id=\"delete_button\" type=\"button\" value=\"Delete Row\" onClick=\"SomeDeleteRowFunction(this)\">";
echo "</td><td>";
echo "<form action=\"join.php?name=$name\" method=\"POST\" >";
echo "<input id=\"join_button\" type=\"submit\" value=\"Join\">";
echo "</td></tr>";
}
echo "</tbody></table>";
?>
Basically I want the user to press a button from a row of the table and they go to a new page called join.php. I need the persons username and the name of the tournament from the row the clicked.
For example here's my page:
When they click the join button at the end of row one it should send them to
'join.php?name=thierusernamehere&tourname=dfgdds'
Any help much appreciated. Thanks.
echo '<td>Join</td>'
There are many way to approach.
The easiest way is just echo 'JOIN';
or you can use a form with hidden input and submit button.
BUT
Your code is really a mess, try to make your code more maintainable and readable. And do NOT use any mysql_* functions, they are deprecated.
Read more about PDO:
http://php.net/manual/en/book.pdo.php
http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/

how to use get method in a session?

here is my code. actually i am displaying some data from mysql on the page and creating dynamic link.i want started a session with session_start() in the very begining of code before starting any code. i want to store the value of the link that is to be display on other pagepage..
page1.php
<a style="color:#F00; font-family:Arial, Helvetica, sans-serif; margin-left:33px; font-weight:bold">
No. of registered students:
</a>
<table border='1' align="center" style="font-size:14px" width="95%" cellspacing="3" class="db_table">
<tr class="db_table_tr" >
<th class="db_table_th" name="submit">USN</th>
</tr>
<?php
include('includes/login_connection.php');
$query = "select p.usn, p.name from personal_details p, course_codes c where p.usn = c.usn order by p.usn";
$run = mysql_query($query) or die($query."<br/><br/>".mysql_error());
$num = mysql_numrows($run);
echo $num;
while($row = mysql_fetch_assoc($run)){
echo "<tr>";
echo "<td>" . $row['usn'] . "" . "</td>";
echo "<td>" . $row['name'] . " </td>";
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
}
}
echo "</tr>";
mysql_close($bd);
?>
</table>
page2.php
<?php
session_start();
if(isset($_SESSION['session_usn']))
{
$_POST['usn'] = $_SESSION['session_usn'];
echo $_POST['usn'];
}
?>
You need to provide a fall-back, in case the URL provided does not contain the proper variables in the $_GET section.
You have:
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
}
You should do something else if $_GET['submit'] isn't set:
if(isset($_GET['submit'])){
$_SESSION['session_usn'] = $_GET['usn'];
} else {
$_SESSION['session_usn'] = "unset";
// or set a warning flag like "unset"
}
You should be feeding your php file a url like:
http://yoururl.com/page1.php?usn='333'
Where 333 is the value you want to store.

Ajax responsetext as a table output?

I'm not really familiar with Ajax Response - I have edited the PHP Ajax Search code from W3schools.com as follows :
<?php
require_once('connect_db.php');
$query = "select item_no from items";
$result = mysql_query($query);
$a = array();
while ($row = mysql_fetch_assoc($result)){
$a[] = $row['item_no'];
}
//get the q parameter from URL
$q=$_GET["q"];
//lookup all hints from array if length of q>0
if (strlen($q) > 0)
{
$hint="";
for($i=0; $i<count($a); $i++)
{
if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))
{
if ($hint=="")
{
$hint=$a[$i];
}
else
{
$hint=$hint." , ".$a[$i];
}
}
}
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint == "")
{
$response="No Suggestion";
}
else
{
$response=$hint;
}
//output the response
echo "<table border=1><tr><td>".$response."</td></tr></table>";
?>
The output of the above code works perfectly, but they are all listed like this (2L500BU , 2L500GO , 2L500NA , 2L500RD , 2L802CA , 2L802WH , 2L803GR , 2L804BE , 2L804BK , 2L804CO , 2L805BU , 2L806BE , 2L806GR ) - Those numbers are Item No in mysql table called items.
Now :
1) I want to output the response into table with <tr> for each like this
2l500BU
2L500GO
.
.
.
.
etc.
2) Do you think its possible to output all table records from Mysql based on the hint entered as follows :
$sql="SELECT * FROM items WHERE item_no = '".**$hint**."'";
$result = mysql_query($sql);
echo "<table align='center' cellpadding='3' cellspacing='3' width='800px' border='1' font style='font-family:arial;'>";
echo "
<tr align=center>
<th style=font-size:18px; bgcolor=#20c500>Item Number</th>
<th style=font-size:18px; bgcolor=#20c500>QTY</th>
<th style=font-size:18px; bgcolor=#20c500>Actual Price</th>
<th style=font-size:18px; bgcolor=#20c500>Selling Price</th>
<th style=font-size:18px; bgcolor=#20c500>Difference</th>
<th style=font-size:18px; bgcolor=#20c500>Date</th>
</tr>";
while($row = mysql_fetch_assoc($result)){
echo "<tr align=center bgcolor=#e3e3e3>";
echo "<td style='font-size:18px; font-weight:bold;'>" . strtoupper($row['item_no']) . "</td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['qty'] . "</td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['actual_price'] . " <font style=font-size:12px;>JD</font></td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['discount_price'] . " <font style=font-size:12px;>JD</font></td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . $row['difference_price'] . " <font style=font-size:12px;>JD</font></td>";
echo "<td style='font-size:18px; font-weight:bold;'>" . date("d-m-Y",strtotime($row['date'])) . "</td>";
echo "</tr>";
}
echo "<table>";
If you're wanting to fetch items from a database and display a row for each of them, this is how you'd do it with jQuery.
Your PHP script:
<?php
$mysqli = new mysqli('localhost', 'user', 'password', 'database');
$sql = "SELECT item_no FROM items";
$res = $mysqli->query($sql);
while ($row = $res->fetch_assoc()) {
$rows[] = $row['item_no'];
}
header('Content-Type: application/json');
echo json_encode($rows);
?>
And your HTML with the table:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<table>
<thead>
<tr>
<th scope="col">Item No.</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
</body>
</html>
<script src="js/lib/jquery.js"></script>
<script>
$(document).ready(function() {
$.getJSON('yourscript.php', function(items) {
$.each(items, function(i, item) {
$('tbody').append('<tr><td>' + item + '</td></tr>);
});
});
});
</script>
I've also used MySQLi (MySQL improved) rather than the standard mysql_ functions, as the mysql_ library is deprecated and you should be using either MySQLi or PDO now.

Categories