redirect to another page - php

hello guys i have this problem that i couldn't solve the thing is i have two buttons one for delete and the other for edit, the delete is working flawlessly, but edit button doesn't seems to work to make it redirect to a php file + how i can get the meeting name with edit button when it's redirect to another page don't know how here's my code
<table class="table table-striped custab" >
<thead>
<tr>
<th>Title</th>
<th>Chairman</th>
<th>Summary</th>
<th> Date & Time</th>
</tr>
<?php
$findMeetings = "SELECT * FROM `meeting` WHERE chairman='".$name."'";
$result = mysqli_query($db, $findMeetings);
$numRows = mysqli_num_rows($result);
if($numRows == 0){
$empty = "<div class='alert alert-danger'>You are currently managing no meetings!</div>";
}
else{
$x = 0;
while($rows = mysqli_fetch_array($result)){
$title = $rows['title'];
$chairman = $rows['chairman'];
$date = $rows['time'];
$summary = $rows['summary'];
$meeting = "
<tr>
<th>".$title."</th>
<th>".$chairman."</th>
<th>".$summary."</th>
<th>".$date."</th>
<th><form method='post'>
<input type='submit' class='btn btn-success' name='edit".$x."' value='Edit'/>
<input type='submit' class='btn btn-danger' name='delete".$x."' value='Delete'/>
</form></th>
</tr>
";
echo $meeting;
if(isset($_POST['delete'.$x.''])){
$query = "DELETE FROM meeting WHERE title='".$title."' LIMIT 1";
if($result = mysqli_query($db, $query)){
header("Location:managemeeting.php");
}
}
}
}
?>
</thead>
<tbody>
</tbody>
</table>
so how to get the meeting name and the passed it to editmeeting.php cause there's multiple data.
if i do it like delete button nothing happen like this
if(isset($_POST['edit'.$x.''])){
header("Location:editMeeting.php");
}

You should wrap a <form> around each submit button, and specify the action in each <form> so that it posts to the correct url with respect to the button the <form> is wrapped around.
Then use <hidden> fields inside each <form> to pass data to the url you are posting to. Example: <input type="hidden" name="row_id" value="99" />

Related

I want to transfer data form one php file to another to update rows using that data

I have a MYSQL table with Update link button on each row. he Update link goes to update.php which has a form. now the problem is i want to fetch the id from the row clicked on to update.php and show that id on update form in id field. i have transfered the id variable through link. but in other .php file it is printing anything.
i want to print id in update form field when i click on update button
Here is the code in index.php file
Display.php=>
<html>
<head><title>Support Page</title></head>
<body>
<table border="2">
<tr>
<th>ID</th>
<th>NAME</th>
<th>Email</th>
<th>phone no</th>
<th>Product Name</th>
<th>Company</th>
<th>Query</th>
<th>Any Other Info</th>
<th>Status</th>
<th>Date</th>
<th>Operations</th>
</tr>
<?php
$query ="SELECT * FROM query_data";
$data = mysqli_query($conn,$query);
$total = mysqli_num_rows($data);
echo $result['id']." ".$result['Name']." ".$result['Email']." ".$result['phone_no']." ".$result['Product_Name']." ".$result['Company']." ".$result['Query']." ".$result['Any_Other_Query']." ".$result['Status']." ".$result['Date'];
if($total!=0)
{
while($result = mysqli_fetch_assoc($data))
{
echo "
<tr>
<td>".$result['id']."</td>
<td>".$result['Name']."</td>
<td>".$result['Email']."</td>
<td>".$result['phone_no']."</td>
<td>".$result['Product_Name']."</td>
<td>".$result['Company']."</td>
<td>".$result['Query']."</td>
<td>".$result['Any_Other_Info']."</td>
<td>".$result['Status']."</td>
<td>".$result['Date']."</td>
<td><a href = 'http://mexyz.tech/?page_id=586?i=$result[id]'>Update</td>
</tr>";
}
}else{
echo "No record found";
}
?>
</table>
</body>
</html>
and this is update.php=>
<?php
include("connection.php");
$id = $_GET['i'];
?>
form action="" method="GET">
Enter id : <input type="text" value="<?php echo "id" ?>" name="id" required>
<br><br>
Enter Query Code: <input type="text" name="QC">
<br><br>
<td>
<select name="Status">
<option>Choose Option</option>
<option>Pending</option>
<option>Assigned</option>
<option>Resolved</option>
</select>
</td>
<br><br>
<input type="submit" value="Submit" name="Submit">
</form>
<?php
if (isset($_POST['Submit'])) {
$ids = $_POST['id'];
$s = $_POST['Status'];
$QC = $_POST['QC'];
$tablename = "query_data";
$data = array("Status" =>$s, "Query_Code" =>$QC);
$wherecondition = array('id' => $ids);
$updated = $wpdb->update( $tablename, $data, $wherecondition );
// $sql = $wpdb->insert("query_data",array("Status" =>$s,"Query_Code" =>$QC));
if ($updated == true) {
echo "<script> alert('Submitted Successfully!...')</script>";
}
}
?>
</div><!-- #primary -->
<?php
if (astra_page_layout() == 'right-sidebar') :
get_sidebar();
endif;
get_footer();
I dont know exactly what you are trying to achieve from your code but
you should pass your update link with a query string if you want to get the id in your update.
this is how to pass the link with a query string
<td>Update</td>
if you do it that way then got to your update page
and do this
if (isset($_GET[id])){
$id = $_GET[id];
}

