this is my first PHP program and I'm kind of stuck in update button code I think the problem is with the Driver_ID I tried many different things but nothing works for me. my problem is how can I get the Driver_ID to make the button work?
in this code below I only get the last row id. If someone can help me I would really appreciate it.
<?php
include("dbconnection.php");
//Fetch the data from tables
$query="SELECT * FROM drivers JOIN users ON users.User_ID = drivers.User_ID";
$result = mysqli_query($connect,$query);
while($row = mysqli_fetch_array($result))
{
echo "<tr>
<td>".$row['Driver_ID']."</td>
<td>" . $row['Driver_Name'] . "</td>
<td>" . $row['User_Name'] . "</td>
<td><p data-placement='top' data-toggle='tooltip' title='Edit'><button class='btn btn-primary btn-xs' data-title='Edit' data-toggle='modal' data-target='#edit'value='".$row['Driver_ID']."' name='editr'><span class='glyphicon glyphicon-pencil'></span></button></p></td>
<td><p data-placement='top' data-toggle='tooltip' title='Delete'><button class='btn btn-danger btn-xs' data-title='Delete' data-toggle='modal' data-target='#delete'><span class='glyphicon glyphicon-trash'></span></button></p></td>
</tr>";
$id=$row['Driver_ID'];
}
mysqli_close($connect);
?>
The html code for the hidden control
<input class="form-control " type="hidden" value="<?php echo $id ?>" name="driverid" >
the code for the update button
<?php
require("dbconnection.php");
if(isset($_POST['update']))
// include Database connection file
{
$driver_id=$_POST['driverid'];
$driver_name = $_POST['txtname'];
$user_id = $_POST['UserN'];
$sql = "UPDATE drivers
SET Driver_Name='$driver_name',User_ID='$user_id'
WHERE Driver_ID='$driver_id'";
if (mysqli_query($connect, $sql)) {
echo "Record updated successfully";
header('Refresh:5 ; url=drivers.php');
}
else {
echo "Error updating record: " . mysqli_error($connect);
}
}
mysqli_close($connect);
?>
Use input like this:
As you need all results you need to store them all
your main while loop could be like
$r=null;
while(){
$r++;
echo '<input class="form-control " type="hidden" value="<?php echo $id ?>" name="driverid[]" >'
}
echo '<input type="hidden" value='".$r."' name="records">';
After that just use some loop to see all results
and then in update /insert
$records=htmlentities($_POST['records'], ENT_QUOTES, "UTF-8");
$i=0;
while ($i <= $records){
$driverid=htmlentities($_POST['driverid'][], ENT_QUOTES, "UTF-8");
//your update/insert query here
$i++;
}
i hope this helps
Related
Here is my code. There's is a "Edit" link. I want to update data and save with that link
$op_sql = "SELECT * FROM tbl_users";
$result = $con->query($op_sql);
if ($result->rowCount() > 0)
{
echo "<table class='table' style='margin-left: 301px; margin-top: 10px; background-color: white;'>-;
echo "<tr>";
echo "<th>ID</th>";
echo "<th>Name</th>";
echo "<th>Email</th>";
echo "<th>Date of Birth<th>";
echo "<th></th>"; echo "</tr>";
while($row = $result->fetch())
{
echo "<tr>";
echo "<td>" . $rowrID1 . "</td>";
echo "<td>" . $row['Name'] . "</td>";
echo "<td>" . $row['Email']. "</td>";
echo "<td>" . $row['Date of Birth'] . "</td>";
echo "<td><a href='?id=".$row['ID']."'>Edit</a></td>";
echo "</tr>";
}
echo "</table>"; unset($result);
You can add the user_edit.php url in anchor tag href to edit the user details like this.
echo "<td><a href='user_edit.php?id=".$row['ID']."'>Edit</a></td>";
user_edit.php
Here you can get the id of specific user and get the details from database and create update form like this.
if(isset($_GET['id']) && $_GET['id']!='')
{
$op_sql = "SELECT * FROM tbl_users where id=$_GET['id']";
$result = $con->query($op_sql);
if ($result->rowCount() > 0)
{
$row = $result->fetch();
?>
<form action="user_update.php" method="post">
<input type="text" name="Name" value="<?php $row['Name'] ?>" />
.
.
<input type="hidden" name="id" value="$_GET['id']" />
.
.
<input type="submit" name="update" value="update" />
<?php
}
}
user_update.php
Here you can get the updated form data and update in database
if(isset($_POST['update']) && isset($_POST['id']) && $_POST['id']!='' )
{
// here you can get the updated form data and update in database
// update user set Name=$_POST['Name'],.... where id=$_POST['id'];
}
NOTE :
Try to use Prepared statement or PDO
I am giving an answer from This question you can use the same answer in your context here I'm providing the code from the accepted answer.
You can use a get method here in place of the post according to your situation.
HTML form:
<form method="post" action="handler.php">
<button type="submit" name="passed" id="passed" class="btn btn-success btn-flat"><i class="fa fa-check"></i></button>
<button type="submit" name="insufficient" id="insufficient" class="btn btn-danger btn-flat"><i class="fa fa-times"></i></button>
</form>
PHP:
<?php
// db connection
if(isset($_POST['passed'])){
$allowed = mysqli_query($conn," UPDATE users SET Access = "1" WHERE id = '27' ");
}
if(isset($_POST['insufficient'])){
$notallowed = mysqli_query($conn," UPDATE users SET Access = "0" WHERE id = '453' ");
}
Be careful if this has any user interaction at any given point.
Use a prepared statement, or PDO with prepared statements, they're much safer.
I have this quiz which displays a question with 4 answers beneath. What I'm trying to do is show the next question when the first question is answered.
The questions and multiple choice answers are grabbed from the database and put inside buttons with a loop. I want the next question to show when any of the 4 buttons is clicked. I have tried to do this with questionId where question 1 has an id of 1 and question 2 an id of 2. The value of k goes to 2 but it doesn't show the question with id 2. If I manually change line 3 from $k=1; to $k=2; it shows the question with id 2. The end goal is to increment $k every time an option is clicked to show the next question.
<?php
$conn = mysqli_connect("127.0.0.1", "root", "", "vragendb");
$k=1;
$sql = "SELECT * FROM vraag WHERE vraagId = '".$k."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$i =0;
while($row = $result->fetch_assoc()) {
echo " Vraag: ". $row["vraag"].
"<ul class = 'answers".$i."'.>
</br> <input type='button' class='btn btn-default' name='Optie".$i."' value=".$row['optie1']." id='optie1".$i."'> </br>".
"</br> <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie2"]." id='optie2".$i."'> </br> ".
"</br> <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie3"]." id='optie3".$i."'> </br>".
"</br> <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie4"]." id='optie4".$i."'> </br>".
"</br></br>
</ul>";
$i++;
$k++;
}
} else {
echo "0 results";
}
echo $k;
?>
Have you tried using $_GET parameters?
You can redirect the user to a URL for the next question, something like:
http://example.com/quiz.php?question=1
where question will be the $k in your case.
As demonstrated here, the user navigates to the ^above-mentioned URL and processes the stuff as follows:
<?php
$conn = mysqli_connect("127.0.0.1", "root", "", "vragendb");
$k = $_GET['question']; //this gets the question depending on your parameter
$sql = "SELECT * FROM vraag WHERE vraagId = '".$k."'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$i =0;
while($row = $result->fetch_assoc()) {
// we changed stuff here to accommodate the changes
echo " Vraag: ". $row["vraag"].
"<ul class = 'answers".$i."'.>
</br> <a href='http://example.com/quiz.php?question=".$k++."'> <input type='button' class='btn btn-default' name='Optie".$i."' value=".$row['optie1']." id='optie1".$i."'> </a> </br>".
"</br> <a href='http://example.com/quiz.php?question=".$k++."'> <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie2"]." id='optie2".$i."'</a> </br> ".
"</br> <a href='http://example.com/quiz.php?question=".$k++."'> <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie3"]." id='optie3".$i."'> </a> </br>".
"</br> <a href='http://example.com/quiz.php?question=".$k++."'> <input type='button' class='btn btn-default' name='Optie".$i."' value=". $row["optie4"]." id='optie4".$i."'> </a> </br>".
"</br></br>
</ul>";
$i++;
$k++;
}
} else {
echo "0 results";
}
echo $k;
?>
I am really struggling here and not sure why. I am trying to remove a row in my data base when the user click the delete button thats in the same button as the row their trying to remove.
Home.php
<table class="table table-bordered table-striped table-responsive sortable">
<tr class="header">
<td>id</td>
<td>Rep</td>
<td>Date</td>
<td>Name</td>
<td>P_O</td>
<td>Due Date</td>
<td>Terms</td>
<td>Aging</td>
<td>Open Balance</td>
<td>remove</td>
</tr>
<?php
while($row = mysql_fetch_array($query)) {
$className ="";
if ($row['Aging'] >= 45)
{
$className="danger";
}
else if($row['Aging'] >= 25 && $row['Aging'] <= 44)
{
$className="warning";
}
echo "<tr class='$className'>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['Rep']."</td>";
echo "<td>".$row['Date']."</td>";
echo "<td>".$row['Name']."</td>";
echo "<td>".$row['P_O']."</td>";
echo "<td>".$row['Due_Date']."</td>";
echo "<td>".$row['Terms']."</td>";
echo "<td>".$row['Aging']."</td>";
echo "<td>".$row['Open_Balance']."</td>";
echo "<td><button id='" .$row['id']. "' value='" .$row['id']. "' class='btn btn-danger'> Delete</button></td>";
}
?>
</table>
deletepage.php
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if(isset($_GET['id'])){
$userID = (int) $_GET['id'];
if(!empty($_GET['id'])){
$delete = mysqli_query($conn, "DELETE FROM Book1 WHERE id='$userID'");
}
if($delete){
echo "Record deleted successfully";
}else{
echo "Sorry, record could not be deleted";
}
}
?>
If you connect with mysqli, use mysqli throughout.
I've added an i here:
$delete = mysqli_query($conn, "DELETE FROM Book1 WHERE id='$userID'");
And change your button once to this. You aren't passing the variable to get currently, you;d do that by adding a question mark to the URL and specifying variables. Also, Since you are using twitter bootstrap, anchors with class btn will look like a button, so a form is not necessary.
echo "<td><a href='deletepage.php?id=" .$row['id']. "' value='" .$row['id']. "' class='btn btn-danger'> Delete</a></td>";
Starting at while($row = mysql_fetch_array($query)) {, it looks like you start a <tr>, but it doesn't look like you ever close it. At the bottom of your <td>s, you'll want a echo "";
Not a huge deal, and this won't fix your issue.
Like I said in the comment, action isn't realyl an attribute of <button>. See here
What you will probably need to do is wrap it in a form, which you said you did, and add type="submit" to the button tags.
<form action="deletepage.php" method="GET">
...table html
echo "<td><button id='" .$row['id']. "' type="submit" value='" .$row['id']. "' class='btn btn-danger'> Delete</button></td>";
</form>
This sounds like what your issue is, to me at least. You need to submit an html form to deletepage.php, or try another route like an AJAX request to deletepage.php.
I am trying to create a table that will allow me to delete a row by clicking a delete button. I did find an article on here at the following link that did provide some help but I am still unable to get it to work: Show all rows in mysql table then give option to delete specific ones .I have written most of the code but I am having problem with line 10 on the delete.php page. Ideas?
Main.php
$result = mysqli_query($con,"SELECT * FROM cs_general_info");
echo "<table><tr><td>Delete</td><td>First Name</td><td>Last Name</td><td>Address</td>td>Phone</td><td>E-Mail Address</td></tr>";
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td>
<form action="delete.php" method="POST">
<input type="hidden" name="delete_id" value="<?php echo $row['id']; ?>">
<input type="Submit" value="Delete"></form>
</td>
<?php
echo "<td>" . $row['id'] ."</td>";
echo "<td>" . $row['first_name'] . "</td>";
echo "<td>" . $row['last_name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['phone'] . "</td>";
echo "<td>" . $row['email'] . "</td></tr>";
}
echo "</table>";
Delete.php
"DELETE FROM cs_general_info WHERE id=".mysql_real_escape_string($delete_id);
Hopefully delete.php is more than that single line. In delete.php you will want to catch the POST method and the $row[id]; bind it, then delete it.
To get the id of the row you want to delete in your delete.php file, you need to get the variable, like this:
$delete_id = $_POST['delete'];
WARNING: This is the very basic functionality you need to get this to work. However, you will need to implement many other levels of security before this should ever go into any production code.
If that is your delete.php, then you are missing a lot. You need a database connection and you need to execute the SQL command against the database. But that is the least of your worries until you get your parameters via $_POST.
Here is the PDO Manual for the database connection and SQL execution.
And here is the manual for $_POST.
You want to either put a link or inline form within each of your table’s rows, and then have a script that deletes the specified row and returns the user to your page.
<table>
<tbody>
<?php foreach ($results as $result): ?>
<tr>
<td><?php echo $result->title; ?></td>
<td>
<form action="delete.php" method="post">
<input type="hidden" name="id" value="<?php echo $result->id; ?>" />
<input type="submit" value="Delete" />
</form>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
And then delete.php:
<?php
if (isset($_POST['id']) && intval($_POST['id']) > 0) {
// create PDO instance; assign it to $db variable
$sql = "DELETE FROM `table` WHERE `id` = :id LIMIT 1";
$smt = $db->prepare($sql);
$smt->bindParam(':id', $_POST['id'], PDO::PARAM_INT);
$smt->execute();
}
header('Location: index.php');
I would like to apologize if the duplicate of this question exist. i tried to find and could find anything here that could solve my problem..
I am using a form to get the input and update it in the mysql database, and then retrieve the records in the html form, and have defined the code for deleting the records individually through hyperlinks. however i want to do more, i want to use the checkboxes to delete the multiple records.
my code goes like this.
<?php
//include connection string
include('connection.php');
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post"/>
Username : <input type="text" name="user"/><br />
Password : <input type="password" name="pass"/><br />
<input type="submit" name="submit" value="Send"/>
</form>
<?php
// query to insert into database
if(isset($_POST['user']) && isset($_POST['pass'])) {
$user = empty($_POST['user']) ? die(mysql_error()) : mysql_escape_string($_POST['user']);
$pass = empty($_POST['pass']) ? die(mysql_error()) : sha1(mysql_escape_string($_POST['pass']));
$query = "INSERT INTO users(name, pass) VALUES ('$user', '$pass')";
$result = mysql_query($query) or die(mysql_error());
}
if(isset($_GET['id'])) {
//query to delete the records
$query = "DELETE FROM users WHERE id = " . intval($_GET['id']);
$result = mysql_query($query);
}
//query to retrieve records
$query = "SELECT * FROM users";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0 ) {
echo "<table cellpadding=10 border=1>";
while ($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "<td>delete";
echo "</tr>";
}
echo "</table>";
}
?>
i would like you to know that i am a newbie to programming world and i am not so sure of how exactly html checkbox work and how do i use it to delete the multiple records. i want to know what extra code do i have to write for it, and i would appreciate a lot if someone explains me that extra code in brief..
thank you..
This is probably a good time for another form:
<?php
// query to insert into database ...
// ... etc...
if(isset($_POST["formDeleteSelected"])) {
//query to delete the records
$query = "DELETE FROM users WHERE id IN (" . implode(", ",$_POST["rowid"]) . ")";
$result = mysql_query($query);
header("Location: mycode.php"); // just so 'refresh' doesn't try to run delete again
exit();
}
?>
<form action="<?php $_SERVER['PHP_SELF'] ?>" method="post">
<?php
//query to retrieve records
$query = "SELECT * FROM users";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0 ) {
echo "<table cellpadding=10 border=1>";
while ($row = mysql_fetch_row($result)) {
echo "<tr>";
echo "<td><input type="checkbox" name="rowid[]" value=\"" . $row[0] . "\" /></td>";
echo "<td>" . $row[0] . "</td>";
echo "<td>" . $row[1] . "</td>";
echo "<td>" . $row[2] . "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
<input type="submit" name="formDeleteSelected" text="Delete Selected" />
</form>
Or something like that (I haven't actually tried that code so there may be a typo). Also note that you should make sure to sanitize any form/get inputs for SQL Injection (plenty of information on that in other Stack Overflow questions).
First of all you need a checkbox and the id you want to delete:
<input id="delete" type="checkbox" name="delete" /><label for="delete">Delete user</label>
<input type="hidden" name="user_id" value="12345" />
You can then test if the checkbox has been set and then manually set the GET parameter to reuse your existing code:
if(isset($_POST['delete'])){
$_GET['id'] = $_POST['user_id'];
}
That's not the most elegant solution but a really simple one that should work with your code.
try an SQL query with a list of IDs
... WHERE id=$sentIds[0] OR id=$sentIds[1] OR ...
or use a set operation
... WHERE id IN ($i1,$i2 ... );
You sure have to send ids in the form for this to work, but You know that ;)