PHP While Loop - Hidden Input Values - SQL - php

I'm trying to shows a number of rows one after the other, with a verify button underneath, which one clicked will go to the update (php) method. I've got it working, however it is always the same value (first alphabetical last name).
What I need to try and do is get it to pick the lastname from the row when submitted. Here is the code:
<div class="container">
<form action="update.php" method="POST">
<?php
//executes the SQL query
$result = mysql_query("SELECT * FROM Sheet1");
//returns the result from the database
while ($row = mysql_fetch_array($result)) {
$census = $row['Date'];
$WebsiteAddress = $row['WebsiteAddress'];
$LastName = $row['LastName'];
echo "<input type='hidden' name='HiddenInput' value='".$LastName."'>";
echo "<b>Last Name:</b> $LastName<br><br> <b>Website:</b> $WebsiteAddress<br><br> <b>Date:</b> $census<br><br>";
echo "<input name='".$LastName."' type='submit' value='Verify' /><hr>";
}
?>
</form>
</div>

You need to create array of input to get all the values like
<input type='hidden' name='HiddenInput[]' value='".$LastName."'>
Then $_POST['HiddenInput'] will return array that contain all value of $LastName

take a look at this code.. give different names to all the hidden fields..
$i = 0;
while ($row = mysql_fetch_array($result)) {
$census = $row['Date'];
$WebsiteAddress = $row['WebsiteAddress'];
$LastName = $row['LastName'];
echo "<input type='hidden' name='HiddenInput_".$i."' value='".$LastName."'>";
echo "<b>Last Name:</b> $LastName<br><br> <b>Website:</b> $WebsiteAddress<br><br> <b>Date:</b> $census<br><br>";
echo "<input name='".$LastName."' type='submit' value='Verify' /><hr>";
$i++;
}

Related

Data from PHPMYADMIN implement in PHP Page into HTML Form to Send via PHP POST

I have data in my PHPMYADMIN in a few columns and rows.
Via PHP I read the Data out of my SQL Table and print it into a table.
The last of my table is a action button. So the whole table is a echo and within the table there's a html form in the last , but only with a submit button (input type="hidden") but the value should be the "id" out of my SQL table.
Here's the problem. How can I get the id of one row into the value of an input field? . $row["id"]. doesn't work. How can I fix this problem?
This is for a Website where the user can vote a table row up and then with the html form it is sending via http post to another page where it overrides the current number in the database with +1
$sql = "SELECT * FROM votingpoll ORDER BY votecount DESC";
$result = $conn->query($sql);
echo "$id";
echo "<table>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>
<form action='vote.php' method='post'>
<input type='text' name='id' value='$id'>
<input type='submit' value='VOTE'>
</form>
</td>
</tr>";
}
} else {
echo "0 results";
}
Thank you!!!
You can do $row[id] i.e. leave the quotes off when the array reference is used inside a double quoted string.
$sql = "SELECT * FROM votingpoll ORDER BY votecount DESC";
$result = $conn->query($sql);
// remove this it does not appear to do anything useful here
//echo "$id";
echo "<table>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>
<form action='vote.php' method='post'>
<input type='text' name='id' value='$row[id]'>
<input type='submit' value='VOTE'>
</form>
</td>
</tr>";
}
} else {
echo "0 results";
}
Or if you prefer you could use
<input type='text' name='id' value='{$row['id']}'>
It looks like you aren't setting $id anywhere. Try setting it from the results like $id = $row["id"];
Full example:
while($row = $result->fetch_assoc()) {
$id = $row["id"];
echo "<tr>
<td>
<form action='vote.php' method='post'>
<input type='text' name='id' value='$id'>
<input type='submit' value='VOTE'>
</form>
</td>
</tr>";
}

How to get the value of radio button which is printed in the rows in the table created by sql in php code

