I'm making a functionality which shows shops in a database in rows with a button behind each shop, and a checkbox in front of each shop. I would like to find another way of getting the id of the shop based on the button pressed. The shop of which the button is selected gets deleted from the database.
$sql = "SELECT * FROM alb_locaties WHERE Verwijderd = '0'";
$result = $conn->query($sql);
if ($result->num_rows > 0){
echo "<form action='' method='get'>";
echo "<table id='MainTable'>
<tr>
<th></th>
<th>Shopnaam</th>
<th></th>
</tr>";
while ($row = $result->fetch_assoc()){
$ID = $row['I_id'];
echo "<tr>
<td><input type='checkbox' name='Shops[]' value='$ID'></td>
<td>$ID</td>
<input type='hidden' name='givenID' value='$ID'>
<td><input type='button' value='Verwijder'/></td>
</tr>";
}
echo "</table>";
echo "<input id='SelectionButton' type='submit' name='SelectionDelete' value='Verwijder selectie'/>";
echo "</form>";
}
if (isset($_GET['givenID'])){
$selectedID = $_GET['givenID'];
// FIRST DELETE
$sql = "SELECT * FROM alb_locaties INNER JOIN alb_afdelingen ON alb_locaties.I_id = alb_afdelingen.locaties WHERE alb_locaties.I_id = {$selectedID}";
Now it's determined by <a href=\"Reset_locatieshops.php?givenID='$ID'\"> and $selectedID = $_GET['givenID'];, but I would rather use a way in which it doesnt show the ID in the URL. Post also seems like a better option, because now when I refresh the page it executes the query again because of the ID in the URL. How can I still get the ID, but by using a different method.
Nevermind I found it. I changed the form method to post,
I changed the <input type='button' value='Verwijder'/> to <input type='submit' value='Verwijder' name='submit'>.
After that I used $selectedID = $_POST['givenID']; to get the ID given to the <input type='hidden' name='givenID'
Related
i have made a form in php and inside form there is a loop that extracts multiple values from database(img,name,availability,..) for multiple books i have made a table to display those values to user and after displaying these data in table i have made an issue button inside loop so that every book has its issue button.
My problem is that i have to only retrieve id of that book for which user click issue button. i tried storing it in cookie but it send the id of the first book displayed then i tried get method but that results in sending the last book that is displayed on screen id. but i want is that it should send the id of book which is selected by user
display books
echo "<form action='issue.php' method='get'>";
while ($row= mysqli_fetch_array($result)) {
echo "<div id='img_div' style='background-color:#fff;'> ";
echo "<img src='books/".$row['image']."'>";
// echo "</div>";<div id='text'>
$isbn=$row['isbn'];
echo "<input type='hidden' name='isb' value='$isbn' />";
echo " <table>";
echo "<tr><td> NAME</td><td> ".$row['name']."</td></tr>";
echo "<tr><td> AVAILABILITY</td><td> ".$row['availabilty']."</td></tr>";
echo "<tr><td> CATEGORY</td><td> ".$row['category']."</td></tr>";
echo "<tr><td colspan='2'>
<button type='submit' name='issue'>issue</button></td></tr>";
echo "</table>";
echo "</div><br/>";
if (isset($_GET['issue'])) {
# code...
$bookid=$isbn;
setcookie("bid",$bookid);
if(!isset($_COOKIE['bid'])){
echo "COOKIE NOT SET";
}
else{
echo "COOKIE SET SUCCESSFULLY";
}
}
issue.php(in which i want to send id)
if(isset($_GET['issue'])){
$bookid=$_GET['isb'];
$dbser="localhost";
$use="[redacted]";
$pasw="[redacted]";
$db="[redacted]";
$con=mysqli_connect($dbser,$use,$pasw,$db);
mysqli_select_db($con,$db)or die("db not connected");
$userid=$_COOKIE['id'];
$id=$_SESSION['user']['username'];
$query = "select id from user_account where username='$id'";
$result=mysqli_query($con,$query);
$row= mysqli_fetch_assoc($result);
$uid=$row['id'];
echo "$uid";
echo "<br/>";
echo "$bookid";
$query = "INSERT INTO issue (bookid, userid)
VALUES ('$bookid', '$uid')";
mysqli_query($con, $query)or die(mysqli_error($con));
What you need is a way to select a single row then submit that with the form. That can be accomplished by adding a Radio button to your table inside the form. The user will check the radio button for the item they want then click the submit button.
Here is an example of what that code could look like for your page.
display books
<form action='issue.php' method='get'>
while ($row= mysqli_fetch_array($result)) {
$isbn=$row['isbn'];
echo " <table>";
echo "<tr>";
echo "<td><input type=\"radio\" name=\"optradio\" value=\"".$isbn."\"></td>";
echo "<td> NAME</td><td> ".$row['name']."</td>";
echo "<td> AVAILABILITY</td><td> ".$row['availabilty']."</td>";
echo "<td> CATEGORY</td><td> ".$row['category']."</td>";
echo "</tr>";
echo "</table>";
}
echo "<button type='submit' name='issue'>issue</button>";
echo "</form>";
Here is a HTML snippet so you can see what that PHP code would output in HTML. Click "Run snippet code" below to see the preview.
<form action='issue.php' method='get'>
<table>
<tr>
<td><input type="radio" name="optradio" value="1"></td>
<td> NAME</td>
<td> name1</td>
<td> AVAILABILITY</td>
<td> availability1</td>
<td> CATEGORY</td>
<td> category1</td>
</tr>
<tr>
<td><input type="radio" name="optradio" value="2"></td>
<td> NAME</td>
<td> name2</td>
<td> AVAILABILITY</td>
<td> availability2</td>
<td> CATEGORY</td>
<td> category2</td>
</tr>
</table>
<button type='submit' name='issue'>issue</button>
</form>
Then in issue.php you would look for $_GET['optradio'] to get the selected value.
if(isset($_GET['issue'])){
$bookid=$_GET['optradio'];
$dbser="localhost";
...
...
...
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>";
}
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.
i have create two table in my databases, 1.loginacc (stay login information(LoginID , Password and Permission)2.borrow (have book that the login borrowed,userid)
below is the code: and i have get all the user id,beside every user that i get from database loginacc ,it has a button called 'get information'.when i click it,it will have order of" select * from borrow where userid = (the loginid that shown out )
<?php
$con = mysql_connect("127.0.0.1","root","password");
mysql_select_db("babytradeapps");
$sql = "Select LoginID , Password , Permission
from loginacc where Permission = 1 ";
$results = mysql_query($sql,$con);
echo "<tr><th>Admin</th></tr>";
echo "<table border=5 cellpadding=10>";
echo "<tr><th></th><th>ac</th><th>pw</th><th>per</th><th></th></tr>";
while($row = mysql_fetch_array($results)) {
echo "<tr><td><form action='searchtable.php' method='get'><button type='button' name='button2' value='button2'>get information</button></td>
<td><input type=text id='row0' name='row0' value='$row[0]' /></td><td><input type=text id='row1' name='row1' value='$row[1]' /></td>
<td><input type=text id='row2' name='row2' value='$row[2]' /></td><td><input type='submit' name='button' value='change' /></td></tr>";
}
echo "</table>";
Why don't you use anchor (link) to send the loginID rather than using button , it will be simple . As far as i've understood your problem , you can do it as below .
echo "<table border=5 cellpadding=10>";
echo "<tr><th></th><th>ac</th><th>pw</th><th>per</th><th></th></tr>";
while($row = mysql_fetch_array($results)) {
echo "<tr><td>
<a href='searchtable.php?lid=$row[0]'>get information</a></td>
<td><input type=text id='row0' name='row0' value='$row[0]' /></td><td><input type=text id='row1' name='row1' value='$row[1]' /></td>
<td><input type=text id='row2' name='row2' value='$row[2]' /></td><td><input type='submit' name='button' value='change' /></td></tr>";
}
echo "</table>";
Now in the next page i.e. searchtable.php, just get that id using GET method and execute the query . This should be somehow like ,
<?php
$loginId = $_GET['lid'];
$con = mysql_connect("127.0.0.1","root","password");
mysql_select_db("babytradeapps");
$sql = "Select * from borrow where userid = $loginId";
$results = mysql_query($sql,$con);
$final_result = mysql_fetch_array($results);
print_r($final_result);
// Manipulate view with the result
?>
heloo i have an ajx call function which brings information from a dropdown populated into a table with text inputs by ajax.
i was wondering if there was anyway that i could update the record in the database by using these text fields and the UPDATE function i am relativity new and the internet didnt bring much to light.
i have a button appearing in this table from a drop down but as far as i am aware you cannot use forms within php and the page this would have been submitted from is already submitting a php function and 2 can not be submitted at once.
i was wondering if it was possible that when the data in the textboxes below is changed when the user clicks the button those details are updated in the database?
im new to ajax and php so help would be amazing.
ps. i know this isnt secure i want it to be functional first and before it goes live i will secure it.
here is the code:
<?php
$q = $_GET['q'];
$con = mysqli_connect('server','uid','pwd','dbname');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"account.php");
$sql="SELECT * FROM account WHERE name = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Your Name</th>
<th>Your Email</th>
<th>Your Password</th>
<th>Your User Level</th>
<th>Save Changes</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <input type='text' name='txt_yourname' id='txt_yourname' value='" .$row['name']."' required='required' /> </td>";
echo "<td> <input type='text' name='txt_email' id='txt_email' value='" .$row['email']."' required='required' /> </td>";
echo "<td> <input type='text' name='txt_password' id='txt_password' value='" .$row['password']."' required='required' /> </td>";
echo "<td> <input type='text' name='txt_userLevel' id='txt_userLevel' value='" .$row['user_level']."' required='required' /> </td>";
echo "<td> <input type='button' name='btn_user' id='txt_user' type='submit' value='cheese'/> </td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
These statements execute user input, opening you up to a SQL Injection attack. You'll want to not do this.
$q = $_GET['q'];
$sql="SELECT * FROM account WHERE name = '".$q."'";
$result = mysqli_query($con,$sql);
To answer your question, in order to update a row like you want you'll need a way to identify in the database the row that has changed. One way to do it would be to include the row's primary key as a field/attribute in the HTML table row, but I'll leave it to someone more well versed in this area to say whether that's a good idea.
You're also going to want to escape and check the type of all of the fields the user can input when you go to do the update.