I'm trying to create a Table where you can 'Sign Off' items populating it. Currently the table looks to be populated fine, however when you go to 'Sign Off' an item - it will only act on the latest mysql row ['directory'] as well as the latest text field accompanying it.
When I say 'Sign Off' I mean to insert someone's initials into the mysql table row associated with that item. I first of course need to get the POST submission handled correctly.
That probably wasn't articulated perfectly, so please take a look at my current code;
echo "<form action='signoff.php' method='post' id='signoffform' style='display:inline;'>";
echo "<p3><table id='myTable3' class='tablesorter'>
<thead>
<tr>
<th>To Delete</th>
<th>Directory</th>
<th>Space Used</th>
</tr>
</thead>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td><input type='text' name='signoff' id='signoff' form='signoffform' /><button>Sign Off</button> Signed Off By:" . $row['signoff'] . "</td>";
echo "<td><input type='text' name='dir' id='dir' form='signoffform' value='" . $row['directory'] . "' readonly /></td>";
echo "<td>" . $row['used'] . "</td>";
echo "</tr>";
}
echo "</table></p3>";
echo "</form>";
signoff.php
<?php
echo $_POST["signoff"];
echo "<br />";
echo $_POST["dir"];
?>
Please feel free to request more info
... when you go to 'Sign Off' an item - it will only act on the latest mysql row ['directory'] as well as the latest text field accompanying it.
The problem is because of your name attributes, name='signoff' and name=dir. When you loop through the result set, the name attribute value gets overwritten in each iteration. Hence whatever button you click, you'll always get the last row values.
So the solution would be like this:
Change your while loop in the following way,
// your code
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td><input type='text' name='signoff[".$row['directory']."]' id='signoff' form='signoffform' /><button>Sign Off</button> Signed Off By:" . $row['signoff'] . "</td>";
echo "<td><input type='text' name='dir[".$row['directory']."]' id='dir' form='signoffform' value='" . $row['directory'] . "' readonly /></td>";
echo "<td>" . $row['used'] . "</td>";
echo "</tr>";
}
// your code
And on signoff.php page, process your form in the following way,
foreach($_POST['dir'] as $dir){
if(!empty($_POST['signoff'][$dir])){
$signoff = $_POST['signoff'][$dir];
// That how you can get $dir and the corresponing $signoff value
}
}
Sidenote: If you want to see the entire $_POST array structure, do var_dump($_POST); on signoff.php page.
Related
I'm trying to make a HTML table as a frontend to a mySQL database. The table displays fine and I can type in the edits I want to make to each row of the table but when I press the submit button the changes aren't actually made. Can anyone see where I'm going wrong?
<?php
include("db.php");
$sql = "SELECT * FROM `artist`";
$result = mysqli_query($conn, $sql);
if (isset($_POST['update'])){
$artID = $_POST['artID'];
$artName = $_POST['artName'];
$key = $_POST['hidden'];
$UpdateQuery = "UPDATE `artist` SET `artID` = '$artID', `artName` = '$artName' WHERE `artist`.`artID` = '$key'";
mysqli_query($conn,$UpdateQuery);
header("Location: {$_SERVER['HTTP_REFERER']}");
exit;
};
echo "<table border='1'>";
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "</tr>";
if ($result->num_rows > 0) {
echo "<form id ='artisttable' action ='getartiststable.php' method ='post'>";
// output data of each row
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" ."<input type='text' name ='artID' value ='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='text' name ='artName' value ='" . $row["artName"] . "' </td>";
echo "<td>" . "<input type = 'hidden' name ='hidden' value='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='submit' name ='update'" . " </td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
The db.php file simply includes the connection info to the mySQL database and I'm 100% sure there's nothing wrong with it as it retrieves the table correctly it just doesn't update.
You are putting form tag inside tr which is not allowed td are only allowed
so you have to remove that tr from there.
You have to use jquery or you can replace the table with some other grid structure so that it can look the same and the form can be placed there as well
One more suggestion Don't mix the php and html together separate them for the clean code
If you do all these you code will be like this
Your form is being constructed with multiple elements with the same name. When you submit the form it is using the last elements as the values so regardless of the record you want updated the last record is being updated (or throwing an error because of string encapsulation). You should use parameterized queries as well.
So instead of:
if ($result->num_rows > 0) {
echo "<form id ='artisttable' action ='getartiststable.php' method ='post'>";
// output data of each row
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" ."<input type='text' name ='artID' value ='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='text' name ='artName' value ='" . $row["artName"] . "' </td>";
echo "<td>" . "<input type = 'hidden' name ='hidden' value='" . $row['artID'] . "' </td>";
echo "<td>" . "<input type='submit' name ='update'" . " </td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
Use:
if ($result->num_rows > 0) {
// output data of each row
while($row = mysqli_fetch_array($result)) {?>
<form class='artisttable' action ='getartiststable.php' method ='post'>
<tr>
<td><input type='text' name ='artID' value ='<?php echo $row['artID'];?>' /></td>
<td><input type='text' name ='artName' value ='<?php echo $row["artName"];?>' /></td>
<td><input type = 'hidden' name ='hidden' value='<?php echo $row['artID'];?>' /></td>
<td><input type='submit' name ='update'" . " </td>
</tr>
</form>
<?php } ?>
</table>
So you get a form for each data set. Here's a link on prepared statements with mysqli: http://php.net/manual/en/mysqli.quickstart.prepared-statements.php. You also should update your mark up. Tables for formatting aren't the best approach. Your inputs also weren't closed missing >.
Also this changed artisttable from an id to class because there will be multiples. Update CSS/JS accordingly.
I'm creating a table and when the user clicks on the button I want it to open a php file called player_profile, where that page will call up more information on that particular player.
When it comes to passing variable through pages I'm aware that something along these lines will work.
//On page 1
$_SESSION['varname'] = $var_value;
//On page 2
$var_value = $_SESSION['varname'];
Yet when I add it to the button the php file wont load. Below is my code without passing a variable that works.
<?php
$result = mysql_query("SELECT *, CONCAT(FirstName,' ', LastName) AS Name FROM Player WHERE TeamID = '$TeamID' ORDER BY LastName ASC");
echo "<table id='customers' border='1'>
<tr>
<th>PlayerID</th>
<th>Name</th>
<th>Position</th>
<th>Tries</th>
<th>Tackles</th>
<th>Turnovers</th>
<th> Info </th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row["PlayerID"] . "</td>";
echo "<td>" . $row["Name"] . "</td>";
echo "<td>" . $row['Position'] . "</td>";
echo "<td>" . $row['Tries'] . "</td>";
echo "<td>" . $row['Tackles'] . "</td>";
echo "<td>" . $row['Turnovers'] . "</td>";
echo "<td><form action=player_profile.php>
<input name=id type=hidden value='".$row['PlayerID']."';>
<input type=submit name=submit value=info>
</form></td>";
echo "</tr>";
echo "</tr>";
}
echo "</table>";
?>
The code above works perfectly at displaying a table and taking me to the player_profile page. But if change the button to the code below the page so that I can pass a variable it doesn't load
echo "<td><form action=player_profile.php>
<input name=id type=hidden value='".$row['PlayerID']."';>
$_SESSION['varname'] = ".$row['PlayerID'].";
<input type=submit name=submit value=info>
</form></td>";
echo "</tr>";
Don't use the session to pass parameters. It breaks the way users expect a page works.
Use $_GET parameters:
John
Then in player_profile.php do:
$id = isset($_GET["id"]) ? $_GET["id"] : false;
if ($id === false) {
exit("missing input");
}
// etc
The problem with using a session here is that if someone opens two different player profiles what can he expect to see? What if he refreshes one of them?
A more advanced and involved way of parameter passing uses URL Rewriting to get nice looking URLs like:
John
You have code trying to set a session inside your echo. You want it separate:
echo "<td><form action=player_profile.php>
<input name=id type=hidden value='".$row['PlayerID']."'>
<input type=submit name=submit value=info>
</form></td>";
echo "</tr>";
If you want to access the PlayerID variable on the player_profile.php you just use:
$var_value = $_GET['id'];
You could also put it into a session var at that point, but I don't know why you would need that.
I have a table with 3 columns like so in index.php:
ID | StName | Inspect
_________________________________
1 Wisconsin INSPECT
2 Alabama INSPECT
3 Nebraska INSPECT
The right most column is a submit button which takes the user to a page called inspect.php. I want to send the corresponding ID column value to inspect.php when a user clicks the INSPECT button so that I can use that value inside inspect.php, how can I do this?
Currently I have:
//index.php
echo "<table border = 1>
<tr>
<td>ID</td>
<td>StName</td>
</tr>";
// go into a loop to create more rows -- omitted
echo "<tr>";
echo "<td>" . $resultArr[0] . "</td>";
echo "<td>" . $resultArr[1] . "</td>";
echo "<td> <form action='inspect.php' method='get'>
<input type='Submit' value='Inspect'>
</form>
</td>";
echo "</tr>";
echo "</table>";
$resultArr[0] contains the ID value I want to send to inspect.php
So add an input field inside the form with a name attribute which will be an index key name on inspect.php page
<form action='inspect.php' method='get'>
<input type='hidden' name="id" value='<?php echo $resultArr[0]; ?>'>
<input type='submit' value='Inspect'>
</form>
A better and simple way to go for this is without a submit, just use a hyperlink like
Inspect
And later process this id on inspect.php page
I think I got what you wanted to work. I am using different data in the array, but you should be able to change to your needs anyway.
On index.php:
$resultArr = array("dave","fff","erere");
echo "<table border = 1>
<tr>
<td>ID</td>
<td>StName</td>
</tr>";
// go into a loop to create more rows -- omitted
echo "<tr>";
echo "<td>" . $resultArr[0] . "</td>";
echo "<td>" . $resultArr[1] . "</td>";
echo "<td> <form action='inspect.php' method='get'>
<input type='hidden' name='id' value=' $resultArr[0] '>
<input type='submit' name='inspect' value='Inspect'> </td>";
echo "</tr>";
echo "</table>";
And on inspect.php:
if( $_GET['inspect']){
$name = $_GET['id'];
echo $name;
}
This outputs the value of $resultArr[0], which I think you wanted.
I have on my page a dataTable with tons of rows. Each row has an id, name, surname and an action column. In the action column there is a textarea where you can add a comment and a button that submits that comment. When I submit the comment I want the page to position itself where it was, but I cannot figure it out how. Any ideas?
Here is the snippet of the code:
$result = mysql_query($query, $connection);
while ($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<form method='post' action='saveComment.php#position_".$row['id']."'>";
echo "<td hidden><input hidden name='id' readonly value=" . $row['id'] . " /></td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['surname'] . "</td>";
echo "<td><input type='submit' name='submit' value='Comment'/></td>";
echo "<a id='position_".$row['id']."'></a>";
echo "</tr>";
}
I tried passing the id of the current row to the script that saves the comment and it does pass the value, after which it should position it back where it was, but it doesn't seem to work :/
use Ajax to submit the form, and on success:
window.location = 'saveComment.php#position_<? echo $row['id']; ?>';
I found out how it works with anchors! The problem was that I didn't pass the id in the saveComment.php script when redirecting back to the original page.
Code in the page where the form is:
if ($_POST['submit']){
$id=$_POST["id"];
header('Refresh: 0.5; URL=originalPage.php#position_' . $id);
//...rest of code goes here...
}
That's it :)
You need to use
<a name='position_".$row['id']."'></a>
instead of
<a id='position_".$row['id']."'></a>
I have a simple PHP form which displays inputs with values from a mysql DB and sends the form results to another page which updates a db table based on the GET results:
echo "<table>";
echo "<tr>";
echo " <th>Project No</th>
<th>Customer Name</th>
<th>Description</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td><input value=" . $row['project_no'] . "></input></td>";
echo "<td><input value='" . $row['cust_name'] . "'></input></td>";
echo "<td><input value='" . $row['description'] . "'></input></td>";
echo "</tr>";
}
echo "</table>";
echo "<input type='submit' value='Update' />";
echo "</form>";
In updateprojects.php when I do:
echo $_GET['project_no'].$_GET['cust_name'].$_GET['description'];
I don't see any values. Why is this?
In the input field you need to specify the parameter name with the "name" attribute.
echo "<tr>";
echo "<td><input name=\"project_no\" value=" . $row['project_no'] . "></input></td>";
echo "<td><input name=\"cust_name\" value='" . $row['cust_name'] . "'></input></td>";
echo "<td><input name=\"description\" value='" . $row['description'] . "'></input></td>";
echo "</tr>";
You are forgetting the input names:
<td><input name='project_no' value=" . $row['project_no'] . "></input></td>
If you don't do this php doesn't know what you mean by 'project_no'. Each input needs a name.
It is because you do not assign names to the input tags:
echo "<td><input name=\"project_no\" value=\"" . $row['project_no'] . \"></input></td>
The above should work.
Your inputs have no name attribute
Since you are creating form inputs with a loop this should work for you (you can't have 2 input fields with same name), just grab an id of row/record from the database too:
echo "<form method='get' action='updateprojects.php'>";
echo "<table>";
echo "<tr>";
echo " <th>Project No</th>
<th>Customer Name</th>
<th>Description</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo '<td><input value="' . $row['project_no'] .'" name="form['.$row['id'].']['project_no']"/></td>';
echo '<td><input value="' . $row['cust_name'] .'" name="form['.$row['id'].']['cust_name']"/></td>';
echo '<td><input value="' . $row['description'] .'" name="form['.$row['id'].']['description']"/></td>';
echo "</tr>";
}
echo "</table>";
echo '<input type="submit" name="submit" value="Update" />';
echo "</form>";
if(isset($_GET['submit'])){
foreach($_GET['form'] as $id=>$column){
//update your database where id=$id. This is just testing
echo 'Row '.$id .' =>'. $column['project_no'].'-'.$column['cust_name'].'-'.$column['description'];
}
}
You need to add the name="" attribute to your inputs.
Something that has helped me in the past, is to use the Net tab in Firebug and enable the Persist option to see what the browser is actually sending back. In one case, the form had duplicate entries for some for fields, but the second entry wasn't sending the correct values.
Please inform the type of submit, or hidden text. And / or verify that you are returning data from sql query.
It appears to me that you need to have a name="project_no" in you HTML input.