I'm making a "delete" function on my form. I'm not quite happy because it doesn't work! I have tried this code:
while($row = $result->fetch_assoc())
{
$ordernr = $row['ordernr'];
$klantnaam = $row['klantnaam'];
$productnaam = $row['productnaam'];
$productid = $row['productid'];
echo "<tr>";
echo "<td width='150px'>" . $ordernr . "</td>";
echo "<td width='150px'>" . $klantnaam . "</td>";
echo "<td width='300px'>" . $productnaam . "</td>";
echo "<td width='100px'>" . $productid . "</td>";
echo "<td align='center' width='50px'><input name='delete[$ordernr]' type='checkbox'></td>";
echo "</tr>";
}
echo "<tr>";
echo "<td><input type='submit' name='verwijderen' value='Verwijderen'/></td>";
echo "</tr>";
echo "</table>";
echo "</form>";
$delete = $_POST['delete'];
if (isset($_POST['verwijderen'])) {
foreach($delete as $ordernr => $delete)
{
$ordernr = mysqli_real_escape_string($ordernr);
$query = mysqli_query("DELETE FROM overzicht WHERE ordernr= $ordernr");
}
}
Do you guys know what I've done wrong?
Thanks in advance!
1)
echo "<td align='center' width='50px'><input name='delete[$ordernr]' type='checkbox'></td>";
transforms to
echo "<td align='center' width='50px'><input name='delete' type='checkbox' value='$ordernr'></td>";
2)
foreach($delete as $ordernr => $delete)
do it:
foreach($delete as $ordernr => $del)
you are overwriting the $_POST
mysqli_query syntaxt is
mysqli_query($con,"your query");
I'd go and say that your input checkbox needs a value. Like
<input type="checkbox" name="delete[1]" value="1" />
On the other hand, then it would be easier to have
<input type="checkbox" name="delete[]" value="$ordernr" />
And then you loop with
foreach ($_POST["delete"] as $id) delete(id)
where con variable in your query...
$query = mysqli_query($con,"DELETE FROM overzicht WHERE ordernr= $ordernr");
You're iterating while reusing your variable here:
foreach($delete as $ordernr => $delete)
Where the second $delete will overwrite the value of the first, and throw a spanner in the works.
I suggest to rename the prior instances of $delete to $deleteList and do:
foreach($deleteList as $ordernr => $delete)
Keep in mind that much of your code could be done in different ways that may be more suitable. You will find those as you follow your path in learning PHP.
Related
I'm working on last part on my project, I'm building web-site, in this part, I want to display options of a job ( whether the job still in progress or Completed )
I gave my row in mysql enum values, "Completed","InProgress"
and when the student pick a job, the JobStatus will be "InProgress"
and the student can change this value from his JobLists page, when it's done, he can change it to Completed. and it will be changed in the Database
and this is my code trying to, in this Code, it shows me an Error on the Update Query
JobStatus = '".$_POST['JobStatus'] is not Defined ?? any one can help PLEASE Guys
<?php
//Connect to DB
include('CIEcon.php');
$sqlCommand ="SELECT Accounts.SSU , Jobs.JobName, Jobs.Description, Jobs.DueDate,Jobs.JobId, JobsLists.JobStatus FROM JobsLists,Jobs,Accounts WHERE Accounts.SSU = JobsLists.SSU AND Jobs.JobId = JobsLists.JobId And Accounts.SSU = '".$_SESSION['SSU']."' ";
$result = mysqli_query($dbCIE,$sqlCommand) or die(mysql_error());
echo "<form action='JobsLists.php' method='post'>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <input type='checkbox' name='JobId[]' value='". $row['JobId'] ."' /> </td>";
echo "<td align=center>" . $row['SSU'] . "</td>";
echo "<td align=center>" . $row['JobName'] . "</td>";
echo "<td align=center> " . $row['Description'] . "</td>";
echo "<td align=center>" . $row['DueDate'] . "</td>";
echo "<td align=center>" .
"<select>
<option name = JobStatus[".$row['JobId']."] value='InProgress' selected> In Progress </option>
<option name = JobStatus[".$row['JobId']."] value='Completed' > Completed </option>
</select>" . "</td>"; // need to be worked on..
echo "</tr>";
}
"</table>";
//Connect to DB
include('CIEcon.php');
// save the SSU for the current user to save the sata when insert jobs in jobslist
$SSU = $_SESSION['SSU'];
/////
//handle this when to save a status.
if( isset($_POST['save']) ){
if( empty($_POST['JobId']) || $_POST['JobId'] == 0 ){
echo"<h4> Status Wasn't Changed.. </h4>";
}else{
include('CIEcon.php'); //$dbCIE
foreach($_POST['JobId'] AS $i){
/// update JobsLists table with the new status..
$sqlUpdate = "UPDATE JobsLists SET JobStatus = '".$_POST['JobStatus'][$i]."' WHERE JobId = '" . $i . "'";
$resultUpdate = mysqli_query($dbCIE,$sqlUpdate) or die(mysqli_error($dbCIE));
}
// TEST ONLY ////////----------------------------------------////////////
if (mysqli_affected_rows($dbCIE) > 0) {
echo "<h4> You have successfully Saved your statuse </h4>";
}else{ echo "<h4> Error occurred </h4> "; }
////////----------------------------------------////////////
} // end of else, when user select something..
}
?>
It's because you haven't named the select box which you are trying to send values with.. HTML <option>s don't have a name, but only a value. it is this value which is the assigned to the name of the <select> in $_POST
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <input type='checkbox' name='JobId[]' value='". $row['JobId'] ."' /> </td>";
echo "<td align=center>" . $row['SSU'] . "</td>";
echo "<td align=center>" . $row['JobName'] . "</td>";
echo "<td align=center> " . $row['Description'] . "</td>";
echo "<td align=center>" . $row['DueDate'] . "</td>";
echo "<td align=center>" .
echo "<select name='JobStatus[".$row['JobId']."]'>";
if($row['JobStatus'] == "InProgress"){
echo "<option value='InProgress' selected>In Progress</option>";
echo "<option value='Completed'>Completed</option>";
} else {
echo "<option value='InProgress'>In Progress</option>";
echo "<option value='Completed' selected> Completed </option>";
}
echo "</select>" . "</td>"; // need to be worked on..
echo "</tr>";
}
"</table>";
I am creating a random generated quiz with 10 questions. In generating the random question is fine, but I want to display also the choices in random, same with the questions.
This is my code that I am currently working with:
<?php
generate();
function generate(){
include('connection.php');
mysql_select_db('exam');
$result=mysql_query("SELECT * FROM questionaires
INNER JOIN choices ON questionaires.q_id=choices.q_id
WHERE RAND()<(SELECT ((10/COUNT(*))*10) FROM questionaires)
ORDER BY RAND() LIMIT 10");
$c=0;
echo "<table border='3' align='center' bordercolor='#CCCCCC'>
<tr>
<th>Number:</th>
<th>Question</th>
</tr>
";
while($row = mysql_fetch_array($result)){
$c++;
echo "<tr>";
echo "<td>" . $c . "</td>";
echo "<td>";
echo $row['question'] . "<br>";
echo "<input type='radio' name='ans'>".$row['choice_a']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_b']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_c']."</input><br>";
echo "<input type='radio' name='ans'>".$row['choice_d']."</input><br>";
echo "</td>";
echo "</tr>";
//}
//}
}
echo "</table>";
}
?>
Little help will highly appreciated.
You could change script to this:
echo "<td>";
echo $row['question'] . "<br>";
$ans=array($row['choice_a'],$row['choice_b'],$row['choice_c'],$row['choice_d']);
shuffle($ans);
foreach ($ans as $choice) {
echo "<input type='radio' name='ans'>".$choice."</input><br>";
} unset($choice);
echo "</td>";
I have a form which displays stock levels and I want the user to be able to delete multiple products so have provided checkboxes. the code is below:
echo "<form method='get'>
<input type='submit' name='removestock' value= 'Remove'>
<table class='display' border='0'>
<tr>
<th>Select</th>
<th>Name</th>
<th>Description</th>
<th>Price (£)</th>
<th>Quantity</th>
<th>Size</th>
</tr>";
echo "<tr>";
require ('connection.php');
$query = mysql_query("SELECT * FROM items")or die(mysql_error());
while($results = mysql_fetch_array($query)){
echo "<td> <input type='checkbox' name='item' value='".$results['item_id']."'></td>";
echo "<td>" . $results['name'] . "</td>";
echo "<td>" . $results['description'] . "</td>";
echo "<td>" . $results['price'] . "</td>";
echo "<td>" . $results['quantity'] . "</td>";
echo "<td>" . $results['size_id'] . "</td>";
echo "</tr>";
}
echo "</table></form>";
And my parsing code is...
if(isset($_GET['removestock']) === true){
$errors = array();
$items = $_GET['item'];
echo $items;
}
But for some reason it only displays the last item_id selected. Your help will be much appreciated!
PS. I tried changing the checkbox name to name="items[]" and implemented a foreach loop to parse the data but it still did not work.
The name of the checkbox should be
<input type='checkbox' name='item[]' value='".$results['item_id']."'>
This wil give you an array which you can read with a foreach
Use name=item [] for the checkboxes. This will give you the array.
Then use var_export ($ items)
Change this line:
"<td> <input type='checkbox' name='item' value='".$results['item_id']."'></td>";
To read:
"<td> <input type='checkbox' name='item[" . $results['item_id'] . "]'></td>";
And you will get your desired array in the variable.
This always works for me:
$i =0;
while($results = mysql_fetch_array($query)){
echo "<td> <input type='checkbox' name='item[".$i."]' value='".$results['item_id']."'></td>";
echo "<td>" . $results['name'] . "</td>";
echo "<td>" . $results['description'] . "</td>";
echo "<td>" . $results['price'] . "</td>";
echo "<td>" . $results['quantity'] . "</td>";
echo "<td>" . $results['size_id'] . "</td>";
echo "</tr>";
$i++;
}
Once posted, the item[] element will be an array.
I need some help regarding pagination, the problem is that I want to show my database records first time on button click with pagination but every time I click on pagination link it requires click on button
Here is my code I know I am making some mistake please check and kindly do me a favor by correcting my mistake ..
if (isset($_POST["search"])){
$result=mysql_query("select count(*) from reg_phone");
$row=mysql_fetch_row($result);
$tr=$row[0];
$rpp=4;
$pn=1;
if(isset($_GET['pn']))
{
$pn=$_GET['pn'];
}
$tp=($tr/$rpp);
if($tr/$rpp>=0)
{
$tp++;
}
$from=(($pn-1)*$rpp)+1;
$to=($pn)*($rpp);
$show = "SELECT * FROM reg_phone where Id between $from and $to";
$rs = mysql_query($show) or die(mysql_error());
#****results in Grid****
echo "<table width='100%' border='1' cellpadding='2' border-color='#000' id='tbl'>";
echo "<tr>";
echo "<td style='background: white;'><b>IMEI #</td>";
echo "<td style='background: white;'><b>Phone #</td>";
echo "<td style='background: white;'><b>From Date</td>";
echo "<td style='background: white;'><b>Status</td>";
echo "</tr>";
$rowID = 1;
while ($row = mysql_fetch_array($rs)) {
echo "<tr>";
echo "<td id='imeinum" . $rowID . "'>$row[imei]</td>";
echo "<td id='phnum" . $rowID . "'>$row[phonenum]</td>";
echo "<td id='datepicker" . $rowID . "'>$row[fdate]</td>";
echo "<td id='rad" . $rowID . "'>$row[status]</td>";
echo "</tr>";
$rowID++;
}
echo "</table>";
echo "<ul id='pages'>";
for($i=1;$i<=$tp;$i++)
{
echo "<li><a href='phonereg.php?pn=$i'>$i</a></li>";
}
echo "</ul>";
#**********************
mysql_free_result($rs);
}
http://www.hscripts.com/scripts/jquery/pagination.php
the script here is really helpful to me. Exactly what i want to do. hope anyone one in future will need this.
thanks all for suggestions.
Regards.
BKay
I have a question on php postback
My code is :
<?php
if(isset($_POST["Delete"]))
{
echo "DELETE";
}
if(isset($_POST["Modifier"]))
{
echo "Modifier";
}
if(!empty($_SESSION["Status"]))
{
if($_SESSION["Status"] == "u")
{
header("Location: Index.php?Action=Acceuil");
}
if($_SESSION["Status"] == "a")
{
$Connection = mysql_connect("localhost","root") or die(mysql_error());
mysql_select_db("tpw34") or die("Nope.");
$query = "Select * From Products";
$result = mysql_query($query);
While($ligne = mysql_fetch_assoc($result))
{
//Index.php?Action=AdminDeleteProduct&Delete=".$ligne["ProductID"]."
echo "<form method='POST' Action='#'>";
echo "<table border='1'>";
echo "<tr>";
echo "<td colspan='2'><center><img width='250' height='250' src='".$ligne["Image"]."'/></center></td>";
echo "</tr>";
echo "<tr>";
echo "<th>Nom du produit :</th>";
echo "<td>".$ligne["ProductName"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<th>Prix :</th>";
echo "<td>".$ligne["Prix"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<th>Description :</th>";
echo "<td>".$ligne["Description"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2'h><input type='Submit' value='Delete' name='Delete'/><input type='Submit' value='Modifier' name='Modifier'/></td>";
echo "</tr>";
echo "</table>";
echo "<br>";
echo "</form>";
}
}
}
?>
My Question is : I want to get the ProductID of the item ( in the table ) to be in the $_POST["Delete"] and $_POST["Modifier"] but i dont wanna change the text on the button. I want to keep DELETE and MODIFIER. I have read many things on the web but i dont find the correct answer.
Include a hidden form value for ProductID. Then you can retrieve the value in $_POST['ProductID']
echo "<input type=hidden name='ProductID' value='" . $ligne["ProductID"] . "'>";
You could work with sessions, where you can temporarily save your information.
Sessions
Or like Tim Dearborn suggested, use a hidden input to send it with the next form submit.