How do I pass multiple ID to jobCart page?

I've a job list page, displaying all jobs and there are checkbox next to each one, and there is an Add button at the bottom of the page, to add it to JobCart.php
I honestly don't know how to pass multiple record ID or single ID to jobCart.php
I want the when the user to click the button "Add" pass all selected ID to jobCart.php
please help me
<?php
// adding JobsLists.php to this page to interact witht it.
require ("../JobsLists.php");
//Connect to DB
//include_once("Project/CIEconn.php");
$mysqlCON= mysqli_connect("localhost", "root", "","CIE") or die(mysqli_connect_error());
mysqli_select_db($mysqlCON,'CIE') or die ("no database");
$ID = isset($_POST['Id']); // 1 2
if( isset($_POST['pick']) ){
if( empty($ID) || $ID == 0 ){
echo"<h4> please choose something to move to your job list </h4>";
}else{
// Code here ..
// here here ONLY for TEST to check if I can interact eith jobLists.php
addJob();
// to get all ID from each selected job
$impid = implode("' , '" , $_POST['Id']);
}
}
$sqlCommand = "SELECT * FROM Fiscal WHERE NoStudent > '0' ";
$result = mysqli_query($mysqlCON,$sqlCommand) or die(mysqli_error($mysqlCON));
echo '
<form action= "Fiscal.php" method = "post">
<table width ="100%" cellpadding ="4" border="1" >
<tr>
<th>Check </th>
<th>Jobs Name</th>
<th>Description</th>
<th> No Students needed</th>
<th>Due Date</th>
</tr>';
while ($row = mysqli_fetch_array($result) ){
// name = 'Id[]'
echo "<tr>
<td> <input type='checkbox' name='Id[]' value='". $row['Id'] ."' /> </td>
<td> ". $row['JobName'] ." </td>
<td> ". $row['Description'] ." </td>
<td> ". $row['NoStudent'] . "</td>
<td>". $row['DueDate'] ." </td>
</tr>";
}
echo '
</table>
<br/>
<div align="center">
<input type="submit" name="pick" value="Add Job" />
<input type="reset" value="Clear Marks" />
</div>
</form>
';
?>
<html>
<head><title> Fiscal </title></head>
<br>
<body>
</body>
</html>
First of all, start session at the very top of your page, like this:
<?php
session_start();
?>
And during the form processing store $_POST['Id'] array to $_SESSION superglobal and redirect the user to jobCart.php page using header() function.
And one more thing, $_POST['Id'] would be an array, so use count() function to see it's empty or not. So your code should be this:
// your code
if(isset($_POST['pick'])){
if(count($_POST['Id'])){
// store `$_POST['Id']` array to `$_SESSION` superglobal
$_SESSION['ids'] = $_POST['Id'];
// redirect the user to jobCart.php page
header("Location: jobCart.php");
exit();
}
}
// your code
And in jobCart.php you can do something like this:
$impid = implode("','" , $_SESSION['ids']);
// rest of your code

Delete multiple mysql rows with check box not working

