Reputation: 307
I am writing a php function which update mysql record everything updating perfectly except radio button values i even tried to print radio values to check whether radios are working or not and i got successful results but the issues remains on position that i am unable to update radio value in my SQL
Important thing to mention here is "radio values are integers 1, 2 3 etc "
here is my html code
<input type="radio" name="active_status" id="radio1" class="we-radio" value="1">
<label for="radio1" class="we-label">Active</label>
<input type="radio" name="active_status" id="radio2" class="we-radio" value="0">
<label for="radio2" class="we-label">Inactive</label>
<input type="radio" name="featured_status" id="radio3" class="we-radio" value="1">
<label for="radio3" class="we-label">Featured</label>
<input type="radio" name="featured_status" id="radio4" class="we-radio" value="0">
<label for="radio4" class="we-label">Normal Video</label>
and its my php code
$activation_vid = $_POST['active_status'];
$featured_vid = $_POST['featured_status'];
$updates = array();
if (!empty($activation_vid))
$updates[] = '`vid_act_stat` ="'.mysqli_real_escape_string($conn,$activation_vid).'"';
if (!empty($featured_vid))
$updates[] = '`vid_featured_stat` ="'.mysqli_real_escape_string($conn,$featured_vid).'"';
$updates = implode(', ', $updates);
$sql = "UPDATE `tblmevids` SET $updates WHERE vid_id = '$vid_edit_id'";
$result=mysqli_query($conn,$sql);
if($result){
$sucess = "<div class='success'>Perfect!!! Vid has been updated: ".$current_vid_code."</div>";
} else {
$error_display = "<div class='errormsgbox'>An error occured. Please Try Again</div>";
}
Upvotes: 1
Views: 50
Reputation: 1580
when you select Inactive Status you pass 0 value in POST and you are using empty function to check which causes problem. So only when you select Active Status your query will update it for inactive Status the condition will be fail instead using empty
if (!empty($activation_vid))
use
isset() and is_numeric()
From PHP Manual http://php.net/manual/en/function.empty.php
The following things are considered to be empty:
"" (an empty string) 0 (0 as an integer) 0.0 (0 as a float) "0" (0 as a string) NULL FALSE array() (an empty array) $var; (a variable declared, but without a value)
it is working for me Try this
$activation_vid = '0';
$featured_vid = '1';
$vid_edit_id=5;
$updates = array();
if (is_numeric($activation_vid) and isset($activation_vid))
$updates[] = '`vid_act_stat` ="'.$activation_vid.'"';
if (is_numeric($featured_vid) and isset($featured_vid))
$updates[] = '`vid_featured_stat` ="'.$featured_vid.'"';
$updates = implode(', ', $updates);
$sql = "UPDATE `tblmevids` SET $updates WHERE vid_id = '$vid_edit_id'";
print_r($sql);
...
//rest of your code
Upvotes: 1
Reputation: 1580
Since we can only guess what is wrong with your program, here are some tips:
1. Don't use mysqli_real_escape_string
on integer values. Instead use intval
function:
$activation_vid = intval($_POST['active_status']);
2. In your SQL statements don't put integer values in quotations:
$updates[] = "`vid_act_stat` = {$activation_vid}";
$sql = "UPDATE `tblmevids` SET $updates WHERE `vid_id` = $vid_edit_id";
3. This one is not actually an error but a suggestion. If you want to alternate between 0 and 1, use checkbox
instead of radio
buttons.
4. As was mentioned by "CY5", empty
also returns false
when the value of the variable is 0
, so you must use a defferent if
statement to check validity of received parameters.
Upvotes: 0