Updating multiple rows using select PHP and MySQL - php

Below is the code where I am trying to change the values of one column in multiple rows but I am a little bit stuck.
File 1:
<form name="update-location" method="post" action="recibos_location_update.php">
<div align="right"><button type="submit">Actualizar Estado</button></div>
<hr />
<ul>
<?php
$get_logs = mysql_query("SELECT * FROM recibos ORDER BY id DESC");
while ($logs = mysql_fetch_array($get_logs)){
$serial = $logs['serial'];
$check = $logs['location'];
$problem = $logs['problem'];
?>
<li class="<?php
if ($check == $location1){echo 'recibos-container-shop-li';}
if ($check == $location2){echo 'recibos-container-haji-li';}
if ($check == $location3){echo 'recibos-container-return-li';} ?>">
<?php echo $serial . " | " . $logs['model'] . " | " . $problem; ?>
<div align="right">
<?php
if ($check != 'Returned'){
?>
<input type="hidden" name="serial[]" value="<?php echo $serial; ?>" />
<select name="location">
<option <?php if ($check == $location1){echo 'selected';} ?> value="<?php echo $location1; ?>" style="background: #fff8bf;">Mobils Calfont</option>
<option <?php if ($check == $location2){echo 'selected';} ?> value="<?php echo $location2; ?>" style="background: #ffc1bf;">Haji Jorda</option>
<option <?php if ($check == $location3){echo 'selected';} ?> value="<?php echo $location3; ?>"style="background: #ecffbf;">Devuelto</option>
</select>
<?php
}
else {
echo "<i><font color='#5e8029'>Devuelto</font></i>";
}
?>
</div>
</li>
<?php } ?>
</ul>
File 2
<?php
include "connect.php";
include "links.php";
foreach($_POST["serial"] as $serial){
$location = $_POST['location'];
echo $location . " " . $serial . "<br>";
}
mysql_query("UPDATE recibos SET location='$location' WHERE serial='$serial'");
//header("Location: " . $_SERVER['HTTP_REFERER']);
?>
and the output I am getting is:
Haji 900005
Haji 900004
Haji 900002
But for all 3 output lines, it should give different names as names in 3 select boxes were different.
What I need is for the process form to update every row with what value is contained in select box options.

html
<select name="location[]">
php
foreach($_POST["serial"] as $k=> $serial){
$location = $_POST['location'][$k];
mysql_query("UPDATE recibos SET location='$location' WHERE serial='$serial'");
}

foreach($_POST["serial"] as $serial){
$location = $_POST['location'];
echo $location . " " . $serial . "<br>";
mysql_query("UPDATE recibos SET location='$location' WHERE serial='$serial'");//update
}
// mysql_query("UPDATE recibos SET location='$location' WHERE serial='$serial'");

Related

How to retain dropdown selected option after submit / page refresh

I want to have dropdown selected option selected after submit/refresh page. I search for other examples but no one works on my code.
How to retain selected value after submit/refresh with this code?
<form name="test" action="test.php?id=<?php echo $row["id"]; ?>" method="post">
<select id="test_email" name="test_email">
<option value="">...select</option>
<?php
$sql2 = "SELECT test_id, test_email FROM test WHERE status='Act'";
$res = $db->query($sql2);
if ($res->num_rows > 0) {
while($row1 = mysqli_fetch_array($res)) {
?>
<option value="<?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?>"><?php echo $row1['test_id'];?></option>
<?php
}
}
?>
</select>
Solved this way:
Because I could not pass $_POST['test_email'] with <?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?>
Because I use explode on $_POST['test_email']
I make one more post (insert in db) $test_temp = trim(mysqli_real_escape_string($db, $_POST['test_email'])); to have my dropdown value (whole string) in db.
I added hidden input in my form <input id="test_temp" type="hidden" name="test_temp" value="<?php echo $row["test_temp"]; ?>">
And changed select option line to <option value="<?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>"<?php if($row1['test_email'] . '-' . $row1['test_id'] == $row['test_temp']) echo ' selected="selected"' ; ?>><?php echo $row1['test_id'];?></option>.
Perhaps this could be simpler but it works like a charm.
#roberto06: Thank you for your guidance.
You need to add the selected="selected" attribute on the option matching your $_POST value.
You might also want to concatenate <?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?> into <?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>, but that's up to you.
Here's a solution (I assumed that $POST['test_email'] has to be equal to $row1['test_email'] . '-' . $row1['test_id'] in order for the condition to be matched). I'm using a shorthand if...else here, but you could also use a classic if :
<form name="test" action="test.php?id=<?php echo $row["id"]; ?>" method="post">
<select id="test_email" name="test_email">
<option value="">...select</option>
<?php
$sql2 = "SELECT test_id, test_email FROM test WHERE status='Act'";
$res = $db->query($sql2);
if ($res->num_rows > 0) {
while($row1 = mysqli_fetch_array($res)) {
?>
<option value="<?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>"<?php echo (isset($_POST['test_email']) && $row1['test_email'] . '-' . $row1['test_id'] == $_POST['test_email']) ? ' selected="selected"' : ''; ?>><?php echo $row1['test_id'];?></option>
<?php
}
}
?>
</select>
</form>