Here Is my problem: I do not get any error with my code but my problem is when i click the 'Delete Multiple' Button it does nothing not even reload the page.
Note: By The Way the redirect_to(); function i created so do not get confused by thinking that is a php function or anything
PHP Code:
display_errors(E_ALL);
if(isset($_POST['muldelete'])) {
$mul = $_POST['checkdelete'];
$sql = "DELETE FROM cmarkers WHERE id = " . $mul;
$result = mysqli_query($db, $sql);
redirect_to("elerts.php");
}
HTML Code:
<form action="elerts.php" method="post">
<table class="table table-striped">
<tr>
<td> </td>
<td>Date</td>
<td>Comment</td>
<td>Actions</td>
</tr>
<?php
$sql = "SELECT * FROM cmarkers";
$result = $db->query($sql);
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><input type="checkbox" name="checkdelete[]" value="<?php echo $row['id']; ?>" /></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['comment']; ?></td>
<td>DeleteEdit</td>
</tr>
<?php
}
?>
<input type="submit" name="muldelete" value="Delete Multiple" />
</table>
</form>
Thank You
If you need more info please let me know
First, your code contain some attention and placements errors.
input between <table> outer of td's is incorrect.
You can't make a multiple delete if you generate one form by value to
delete.
Fix them.
Getting Array of muldelete
To all the checked inputs, you must add the array field symbol
to clusterize the name "muldelete" to a post array.
<td><input type="checkbox" name="checkdelete[]" value="<?php $row['id']; ?>" /></td>
PHP side
Now you can fetch whole deletion array, like this:
if(!empty($_POST["muldelete"]))
{
$mul = join(',', $_POST['checkdelete']);
// Using IN() to make only one query for all records instead of multiple
// ex: IN(3, 4, 54, 8)
$query = "DELETE FROM cmarkers WHERE id IN(".$mul.")";
$result = mysqli_query($db, $query);
redirect_to("elerts.php");
}
Security
If ID's are integer value, you can prevent string injection into the sql query
$mul = array_map(function($id)
{
return intval($id);
}, $mul);
Your button is outside the <form></form> tags, so it is not related to the form elements or the form method at all. Instead of having a different form for each checkbox you should surround the entire table with the form tags thus ensuring that all the checkboxes and the button are in the same form.
<form method='post' action='elerts.php'>
<table class="table table-striped">
...all your table data including checkboxes...
<input type="submit" name="muldelete" value="Delete Multiple" />
</table>
</form>
I think because You are closing form tag earlier than submit button.
Try to put whole table into and should work.
PHP should looks like
display_errors(E_ALL);
if(isset($_POST['muldelete'])) {
$mul = implode(',',$_POST['checkdelete']);
$sql = "DELETE FROM cmarkers WHERE id IN(" . $mul.")";
$result = mysqli_query($db, $sql);
redirect_to("elerts.php");
}

Cannot delete row using check box

I'm trying to delete a row from table which is connected to database using checkbox (and it wouldn't matter if its just one row or multiple rows), but it is not working, as in nothing happens. It doesn't delete, no errors or warnings appear just a refresh.
php:
<?php
if(isset($_POST['del_event']))
{
if(isset($_POST['check']) && count($_POST['check']))
{
$array = array("check" => $_POST['check']);
$id = implode(',', $array['check']);
$result = mysql_query("DELETE FROM event WHERE event_id = '$id'") or die(mysql_error());
}
}
?>
html:
<form class="buttons" method="post" action="event.php">
<div class="button-wrap">
<input type="button" id="add_event" name="add_event" value="Add Event"/>
<input type="submit" id="del_event" name="del_event" value="Delete Event"/>
</div>
</form>
<tbody class="tbody-event-list-table2">
<?php
require "connection.php";
$check = mysql_query("SELECT * FROM event") or die(mysql_error());
if(mysql_num_rows($check) > 0)
{
while($row = mysql_fetch_array($check))
{
$id = $row['event_id'];
$name = $row['event_name'];
$start = $row['start'];
$end = $row['end'];
$venue = $row['event_venue'];
echo
"<tr>
<td><input type='checkbox' name='check' id='check' class='check' value='$id'/><a href=''
class='event-link'>$name</a></td><td>$start</td><td>$end</td><td>$venue</td>";
echo "</tr>";
}
}
else
{
echo
"<tr>
<td colspan='4'>There Are No Events.</td>
</tr>";
}
?>
</tbody>
It is if(isset($_POST['del_event'])) because 'del_event' is a name of a delete button which is disabled unless a checkbox is checked.
<script>
var chkbox = $(".check"),
button = $("#del_event");
button.attr("disabled","disabled");
chkbox.change(function(){
if(this.checked){
button.removeAttr("disabled");
}else{
button.attr("disabled","disabled");
}
});
</script>
php version is 5.5.9
This would still give you an undefined error if it is the check variable
if (isset($_POST['check']) && count($_POST['check']) != 0)
but if a checkbox is off in a form it is not sent with the form so it does not exist.
Carpetsmoker was halfway right, a proper solution would be:
if (isset($_POST['check'])) {
if ( (int) $_POST['check'] !== 0) {
}
}
btw, it is better to always cast your variables to a propert type you are checking for.
In PHP 0 can be 0, false or null.
Prevent this by safe type checking with the ===
so i fixed the prob. first i had to remove "form" that was separating the buttons and the table so now "form" is wrapped around both buttons and table. second is the php coding for deletion has changed too.
<form class="buttons" method="post" action="event.php">
<div class="button-wrap">
<input type="button" id="add_event" name="add_event" value="Add Event"/>
<input type="submit" id="del_event" name="del_event" value="Delete Event"/>
<input type="submit" id="edit_event" name="edit_event" value="Edit Event">
</div>
<div class="event-list-table2" >
<table class="event-table">
<thead>
<tr>
<th>Event Name</th>
<th>Start Event</th>
<th>End Event</th>
<th>Venue</th>
</tr>
</thead>
<tbody class="tbody-event-list-table2">
<?php
require "connection.php";
$check = mysql_query("SELECT * FROM event") or die(mysql_error());
if(mysql_num_rows($check) > 0)
{
while($row = mysql_fetch_array($check))
{
$id = $row['event_id'];
$name = $row['event_name'];
$start = $row['start'];
$end = $row['end'];
$venue = $row['event_venue'];
echo
"<tr>
<td><input type='checkbox' name='check[]' class='check' value='$id'><a href='' class='event-link' value='$id' name='event-link'>$name</a></td><td>$start</td><td>$end</td><td>$venue</td>";
echo "</tr>";
}
}
else
{
echo
"<tr>
<td colspan='4'>There Are No Events.</td>
</tr>";
}
?>
</tbody>
<?php
if (isset($_POST['del_event']) && isset($_POST['check']))
{
foreach($_POST['check'] as $del_id)
{
$del_id = (int)$del_id;
$sql = mysql_query("DELETE FROM event WHERE event_id = $del_id") or die(mysql_error());
if($sql)
{
echo "<meta http-equiv=\"refresh\" content=\"0;URL=event.php\">";
}
}
}
?>
</table>
</div>
</form>

