PHP POST value from <input> that was created with While loop - php

I am attempting to create a page that displays buttons that are associated to an account number that is a primary key in a database. When I click on the button I want that button to take its value and assign it to a variable that can be used for loading information into a profile page that will load after the button click.
After the button click I would like to load a profile page that uses the value (acc_ID) to load each of the profile attributes (name, location, etc) from a database. I have the functionality to do this already because I have it set up for when a user logs in they see their information populate in their profile page.
So far I have a While loop that populates a table with all of the player account numbers and "View Profile" buttons. Each button is assigned the player account number as a value and is also part of the name.
$query = "SELECT * FROM player";
$result = mysqli_query($conn, $query);
echo "<table>
<tr>
<th>View Profile</th>
<th>Acct Id</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo" <form action='php/createPlayerProfilesList.php'>";
echo "<tr>";
echo"<td> <input type='submit' name='submit-'.$row[acc_ID].'' value='View
Profile - $row[acc_ID]'> </input> </td>";
echo "<td class='viewProfile'><input name='player[]' class='getPlayerId'
readonly type='text' value='". $row['acc_ID'] ."' > " . $row['acc_ID'] . "
</input></td>";
echo "<td>" . $row['player_FName'] . "</td>";
echo "<td>" . $row['player_LName'] . "</td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
What I can't figure out is how to display the players profile information into a profile template after I click on their specific View Profile button which takes me to the profile.php template. I know how to load the information into the profile template based on acc_ID because it does that when they login but I can't figure this one out.
This is a sample of the profile code that populates the template based on what acc_ID is the SESSION variable.
session_start();
include("navbarphp.php");
if($_SESSION['person_type'] == 'P'){
$acct_ID = $_SESSION['acc_ID'];
$query = "SELECT * FROM player WHERE acc_ID = '$acct_ID'";
$result = mysqli_query($conn, $query);
while($row = $result->fetch_assoc()){
$location = $row['player_location'];
$fname = $row['player_FName'];
$lname = $row['player_LName'];
$school = $row['high_school'];
$gpa = $row['GPA'];
$bio = $row['BIO'];
$pos = $row['position'];
}
}
I searched for a good while on this site and had a couple things that helped but in the end I still am not able to pass a specific View Profile button value. Any help would be great. Thanks

You may have better luck by adjusting your html output to the following format:
while($row = mysqli_fetch_array($result))
{
echo '<tr>';
echo '<td><form action="php/createPlayerProfilesList.php">';
echo '<input type="hidden" name="playerId" value="'. $row['acc_ID'] .'">';
echo '<input type="submit" name="submit" value="View Profile - '. $row['acc_ID'] .'">';
echo '</form></td>';
echo '<td>'. $row['player_FName'] .'</td>';
echo '<td>'. $row['player_LName'] .'</td>';
echo '</tr>';
}
What this does is pull the form element inside a table cell, because where you have it now, wraps a table row (which is bad html). Then you put the acc_ID into a hidden input field, which is what you would use to pull the data.
In your createPlayerProfilesList.php script you would use it like this:
<?php
if (!empty($_POST['playerId'])) {
// ... pull from db based on value in $_POST['playerId']
$acct_ID = (int)$_POST['playerId'];
// ...
}
?>
This is of course a shortened example of one of the ways you could go about doing this. For example another method used these days, is by styling elements and using javascript with ajax to pull a 'record' of data, to then display dynamically in a box or overlay on the page when you click a button to 'view more'. However that example would go beyond the scope of this question ;)

Related

How can i take data from a radio button when it's name is a variable?

