I'm trying to update an existing row in the table cms_events. But my option value wont trigger the column "code". The option values is listed, but have no effect after saving. Any suggestions? best regards
<select class="form-control" name="code" style="width:65%;">
<?php
$code = mysql_real_escape_string($_POST['code']);
$stripped = mysql_real_escape_string($_GET['id']);
if(isset($_POST['event']))
{
$result = mysql_query("UPDATE cms_events SET code = '$code', userid = '".$_SESSION['user']['id']."'
WHERE id = $stripped
")
or die(mysql_error());
header('Location: /index.php?url=events&id='.htmlspecialchars($stripped).'&code='.htmlspecialchars($code).'');
exit;
}
$getKommentar = mysql_query("SELECT * FROM `cms_prosjekt` WHERE userid = '".htmlspecialchars($_SESSION['user']['id'])."'");
while($kommentarinfo = mysql_fetch_array($getKommentar))
{
echo'<option value="'.htmlspecialchars($kommentarinfo['code']).'">'.htmlspecialchars($kommentarinfo['code']).'</option>';
}
}
?>
</select>
Related
This is a table column that display message depends on user preferred language choice in the DB. eg if preferred language is ENG only the eng
message will show
PS: in the database, there's 3 column storing 3 types of message of different languages
<?php
$possibleLang = ["繁體","简体","ENG"];
$testAreaField = ["traditionalmessage","simplifiedmessage","engmessage"];
$treatmentName = ["vaccinename1","vaccinename2","vaccinename3"];
$treatmentNameSuffix = ["\n下一個注射期為:","\n下一个注射期为:","\nNext
injection period will be:"];
$index = array_search($row['language'],$possibleLang);
?>
<td>
<textarea rows="3" cols="18" class="url" name="<?php echo
$testAreaField[$index]; ?>[]" data-value="<?php echo
$row[$treatmentName[$index]] . $treatmentNameSuffix[$index]; ?>"><?php echo
$row[$testAreaField[$index]]; ?>
</textarea>
</td>
This table is at an edit page that displays data from DB and also updates data back to the DB.
if (isset($_POST['submit'])){
foreach ($_POST['patientid'] as $index => $patientid) {
$id = mysql_real_escape_string($_POST['id'][$index]);
$data1 = mysql_real_escape_string($patientid);
$data17 = mysql_real_escape_string($_POST['patienthkid'][$index]);
$data2 = mysql_real_escape_string($_POST['vaccineid'][$index]);
$data5 = mysql_real_escape_string($_POST['vaccinename3'][$index]);
$data6 = mysql_real_escape_string($_POST['totalnoofinjection'][$index]);
$data7 = mysql_real_escape_string($_POST['nthinjection'][$index]);
$data8 = mysql_real_escape_string($_POST['date'][$index]);
$data9 = mysql_real_escape_string($_POST['nextdate'][$index]);
$data10 = mysql_real_escape_string($_POST['skip'][$index]);
$data11 = mysql_real_escape_string($_POST['language'][$index]);
$data12 = mysql_real_escape_string($_POST['traditionalmessage'][$index]);
$data13 = mysql_real_escape_string($_POST['simplifiedmessage'][$index]);
$data14 = mysql_real_escape_string($_POST['engmessage'][$index]);
$data15 = mysql_real_escape_string($_POST['status'][$index]);
$data16 = mysql_real_escape_string($_POST['nurse'][$index]);
if ($data12 != null) {
mysql_query("UPDATE patientvaccinedetail SET patientid
='$data1',patienthkid
='$data17',vaccineid='$data2',vaccinename3='$data5',
totalnoofinjection='$data6',nthinjection='$data7',
date='$data8',nextdate='$data9',skip='$data10',language='$data11',
traditionalmessage='$data12',status='$data15',nurse='$data16' WHERE
id=$id") or die(mysql_error());
}
else if ($data13 != null) {
mysql_query("UPDATE patientvaccinedetail SET patientid
='$data1',patienthkid
='$data17',vaccineid='$data2',vaccinename3='$data5',
totalnoofinjection='$data6',
nthinjection='$data7',date='$data8',nextdate='$data9',
skip='$data10',language='$data11',
simplifiedmessage='$data13',status='$data15',nurse='$data16' WHERE
id=$id") or die(mysql_error());
}
else if ($data14 != null) {
mysql_query("UPDATE patientvaccinedetail SET patientid
='$data1',patienthkid ='$data17',vaccineid='$data2',
vaccinename3='$data5', totalnoofinjection='$data6',
nthinjection='$data7',date='$data8',nextdate='$data9',
skip='$data10',language='$data11',
engmessage='$data14',status='$data15',nurse='$data16' WHERE id=$id")
or die(mysql_error());
}
}
}
The issues are whenever I click save and only one column of the message is SAVED, other two column are updated as NULL in the database. The desired outcome is the update both 3 columns while only 1 column is visible to the page.
Any idea. Thanks in advance
Do not update columns which are not being targeted for update. For example, if the traditional message were selected, then you would update only this column. I would probably use separate queries here.
foreach ($_POST['patientid'] as $index => $patientid) {
$id = mysql_real_escape_string($_POST['id'][$index]);
$data1 = mysql_real_escape_string($patientid);
$data2 = mysql_real_escape_string($_POST['traditionalmessage'][$index]);
$data3 = mysql_real_escape_string($_POST['simplifiedmessage'][$index]);
$data4 = mysql_real_escape_string($_POST['engmessage'][$index]);
if ($data2 != null && $data2 != '') {
mysql_query("UPDATE table SET patientid ='$data1',
traditionalmessage='$data2' WHERE id=$id") or die(mysql_error());
}
else if ($data3 != null && $data3 != '') {
mysql_query("UPDATE table SET patientid ='$data1',
simplifiedmessage='$data3' WHERE id=$id") or die(mysql_error());
}
else if ($data4 != null && $data4 != '') {
mysql_query("UPDATE table SET patientid ='$data1',
engmessage='$data4' WHERE id=$id") or die(mysql_error());
}
}
Note that I believe you are using a very old and deprecated PHP API for MySQL. For starters, it would benefit you a lot by using prepared statements.
I am trying to generate pin and serial number stored in database and display base on demand. For example, if someone needs 1 or 2 pin and serial number, after dispaying those pin and serial number, I want to update their value so that it can't be generated again. I have trouble updating the value.
<?php
if (isset($_POST['card']) && isset($_POST['cn'])) {
$card = $_POST['card'];
$no_of_pin = $_POST['cn'];
if (!empty($card) && !empty($cn)){
$query =mysql_query("SELECT id, pin, sn FROM pin_test WHERE value = '0' LIMIT $no_of_pin");
while($row = mysql_fetch_assoc($query))
echo 'PIN: ' .$row['pin']. ' SN:'. $row['sn']. ' id:'. $row['id'].'<br>';
$pin = $row['pin'];
$sn = $row['sn'];
$id = $row['id'];
$query2 = "UPDATE pin_test SET value = '1' WHERE id = $id";
if(mysql_query($query2)){
echo 'Successful';
}else{
echo 'Unsuccessful';
}
}else{
echo 'All fields are required.';
}
}
?>
You have a logical error which runs but doesn't really affect the table.
Try doing it like this:
$query2 = "UPDATE pin_test SET value='1' WHERE id='$id'";
Try the following syntax:
UPDATED:
$query2 = "UPDATE pin_test SET value=1 WHERE id='$id'";
MY HTML
<div class="normal-text">
<? $check = mysql_query("SELECT * FROM client")or die(mysql_error());
while ($check2 = mysql_fetch_array( $check ))
{
$checkgather = mysql_query("SELECT * FROM gather where client_id = '".$check2['client_id']."' ")or die(mysql_error());
$checkgather2 = mysql_fetch_array( $checkgather );
echo $check2['client_name'].' :
<select name="gather" class=\"form-field\">
<option value="hashtag" '.(($checkgather2['gather_choice']=='hashtag')?'selected="selected"':"").' >hashtag</option>
<option value="latitude" '.(($checkgather2['gather_choice']=='latitude')?'selected="selected"':"").' >latitude/longitude</option>
<option value="followers" '.(($checkgather2['gather_choice']=='followers')?'selected="selected"':"").'>followers</option>
</select>
Start Thread to gather<br>';
}
?>
<br>
<input class="submit-button" type="submit" name="submit" value="Update" />
</div>
I have several rows of results.. each with a dropdown menu of what's in the DB... If I change 1 or multiple values, and I press the UPDATE button... How can I treat the code..
I'm assuming I'll need a foreach(.... )
my current PHP is this:
if (isset($_POST['submit']))
{
$update = mysql_query("UPDATE gather set gather_choice = ' ' where client_id = ' ' ")or die(mysql_error());
}
But I'll probably need a foreach somewhere... Any tips on how to make this work?
thanks
You can do this, assuming you have a unique key on client_id
INSERT INTO gather (gather_choice, client_id)
VALUES (first_choice, first_client_id), (second_choice, second_client_id)
ON DUPLICATE KEY UPDATE client_id = VALUES(client_id)
This will basically try to insert first, but seeing that it already exists, will just update it.
I am having a problem with updating some values in my database.
When I want to change a value using a drop down box, it automatically puts in the first value in the dropdown menu. What I want is to get the value that is already set in the database.
Here is my code:
<select name="vrijwilligerID">
<?php
$vrijwilligerID = $_POST["vrijwilligerID"];
$query = "SELECT voornaam, achternaam, vrijwilligerID FROM vrijwilliger;";
$result = mysql_query($query);
while($row=mysql_fetch_array($result)){
echo "<option value=".$row['vrijwilligerID'].">".$row["voornaam"]." ".$row["achternaam"]."</option>";
}
?>
</select>
Does anyone know how to get this right?
Thank you in advance.
Based on the selected item you have to add selected attribute to the option.
try this
<select name="vrijwilligerID">
<?php
$vrijwilligerID = $_POST["vrijwilligerID"];
$query =" SELECT voornaam, achternaam, vrijwilligerID
FROM vrijwilliger;";
$result = mysql_query($query);
while($row=mysql_fetch_array($result)){
if($row["vrijwilligerID"]==$vrijwilligerID)
echo "<option value=".$row['vrijwilligerID']." selected>".$row["voornaam"]." ".$row["achternaam"]."</option>";
else
echo "<option value=".$row['vrijwilligerID'].">".$row["voornaam"]." ".$row["achternaam"]."</option>";
}
?>
</select>
Its easy enough to resolve that using php, but using mysqls IF() function you can have the selected option directly. Using mysqls concat() you could get your desired result as well.
(ofcourse we're not using any postsvars in our scripts unsanitized are we ;) )
$iVrijwilligerid = filter_input(INPUT_POST, 'vrijwilligerID', FILTER_VALIDATE_INT, array("options"=> array("min_range"=>0, "max_range"=>999))) ;
if($iVrijwilligerid)
{
$sQry = <<<QRY
SELECT
CONCAT('<option value="', vrijwilligerID, '"',IF((SELECT vrijwilligerID FROM vrijwilliger WHERE vrijwilligerID=$iVrijwilligerid), ' selected="selected"', '' ),'>',voornaam, ,achternaam,'</option>') AS optItem
FROM
vrijwilliger
WHERE
your clause here
QRY;
$oResult = mysql_query( $sQry );
while($aRow=mysql_fetch_assoc( $oResult ))
{
echo $aRow['optItem'];
}
}
else
{
// nada
}
Am facing troubles in this code, i just want to get all data from table row if the user selected "show all" from the select drop menu.
here is the select menu !
so, this menu grab data from this table, but if he selects All, what is the suitable code to echoing in between option value :)
<b>speciality:</b> <select id="main_mav" name="speciality">
<option value="none">Select speciality:</option>
<option value=""> All specialities </option>
<?php
$result = mysql_query('SELECT speciality FROM visits') or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
echo '<option value="'.$row['speciality'].'">'.$row['speciality'].'</option>';
}
?>
</select><br />
That's the Submit form !
if ($region=="All regions" ){
$region=$_POST['""'];
}
else ( $region=$_POST['region']);
$date1 =$_POST['from_date'];
$date2 = $_POST['to_date'];
$product=$_POST['product'];
$speciality=$_POST['speciality'];
$type=$_POST['visit_type'];
sql="SELECT id, customer_name, seller_1_name, seller_2_name FROM visits Where (speciality ='$speciality') AND (visit_type ='$type') AND (product ='$product') AND (region ='$region') AND (visit_date BETWEEN '$date1' AND '$date2')";
$result=mysql_query($sql); ## This line is new.
$num=mysql_numrows($result);
$row = mysql_fetch_array($result);
What's the correct code to enter if user selected " show all in drop menu " ?!
You really need to sanitize your inputs, at least with mysql_real_escape_string!
On to your actual question: just check if $speciality is empty, and generate a different query without the (speciality ='$speciality') condition.
Since your HTML referenced 'specialties' and your PHP referenced 'regions' I'm gonna just stick with 'regions', but here's the idea.
if ($region=="All regions" ){
$sql = 'SELECT id, customer_name, seller_1_name, seller_2_name, FROM visits';
} else {
$region = mysql_real_escape_string($_POST['region']);
$date1 = mysql_real_escape_string($_POST['from_date']);
$date2 = mysql_real_escape_string($_POST['to_date']);
$product = mysql_real_escape_string($_POST['product']);
$speciality = mysql_real_escape_string($_POST['speciality']);
$type = mysql_real_escape_string($_POST['visit_type']);
$sql = "SELECT id, customer_name, seller_1_name, seller_2_name FROM visits Where (speciality ='$speciality') AND (visit_type ='$type') AND (product ='$product') AND (region ='$region') AND (visit_date BETWEEN '$date1' AND '$date2')";
}
$result = mysql_query($sql); ## This line is new.
$num = mysql_numrows($result);
$row = mysql_fetch_array($result);