I am having trouble in getting the value of radio button for each row of the table plus the id of that row fetched by while loop . this is table for getting the attendance of students and save it against their ids .
but i am unable to do so as i have pasted the radio button in loop in echo statement.
If i get the value in php code it just take the value of radio button but it does not take the value of that rows id. But I want both of them at a same time
Here is the code for table and radio button
<?php
$sql = "Select grno, CONCAT(stdname ,' ', stdlname)AS sname from student";
$result = $conn->query($sql);
if($result->num_rows >0)
{
while($rows = $result->fetch_assoc())
{
$id = $rows['grno'];
$name = $rows['sname'];
$sch = $rows['grno'];
$asql = "Select * from attendence where lid = '$id'";
$aresult = $conn->query($asql);
$a= 'add';
$b= 'Present';
$c= 'Absent';
$d= 'Leave';
echo "
<tr>
<td><a href = 'view.php?id=".$id."'>".$name."</a></td>
<td>".$sch."</td>
<td><form method='post' >
<input type='radio' name='att' value='PRESENT' />".$b."
<input type='radio' name='att' value='ABSENT' />".$c."
<input type='radio' name='att' value='LEAVE' />".$d."
<input name='submit' type='submit' id='submit' value='Enter' />
</form> </td>";
}
if (isset($_POST['submit']))
{
$att=$_POST['att'];
$date = date('Y-m-d', time());
$ssql = "Select * from attendence where date = '$date' and lid = '$id'";
$sresult = $conn->query($ssql);
if($sresult->num_rows > 0)
{
echo "<script>alert('Today`s attendence already inserted.');</script>";
}
else
{
$insql = "Insert into attendence(lid, date, status) values('$id', '$date', '$att')";
if ($conn->query($insql) === TRUE) {
echo "<script>alert('Attendence record Inserted');</script>";
} else {
echo "<script>alert('Error Occurred');</script>";
}
}
}
}
?>
When I write it like this then it work fine but the value of that id is not entered in database . Can you tell me how can i put the value of radio button in that echo statement or else how can I pass that value of particular row of while loop when the submit button is pressed. Because now if I print the value of $id in the code of ISSET submit button then it gets the id of last row but I want to get the id of the row on which I press the radio button.
or in second method if i wanna send the value of radio button plus id to the next page for example
echo "
<tr>
<td><a href = 'view.php?id=".$id."'>".$name."</a></td>
<td>".$sch."</td>
<td><form method='post' >
<input type='radio' name='att' value='PRESENT' />".$b."
<input type='radio' name='att' value='ABSENT' />".$c."
<input type='radio' name='att' value='LEAVE' />".$d."
<a href = 'view.php?id=".$idd."& att=".$att."'><input name='submit' type='submit' id='submit' value='Enter' /></a>
</form></a> </td>";
how can i do that
Every row has its own form that can be submitted separately. One possibility is to add a hidden field to each row containing the id of that row. In your PHP script that is called when clicking on the submit button, you can UPDATE or INSERT your entry according to this id.
<form method='post' action='YOUR_PHP_SCRIPT'>
<input type='hidden' name='row_id' value='" . $id . "'/>
<input type='radio' name='att' value='PRESENT' />" . $b . "
<input type='radio' name='att' value='ABSENT' />" . $c . "
<input type='radio' name='att' value='LEAVE' />" . $d . "
<input name='submit' type='submit' id='submit' value='Enter' />
</form>
In your PHP script (called YOUR_PHP_SCRIPT in this case), you can check for $_POST['row_id'] and use this value to determine the row where the Enter button was clicked. I hope that I read your code correctly because I assumed that grno is your unique identifier.
if (isset($_POST['submit'])) {
$id = $_POST['row_id']; // add this line
// ...
if($sresult->num_rows > 0) {
echo "<script>alert('Today`s attendence already inserted.')</script>";
} else {
$insql = "Insert into attendence(lid, date, status) values('$id', '$date', '$att')"; // $id is used here
// ...
}
}
EDIT: You are wrapping your form into an <a></a> element that could prevent the submission of your form because it is triggered before Enter (submit) can be triggered. You might rethink this structure.
Please use PHP Prepared Statements in order to avoid SQL injections.

update mysql datarow from php form with button