I have a table that displays tuples of books and data about them. There is also a radio button for each row. The idea is that the user selects the button to indicate that they want to order that book.
function displayAllBooks(){
$dbhost = 'localhost:3306';
$dbuser = 'root';
$conn = mysqli_connect($dbhost, $dbuser);
//sql statement to use the database
$sql = 'use BookStore';
mysqli_query($conn, $sql);
$sql = 'SELECT * FROM Author, Books, Written_By, Book_Categories, Assigned_To '
. 'WHERE Author.Author_ID = Written_By.Author_ID'
. ' AND Books.ISBN = Written_By.ISBN'
. ' AND Books.ISBN = Assigned_To.ISBN'
. ' AND Assigned_To.Cat_Code = Book_Categories.Cat_Code'
. ' ORDER BY ALname ASC';
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
echo '<form action = "customerDashboard.php" method = "post">';
echo 'First name:
<input type= "text" name ="CFname"<br>
Last name:
<input type= "text" name ="CLname"<br>';
echo '<p style="text-align:center"><b> All Books </b></p>';
echo '<table class="center">'
. '<tr>'
. '<th>Order</th>'
. '<th>Title</th>'
. '<th>Price</th>'
. '<th>Author</th>'
. '<th>Publication Date</th>'
. '<th>User Review</th>'
. '<th>Category</th>'
. '</tr>';
if (mysqli_num_rows($result) > 0) {
$bookCt = 0;
//mysqli_fetch_assoc associates an array with the results
while ($row) {
$retTitle = $row["Title"];
$retPrice = $row["Price"];
$retALname = $row["ALname"];
$retPubDate = $row["Publication_Date"];
$retReview = $row["User_Reviews"];
$retCat = $row["Cat_Desc"];
//fetch ISBN for each book, for use with the radio buttons to
//place orders
$sql = 'SELECT ISBN from Books WHERE Title="'.$retTitle .'"';
$resultISBN = mysqli_query($conn, $sql);
$rowISBN = mysqli_fetch_assoc($resultISBN);
$currISBN = $rowISBN["ISBN"];
echo"<tr>";
echo '<td><input type="radio" name="'.$currISBN.'"></td>';
echo "<td> $retTitle </td>";
echo "<td> $retPrice </td>";
echo "<td> $retALname </td>";
echo "<td> $retPubDate </td>";
echo "<td> $retReview </td>";
echo "<td> $retCat </td>";
echo "</tr>";
$row = mysqli_fetch_assoc($result);
}
}
else {
echo "0 results";
}
echo "</table>";
echo '<input type="submit" name="placeOrder" value="Order Selected Book">';
echo '</form>';
I've been trying something like onselect="load the button name into a session variable" but I have been unable to implement it.
Each radio button has a name value that is the ISBN (primary key) for the current book thats being put into the table. I want to be able to select a radio button, have that ISBN be stored in a session or global variable, and use that specific ISBN for another method, placeOrder(). After a radio button is checked, the user inputs their first and last name and presses "order selected book", which reloads the page and triggers the placeOrder() function via:
else if(isset($_POST["placeOrder"])){
//for placing orders on a single book
placeOrder();
}
which is present at the beginning of the PHP portion, alongside other function calls.
I'm pretty new to PHP and HTML, so forgive me if the answer is obvious. I could do this if the radio button name was explicit, but since it is changing with each row, I cannot figure it out.
Main idea: How can i capture info that a selected radio button corresponds with so I can use said info in another function?
The answer doesn't have to involve session or global variables, any help is appreciated.
You could an array to represent the radio buttons with the isbn as the index
echo '<td><input type="radio" name="books['.$currISBN.']"></td>';
and then loop through it on the server side
foreach ($_POST['books'] as $isbn => $on){
// do something with $isbn
}

How to select a specific row from a table to edit or delete

I have implemented a booking system, part of the system involves allowing users to modify or delete a booking. I am able to retrieve bookings from the database based on user's session and id which is displayed in a html table. I have been having trouble selecting a specific row from the html table to edit or delete a booking. I want to be able to select a specific row from the html table which i will then be able to edit or delete that record.
The code below retrieves the bookings and displays it in a table
bookings.php
$sqlquery = "SELECT * FROM bookings1,users WHERE bookings1.userid =
users.userid AND users.username = '".$_SESSION['loggedInUser']."' ";
$results = $mysqli->query($sqlquery);
if ($results->num_rows > 0) {
while ($row = $results->fetch_assoc()){
echo'<form method = "POST" action = "editmot.php" >';
echo'<tr>';
echo "<td>". $row["booking_id"]. "</td>";
echo "<td>". $row["Type"]. "</td>";
echo "<td>". $row["BookingDate"]. "</td>";
echo "<td>". $row["Timeslot"]. "</td>";
echo "<td>". $row["Manufacture"]. "</td>";
echo "<td>". $row["Model"]. "</td>";
echo "<td>". $row["RegistrationNo"]. "</td>";
echo "<td><a href = 'editmot.php' id ='".$row['booking_id']."'> Edit </td>";
echo "<td><a href = 'delete.php' id = '".$row['booking_id']."'> Remove </td>";
echo '</tr>';
echo '</form>';
In the href attribute of the Edit Link, include the Booking_Id as a URL parameter. When the Edit page loads, pick up the booking_id from the URL parameter and fetch the other fields for edit. Like :
echo " <a href = 'editmot.php' . '?EditBookingId=' . $row['booking_id'] "
and then so on and so forth concatenate other things.
Your final href URL should look like :
'http://editmot.php?EditBookingId=24798' or some such.
use this code to edit or delete
for edit
for delete
if(isset($_REQUEST['action']))
if($_REQUEST['action']=="update")
update query
else if($_REQUEST['action']=="delete")
delete query
Instead of using the anchor tag, you could use a submit button and a hidden input field to hold the booking ID, since each booking detail (or row) is in a separate form. You could use something like this in each booking row:
echo "<td><input name='edit' type='submit' value='Edit'>"
. "<input type='hidden' name='booking_id' value='".$row['booking_id']."'></td>";
echo "<td><input name='delete' type='submit' value='Remove'>"
. "<input type='hidden' name='booking_id' value='".$row['booking_id']."'></td>";
And then in editmot.php, process the delete or edit form action:
if (isset($_POST['edit'])) {
// edit video here
}
if (isset($_POST['delete'])) {
// delete video here
}
You need to insert the id in the url so you can get it with the $_GET method in the edit or delete file.
So your anker needs to look something like:
<a href = 'delete.php?id=".$row['booking_id']."'>Delete</a>;
Now when a user go's to that url the url wil look something like:
localhost/delete.php?id=20
Now in your delete.php you can get the id with the $_GET method:
$id = $_GET['id'];
$qry = "DELETE FROM booking1 WHERE id=$id"; //this will delete the row where id is 20

PHP Add button to view all the contents of entire record MySQL

Currently I have a query running that brings up all the contents of the 'products' table and the 'user' associated with the products. It prints on a table. I want to create a button, that brings up the entire records.
Please note that I must only be able to view the selected record only.. How would I go about this?
echo "<table border='1'>
<tr>
<th>user_id</th>
<th>user_name</th>
<th>selected_product</th>
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
echo "<td>" . $row['user_id'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "<td>" . $row['selected_product'] . "</td>";
So a button should appear for each record that is returned as a result. When I click this button another window should appear which shows the entire contents (for this record only) and the user associated.
How does the button know which record its associated with.
<a href="anotherpage.php?id=<?php echo $row['user_id']?>" ><button>View Details</button></a>
This will take you to another page.
Use get method tho get the id into php variable.
$userId = $_GET['id'];
And now use sql query to fetch all the data from the database for single user.
$userResult = mysql_query("SELECT * FROM userTable WHERE user_id = ".$userId);

Grabbing the text within text area with php html

Hey so I am trying to grab the user input text within a textarea but it is not working out too well. What is happening is that we are grabbing a text (movie review) from our server and we want the user to be able to update it and then send it back to the server. Anyone know what we are doing wrong??
We arent getting any error, it just that we are unable to grab the textarea field data. We are pretty new to php and html so I am assume it is some small typeo we are overlooking.
UPDATE: Full fills here.
http://dl.dropbox.com/u/21443163/Reviews.php
http://dl.dropbox.com/u/21443163/updateReview.php
while($RecordSetMovieRow = odbc_fetch_array($RecordSetMovie))
{
echo "<tr>";
$review = $RecordSetMovieRow['Review'];
echo "<td align = 'center'>" . $RecordSetMovieRow['FirstName']. $RecordSetMovieRow['LastName'] . "</td>";
echo "<td align = 'center'><textarea name = 'textarea' rows = '5' cols= '40'>" . $review . "</textarea></td>";
$textarea = $_GET['textarea'];
$u = $Re[0];
echo "<td><form action = 'updateReview.php?id=".$RecordSetMovieRow['ReviewID']."&review=$textarea' method = 'POST'><input type='submit' value='Update'></form></td>";
echo "</tr>";
}
echo "</table>";
odbc_close($Conn);
If you want to send large blocks of data to the database then enclose everything in a form with the method=POST name/attribute
<form action="updatingScript.php" name="myForm" method="POST" >
<textarea name="textArea" rows="5" cols="40"><?=$review ?></textarea>
</form>
Then in your updatingScript.php do this
if(isset($_POST['myForm'])) {
$textInfo = mysql_real_escape_string($_POST['textArea']);
//move this info in your database
mysql_connect("localhost", "root", "");
mysql_select_db("myDb")
$query="UPDATE myTable SET userTextInfo='$textInfo' WHERE userId='$userId' ";
$result=mysql_query($query);
}
Also set error_reporting(E_ALL); at the beginning of your PHP script as this will display what went wrong (in response to your "we aren't getting any errors")
You mention method='POST' in your form definition (which is right), but attempt to check $_GET['textarea'] (which is wrong either way). I'd suggest fixing the latter: sending large blocks of text in URL itself is usually not great.
Don't forget to get rid of the &review=$textarea as well; no need to send the content twice, in two different variables. )
Your code, with just a few minor tweaks to make it get the proper data from the form. The credit goes to raina77ow, though - his answer is absolutely correct. I just saw that you requested some code, so here it is.
Also, you need to have the form tags such that the textarea is WITHIN them, otherwise it is not part of the form, and it's data does not get posted (that edit is included below).
echo '<form action = 'updateReview.php?id=".$RecordSetMovieRow['ReviewID']."' method = 'POST'>'; // Moved this outside of the while - BUT it needs to be BEFORE the <table> tag also!
echo '<table>'; // If this is not where you want your opening table tag, that's fine - but move the opening FORM tag to BEFORE the opening Table tag
while($RecordSetMovieRow = odbc_fetch_array($RecordSetMovie))
{
echo "<tr>";
$review = $RecordSetMovieRow['Review'];
echo "<td align = 'center'>" . $RecordSetMovieRow['FirstName']. $RecordSetMovieRow['LastName'] . "</td>";
echo "<td align = 'center'><textarea name = 'textarea' rows = '5' cols= '40'>" . $review . "</textarea></td>";
$textarea = $_POST['textarea']; // Changed from $_GET["textarea"] because you are using method='post' in form
$u = $Re[0];
echo "<td><input type='submit' value='Update'></td>";
echo "</tr>";
}
echo "</table>";
echo '</form>'; // Moved this to the end of the form, so data from form will get passed
odbc_close($Conn);

populate php form fields with mysql array (on click)

I'm a php/mysql newbie, working on an invoicing application. I have a summary.php page, in which I'm using php to query a db and render a table with the retrieved data:
< ?php
mysql_connect ... [ snip ] ...
// retrieve all data in the 'Invoices' table ...
$result = mysql_query("SELECT * FROM Invoices") or die(mysql_error());
// ... and store each record in a variable:
$row = mysql_fetch_array($result);
and my table is:
<table class="record-summary">
<form action="/edit.php" method="post">
<tr>
<th ...
[ snip: more table headers ] ...
<?php
$controls = "<input type=\"submit\" value=\"details.php\" /><input type=\"submit\" value=\"edit.php\" />";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td class=\"id\">". $row['ID']. "</td>";
echo "<td class=\"inv\">". $row['invoiceNumber']. "</td>";
[ snip: more data cells ] ...
echo "<td class=\"controls\">". $controls . "</td>";
The last cell (td.controls) in each row contains two submits, one linking to a detail view (details.php), the other to a form page (edit.php). My objective is to populate either page with the values of the record/row. Where I need help is how exactly to use the $row variable to carry this array to e.g., edit.php, and populate the fields in the form (actually I'm fairly certain I have the syntax for the form inputs' value attributes). Both details.php and edit.php have all the data fields in the record represented, though in summary.php only a portion of the fields are displayed (ergo, 'summary.php'). So my questions are:
How do I ensure that when either button is clicked, the $row contains the values of the given record, and how to push the array into either details.php or edit.php?
Many thanks in advance,
src
You can pass the ID to edit.php and details.php but without the form just with two links to edit.php?id=and details.php?id=:
<?php
// $controls = "<input type=\"submit\" value=\"details.php\" /><input type=\"submit\" value=\"edit.php\" />"; <-- is not necessary
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td class=\"id\">details.php | edit.php</td>";
echo "<td class=\"inv\">". $row['invoiceNumber']. "</td>";
// [ snip: more data cells ] ...
// echo "<td class=\"controls\">". $controls . "</td>"; <-- is not necessary
You can grab the data using the id:
//....
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
$id = $_GET['id'];
}
else
{
die("There is no id.");
}
$sql = sprintf("SELECT * FROM Invoices WHERE id = %s", $id);
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
echo "<input type=\"hidden\" name=\"row\" value=\"" . $row . "\"/>";
Include this in your loop and your form's "action" script will be able to access the corresponding $row variable via $_POST['row'].

Categories