Pass a dynamic variable through URL php - 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/

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 to echo html and row from database

I have a script written to grab a row from my database based on current session user, which outputs the row correctly, however I want to insert a small image to be displayed alongside of the echo'd row, and cannot figure out the proper syntax.
if ($row['lifetime']!="")
echo "<div style ='font:12px Arial;color:#2F6054'> Lifetime Member: </div> ".$row['lifetime'];
else
echo '';
?>
basically I want the image to appear right before or after the .$row appears, either or.
You can try:
<?php
if ($row['lifetime'] !== "") {
echo "<div style ='font:12px Arial;color:#2F6054'> Lifetime Member: </div>";
echo $row['lifetime'];
echo "<img src='' alt='' style='width:100px'/>";
}
?>
Just put the HTML for the image into the string you're echoing:
echo "<div style ='font:12px Arial;color:#2F6054'><img src="fill in URL here"> Lifetime Member: </div> ".$row['lifetime'];
You can try as below example
HTML
<table>
<thead>
<tr>
<th>No.</th>
<th>Customer Name</th>
<th>Photo</th>
<th ></th>
</tr>
</thead>
<tbody>
<?php
$tst=0;
$result = mysql_query("select * from acc_cust");
while($row = mysql_fetch_array($result))
{
echo "<tr class='odd gradeX'>";
echo "<td width=5%'>" . $row['ent_no']. "</td>";
echo "<td>" . $row['cust_name']. "</td>";
echo "<td><img src='[path]" . $row['cust_img'] . "' /></td>";
}
?>
</tbody>
</table>

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.

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

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

PHP and MySQL echoing out a Table

I've done this before, and it worked. I am trying to echo out specific rows on my database in a table. Here is my code:
<?php
$connect = mysql_connect("localhost", "xxx", "xxx") or
die ("Hey loser, check your server connection.");
mysql_select_db("xxx");
$quey1="select * from `Ad Requests`";
$result=mysql_query($quey1) or die(mysql_error());
?>
<table border=1 style="background-color:#F0F8FF;" >
<caption><EM>Student Record</EM></caption>
<tr>
<th>Student ID</th>
<th>Student Name</th>
<th>Class</th>
</tr>
<?php
while($row=mysql_fetch_array($result)){
echo "</td><td>";
echo $row['id'];
echo "</td><td>";
echo $row['twitter'];
echo "</td><td>";
echo $row['why'];
echo "</td></tr>";
}
echo "</table>";
?>
It gives me no errors, but It just shows a blank table with none of these rows.
My Question: How come this wont show any rows in the table, what am I doing wrong?
mysql_fetch_array() returns a numerically-keyed array, e.g. $row[1]. You want mysql_fetch_assoc() instead. Or use mysql_fetch_row(), which fetches a dual-array - numerical AND string keys.
<?php
while($row=mysql_fetch_array($result)){
echo "<tr><td>";
echo $row['id'];
echo "</td><td>";
echo $row['twitter'];
echo "</td><td>";
echo $row['why'];
echo "</td></tr>";
}
echo "</table>";
?>
I think tr's and td's where not correct
You forgot to open a new table row for every row of your database.
while($row=mysql_fetch_array($result)){
echo "<tr>"; // Your code lacks this
echo "</td><td>";
Currently, your outputted HTML will have a lot of table row closures </tr> but no openers, hence the lack of table rows in your resulting visual output.

Categories