My checkboxes does not store in the database table that i have
Here is the whole code..
The code that i use for the insert into is this
queryMysql("INSERT INTO patient_diseases (disease,username) VALUES('$disease','$username')");
and my checkboxes code is this...
<?php
$sql = "SELECT name FROM disease";
$query_resource = mysql_query($sql);
while( $name = mysql_fetch_assoc($query_resource) ):
?>
<span><?php echo $name['name']; ?></span>
<input type="checkbox" name="disease[]" value="<?php echo $name['name']; ?>" /><br />
<?php endwhile; ?>
What is wrong with the code?
I had the same code for the insert into for the dropdown list and it worked fine but then i was asked to change it to checkboxes and now it doesnt store the selected values
Also i am getting this error Notice: Array to string conversion in E:\xampp\htdocs\ptixiaki\signup.php on line 50 and the line 50 is this queryMysql("INSERT INTO patient_diseases (disease,username) VALUES('$disease','$username')");
And it store in my data this
Why it stores Array in the disease column and why it doesn't store all the selected checkboxes
The solution was the below..
It was one line of code
$disease = implode(",",$_POST["disease"]);
queryMysql("INSERT INTO patient_diseases (disease,username) VALUES('$disease','$username')");
Related
I want to make a dropdown and values should be selected from the database. But in my case values are not shown in the dropdown.
[1]: https://i.stack.imgur.com/6F6O9.png
Do you get any errors?
Also here's the way I usually do this kind of thing
<?php
$query = "SELECT * FROM parent_category";
$results=mysqli_query($conn, $query);
while($row = mysqli_fetch_assoc($results))
{
?>
<option.....<?php echo $row['parent_id']; ?>
<?php
}
//php continues
?>
I've been trying to understand what is wrong ... but i am still clueless. Any help will be appreciated.
Following code inserts a form element
<?php foreach ($coal_type as $coal) {?>
<div class="checkbox">
<label>
<input type="checkbox" name="usage_checkbox[]" class="usage_checkbox"
value="<?php echo $coal['coal_type_id']; ?>">
<?php echo $coal['coal_type']; ?>
</label>
</div>
<?php } ?>
This data is sent to a form handler which executes following code
obj_params.usage_checkbox = $("input[type='checkbox'][name='usage_checkbox[]']:checked").val();
This data is sent to the controller ( i am using codigniter) which inserts it
$i = 0;
foreach ($this->input->post('usage_checkbox') as $key) {
$coal_type[$i] = $key;
$i++;
}
$coal_type[$i] = $this->input->post('other_usage');
$c_type = implode(',', $coal_type);
$arr_to_insert = array('type_of_coal' => $c_type,);
$insert_id = $this->common_model->insertRow($arr_to_insert,'ekyc_details');
Now when i submit no value is inserted. I have tried everything but I can't figure out what is wrong.
Here multiple values are get from the the checkbox.So you need to serialize to send the values through jquery.The following code will help you.
obj_params.usage_checkbox = $("input[name='usage_checkbox[]']:checked").serialize();
i have a problem with that i need to get the value from the dropdown list to be a number and the name for a kategory to be the name that the user picks.
<select name="kategori">
<?php
$query=mysql_query("SELECT KategoriID from Kategori");
$second=mysql_query("SELECT KategoriNavn from Kategori");
while($r=mysql_fetch_row($query) && $v=mysql_fetch_row($second)){
echo "<option value='$r[0]>$v[0]</option>";
}
?>
This is the code i have, but i cant make it to work.
Im kinda new to PHP. Thanks!
There's no need to write two different queries. You could have written just a single one. I think mysql fetch_assoc is a tad easier to understand.
You can try something like this:
<?php
$query = mysql_query("SELECT KategoriID, KategoriNavn from Kategori") or die(mysql_error()); // Debugging displays SQL syntax errors, if any.
echo "<pre>";
print_r($query);
exit; // Let me know what the array looks like.
while ($r= mysql_fetch_assoc($query)) { ?>
<option value=<?php echo $r['KategoriID']; ?> >
<?php echo $r['KategoriNavn']; ?>
</option>
<?php } ?>
<?php
echo "<pre>";
print_r($_POST); // Do this where you're checking your POST data
exit;
?>
Assuming you want option value to be KategoriNavn and the option to display to be KategoriID.
Hope this helps.
Peace! xD
This question already has an answer here:
Post variables from a second table in form
(1 answer)
Closed 9 months ago.
I'm trying to grab a two variables from 2 columns (cover and pageno). As you can see the select is for the name column so I can't grab the other variables and insert them into the cover and pageno columns. I have values in the inputs but there's one issue.
If there's more than one book it will echo all covers and all pageno for all books in the input values.
When the db is populated it does the same as the value, it populates the columns with all - covers and pageno's for all books instead of the specific book selected.
I'm not sure how to loop through the inputs and print only the book selected and populate the db with what was selected only. Maybe javascript of Jquery? I hope that makes sense.
<select name="name">
<option value="<?php echo "{$_POST['name']}"; ?>"> </option>
<?php
include('theconnection.php');
$con = mysqli_connect($host,$user,$pass,$dbName);
if (!$con)
{
die('cannot connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"thebooks");
$result = mysqli_query($con,"SELECT b.id, b.name, b.cover, b.pageno FROM books");
$cover = '';
$pageno = '';
while($row = mysqli_fetch_array($result))
{
echo ("<option value='$row[name]'>$row[name] $row[cover], $row[pageno] </option>");
$cover .= "$row[cover]";
$pageno .= "$row[pageno]";
}
?>
</select>
<input type="hidden" name="cover" value="<?php echo $cover; ?>">
<input type="hidden" name="pageno" value="<?php echo $pageno; ?>">
i am not clear on your question. from what i could make out of it is that the hidden fields cover and page no should only show the selected row. looking at your code i don't get why are you using the select query twice anyway you are not using the first query and can be safely removed and also you can add the where clause to filter out your result and even if that has multiple results you can put some sort of comparison in the while loop but i suggest you do that in the query itself. if showing just one value in the hidden cover and pageno is your motive then you can use
$cover = "$row[cover]";
$pageno = "$row[pageno]";
if you could explain it more clearly i would be happy to help
i am working on wordpress based coupon site. I have to create a amount calculator which would work on all the individual category pages . I would be having a amount slider which would be having $ values.
Once a value is selected and on clicking the submit button, i want the percentage deals (under that respective category) to calculate a amount with respect to the $ amount selected using the slider. and then displaying it on their respective deals.
I hope the idea is clear.
Till now, i have managed to, take all the posts title of the current category page into an array, and then using the preg_match feature, i have managed to extract out the '%' deal amount.
Also i have created a simple slider which the user will need to input their $ amount.
<?php
$array = array();
global $post;
$category = get_the_category($post->ID);
$category = $category[0]->cat_ID;
$myposts = get_posts(array('numberposts' => 1, 'offset' => 0, 'category__in' => array($category), 'post_status'=>'publish'));
foreach($myposts as $post) :
setup_postdata($post);
$title = get_the_title();
array_push($array,$title);
endforeach; ?>
<?php wp_reset_query(); ?>
<?php
foreach($array as $str) {
if(preg_match('/(\d+)\%/i' ,$str,$m)) {
echo $m[1],"\n"; ?>
<input type="text" name="a" value="<?php echo $m[1]; ?>" size=5>
<?php }
} ?>
the above code is used to fetch all the post under the current category and extracting the % value from the respective post title. The extracted number is in '$m[1]' which i would like to pass against the respective post.
I am not able to define the respective post and passing that '%' amount and in return sending the calculated amount and saving it back to that particular post. that is, on clicking the submit button, i would want that each post having percentage value would get calculated and get displayed against that particular post. Sorry for such a huge explanation. I didnt want any detail to get missed out. Any help would be appreciated.
EDITED CODE - This code is responsible to display a single deal. I have placed the above mentioned in the sidebar file of my theme. I want to display the savings within the respective percentage deal.
<div style="float:left; <?php if($GLOBALS['themename']['display_previewimage'] =="yes"){ ?>width:357px;<?php }else{ ?>width:477px;margin-left:10px;<?php } ?>">
<h2 class="coupontitle">
<a href="<?php echo $link; ?>" title="<?php the_title_attribute(); ?>" <?php if($GLOBALS['premiumpress']['analytics_tracking'] =="yes"){ ?>onclick="pageTracker._trackEvent('COUPON CLICK', 'TITLE CLICK', '<?php the_title(); ?>');"<?php } ?> <?php if(is_single()){ ?> target="_blank"<?php } ?>>
<?php the_title(); ?>
</a>
</h2>
<p><?php echo $post->post_content; ?></p>
<?php if($code != "" && $GLOBALS['themename']['system'] =="link"){ ?>
</div>
Ok I have created a JSFIDDLE that will give you an idea about what to do. http://jsfiddle.net/Q5sAK/1/
For this I used the jquery UI slider but this should work for your own slider you just need to call the $.each() function when you are ready.
So we know that your percentage is in the h2 tag with the class coupontitle of all of you products.
First we are going to start by modifying that h2 to have a span tag at the end that we will use to hold the computed savings:
<h2 class="coupontitle">
<a href="<?php echo $link; ?>" title="<?php the_title_attribute(); ?>" <?php if($GLOBALS['premiumpress']['analytics_tracking'] =="yes"){ ?>onclick="pageTracker._trackEvent('COUPON CLICK', 'TITLE CLICK', '<?php the_title(); ?>');"<?php } ?> <?php if(is_single()){ ?> target="_blank"<?php } ?>>
<?php the_title(); ?>
</a>
<span><span>
</h2>
Then we need to add the javascript that will calculate the savings.
function updateSlider(){
//Get the value of the slider.
var curentSliderAmount = $('#sliderId').val();
//Loop over all of the titles
$.each($('.coupontitle'),function(index,coupObj){
//This will create the variable from the regex search that will have all of the parts for the percent we need
var percent = $(coupObj).text().match(/(\d+)\%/i);
//We will then take the 2nd part which is just the number without the % sign and make it a percent then multiply that by the slider value and then fix it to a 2 decimal value so it can be used a curency.
var savings = ((percent[1]*.01) * curentSliderAmount ).toFixed(2);
//We then set the span html content = to our newly calculated value.
$('span',coupObj).html('Save: $'+savings);
});
}
//Run this when the page starts
$(document).ready(function(){ updateSlider() });
Then you just need to call updateSlider() when ever the slider updates.