echo "<form name='input' action='admin_selecteren_voor_verwijderen.php' method='post'>";
$sql_bestelling= "SELECT * FROM producten";
foreach($dbh->query($sql_bestelling) as $row)
{
$product_id=$row['product_id'];
$product_naam=$row['product_naam'];
$prijs=$row['prijs'];
$foto=$row['foto'];
echo "
<br>
<img src='$foto' height='70' width='50' border='0'>
<b>$product_naam</b> <input type='checkbox' name='$product_naam' value='$product_naam'></br>
</br></br></br>";
//if (isset($_POST['submit'])){
// $sql = "DELETE FROM `producten` WHERE product_naam='$product_naam'";
// $query = $dbh->prepare( $sql );
// $result = $query->execute();
//}
}
if(!empty($_POST['checkbox'])) {
foreach($_POST['checkbox'] as $check) {
echo "check: ", $check;
}
}
echo "
<input type='submit' value='Delete'>
</form>";
?>
I want to have a list of product in my webshop administrator page. Every product has a checkbox. I want to be able to delete all of the product of the checkboxes are checked. But I don't know how.. The above is what I have so far. The added picture is how it looks on the page. But the page is a list of product selected from the database with a foreach loop as can be seen in the code. The checkbox also is in the loop. I don't know how to assign every product which is check to a variable and then delete them from the database. Can anyone help me?
Name all the checkboxes a same, like delete[] , and and put the name of product in the value of each checkbox.
Example :
<form action="..." method='post' >
user1<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
user2<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
user3<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
user4<input type="checkbox" name="delete[]"
value="<?php echo $product_naam ?>" /><br />
<input type='submit' value='delete' />
</form>
Delete query :
<?php
if(isset($_POST['delete'])){
$ids = $_POST['delete'];
$sql = "DELETE FROM `producten` WHERE product_naam
IN('".implode("','", $ids)."')";
//execute query
}
?>
You're looking for $_POST['checkbox'] but that's not what you have in your form. Name the checkboxes all checkbox[] and use $product_naam as the value.
<input type='checkbox' name='checkbox[]' value='$product_naam'>
Now you can loop over it and delete with your foreach loop.
you should not name the checkbox to the product name, just call it delete or something. Then you can use the foreach method to delete them from the database, like this:
<?php
echo "<form name='input' action='admin_selecteren_voor_verwijderen.php' method='post'>";
$sql_bestelling= "SELECT * FROM producten";
foreach($dbh->query($sql_bestelling) as $row)
{
$product_id=$row['product_id'];
$product_naam=$row['product_naam'];
$prijs=$row['prijs'];
$foto=$row['foto'];
echo "
<br>
<img src='$foto' height='70' width='50' border='0'>
<b>$product_naam</b> <input type='checkbox' name=delete' value='$product_naam'></br>
</br></br></br>";
//if (isset($_POST['submit'])){
// $sql = "DELETE FROM `producten` WHERE product_naam='$product_naam'";
// $query = $dbh->prepare( $sql );
// $result = $query->execute();
//}
}
if(isset($_POST['delete'])) {
foreach($_POST['delete'] as $delete){ {
$sql = "DELETE FROM producten WHERE product_naam= $delete";
$query = $dbh->prepare( $sql );
$result = $query->execute();
}
}
echo "
<input type='submit' value='Delete'>
</form>";
?>
Also, it seems to me you're mistaking "value" with "name", read up on it. And it would be good to read something about PDO, mysql isn't safe. http://www.php.net/manual/en/book.pdo.php
Related
I have the following code, the variable 'name' and 'project' need to go to session variables on submit, in this case the submit happens through an 'onchange'event. The 'name' variable is POSTed but not the 'project' variable. Any ideas where my issue is?
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<?php
$query = $conn->prepare("SELECT name, project FROM models.models where models.active = 'yes'");
$query->execute();
while($r = $query->fetch(PDO::FETCH_OBJ)){
echo "
<div class='swiper-slide'>
<input type='image' src='models/thumbs/",$r->name,".jpg' id='name' name='name' value='",$r->name,"' onchange='this.form.submit();'/>
<input type='hidden' value='",$r->project,"' name='project' />
</div>
";
}
?>
</form>
<?php
$query = $conn->prepare("SELECT name, project FROM models.models where models.active = 'yes'");
$query->execute();
while($r = $query->fetch(PDO::FETCH_OBJ)){
echo "
<div class='swiper-slide'>
<input type='image' src='models/thumbs/",$r->name,".jpg' id='name' name='name[]' value='",$r->name,"' onchange='this.form.submit();'/>
<input type='hidden' value='",$r->project,"' name='project' />
</div>
";
}
?>
How to access:
$names = $_POST['name'];
for($i=0;$i<count($names);$i++)
{
echo $names[$i]."<br>";
}
I'm trying to update my data in MySQL database using the displayed data from it. But I haven't much luck in figuring out what I'm doing wrong. I don't know how can I pass my data from my $rows in order for me to use it in my UPDATE SQL statement.
I just want to complete all the data before I clicked the save button.
Please see attached image for the output..
And also, I got a warning message like this.
WHenever I Clicked the Save Data button..
Notice: Undefined variable: reason in C:\xampp\htdocs\BTR\get_forms.php on line 109
Notice: Array to string conversion in C:\xampp\htdocs\BTR\get_forms.php on line 109
Can someone enlighten me.. How to do this.? Thank you..
<?php
require 'config.php';
$form_type = $_POST['form_type'];
if ($form_type == 'MCCV-F2'){
$region = $_POST['region'];
$province = $_POST['province'];
$municipality = $_POST['municipality'];
$barangay = $_POST['barangay'];
$period = $_POST['period'];
$form_type = $_POST['form_type'];
echo "NON COMPLIANT IN EDUCATION<br>";
echo "<br><br>MUNICIPALITY: ".$municipality;
echo "<br><br>BARANGAY: ".$barangay;
echo "<br><br>PERIOD: ".$period;
?>
<form name="get_forms_f2" action="" method="post">
<br><br>
<center><table border = 1 style =2 width=1800>
<tr>
<td><center><b>Household ID </center></td>
<td><center><b>Member ID </center></td>
<td><center><b>Name</center></td>
<td><center><b>Sex</center></td>
<td><center><b>HH Status</center></td>
<td><center><b>Grade Level </center></td>
<td><center><b>School ID</center></td>
<td><center><b>Name Of Dominant School</center></td>
<td><center><b>CV Remarks</center></td>
<td><center><b>Reason</center></td>
<td><center><b>Other Reason</center></td>
<td><center><b>Intervention</center></td>
</tr>
<?php
$sql = "SELECT A.family_id, A.barangay, A.person_id, A.gender, A.family_status, A.current_grade_level,
A.school_facility_id, A.school_facility_name, A.municipality, CONCAT(B.last_name, ', ',B.first_name) as 'name',
B.person_id,B.cv_remarks, B.reason, B.other_reason, B.intervention, B.status FROM roster AS A RIGHT JOIN compliance AS B ON A.person_id = B.person_id
WHERE B.period='$period' AND B.form_type='$form_type' AND A.municipality='$municipality' AND A.barangay='$barangay'";
$query=$conn->prepare($sql);
$query->execute();
$result= $query->fetchALL(PDO::FETCH_ASSOC);
$count=(int)$query->rowCount();
foreach ($result as $row){
$person_id[] = $row['person_id'];
echo "<tr>";
echo "<td>".$row['family_id']."</td>";
echo "<td>".$row['person_id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['gender']."</td>";
echo "<td>".$row['family_status']."</td>";
echo "<td>".$row['current_grade_level']."</td>";
echo "<td>".$row['school_facility_id']."</td>";
echo "<td>".$row['school_facility_name']."</td>";
echo "<td><input type='text' name='cv_remarks[]' value='".$row['cv_remarks']."'></td>";
echo "<td><select name='reason[]'>";
if (is_null($row['reason'])){
$sql2= "SELECT reason_code, reason_desc FROM reasons WHERE form_type ='2' ORDER BY reason_code ASC";
echo "<option value=''>SELECT REASON FOR Non-Compliance</option>";
foreach($conn->query($sql2) as $row2){
echo "<option value='".$row2['reason_desc']."'>".$row2['reason_code']." - ".$row2['reason_desc']."</option>";
}
}
if (!is_null($row['reason'])){
$sql2= "SELECT reason_code, reason_desc FROM reasons WHERE form_type ='2' ORDER BY reason_code ASC";
echo "<option value='".$row['reason']."'>".$row['reason']." (SELECTED)"."</option>";
foreach($conn->query($sql2) as $row2){
echo "<option value='".$row2['reason_desc']."'>".$row2['reason_code']." - ".$row2['reason_desc']."</option>";
}
}
echo "</select></td>";
echo "<td><input type='text' name='other_reason[]' value='".$row['other_reason']."'></td>";
echo "<td><input type='text' name='intervention[]' value='".$row['intervention']."'></td>";
echo "</tr>";
}
}
?>
</table></center><br><br>
<input type="submit" name="submit" value="Save Data">
<?php
$sql3 = "UPDATE compliance SET reason='{$reason}' WHERE person_id='{$person_id}' AND form_type='$form_type' AND period='$period'";
$query = $conn->prepare($sql3);
$query->execute();
?>
</form>
I have posted with the answer of how to update form with an array variable as a whole.
I have kept person,reason as array since you have added that alone as array().
<?php
require 'config.php';
if(isset($_POST['submit']))
{
for ($i=0; $i<count($_POST['person_id']); $i++)
{
$sql3 = "UPDATE compliance SET reason='".$_POST['reason'][$i]."' WHERE person_id='".$_POST['person_id'][$i]."' AND form_type='".$_POST['form_type']."' AND period='".$_POST['period']."'";
$query = $conn->prepare($sql3);
$query->execute();
}// end of For
}// end of IF
$form_type = $_POST['form_type'];
if ($form_type == 'MCCV-F2'){
$region = $_POST['region'];
$province = $_POST['province'];
$municipality = $_POST['municipality'];
$barangay = $_POST['barangay'];
$period = $_POST['period'];
echo "NON COMPLIANT IN EDUCATION<br>";
echo "<br><br>MUNICIPALITY: ".$municipality;
echo "<br><br>BARANGAY: ".$barangay;
echo "<br><br>PERIOD: ".$period;
?>
<form name="get_forms_f2" action="" method="post">
<br><br>
<center><table border = 1 style =2 width=1800>
<tr>
<td><center><b>Household ID </center></td>
<td><center><b>Member ID </center></td>
<td><center><b>Name</center></td>
<td><center><b>Sex</center></td>
<td><center><b>HH Status</center></td>
<td><center><b>Grade Level </center></td>
<td><center><b>School ID</center></td>
<td><center><b>Name Of Dominant School</center></td>
<td><center><b>CV Remarks</center></td>
<td><center><b>Reason</center></td>
<td><center><b>Other Reason</center></td>
<td><center><b>Intervention</center></td>
</tr>
<?php
$sql = "SELECT A.family_id, A.barangay, A.person_id, A.gender, A.family_status, A.current_grade_level,
A.school_facility_id, A.school_facility_name, A.municipality, CONCAT(B.last_name, ', ',B.first_name) as 'name',
B.person_id,B.cv_remarks, B.reason, B.other_reason, B.intervention, B.status FROM roster AS A RIGHT JOIN compliance AS B ON A.person_id = B.person_id
WHERE B.period='$period' AND B.form_type='$form_type' AND A.municipality='$municipality' AND A.barangay='$barangay'";
$query=$conn->prepare($sql);
$query->execute();
$result= $query->fetchALL(PDO::FETCH_ASSOC);
$count=(int)$query->rowCount();
foreach ($result as $row){
?>
<input type="hidden" name="person_id[]" value="<?php echo $row['person_id'];?>">
<input type="hidden" name="form_type" value="<?php echo $form_type; ?>">
<input type="hidden" name="period" value="<?php echo $period; ?>">
<?php
echo "<tr>";
echo "<td>".$row['family_id']."</td>";
echo "<td>".$row['person_id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['gender']."</td>";
echo "<td>".$row['family_status']."</td>";
echo "<td>".$row['current_grade_level']."</td>";
echo "<td>".$row['school_facility_id']."</td>";
echo "<td>".$row['school_facility_name']."</td>";
echo "<td><input type='text' name='cv_remarks[]' value='".$row['cv_remarks']."'></td>";
echo "<td><select name='reason[]'>";
if (is_null($row['reason'])){
$sql2= "SELECT reason_code, reason_desc FROM reasons WHERE form_type ='2' ORDER BY reason_code ASC";
echo "<option value=''>SELECT REASON FOR Non-Compliance</option>";
foreach($conn->query($sql2) as $row2){
echo "<option value='".$row2['reason_desc']."'>".$row2['reason_code']." - ".$row2['reason_desc']."</option>";
}
}
if (!is_null($row['reason'])){
$sql2= "SELECT reason_code, reason_desc FROM reasons WHERE form_type ='2' ORDER BY reason_code ASC";
echo "<option value='".$row['reason']."'>".$row['reason']." (SELECTED)"."</option>";
foreach($conn->query($sql2) as $row2){
echo "<option value='".$row2['reason_desc']."'>".$row2['reason_code']." - ".$row2['reason_desc']."</option>";
}
}
echo "</select></td>";
echo "<td><input type='text' name='other_reason' value='".$row['other_reason']."'></td>";
echo "<td><input type='text' name='intervention' value='".$row['intervention']."'></td>";
echo "</tr>";
}
}// end of MCCV-F2 form
?>
</table>
</center>
<br><br>
<input type="submit" name="submit" value="Save Data">
</form>
Hope so this would solve your problem br. have a try and let me know if any hindrance you find.
Trying to make a CRUD, everything works except my Update function. I feel like the problem is in the second sql query. When I click on submit it just refreshes and the change is gone. Can anyone show me how to find what I need to change/show me what to change?
<head>
<title>Update</title>
</head>
<body>
</form>
<?php
require_once('dbconnect.php');
$id = $_GET['id'];
$sql = "SELECT * FROM dealers where ID=$id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<form action="" method="post">';
echo "Company: <input type=\"text\" name=\"CName\" value=\"".$row['CName']."\"></input>";
echo "<br>";
echo "Contact: <input type=\"text\" name=\"Contact\" value=\"".$row['Contact']."\"></input>";
echo "<br>";
echo "City: <input type=\"text\" name=\"City\" value=\"".$row['City']."\"></input>";
echo "<br>";
echo "<input type=\"Submit\" = \"Submit\" type = \"Submit\" id = \"Submit\" value = \"Submit\">";
echo "</form>";
}
echo "</table>";
} else {
echo "0 results";
}
if(isset($_POST['Submit'])){
$sql = "UPDATE dealers SET CName='$CName', Contact='$Contact', City='$City' where ID=$id";
$result = $conn->query($sql);
}
$conn->close();
?>
Instead of building a form inside PHP, just break with ending PHP tag inside your while loop and write your HTML in a clean way then start PHP again. So you don't make any mistake.
Also you've to submit your $id from your form too.
Try this
<?php
require_once('dbconnect.php');
$id = $_GET['id'];
$sql = "SELECT * FROM dealers where ID=$id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<form action="" method="post">
<input type="hidden" name="id" value="<?= $id ?>" />
Company: <input type="text" name="CName" value="<?= $row['CName'] ?>" />
<br>
Contact: <input type="text" name="Contact" value="<?= $row['Contact'] ?>" />
<br>
City: <input type="text" name="City" value="<?= $row['City'] ?>" />
<br>
<input type="Submit" name="Submit" id="Submit" value="Submit" />
</form>
<?php
} // end while loop
echo "</table>";
}
else {
echo "0 results";
}
Note: You are passing undefined variables into your update query. As you are submitting your form you must have to define those variables before you use them.
if (isset($_POST['Submit'])) {
$CName = $_POST['CName'];
$Contact = $_POST['Contact'];
$City = $_POST['City'];
$id = $_POST['id'];
$sql = "UPDATE dealers SET CName='$CName', Contact='$Contact', City='$City' where ID=$id";
$result = $conn->query($sql);
}
$conn->close();
that loop? ID primary key or not?
maybe u need create more key in table dealer like as_id
<input type="hidden" name="idform" value="$as_id">
in statment
if($_POST){
$idf = $_POST['idform'];
if(!empty($idf)){
$sql = "UPDATE dealers SET CName='$CName', Contact='$Contact', City='$City' where as_id=$idf";
$result = $conn->query($sql);
}
$conn->close();
}
I'm trying to display some information stored in MySQL comments table to an input but I'm having issues with that. Input named enterComment inserts data to my DB and I want it to redirect back to showComment input.
HTML:
form action='takedata.php' method='POST'>
<input type='text' id='enterComment' name='enterComment' width='400px' placeholder='Write a comment...'>
<input type='submit' id='combuton' name='comButon' value='Comment!'>
<input type='text' id='showComment' name='showComment'>
</form>
PHP:
<?php include "mysql.php"; ?>
<?php
if (isset($_POST['comButon'])){
$enterComment = strip_tags($_POST['enterComment']);
if($enterComment){
$addComment = mysql_query("INSERT INTO comments(comment) VALUES('$enterComment')");
if($addComment==1)
//INSERT INTO showComment input;
}
}
?>
try this, and use mysqli instead of mysql
include "dbconnect.php";
if (isset($_POST['comButon'])){
$enterComment = strip_tags($_POST['enterComment']);
if($enterComment){
$addComment = mysqli_query($conn, "INSERT INTO comments(comment) VALUES('$enterComment')");
if($addComment) {
$sql = "select comment from comments order by id desc limit 1";
$result = mysqli_query($conn, $sql);
while($row = $result->fetch_assoc()) { ?>
<input type="text" value="<?php echo $row['comment']; ?>">
<?php }
}
}
}
your form
<form action='' method='POST'>
<input type='text' id='enterComment' name='enterComment' width='400px' placeholder='Write a comment...'>
<input type='submit' id='combuton' name='comButon' value='Comment!'>
<?php if(!isset($_POST['comButon'])) { ?>
<input type="text" value="">
<?php } ?>
</form>
I am trying to create a compare option between selected cars.
<?php
if(isset($_POST['compares'])) {
$id_nums = array($_POST['cBox']);
//$id_nums = array(1,6,12,18,24);
$id_nums1 = implode(", ", $id_nums);
$query = "SELECT * FROM wp_cars WHERE id in ($id_nums1)";
$cCars = mysql_query($query) or mysql_error();
while($car = mysql_fetch_array($cCars)) {
echo $car['cartitle']."<br/>";
echo $car['saleprice']."<br/>";
}
} else {
$query1 = "SELECT * FROM wp_cars";
$allcars = mysql_query($query1) or die(mysql_error()); `
while($car1 = mysql_fetch_array($allcars)) {
echo "<input type='checkbox' value=".$car1['id']." name='cBox[]' />";
echo $car1['cartitle']."<br/>";
echo $car1['saleprice']."<br/>";
}
}
?>
How to pass the checkbox name(cBox[]) array based on checkboxes selection.
<form action="compares.php" method="post">
<button name="compares">Select Cars to Compare</button>
</form>
$id_nums = array($_POST['cBox']);
$_POST['cBox'] is already an array, you are making a 2d array. Doing
$id_nums1 = implode(", ", $_POST['cBox']);
would do what you want. Although it is wide open to SQL injection.
From your HTML part, send inputs this way:
<form action="compares.php" method="post">
<?php foreach (mysql_fetch_array($allcars) as $car: ?>
<input type="checkbox" value="<?php echo $car['id']; ?>" name="cBox[]" />
<?php echo $car['cartitle']; ?><br />
<?php echo $car['saleprice']; ?><br />
<?php endforeach; ?>
</form>
And in your PHP part, receive inputs this way:
$id_nums = implode(",", $_POST['cBox']);