Database driven select box being empty on submit

I am trying to submit a form value in a database with php. In form a select box value comes from database.
<?php include_once 'header.php';
$sql="SELECT uid,name FROM emitra_basic where block='$user'";
$result = $conn->query($sql);
//form validion
if(isset($_POST['submit']))
{
$eid =$_POST["eid"];
if($eid=="blank")
{
$flag=1;
$idErr="please Select E-MITRA";
}
$miatm =trim($_POST["miatm"]);
if(empty($miatm) || !preg_match("/^[a-zA-Z0-9 ]*$/",$miatm)) {
$flag=1;
$miErr="Please Enter Valid Id";
}
.............like this
if($flag==0)
{
$sqll="insert into **********";
}
//my form is
<form id="basic" method="post" name="basic">
<select class="select-style gender" name="eid">
<option value="blank">Please Select E-MITRA ID</option>
<?php
while($row=mysqli_fetch_array($result))
{
?>
<option value="<?php echo $row['uid']; ?>"><?php echo $row['uid']." (" . $row['name'] .")"; ?></option>
<?php
}
?>
</select>
<p class="contact"><label for="bid">Micro-ATM Serial No</label></p>
<input type="text" name="miatm" value ="<?php if (isset($miatm)) echo $miatm; ?>" /> <?php echo $miErr; ?>
<p class="contact"><label for="bid">Micro-ATM TID No</label></p>
<input type="text" name="tid" value ="<?php if (isset($tid)) echo $tid; ?>" /> <?php echo $tiErr; ?>
<input class="buttom" name="submit" id="submit" value="Add Me" type="submit">
Its seems Ok.but when i tried to submit the form if some of one field remain empty then its show blank value in select box.
how can i remain the same selected value in select box even if textbox remain empty.
You need to retain the value of drop down after form submit.
User selected attribute of select option.
<?php
if (isset($_POST['submit'])) {
$eid =$_POST["eid"];
if ($eid=="blank") {
$flag=1;
$idErr="please Select E-MITRA";
}
}
$sql="SELECT uid,name FROM emitra_basic where block='$user'";
$result = $conn->query($sql);
?>
<select class="select-style gender" name="eid">
<option value="blank">Please Select E-MITRA ID</option>
<?php
while($row=mysqli_fetch_array($result)) {
$selected = (isset($_POST["eid"]) && $_POST["eid"] == $row['uid']) ? 'selected="selected"' : '';
?>
<option value="<?php echo $row['uid']; ?>" <?php echo $selected;?>><?php echo $row['uid']." (" . $row['name'] .")"; ?></option>
<?php
}
?>
</select>
You need to use selected="" or selected="selected" after submission in your select tag as a attribute as:
<?
$sql="SELECT uid,name FROM emitra_basic where block='$user'";
$result = $conn->query($sql);
?>
<select class="select-style gender" name="eid">
<option value="blank">Please Select E-MITRA ID</option>
<?php
while($row=mysqli_fetch_array($result))
{
$selected = ((isset($_POST["eid"]) && $_POST["eid"] == $row['uid']) ? 'selected=""' : '');
?>
<option <?=$selected?> value="<?php echo $row['uid']; ?>"><?php echo $row['uid']." (" . $row['name'] .")"; ?></option>
<?php
}
if(isset($_POST['submit']))
{
$eid = $_POST["eid"];
if($eid=="blank")
{
$flag=1;
$idErr="please Select E-MITRA";
}
?>
</select>
Side Note:
In your question ist two lines are not inside the php, i hope this is type error.

Dynamic Select Boxes work half

I have two fail for my script.
1.publicAdvart.php
<script type="text/javascript">
$(document).ready(function () {
$('#select_marka').change(function () {
$.post(
'modeli.php',
{id: $('#select_marka').val()},
function (res) {
$('#model').html(res);
}
);
});
});
</script>
<select class="form-control" name="marka" id="select_marka">
<option></option>
<?php
$sel = "SELECT * FROM marka";
$query = mysqli_query($conn, $sel);
while ($row = mysqli_fetch_array($query)) {
?>
<option value="<?php echo $row['id']; ?>"><?php echo $row['marka']; ?></option>
<?php
}
?>
</select>
2.modeli.php
<?php
if (isset($_POST['id']) && $_POST['id'] != '') {
$marka_id = $_POST['id'];
?>
<select id="select_marka" class="form-control">
<?php
$selModel = "SELECT * FROM marka_model WHERE marka_id='$marka_id'";
$queryModel = mysqli_query($conn, $selModel);
while ($rows = mysqli_fetch_array($queryModel)) {
?>
<option value="<?php echo $rows['model']; ?>"><?php echo $rows['model']; ?></option>
<?php
}
?>
</select>
<?php
}
?>
Where is a problem? These two script half work.
This is OK click "Opel" -> "meriva". http://i.stack.imgur.com/ma5XZ.jpg
Next step 2 is a problem, i want "name" marka and "name" model.
Problem this is here.I don't get "marka - id", i want "name" http://i.stack.imgur.com/Nm1Yk.jpg
Update:
step2.php
<?php
if (isset($_POST['submit'])) {
$marka = $_POST['marka'];
$model = $_POST['model'];
$_SESSION['marka'] = $marka;
$_SESSION['model'] = $model;
}
?>
<div class="col-md-12 well advertTittle">
<h4 class="well">
<?php echo $_SESSION['marka'] . '&nbsp' . $_SESSION['model'] . '&nbsp' . stripslashes($_SESSION['modify']); ?>
</h4>
<span><?php echo $_SESSION['price'] . ' лв.'; ?></span>
</div>
You forget to give the "name" attribute for yours "marka" select field:
<?php
...
?>
<select id="select_marka" class="form-control"> // <- here
<?php
$selModel = "SELECT * FROM marka_model WHERE marka_id='$marka_id'";
....
Should be something like this:
<?php
...
?>
<select id="select_model" class="form-control" name="model"> // <- here!!!
// "id" attribute also should be changed
<?php
$selModel = "SELECT * FROM marka_model WHERE marka_id='$marka_id'";
....
Also, yours step2.php should be like this:
<?php
if (isset($_POST['submit'])) {
$_SESSION['marka'] = $_POST['marka'];
$_SESSION['model'] = $_POST['model'];
}
function validData($data) {
return
isset($data['marka'])
&& isset($data['model'])
&& isset($data['price'])
;
}
if (validData($_SESSION)) {
$marka_id = intval($_SESSION['marka']);
$model_id = intval($_SESSION['model']);
$marka = mysqli_fetch_array(mysqli_query($conn, "SELECT name FROM marka where id = $marka_id"));
$model = mysqli_fetch_array(mysqli_query($conn, "SELECT name FROM marka_model WHERE id = $model_id"));
?>
<div class="col-md-12 well advertTittle">
<h4 class="well">
<?php echo $marka['name'] . '&nbsp' . $model['name'] . '&nbsp' . (isset($_SESSION['modify']) ? stripslashes($_SESSION['modify']) : ''); ?>***
</h4>
<span>
<?php echo $_SESSION['price'] . ' лв.'; ?>
</span>
</div>
<?php } else { ?>
Not all fields filled.
<?php } ?>

edit_subject.php is not working, the error is saying about database query failed

This is kind of the error I'm getting:
Database query failed.
I've uploaded this webpage: http://widgetcorp.bugs3.com/public/edit_subject.php?subject=1
Here's my file:
<?php require_once("../includes/session.php"); ?>
<?php require_once("../includes/db_connection.php"); ?>
<?php require_once("../includes/functions.php"); ?>
<?php require_once("../includes/validation_functions.php"); ?>
<?php find_selected_page(); ?>
<?php
if (!$current_subject)
{
// subject ID was missing or invalid or
// subject couldn't be found in database
redirect_to("manage_content.php");
}
?>
<?php
if (isset($_POST['submit']))
{
// validations
$required_fields = array("menu_name", "position", "visible");
validate_presences($required_fields);
$fields_with_max_lengths = array("menu_name" => 30);
validate_max_lengths($fields_with_max_lengths);
if (empty($errors))
{
// Perform Update
$id = $current_subject["id"];
$menu_name = mysql_prep($_POST["menu_name"]);
$position = (int) $_POST["position"];
$visible = (int) $_POST["visible"];
$query = "UPDATE subjects SET ";
$query .= "menu_name='{$menu_name}', ";
$query .= "position={$position}, ";
$query .= "visible={$visible} ";
$query .= "WHERE id={$id} ";
$query .= "LIMIT 1";
$result = mysqli_query($connection, $query);
if ($result && mysqli_affected_rows($connection) >= 0)
{
// Success
$_SESSION["message"] = "Subject updated.";
redirect_to("manage_content.php");
}
else
{
// Failure
$message = "Subject update failed.";
}
}
}
// else
// {
// // This is probably a GET request
// }
?>
<?php include("../includes/layouts/header.php"); ?>
<div id="main">
<div id="navigation">
<?php
echo navigation($current_subject, $current_page);
?>
</div>
<div id="page">
<?php
// echo message();
// $message is just a variable, doesn't use the SESSION
if(!empty($message))
{
echo "<div class=\"message\">" . htmlentities($message) . "</div>";
}
?>
<?php echo form_errors($errors); ?>
<h2>Edit Subject: <?php echo htmlentities($current_subject["menu_name"]); ?></h2>
<form action="edit_subject.php?subject=<?php echo htmlentities($current_subject["menu_name"]); ?>" method="post">
<p>Menu name:
<input type="text" name="menu_name" value="<?php echo htmlentities($current_subject["menu_name"]); ?>" />
</p>
<p>Position:
<select name="position">
<?php
$subject_set = find_all_subjects();
$subject_count = mysqli_num_rows($subject_set);
for ($count=1; $count <= $subject_count; $count++)
{
echo "<option value=\"{$count}\"";
if ($current_subject["position"] == $count)
{
echo " selected";
}
echo ">{$count}</option>";
}
?>
</select>
</p>
<p>Visible:
<input type="radio" name="visible" value="0" <?php if ($current_subject["visible"] == 0) { echo "checked"; } ?> /> No
<input type="radio" name="visible" value="1" <?php if ($current_subject["visible"] == 1) { echo "checked"; } ?> /> Yes
</p>
<input type="submit" name="submit" value="Edit Subject" />
</form>
<br />
Cancel
Delete Subject
</div>
The problem is somewhere else and not with your UPDATE query actually. If you see the link you posted, you are passing subject parameter with url, whose value is 1 which is integer.
Now when you click submit it's changing the url to http://widgetcorp.bugs3.com/public/edit_subject.php?subject=About%20Widget%20Corp .
Here as you see the subject parameter is not integer but string value name of subject. And that is causing the problem.
You are getting error as it's not retrieving the subject data from database correctly because of wrong id type. You just need to make sure the form is being posted to right url, which would be http://widgetcorp.bugs3.com/public/edit_subject.php?subject=1.
You need to correct the action parameter on the <form> tag for that.
Look for the line below in your code:
<form action="edit_subject.php?subject=<?php echo htmlentities($current_subject["menu_name"]); ?>" method="post">
And change it to
<form action="edit_subject.php?subject=<?php echo htmlentities($current_subject["id"]); ?>" method="post">
If you see, now the form will be submitted to http://widgetcorp.bugs3.com/public/edit_subject.php?subject=1, which is the correct url.

PHP how to make some selectbox from a database query

i wanna get this, value of 3 selectbox are same:
http://i.stack.imgur.com/EqvpT.jpg
then, i write this code:
$jabatan = mysql_query("SELECT * FROM jabatan WHERE id_jabatan NOT IN (1,10,11,12)") or die(mysql_error());
if (isset($_GET['id'])) {
$query = mysql_query("SELECT *, month(tanggal_larut) as bulan_larut, day(tanggal_larut) as hari_larut, year(tanggal_larut) as tahun_larut FROM larut WHERE id_larut ='$_GET[id]'") or die(mysql_error());
$row = mysql_fetch_object($query);
}
<li>
<label>Pemateri 1</label>
<select name="pemateri1">
<?php
while ($p1 = mysql_fetch_object($jabatan)) {
?>
<option value="<?php echo isset($p1->id_jabatan) ? $p1->id_jabatan : '' ?>" <?php if (isset($_GET['id'])) { if($p1->id_jabatan == $row->pemateri1) {echo "selected";} else {echo "";} } ?> ><?php echo isset($p1->nama_jabatan) ? $p1->nama_jabatan : '' ?></option>
<?php
}
?>
</select>
</li>
<li>
<label>Pemateri 2</label>
<select name="pemateri2">
<?php
while ($p2 = mysql_fetch_object($jabatan)) {
?>
<option value="<?php echo isset($p2->id_jabatan) ? $p2->id_jabatan : '' ?>" <?php if (isset($_GET['id'])) { if($p2->id_jabatan == $row->pemateri2) {echo "selected";} else {echo "";} } ?> ><?php echo isset($p2->nama_jabatan) ? $p2->nama_jabatan : '' ?></option>
<?php
}
?>
</select>
</li>
<li>
<label>Pemateri 3</label>
<select name="pemateri3">
<?php
while ($p3 = mysql_fetch_object($jabatan)) {
?>
<option value="<?php echo isset($p3->id_jabatan) ? $p3->id_jabatan : '' ?>" <?php if (isset($_GET['id'])) { if($p3->id_jabatan == $row->pemateri3) {echo "selected";} else {echo "";} } ?> ><?php echo isset($p3->nama_jabatan) ? $p3->nama_jabatan : '' ?></option>
<?php
}
?>
</select>
</li>
i get this:
http://i.stack.imgur.com/vYnpd.jpg
Ok, please somebody help me, how to correct the code? thanks
Can you try this.... if you want the same select box option why using more time while loop, you can do in one loop. For sql injection read this REF
<?php
$jabatan = mysql_query("SELECT * FROM jabatan WHERE id_jabatan NOT
IN (1,10,11,12)") or die(mysql_error());
if (isset($_GET['id'])) {
$query = mysql_query("SELECT *, month(tanggal_larut) as bulan_larut,
day(tanggal_larut) as hari_larut, year(tanggal_larut) as tahun_larut
FROM larut WHERE id_larut ='".mysql_real_escape_string($_GET[id])."'") or
die(mysql_error());
$row = mysql_fetch_object($query);
}
$Selectbox_options = '';
while ($p1 = mysql_fetch_object($jabatan)) {
$value = isset($p1->id_jabatan) ? $p1->id_jabatan : '';
$selection_value = '';
if (isset($_GET['id'])) {
if ($p1->id_jabatan == $row->pemateri1) {
$selection_value = "selected";
} else {
$selection_value = "";
}
}
$option_title = isset($p1->nama_jabatan) ? $p1->nama_jabatan : '';
$Selectbox_options .= '<option value="' . $value . '" ' . $selection_value . ' >' . $option_title . '</option>';
}
?>
<li>
<label>Pemateri 1</label>
<select name="pemateri1">
<?php echo $Selectbox_options;?>
</select>
</li>
<li>
<label>Pemateri 2</label>
<select name="pemateri2">
<?php echo $Selectbox_options;?>
</select>
</li>
<li>
<label>Pemateri 3</label>
<select name="pemateri3">
<?php echo $Selectbox_options;?>
</select>
</li>

Categories