I am trying to update a php form that holds a few rows of mysql data. I have a button next to each row and when i click on that I want to update the row. The issue im having below is the ID is only set as the last row. How do i get this to push the ID to the button? So basically no matter what button i press i always get the same ID which is the last one to load.
if($result){
while($row = mysqli_fetch_array($result)){
$id = $row["ID"];
$beername = $row["BeerName"];
$beertype = $row["BeerType"];
$beerpercent = $row["BeerPercent"];
$beerdescription = $row["BeerDescription"];
$nowpouring = $row["NowPouring"] =='0' ? '' : 'checked=\"checked\"';
$glutenreduced = $row["GlutenReduced"] =='0' ? '' : 'checked=\"checked\"';
$beertogo = $row["BeerToGo"] =='0' ? '' : 'checked=\"checked\"';
echo "<form action='' method='POST'>";
echo "<tr><td><h6><input type=\"text\" size=\"5\" name=\"id\" value=\"$id\"></h6></td>";
echo "<td><h6><input type=\"text\" size=\"30\" name=\"BeerName\" value=\"$beername\"></h6></td>";
echo "<td><h6><input type=\"text\" size=\"30\" name=\"BeerType\" value=\"$beertype\"></h6></td>";
echo "<td><h6><textarea size=\"90\" style=\"width:250px;height:150px;\" name=\"BeerDescription\" value=\"\">$beerdescription</textarea></h6></td>";
echo "<td><h6><input type=\"text\" size=\"5\" name=\"Percent\" value=\"$beerpercent\"></h6></td>";
echo "<td><h6><input type=\"checkbox\" name=\"NowPouring\" value=\"true\" $nowpouring></h6></td>";
echo "<td><h6><input type=\"checkbox\" name=\"GlutenReduced\" value=\"true\" $glutenreduced></h6></td>";
echo "<td><h6><input type=\"checkbox\" name=\"BeerToGo\" value=\"true\" $beertogo></h6></td>";
#echo "<td><h6> <a href=\". $_SERVER["PHP_SELF"] .?id=".mysql_result($result,$j,'id')."\" onclick=\"\"></h6></td>";
echo "<td><h6> <button name=\"submit\" type=\"submit\" value=\"$id\">Save</button></h6></td>";
echo "</tr>";
echo "</form>";
}
}
if (isset($_POST['submit'])) {
$user = $_POST['submit'];
echo "<p style=\"color:#ffffff\">$id</p>";
#$delet_query = mysqli_query($mysqli, "UPDATE NowPouring SET NowPouring = '1' WHERE ID = '4'") or die(mysql_error());
if ($delet_query) {
echo '<p style="color:#ffffff">Beer with id '.$id.' is updated. To refresh your page, click ' . ' <a href=' . $_SERVER["PHP_SELF"] . ' > here </a></p>';
}
}
?>
The main problem I see here is that the while loop your code has is generating the same name for the inputs...
All of your "<button name=\"submit\" type=\"submit\" value=\"$id\">Save</button>" will have the same name, that's why it always has the last id as value.
Maybe you should try something such as..
<button name=\"$id_submit\" type=\"submit\" value=\"$id\">Save</button>
or if you want you can store it in an array..
<button name=\"submit[]\" type=\"submit\" value=\"$id\">Save</button>
You are seeing this result because the 'name' of each of your inputs is the same, so essentially you have a form with a bunch of elements that have the same names. You need to add a dynamic aspect to each name.
For example, you could update your output to something like this:
echo "<tr><td><h6><input type=\"text\" size=\"5\" name=\"id_$id\" value=\"$id\"></h6></td>";
Where each line adds the current id. Then when you retrieve the form data, you can append the submitted id to the field you want to update.
Have you considered using an AJAX approach so you can submit just the line in question and not have to reload the page and return the whole data set each time?
Make <form> for each submit button. Adding <form> in the while():
if($result){
while($row = mysqli_fetch_array($result)){
echo "<form action='' method='POST'>";
//...
echo "</form>";
}
}
Your form tag is placed at the wrong place.
It should be within:
while($row = mysqli_fetch_array($result)){
$id = $row["ID"];
//....
//....
echo "<form action='' method='POST'>";
echo"<tr>";
echo "<td>" . $id . "</td>";
//....
//....
echo "<td><button type='submit' name='submit'>Save</button></td>";
echo"</tr>";
echo "</form>";
}

How to update an element of the database depending the id, with a submit button

I have this code that inserts data into a table at index, but how can I make the submit button modifystatus work? I have been trying with no results, the aim is that when I press the submit button "atendido" the table of the database column name status will be = 0, depending the on id that is updating
<?php if($total>0 && $search!='')
{
do {
echo "<tr><form method='post' action='../solicitudes/modifystatusdown.php'>
<td style='width:20%; '><input type='hidden' name='$fila[nombre]''><input type='text'>";
echo $fila['nombre'];
echo "</td><td style='width:20%;'>";
echo $fila['telefono'];
echo "</td><td style='width:20%;'>";
echo "<input type='Submit' name='delete' value='Atendido'></td></form></tr>";
}
while ($fila=mysqli_fetch_assoc($resultado));
} ?>
this is the modifystatus.php
<?php
$nombre = $_POST[$nombre];
$nombre = $_GET['nombre'];
$link = mysql_connect("localhost","root","");
if($link)
{
mysql_select_db("users",$link);
}
mysql_query("UPDATE usuarios SET status=0 WHERE nombre = '$nombre'");
?>
You don't give your complete logic, but it is more likely that you want to use a specific name for your input and change the value. For example:
<?php $nombre = $fila['nombre'] ?>
<td style='width:20%; '>
<input type='hidden' name='nombre' value='$nombre']'><input type='text'>";
Then the value
$nombre = $_POST['nombre']
will contain the name.
So why do you use:
$nombre = $_POST[$nombre];
$nombre = $_GET['nombre'];
you just need:
$nombre = $_POST[$nombre];
and stop using mysql_ it is deprecated, use mysqli or PDO

Display data from sql database to a textbox

I am performing a search of my sql database, and assigning the data from each row to a variable. Ex.
$query = mysql_query("SELECT * FROM `PropertyInfo` WHERE `sitestreet` LIKE '$street'");
// display query results
while($row = mysql_fetch_array($query))
{
$sitestreet = $row['sitestreet'];
$sitestate =$row['sitestate'];
}
Now my question is how do I get that info to display as text in a text box?
Thanks!
By echoing a textbox with those variables in the value="" attribute
echo "<input type='text' id='sitestreet' value='$sitestreet' />"
echo "<input type='text' id='sitestate' value='$sitestate' />"
It's as simple as:
<input value='<?php echo $sitestreet; ?>'>
You can fetch and assign in some time:
while($row = mysql_fetch_array($query))
{
echo "<input type='text' id='sitestreet' value='$row['sitestreet']' /><br />
<input type='text' id='sitestate' value='$row['sitestate']' />";
}

Categories