Attempting to update results generated from a while loop

My main issue that I am running into is basically this:
I have a while loop that generates results from a query. With the results that have been generated, I want the ability to update the table the original query was from.
The query produces the expected results, but the table is not being updated when I click the REMOVE button. I am also trying to find a solution for the results to be updated after the UPDATE query executes...
<?php
$sql = "SELECT * FROM vehicles WHERE sold='n' ORDER BY year DESC";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
echo
"
<tr>
<td style='border-bottom-style:dotted;padding-top:10px;padding-bottom:10px;font-size:.9em'>",$row['year'],"</td>
<td style='border-bottom-style:dotted;padding-top:10px;padding-bottom:10px;font-size:.9em'>",$row['make'],"</td>
<td style='border-bottom-style:dotted;padding-top:10px;padding-bottom:10px;font-size:.9em'>",$row['model'],"</td>
<td style='border-bottom-style:dotted;padding-top:10px;padding-bottom:10px;font-size:.9em'><input type='submit' name='remove' value='REMOVE' style='background-color:#C33;color:white;padding:10px;border-radius:5px;width:70px'/></td>
</tr>";
if(isset($_POST['remove'])){
$removeSql = "UPDATE `table`.`vehicles` SET `display`='0' WHERE `vin`='{$row['vin']}'";
mysql_query($removeSql) or die('check that code dummy');
}
}
mysql_close($connection);
?>
That's a submit button, will not work without form tag. You can't do it this way.
You can write the remove code on a separate page and convert that submit button to normal button and pass vin id on click of that button and call that page using ajax.
Or if you don't know ajax and want to do it on that page itself then do it this way :
<?php
$sql = "SELECT * FROM vehicles WHERE sold='n' ORDER BY year DESC";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
echo
"
<tr>
<td style='border-bottom-style:dotted;padding-top:10px;padding-bottom:10px;font-size:.9em'>",$row['year'],"</td>
<td style='border-bottom-style:dotted;padding-top:10px;padding-bottom:10px;font-size:.9em'>",$row['make'],"</td>
<td style='border-bottom-style:dotted;padding-top:10px;padding-bottom:10px;font-size:.9em'>",$row['model'],"</td>
<td style='border-bottom-style:dotted;padding-top:10px;padding-bottom:10px;font-size:.9em'>
<form action="" method="POST">
<input type="hidden" name="vin_id" value="<?php echo $row['vin']; ?>">
<input type='submit' name='remove' value='REMOVE' style='background-color:#C33;color:white;padding:10px;border-radius:5px;width:70px'/>
</form></td>
</tr>";
}
if(isset($_POST['remove'])){
$removeSql = "UPDATE `table`.`vehicles` SET `display`='0' WHERE `vin`='".$_POST['vin_id']."'";
mysql_query($removeSql) or die('check that code dummy');
}
mysql_close($connection);
?>

Categories