I have a HTML/PHP(MySql) related question
I just have troubles finding out how to pass more than one array to the same row in a database
Code: (- at the start of the page)
<?php
if ( isset($_POST['logActivity']) ) {
$date = trim($_POST['date']);
$date = strip_tags($date);
$date = htmlspecialchars($date); //Aquiring a date() function variable
foreach ($_POST['activity'] as $activity => $value){
$query="INSERT INTO monitorlog(dateofentry,activity)
VALUES ('$date','$value')";
mysqli_query($conn,$query);
}} ?>
(In the < body> section)
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" >
<table>
<tr>
<td width='100px' align='center'><input width='100%' value='" . $activity . "' name='activity[]' type='text' class='form-control' /></td><td>
<input width='100%' value='" . $activity2 . "' name='activity2[]' type='number' class='form-control' /></td>
</tr><tr>
<td width='100px' align='center'><input width='100%' value='" . $activity . "' name='activity[]' type='text' class='form-control' /></td><td>
<input width='100%' value='" . $activity2 . "' name='activity2[]' type='number' class='form-control' /></td>
</tr><tr>
<td width='100px' align='center'><input width='100%' value='" . $activity . "' name='activity[]' type='text' class='form-control' /></td><td>
<input width='100%' value='" . $activity2 . "' name='activity2[]' type='number' class='form-control' /></td>
</tr><tr><td>
<button type='submit' class='mybuttons' name='button'>Log to database</button>
</td></tr>
</table>
</form>
The code above successfully inputs the data of $activity in the database but it also limits the value of the input to $activity's value on that particular row in the database, whereas I need to pass both activity and activity2 to the same row.
Feels like I've tried everything by now to no effect. Hope you guys can help me
Best regards.
Related
Im not a PHP pro i started php 3 weeks ago my knowledge is low.
My Problem:
In my outdex.php there is table with content read out of my database.
if i click the icon to edit a column i get to edit.php
But it doesnt shown my editable column.
My Code:
outdex.php
<?php
require('config/config.inc.php');
$sql = "SELECT id, name, Status, Strasse, Telefon, ProblemBeschreibung, Datum FROM probleme WHERE id = 1 OR id > 1";
$result = mysqli_query($db, $sql);
if(mysqli_num_rows($result) > 0) {
// Ausgabe der Daten in einer row
echo '<table class="bordered"><thead>';
echo '<tr>';
echo '<th style="display: none;">ID</th>';
echo '<th>Name</th>';
echo '<th>Status</th>';
echo '<th>Straße</th>';
echo '<th>Telefon</th>';
echo '<th>Problembeschreibung</th>';
echo '<th>Eintragsdatum</th>';
echo '<th width="36"></th>';
echo '</tr></thead>';
echo '<tbody>';
while($adr = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>" . $adr['name'] . "</td>
<td>" . $adr['Status'] . "</td>
<td>" . $adr['Strasse'] . "</td>
<td>" . $adr['Telefon'] . "</td>
<td>" . $adr['ProblemBeschreibung'] . "</td>
<td>" . $adr['Datum'] . "</td>
<td>
<div id=\"test\">
<div id=\"div1\"><form action='edit.php' method='GET'>
<input type='hidden' name='id' value='" .htmlspecialchars($adr['id'], ENT_COMPAT, 'UTF-8') . "'/>
<input type='hidden' name='name' value='".htmlspecialchars($adr['name'], ENT_COMPAT, 'UTF-8')."'/>
<input type='hidden' name='Status' value='".htmlspecialchars($adr['Status'], ENT_COMPAT, 'UTF-8')."'/>
<input type='hidden' name='Strasse' value='".htmlspecialchars($adr['Strasse'], ENT_COMPAT, 'UTF-8')."'/>
<input type='hidden' name='Telefon' value='".htmlspecialchars($adr['Telefon'], ENT_COMPAT, 'UTF-8')."'/>
<input type='hidden' name='ProblemBeschreibung' value='".htmlspecialchars($adr['ProblemBeschreibung'], ENT_COMPAT, 'UTF-8')."'/>
<input type='hidden' name='Datum' value='".htmlspecialchars($adr['Datum'], ENT_COMPAT, 'UTF-8')."'/>
<input type='image' src='edit.png' height='16' width='16' name='edit' value='Bearbeiten' onClick=\"self.location.href='edit.php?=\">
</form></div>
<div id=\"div2\">
<form action='delete.php' method='POST'>
<input type='hidden' name='id' value='" . htmlspecialchars($adr['id'], ENT_COMPAT, 'UTF-8') . "'/>
<input type='image' src='delete.png' height='16' width='16' name='delete' value='Löschen' onClick=\"self.location.href='delete.php?\">
</form></div>
</div>
</td>
</tr>\n";
}
echo "</tbody></table>";
} else {
echo "0 Einträge gefunden<br>Es konnten keine Einträge in der Datenbank gefunden werden.";
}
mysqli_close($db);
?>
My edit.php
<table class="bordered">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Status</th>
<th>Strasse</th>
<th>Telefon</th>
<th>Problembeschreibung</th>
<th>Eintragsdatum</th>
</tr>
</thead>
<?php
include('config.inc.php');
if(isset($_GET['id'])){
$id = $_GET['id'];
} else {
die('Bitte eine ?id übergeben');
}
$statement = $pdo->prepare("SELECT * FROM probleme WHERE id = ?");
$statement->execute(array($id));
while($row = $statement->fetch()){
echo "<tr>
<form method='post' action='edit1.php'>
<td><input name='id' type='text' value='".$id."'</input></td>
<td><input name='name' type='text' value='" .$row['name']."'</input></td>
<td>
<select name='Status'>
<option>".$row['Status']."</option>
<option>FINISHED</option>
</select>
</td>
<td><input name='Strasse' type='text' value='".$row['Strasse']."'</input></td>
<td><input name='Telefon' type='number' value='".$row['Telefon']."'</input></td>
<td><input name='ProblemBeschreibung' type='text' value='".$row['ProblemBeschreibung']."'</input></td>
<td>".$row['Datum']."</td>
</tr>
</table>";
echo "
<input type='submit' value='Absenden' />
</form>";
}
?>
If I change GET to POST it doesnt work too.
hope you can help me.
I need my system to show only the set of 'minutes' a user clicks on and not every set of minutes that the current user is involved in.
At the minute when I click the 'image' to view a set of minutes it prints out each set of minutes that user is involved in, I need it to only print out and be able to edit that one specific set of minutes... the edit function works but I just need it to print out what the user clicks on and not every set..PLEASE Can ANYONE help with this I have spent all day and don't understand how to get this to work.
Below is the code that shows the php and query in the minutes.php page, when you click the image for 'view' it takes you to the 'viewstudentminutes.php' page, I only want it to print out the minutes you click on not every set.
<?php
session_start();
if (!(isset($_SESSION["sess_username"]))) header ("Location: index.php");
$currentUser=$_SESSION["sess_username"];
$dbQuery = $db->prepare("select * from minute where '$currentUser'=B_number ");
$dbQuery->execute();
$numMinutes = $dbQuery->rowCount();
echo "<p>There are $numMinutes sets of minutes in the system</p>";
$oddRow=true;
while ($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {
$ID = $dbRow['ID'];
$B_number = $dbRow['B_number'];
$Date = $dbRow['Date'];
$Time = $dbRow['Time'];
$Discussion = $dbRow['Discussion'];
$Actions = $dbRow['Actions'];
$Dateofnextmeeting = $dbRow['Dateofnextmeeting'];
$Status = $dbRow['Status'];
$E_number = $dbRow['E_number'];
$Supervisor_comments = $dbRow['Supervisor_comments'];
if ($oddRow) $rowClass="odd"; else $rowClass="even";
$oddRow=!$oddRow;
if (isset($_POST['editMinuteID']) && $_POST['editMinuteID']==$ID) {
echo "<tr class='$rowClass '>
<form style='display:inline' method='post' action='minutes.php'>
<input type='hidden' name='editCommitID' value='$ID'>
<td><input type='varchar' name='editB_number' value='$B_number'></td>
<td><input type='date' name='editDate' value='$Date'></td>
<td><input type='time' name='editTime' value='$Time'></td>
<td><input type='text' name='editDiscussion' value='$Discussion'></td>
<td><input type='text' name='editActions' value='$Actions'> </td>
<td><input type='date' name='editDateofnextmeeting' value='$Dateofnextmeeting'></td>
<td><input type='enum' name='editStatus' value='$Status'> </td>
<td><input type='varchar' name='editE_number' value='$E_number'></td>
<td><input type='text' name='editSupervisor_comments' value='$Supervisor_comments'></td>
<td colspan='2'><input type='image' src='edit.png'>
</form>
</tr>";
} else {
echo "<tr class='$rowClass'><td>$B_number</td><td>$Date</td> <td>$Time</td><td>$Discussion</td><td>$Actions</td><td>$Dateofnextmeeting</td> <td>$Status</td><td>$E_number</td><td>$Supervisor_comments</td>
echo "<tr class='$rowClass'><td>$B_number</td><td>$Date</td><td>$Time</td><td>$Discussion</td><td>$Actions</td><td>$Dateofnextmeeting</td><td>$Status</td><td>$E_number</td><td>$Supervisor_comments</td>
<td class='operation'>
<form class='display:inline' method='post' action='viewstudentminutes.php'>
<input type='hidden' name='editMinute_ID' value='$ID'>
<input type='image' src='edit.png' style='padding-top:7px'>
</form></td>
<td class='operation'>
<form class='display:inline' method='post' action='minutes.php'
onsubmit='return confirm(\"Are you sure?\")'>
<input type='hidden' name='deleteMinuteID' value='$ID'>
<input type='image' src='delete.png' style='padding-top:7px'>
</form></td>
<td class='operation'>
<form class='display:inline' method='post' action='viewstudentminutes.php'
onsubmit='viewstudentminutes.php'>
<input type='image' src='view.png' style='padding-top:7px'>
</form></td>
</tr>";
}
}
?>
Below is the code for the 'viewstudentminutes.php' when trying to edit the set of minutes
<h2>Minutes</h2>
<table class="table table-condensed">
<?php
$dbQuery= $db->prepare("SELECT * FROM `Minute` WHERE `B_number` = '" . $currentUser . "' AND `id` = " . $_POST['Minute_ID']);
$dbQuery->execute();
$oddRow=true;
while ($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {
$ID = $dbRow['ID'];
$B_number = $dbRow['B_number'];
$Date = $dbRow['Date'];
$Time = $dbRow['Time'];
$Discussion = $dbRow['Discussion'];
$Actions = $dbRow['Actions'];
$Dateofnextmeeting = $dbRow['Dateofnextmeeting'];
$Status = $dbRow['Status'];
$E_number = $dbRow['E_number'];
$Supervisor_comments = $dbRow['Supervisor_comments'];
if ($oddRow) $rowClass="odd"; else $rowClass="even";
$oddRow=!$oddRow;
if (isset($_POST['editMinuteID']) && $_POST['editMinuteID']==$id) {
echo "<tr class='$rowClass'>
<form style='display:inline' method='post' action='viewstudentminutes.php'>
<input type='hidden' name='editCommitID' value='$ID'>
<tr><td colspan='2'><input type='image' src='edit.png'> </tr>
<tr><th>BNumber</th><td><input type='varchar' name='editB_number' value='$B_number'></td></tr>
<tr><th>Date</th><td><input type='date' name='editDate' value='$Date'></td></tr>
<tr><th>Time</th><td><input type='time' name='editTime' value='$Time'></td></tr>
<tr><th>Discussion</th><td><input type='text' name='editDiscussion' value='$Discussion'></td></tr>
<tr><th>Actions</th><td><input type='text' name='editActions' value='$Actions'></td></tr>
<tr><th>Date of Next Meeting</th><td><input type='date' name='editDateofnextmeeting' value='$Dateofnextmeeting'></td></tr>
<tr><th>Status</th><td><input type='enum' name='editStatus' value='$Status'></td></tr>
<tr><th>E Number</th><td><input type='varchar' name='editE_number' value='$E_number'></td></tr>
<tr><th>Supervisor Comments</th><td><input type='text' name='editSupervisor_comments' value='$Supervisor_comments'></td></tr>
</form>
</tr>";
} else {
echo "<tr class='$rowClass'>
<td class='operation'>
<form class='display:inline' method='post' action='viewstudentminutes.php'>
<input type='hidden' name='editMinute_ID' value='$ID'>
<input type='image' src='edit.png' style='padding-top:7px'>
</form></td>
<tr><th>BNumber</th><td>$B_number</td></tr>
<tr><th>Date</th><td>$Date</td></tr>
<tr><th>Time</th><td>$Time</td></tr>
<tr><th>Discussion</th><td>$Discussion</td></tr>
<tr><th>Actions</th><td>$Actions</td></tr>
<tr><th>Date of Next Meeting</th><td>$Dateofnextmeeting</td></tr>
<tr><th>Status</th><td>$Status</td></tr>
<tr><th>E Number</th><td>$E_number</td></tr>
<tr><th>Supervisor Comments</th><td>$Supervisor_comments</td></tr>
</tr>";
}
}
?>
</table>
The Minute table within the database holds the following fields...
1 ID Primary int(10) AUTO_INCREMENT
2 B_number varchar(15) latin1_swedish_ci
3 Date date
4 Time time
5 Discussion text latin1_swedish_ci
6 Actions text latin1_swedish_ci
7 Dateofnextmeeting date
8 Status enum('Submitted', 'Approved', 'Rejected', '') latin1_swedish_ci
9 Supervisor_comments text latin1_swedish_ci
10 E_number varchar(15) latin1_swedish_ci
Please help :(
There are a few variables that I'm unsure of what they are - updating your question to include what $currentUser is would help.
That said, the issue may be in your SQL statement:
$db->prepare("select * from minute where '$currentUser'=B_number ");
You are selecting where $currentUser is equal to B_number. But in your database schema, you show that B_number is a column.
I'd try this SQL statement instead:
$db->prepare("SELECT * FROM `Minute` WHERE `B_number` = '" . $currentUser . "' AND `id` = " . $yourIDVariable );
Edit
Your viewstudentminutes.php will iterate over every record returned in your SQL query. You need to uniquely identify your Minute record to show just one on your viewstudentminutes.php page. You access this page via clicking on an <img> tag. But you never send data for which Minute object you want with this form. You could add this via:
<form class='display:inline' method='post' action='viewstudentminutes.php' onsubmit='viewstudentminutes.php'>
<input type='image' src='view.png' style='padding-top:7px'>
<input type='hidden' name='minute_id' value='" . $ID . "'>
</form>
Then access this on your viewstudentminutes.php as
$db->prepare("SELECT * FROM `Minute` WHERE `B_number` = '" . $currentUser . "' AND `id` = " . $_POST['minute_id']);
So i guess i don't understand this i am a very newbie to coding in general. I have searched and can't find a good enogh explanation to get it to work in my situatation. I need to fill a table from a fetch command then update each result with an input of information into a new column. Here is the code i have:
This fills the table:
echo "<table border='1'>
<tr>
<th>Envelope</th>
<th>Budget</th>
<th>Amount</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['envelopename'] . "</td>";
echo "<td>" . $row['envelopebudget'] . "</td>";
?><td><input type="text" name="budgetamount"></td><?php;
echo "</tr>";
}
echo "</table>";
?>
<input type="submit">
And this writes the input for budgetname into the column budgetname:
$paycheckname = mysqli_real_escape_string($con, $_POST['paycheckname']);
$budgetamount = mysqli_real_escape_string($con, $_POST['budgetamount']);
$envelopename = mysqli_real_escape_string($con, $_POST['envelopename']);
}
$sql="UPDATE envelopes SET $paycheckname='$budgetamount' WHERE envelopename ='$envelopename'";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
Right now it just doesn't write anything. I have used echo var_dump($envelopename)."<br>"; and echo var_dump($budgetamount)."<br>"; to try and see what it is doing but $envelopename is always blank. Thanks for any help you can provide.
only <input> , <textarea>, <select> and <button> are submitted to the server in a form
If you want to re-submit some static values create some hidden inputs
while($row = mysqli_fetch_array($result)) {
echo "<input type='hidden' value='$row[envelopename]' name='envelopename'/>";
echo "<input type='hidden' value='$row[envelopebudget]' name='envelopebudget'/>";
echo "<tr>";
echo "<td>" . $row['envelopename'] . "</td>";
echo "<td>" . $row['envelopebudget'] . "</td>";
?><td><input type="text" name="budgetamount"></td><?php;
echo "</tr>";
}
echo "</table>";
But your next issue will be that you're creating the elements in a loop so you'll have multiple inputs with the same name
<tr>
<input type='hidden' value='envName1' name='envelopename'/>
<input type='hidden' value='envBudget1' name='envelopename'/>
<td>envName1</td>
<td>envBudget1</td>
<td><input type="text" name="budgetamount"></td>
</tr>
<tr>
<input type='hidden' value='envName2' name='envelopename'/>
<input type='hidden' value='envBudget2' name='envelopename'/>
<td>envName2</td>
<td>envBudget2</td>
<td><input type="text" name="budgetamount"></td>
</tr>
so you must submit as an array
while($row = mysqli_fetch_array($result)) {
echo "<input type='hidden' value='$row[envelopename]' name='envelopename[]'/>";
echo "<input type='hidden' value='$row[envelopebudget]' name='envelopebudget[]'/>";
echo "<tr>";
echo "<td>" . $row['envelopename'] . "</td>";
echo "<td>" . $row['envelopebudget'] . "</td>";
?><td><input type="text" name="budgetamount[]"></td><?php;
echo "</tr>";
}
echo "</table>";
and at the server end process as an array
foreach ($_POST['budgetamount'] as $budgetamount){
echo $budgetamount. '<br>';
}
suppose the form rendered looks like this:
<tr>
<input type='hidden' value='envName1' name='envelopename[]'/>
<input type='hidden' value='envBudget1' name='envelopebudget[]'/>
<td>envName1</td>
<td>envBudget1</td>
<td><input type="text" name="budgetamount[]"></td>
</tr>
<tr>
<input type='hidden' value='envName2' name='envelopename[]'/>
<input type='hidden' value='envBudget2' name='envelopebudget[]'/>
<td>envName2</td>
<td>envBudget2</td>
<td><input type="text" name="budgetamount[]"></td>
</tr>
<tr>
<input type='hidden' value='envName3' name='envelopename[]'/>
<input type='hidden' value='envBudget3' name='envelopebudget[]'/>
<td>envName3</td>
<td>envBudget3</td>
<td><input type="text" name="budgetamount[]"></td>
</tr>
when the user hits submit the $_POST that arrives at newpaycheck.php will look like:
$_POST
['envelopename']{
[0]=>'envName1',
[1]=>'envName2',
[2]=>'envName3'
},['envelopebudget']{
[0]=>'envBudget1',
[1]=>'envBudget2',
[2]=>'envBudget3'
},['budgetamount']{
[0]=>'someValueEnteredByUser',
[1]=>'anotherValueEnteredByUser',
[2]=>'yetAnotherValueEnteredByUser'
}
so you can do something like this:
foreach ($_POST['envelopename'] as $envelopename){
$arrayIndex = array_search($envelopename,$_POST['envelopename']);
$envelopebudget = $_POST['envelopebudget'][$arrayIndex];
$budgetamount= $_POST['budgetamount'][$arrayIndex];
$paycheckname = mysqli_real_escape_string($con, $envelopebudget);
$budgetamount = mysqli_real_escape_string($con,$budgetamount);
$envelopename = mysqli_real_escape_string($con,$envelopename);
$sql="UPDATE envelopes SET $paycheckname='$budgetamount' WHERE envelopename ='$envelopename'";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
}
I've tried asking this question before and I was rudely told I've posted too much code. So this time around I will do my best to only post relevant code.
So, I am trying to delete multiple rows using check boxes. I've looked up sooo many tutorials on it, and most use a for each loop. I haven't really figured out the difference between a while and for each loop. So, this is my front end php code:
$sql = "SELECT * FROM appointments WHERE date = '".$date."' ORDER BY appttime";
$result = mysqli_query($transport, $sql);
echo "<h2 align='center'>Schedule for $raw_date</h2>";
echo "<table border='0' style='width: 100%; margin: auto; border-width: 1px'><tr><th>Resident Name</th><th>APT #</th><th>Appt. Time</th><th>Location Phone</th><th>Location Name</th><th>Address</th><th>City</th><th>Zip</th><th>Bus or Car</th><th>Escort Name</th><th>Transfer</th><th>Comments</th><th>Dparting Times</th><th>Delete</th></tr>";
?>
<form name="update_times" method="post" action="depart_t.php">
<?php
$i=0;
while($row = mysqli_fetch_array($result))
{
echo "<input type='hidden' name='id[$i]' value='" . $row['id'] . "'>";
echo "<tr>";
echo "<td align='center'><input name='resident[$i]' style='width: 80px' type='text' value='" . htmlspecialchars($row['r_name'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='room_n[$i]' style='width: 40px' type='text' value='" . htmlspecialchars($row['room'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='appt_time[$i]' style='width: 55px' type='text' value='" . date("g:i A", strtotime($row['appttime'])) . "' /></td>";
echo "<td align='center'><input name='appt_phone[$i]' style='width: 65px' type='text' value='" . htmlspecialchars($row['apptphone'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='name_l[$i]' style='width: 80px' type='text' value='" . htmlspecialchars($row['l_name'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='address[$i]' style='width: 90px' type='text' value='" . htmlspecialchars($row['address'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='city[$i]' style='width: 70px' type='text' value='" . htmlspecialchars($row['city'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='zip[$i]' style='width: 50px' type='text' value='" . $row['zip'] . "' /></td>";
echo "<td align='center'><input name='buscar[$i]' style='width: 30px' type='text' value='" . htmlspecialchars($row['buscar'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='e_name[$i]' style='width: 60px' type='text' value='" . htmlspecialchars($row['escort_name'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='transfer[$i]' style='width: 40px' type='text' value='" . htmlspecialchars($row['transfer'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='comments[$i]' style='width: 80px' type='text' value='" . htmlspecialchars($row['comments'], ENT_QUOTES) . "' /></td>";
echo "<td align='center'><input name='out[$i]' style='width: 70px' type='text' value='" . date("g:i A", strtotime($row['depart'])) . "' /></td>";
echo "<td align='center'><input name='delete[$i]' type='checkbox' value='" . $row['id'] . "' /></td>";
echo "</tr>";
++$i;
}
echo "</table>";
?>
<br>
<input type="submit" value="Update" style="float:left; margin:5px" onclick="return confirm('Are you sure you want to proceed?')" /></form>
<form name="print" action="printer_f.php" method="post"><input name="p_friendly" type="submit" value="Printer Friendly View" style="float:left; margin:5px" /></form><form name="delete" action="delete.php" method="post"><input name="delete" type="submit" value="Delete Selected" onclick="return confirm('Are you sure you want to delete these entrys?')" style="float:left; margin:5px" /></form>
This is my delete code:
$size = count($_POST['delete']);
$i=0;
while ($i < $size)
{
$id = $_POST['id'][$i];
$sql = "DELETE FROM appointments WHERE id = $id";
echo $sql;
mysqli_query($transport, $sql) or die('Error: ' .mysqli_error($transport));
++$i;
}
With this code, all that ever happens is, the first row gets deleted no matter which one I select. I've tried using an if statement before it to check if a checkbox is actually checked, but that didn't seem to work either. So I am at a loss. Thank you for the help!
Use this:
$stmt = mysqli_prepare($transport, "DELETE FROM appointments WHERE id = ?");
mysqli_stmt_bind_param($stmt, "i", $id);
foreach ($_POST['delete'] as $id) {
mysqli_stmt_execute($stmt);
}
The foreach statement is better because the only delete[i] elements that are posted are the ones that are checked, so there will be gaps in the sequence. This code also shows how to use prepared statements instead of variable substitution.
The other problem you have is that the delete checkboxes conflict with this submit button:
<input name="delete" type="submit" value="Delete Selected" onclick="return confirm('Are you sure you want to delete these entrys?')" style="float:left; margin:5px" />
When you use that submit button, it sets $_POST['delete'] to Delete Selected, which overrides all the checkboxes. Give one of them a different name, and change the code that reads from that $_POST field.
okay this is the updated main php page now, ive cleared my previous post to make this clean, FYI...what im posting here are just my test page which is he exact replica of my actual page..just have different php page name...
<link href="jquery-ui-1.10.2.custom/css/dark-hive/jquery-ui-1.10.2.custom.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery-ui-1.10.2.custom/js/jquery-1.9.1.js" ></script>
<script type="text/javascript" src="jquery-ui-1.10.2.custom/js/jquery-ui-1.10.2.custom.js" ></script>
<script type="text/javascript" src="jquery-ui-1.10.2.custom/js/jquery-ui-1.10.2.custom.min.js" ></script>
<link rel="stylesheet" href="style.css" />
<script type="text/javascript" src="SMP1_deletefromDB.js"></script>
<script>
$(document).ready(function(){
$("#results").show();
});
</script>
<script type="text/javascript">
$(document).ready(function(){
$("#RetrieveList").on('click',function() {
var xid = $('#XiD').val();
var date = $('#Date').val();
$.post('resultgenerator_test.php',{xid:xid, date:date}, function(data){
$("#results").html(data);
});
return false;
});
});
//post to delete.php file. In delete.php you can get the id's in $_POST['id'] as multidimensional array. You can handle the delete operation based on that
$(document).ready(function(){
$("#DeletefromDB").on('click',function() {
//get all the checked values from checkboxes
var ids = $('input[name=checkbox]:checked').map(function () {
return this.value;
}).get();
if (ids.length === 0)
return false; //show some error message
//post to delete.php file. In delete.php you can get the id's in $_POST['id'] as multidimensional array. You can handle the delete operation based on that
$.post('deletedata.php',{id : ids}, function(data){
$("#results").html(data);
//handle the message based on success or error
});
return false;
});
});
</script>
</head>
<body class="oneColFixCtrHdr">
<div id="container" style="width:auto">
<div id="header" style="background-color:#7BD12E">
<h1 align="left" style="color:#FFF; font-family: Arial, Helvetica, sans-serif;">PIS Ticket Tracking System</h1>
<!-- end #header --></div>
<div id="mainContent">
<h1 style="font-size:9"></h1>
<form id="form1" name="form1" method="post" action="">
<p>
<label for="Back"></label>
<input type="button" name="Back" id="Back" value="Back To Main" href="#" onclick="return backAway();" />
</p>
</form>
<form id="form2" name="form2" method="post" action="">
<table width="741" border="0" align="center">
<tr>
<th colspan="9" align="center" style="font-size:12px" scope="col">Xid, Name:<span>
<select name="XiD" id="XiD">
<option value="AAA">AAA</option>
<option value="BBB">BBB</option>
<option value="CCC">CCC</option>
<option value="DDD">DDD</option>
<option value="EEE">EEE</option>
<option value="FFF">FFF</option>
<option value="" selected="selected"></option>
</select>
</span><span style="font-size:12px">
<label for="date">Date:</label>
<input type="text" name="Date" id="Date" />
</span></th>
</tr>
<tr>
<th colspan="9" scope="col"> </th>
</tr>
<tr>
<th colspan="9" scope="col">
<div align="center">
<input name="action" type="button" id="RetrieveList" value="Retrieve List" />
<input name="action" type="button" id="DeletefromDB" value="Delete from DB" />
<input name="Clear" type="reset" id="Clear" value="Clear" />
</div>
<label for="Clear"></label>
<div align="center"></div></th>
</tr>
</table>
</form>
<div id="results">
</div>
</div>
and here is my 2nd php page that echoes the data into table format: Please read my comments below as I need it to clarify...
jQuery(document).ready(function () {
jQuery("input[name=checkall]").click(function () {
jQuery('input[name=checkall]').prop('checked', this.checked);
jQuery('input[name=checkbox]').prop('checked', this.checked);
});
// if all checkbox are selected, check the selectall checkbox
// and viceversa
jQuery("input[name=checkbox]").click(function(){
if(jQuery("input[name=checkbox]").length == jQuery("input[name=checkbox]:checked").length) {
jQuery("input[name=checkall]").prop("checked", true);
} else {
jQuery("input[name=checkall]").prop("checked", false);
}
});
});
</script>
<?php
require 'include/DB_Open.php';
$xid = $_POST['xid'];
$date = $_POST['date'];
$sql="SELECT ars_no, phone_number, category_1, category_2, status, create_date, resolved_date, trouble_type_priority, ban_type, employee_id_name
FROM tbl_main
WHERE employee_id_name = '" . $xid . "' AND resolved_date = '" . $date . "'";
$myData = mysql_query($sql);
echo "<table width='auto' cellpadding='1px' cellspacing='0px' border=1 align='center'>
<tr>
<th align='center'><input id=checkall name=checkall type=checkbox value='' /></th>
<th align='center'>Remedy Ticket No.</th>
<th align='center'>Phone/Incident No.</th>
<th align='center'>Category 2</th>
<th align='center'>Category 3</th>
<th align='center'>Status</th>
<th align='center'>Create Date</th>
<th align='center'>Severity</th>
<th align='center'>Ban Type</th>
<th align='center'>Resolved Date</th>
//*I have added this header so that im fetching the employee_id_name as well but just hiding it so i can delete it from my 3rd php...i used the following but still showing a very small cell on the Xid column...*
<th align='center' style='display:none'>XiD</th>
</tr>";
while($info = mysql_fetch_array($myData))
{
echo "<form action='resultgenerator_test.php' method='post'>";
echo"<tr>";
echo "<td align='center'>" . "<input type=checkbox name=checkbox value=" . " </td>";
echo "<td align='center'>" . $info['ars_no'] . "<input type=hidden name=ars_no value=" . $info['ars_no'] . " </td>";
echo "<td align='center'>" . $info['phone_number'] . "<input type=hidden name=phone_number value=" . $info['phone_number'] . " size='11' maxlength='11' /> </td>";
echo "<td align='center'>" . $info['category_1'] . "<input type=hidden name=category_1 value=" . $info['category_1'] . "' /> </td>";
echo "<td align='center'>" . $info['category_2'] . "<input type=hidden name=category_2 value=" . $info['category_2'] . "' /> </td>";
echo "<td align='center'>" . $info['status'] . "<input type=hidden name=status value=" . $info['status'] . "' /> </td>";
echo "<td align='center'>" . $info['create_date'] . "<input type=hidden name=create_date value=" . $info['create_date'] . "' /> </td>";
echo "<td align='center'>" . $info['trouble_type_priority'] . "<input type=hidden name=trouble_type_priority value=" . $info['trouble_type_priority'] . " size='1' maxlength='1' /> </td>";
echo "<td align='center'>" . $info['ban_type'] . "<input type=hidden name=ban_type value=" . $info['ban_type'] . " size='1' maxlength='1' /> </td>";
echo "<td align='center'>" . "<input type=text name=resolved_date value=" . $info['resolved_date'] . " size='8' maxlength='8' /> </td>";
echo "<td align='center'>" . "<input type=hidden name=employee_id_name value=" . $info['employee_id_name'] . "' /> </td>";
echo "</tr>";
echo "</form>";
}
echo "</table>";
include 'include/DB_Close.php';
?>
</body>
</html>
now look at the 3rd php page that ive created as u suggested and explained...please correct if u see any errors on the code:
LATEST UPDATE delete php:
<?php
require 'include/DB_Open.php';
$id = $_POST['id'];
$idtodelete = "'" . implode("','",$id) . "'";
$query = "DELETE FROM tbl_main WHERE ars_no in (" . $idtodelete . ")";
$myData = mysql_query($query);
include 'include/DB_Close.php';
?>
thanks for all the help...:)
Here what you need to do is to handle the update and delete operations with the submit button name. What you need to do is to create two submit buttons with same name but different values.
This is your form :
<form action="" method="post">
<input name="action" type="submit" id="RetrieveList" value="RetrieveList" />
<input name="action" type="submit" id="DeletefromDB" value="DeleteFromDB" />
<input name="Clear" type="reset" id="Clear" value="Clear" />
</form>
I have given same name action for the two submit buttons. Once this form is submitted you can check the $_POST['action'] in the server side.
You can do it like:
if ($_POST['action'] == 'RetrieveList') {
//retreive list functionality
} elseif ($_POST['action'] == 'DeleteFromDB') {
//delete from dB functionality.
}
UPDATE:
You need to change your form like this :
echo "<form action='resultgenerator.php' method='post'>";
echo '<input name="action" type="submit" id="DeletefromDB" value="Delete from DB" />';
echo "<table width='auto' cellpadding='1px' cellspacing='0px' border=1 align='center'>
<tr>
<th align='center'><input id=checkall name=checkall type=checkbox value='' /></th>
<th align='center'>Remedy Ticket No.</th>
<th align='center'>Phone/Incident No.</th>
<th align='center'>Category 2</th>
<th align='center'>Category 3</th>
<th align='center'>Status</th>
<th align='center'>Create Date</th>
<th align='center'>Severity</th>
<th align='center'>Ban Type</th>
<th align='center'>Resolved Date</th>
</tr>";
while($info = mysql_fetch_array($myData)) {
echo"<tr>";
echo "<td align='center'>" . "<input type='checkbox' name='checkbox[]' value='add the id here which needs to be deleted'/></td>";
echo "<td align='center'>" . $info['ars_no'] . "<input type=hidden name=ars_no value=" . $info['ars_no'] . " </td>";
echo "<td align='center'>" . $info['phone_number'] . "<input type=hidden name=phone_number value=" . $info['phone_number'] . " size='11' maxlength='11' /> </td>";
echo "<td align='center'>" . $info['category_1'] . "<input type=hidden name=category_1 value=" . $info['category_1'] . "' /> </td>";
echo "<td align='center'>" . $info['category_2'] . "<input type=hidden name=category_2 value=" . $info['category_2'] . "' /> </td>";
echo "<td align='center'>" . $info['status'] . "<input type=hidden name=status value=" . $info['status'] . "' /> </td>";
echo "<td align='center'>" . $info['create_date'] . "<input type=hidden name=create_date value=" . $info['create_date'] . "' /> </td>";
echo "<td align='center'>" . $info['trouble_type_priority'] . "<input type=hidden name=trouble_type_priority value=" . $info['trouble_type_priority'] . " size='1' maxlength='1' /> </td>";
echo "<td align='center'>" . $info['ban_type'] . "<input type=hidden name=ban_type value=" . $info['ban_type'] . " size='1' maxlength='1' /> </td>";
echo "<td align='center'>" . "<input type=text name=resolved_date value=" . $info['resolved_date'] . " size='8' maxlength='8' /> </td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
Now in your resultgenerator.php file you can check :
if ($_POST['action'] == 'DeleteFromDB') {
$ids_to_be_deleted = isset($_POST['checkbox']) ? $_POST['checkbox'] : array();
//$ids_to_be_deleted will contain all the checked id's from the other page. You can get all those values in the array. Handle the remaining operation for delete here.
}
NEW UPDATE :
So if you are using jQuery to submit your form, then it would be better to change the form submit binding to click function on the button. You can change your form to :
<link href="jquery-ui-1.10.2.custom/css/dark-hive/jquery-ui-1.10.2.custom.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="jquery-1.9.1.min.js" ></script>
<script type="text/javascript" src="jquery-ui-1.10.2.custom/js/jquery-ui-1.10.2.custom.js" ></script>
<script type="text/javascript" src="jquery-ui-1.10.2.custom/js/jquery-ui-1.10.2.custom.min.js" ></script>
<link rel="stylesheet" href="style.css" />
<script type="text/javascript" src="SMP1_deletefromDB.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#RetrieveList").on('click',function() {
var xid = $('#XiD').val();
var date = $('#Date').val();
$.post('resultgenerator.php',{xid:xid, date:date}, function(data){
$("#results").html(data);
});
return false;
});
$("#DeletefromDB").on('click',function() {
//get xid for delete like you do above. create a page delete.php or something and make an ajax call to some page to delete data
return false;
});
});
</script>
</head>
<body class="oneColFixCtrHdr">
<div id="container" style="width:auto">
<div id="header" style="background-color:#7BD12E">
<h1 align="left" style="color:#FFF; font-family: Arial, Helvetica, sans-serif;">PIS Ticket Tracking System</h1>
<!-- end #header --></div>
<div id="mainContent">
<h1 style="font-size:9"></h1>
<form id="form1" name="form1" method="post" action="">
<p>
<label for="Back"></label>
<input type="button" name="Back" id="Back" value="Back To Main" href="#" onclick="return backAway();" />
</p>
</form>
<form id="form2" name="form2" method="post" action="">
<table width="741" border="0" align="center">
<tr>
<th colspan="9" align="center" style="font-size:12px" scope="col">Xid, Name:<span>
<select name="XiD" id="XiD">
<option value="AAA">AAA</option>
<option value="BBB">BBB</option>
<option value="CCC">CCC</option>
<option value="DDD">DDD</option>
<option value="EEE">EEE</option>
<option value="FFF">FFF</option>
<option value="" selected="selected"></option>
</select>
</span><span style="font-size:12px">
<label for="date">Date:</label>
<input type="text" name="Date" id="Date" />
</span></th>
</tr>
<tr>
<th colspan="9" scope="col"> </th>
</tr>
<tr>
<th colspan="9" scope="col">
<div align="center">
<input name="action" type="button" id="RetrieveList" value="RetrieveList" />
<input name="action" type="button" id="DeletefromDB" value="DeleteFromDB" />
<input name="Clear" type="reset" id="Clear" value="Clear" />
</div>
<label for="Clear"></label>
<div align="center"></div></th>
</tr>
</table>
<p> </p>
</form>
<div id="results">
</div>
</div>
</body>
</html>
Here What I have done is changed the binding event on form submit and binded on click event of input type buttons. I changed type submit to button.
Now you can check on the click event of each buttons and handle the operations.
UPDATE:
Assuming that you have the retrieved list in your results div. When the user check the checkboxes and click the delete button you can handle it using this script:
$("#DeletefromDB").on('click',function() {
//get all the checked values from checkboxes
var ids = $('input[name=checkbox]:checked').map(function () {
return this.value;
}).get();
if (ids.length === 0)
return false; //show some error message
//post to delete.php file. In delete.php you can get the id's in $_POST['id'] as multidimensional array. You can handle the delete operation based on that
$.post('delete.php',{id : ids}, function(data){
//handle the message based on success or error
});
return false;
});
UPDATE :
This can be your delete.php file
$ids = isset($_POST['id']) ? $_POST['id'] : '';
if (!empty($ids)) {
//implode the id's separated by commaas
$ids_to_be_deleted = implode(',', $ids);
$query = "DELETE FROM your_table WHERE field_to_be_checked IN ($ids_to_be_deleted)";
//now run your query using mysql_query
}
NB: : mysql* functions are deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used
Hope this helps you :)
For your first concern, give the same name to both your submit button (e.g: 'action'), and on the server side after submit check the value : if ($_POST['action'] == 'Retrieve List') {} else if ($_POST['action'] == 'Delete from DB')
Second concern, the name of the checkbox is the key :
<input type="checkbox" name="checkbox[]" value="{$info['ars_no']}"/>
Then on server side :
foreach($_POST['checkbox'] as $checkbox) {}
You could use a dropdown list where you can chose if you want to retrieve or delete:
<select name='choseaction'>
<option value="RetrieveList">Retrieve List</option>
<option value="DeletefromDB">Delete from DB</option>
</select>
And then use a single submit button.
<INPUT TYPE ="Submit" Name ="Submit" VALUE ="Submit">
After that you can verify like this:
if ($_POST['choseaction']=='RetrieveList')
{
...
} else if ($_POST['choseaction']=='DeletefromDB')
{
...
}
Hope this helps!
Good day.