Send value from database to checkbox checked - php

I have a table with 3 fields looking_id,user_id,looking. looking_id is unique.
$sql="SELECT looking_id,user_id,looking FROM `provider_service_need` WHERE `user_id`='$user_id'";
$stmt=$dbg->query($sql);
<input type="checkbox" name="What_services[]" id="What_services" value="person_care" <?php if($stmt=='person_care')echo checked;?> >
<input type="checkbox" name="What_services[]" id="What_services" value="child_care" <?php if($stmt=='child_care')echo checked;?> >
<input type="checkbox" name="What_services[]" id="What_services" value="pet_care" <?php if($stmt=='pet_care')echo checked;?> >
but it's not working.

Try this
$sql="SELECT looking_id,user_id,looking FROM `provider_service_need` WHERE `user_id`='$user_id'";
$stmt=mysql_query($sql);
while($result = mysql_fetch_array($stmt))
{
?>
<input type="checkbox" name="What_services[]" id="What_services_<?php echo $result['looking'];?>" value="person_care" <?php if($result['looking']=='person_care')echo "checked";?> >
<?php
} ?>

For pdo try this
$sql="SELECT looking_id,user_id,looking FROM `provider_service_need` WHERE `user_id`='$user_id'";
foreach ($stmt->query($sql) as $row)
{
?>
<input type="checkbox" name="What_services[]" id="What_services_<?php echo $row['looking'];?>" value="person_care" <?php if($row['looking']=='person_care')echo "checked";?> >
<?php
} ?>

Related

fetch from database and display in input fields

I have a 7 check-boxes(monday,tuesday,wed,thur,friday,sat,sunday) and each of these tag i am having start and end time tag. Down below you can see the picture of the design
Now in database there is couple of entries like 1st id is having monday and bluh..bluh. You can see the picture of the database
<form id="checking" method="POST" >
<?php
$data=new User();
// in row it is array of the database details
$row=$data->Display('date');
$checkbox='';
$days = ["","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];
$out[]='<table>';
for($i = 1; $i <=7; $i++){
$out[]= '<tr>';
$out[]= '<td><input type="checkbox" name="events[]" value="'.$i.'" />'.$days[$i].'</td>';
$out[]= '<td><input type="text" name="start'.$i.'" value="'.$row[$i]['starttime'].'" />da</td>';
$out[]= '<td><input type="text" name="end'.$i.'" id="endTime'.$i.'" />End Time</td>';
$out[]= '</tr>';
}
$out[]='<table>';
echo join($out);
?>
<br/> <button id="sub" name="btnregister">Submit</button>
</form>
Now here is the trick- Fetch the details from the database and display it in input fields and if monday is having then it should checked that checkbox.
I really do not know what type of code i write and how i can print weekend name and should checked the checkbox if enteries is entered in database.
Hope this will helps you...
<?php
// Create connection
$con=mysqli_connect("hostname","username","pass","dbname");
$result = mysqli_query($con,"SELECT * FROM table_name");
while($row = mysqli_fetch_array($result))
{
$a=$row['field_name'];
?>
<input type="checkbox" name="week[]" value="monday" <?php if(in_array("monday",$a)) { ?> checked="checked" <?php } ?> >
<input type="checkbox" name="week[]" value="tuesday" <?php if(in_array("tuesday",$a)) { ?> checked="checked" <?php } ?> >
<input type="checkbox" name="week[]" value="Wednesday" <?php if(in_array("Wednesday",$a)) { ?> checked="checked" <?php
} ?> >
<?php
}
?>

Check checkbox depending on a value in database

I'm trying to check a checkbox based on its database value, i have tried the following but no joy, any suggestions?
I am using Kohana framework.
public static function defaultdistance(){
$result = DB::select('value')->from('mytable')->where('key', '=', 'default-distance')->execute();
$distancestores = $result->as_array();
foreach($distancestores as $distancestore)
{
echo 'Value: '.$distancestore['value'];
}
}
<label class="shortLabel"><input type="radio" name="distance" value="10" <?php if ($distancestore['value'] == '10') echo "checked='checked'"; ?> /> 10 <?php echo $dict->transl('distance_km'); ?></label>
<label> </label><label class="shortLabel"><input type="radio" name="distance" value="15" <?php if ($distancestore['value'] == '15') echo "checked='checked'"; ?> /> 15 <?php echo $dict->transl('distance_km'); ?></label>
May be you need like this
$distancestores = $result->as_array();
foreach($distancestores as $distancestore)
{
?>
<label class="shortLabel">
<input type="radio" name="distance" value="10"
<?php if ($distancestore['value'] == '10') echo "checked='checked'"; ?> />
10 <?php echo $dict->transl('distance_km'); ?>
</label>
<?php
}
try this i think you have to use ternary operator to resolve this issue.
<input type="radio" name="distance" value="10"
<?php $select = $distancestore['value']=='10'?'checked':'';
echo $select; ?>
10 <?php echo $dict->transl('distance_km'); ?>

PHP - Loop number of selected check boxes on form submit

I have an issue where I need to loop the number of check boxes on a form submit. Foreach check box that is looped I need to then insert data into the database.
How Would I go about looping over the amount of check boxes that have being passed via form submit?
My code is as follows:
Form:
<form action="createChallenge.php" method="post" name="chalCreate">
Challenge Name:<input type="text" name="chalName" />
<br />
Challenge Target:<input type="text" name="chalTarget"/>
<br />
End Date:<input type="text" name="chalDate">
<br />
<!-- Needs a jquery datepicker -->
Select Friends: <br />
<?php
$selFriend = $conn->prepare("SELECT * FROM Friends WHERE UserID = '$userID' AND Friend = 'y' ORDER BY FriendName ASC");
$selFriend->execute();
foreach($selFriend as $row){
?>
<input type="checkbox" name="test" value="<?php echo $row['FriendID'] ?>"><?php echo $row['FriendName'] ?><br>
<?php
}
?>
<br />
<button type="submit">Create Challenge</button>
</form>
PHP to handle the form:
<?php
if(isset($_POST['test']))
{
$i = 0;
foreach($_POST['test'] as $checked)
{
echo $friend = $checked;
$i++;
}
echo $name = $_POST['chalName'];
echo $target = $_POST['chalTarget'];
echo $date = $_POST['chalDate'];
echo $friend = $_POST['test'];
echo $setby = $_COOKIE['userID'];
$create = $conn->prepare("INSERT INTO Challenge ( chalSetBy, chalName, chalTarget, chalDate ) VALUES ('$setby', '$name', '$target', '$date') ");
$create->execute();
if($create)
{
echo "Challenge made successfully";
}
else
{
echo "There was a problem";
}
}
?>
I thought doing the following would echo out data, but it didn't, it only selected the last check box:
$i = 0;
foreach($_POST['test'] as $checked)
{
echo $friend = $checked;
$i++;
}
Make an array of your checkbox in HTML page like as below,
<form name="frm" method="post">
<input type="checkbox" value="1" name="test[]">
<input type="checkbox" value="2" name="test[]">
<input type="checkbox" value="3" name="test[]">
<input type="checkbox" value="4" name="test[]">
<input type="checkbox" value="5" name="test[]">
<input type="checkbox" value="6" name="test[]">
<input type="submit">
</form>
<?php
foreach($_POST['test'] as $key=>$value)
{
echo $value."<br>";
}

PHP Update multiple rows in MySQL

I have this PHP/HTML Code that is selecting data from a MySQL Database:
<?php
$sql3="SELECT * from property_images where property_seq = '".$property["sequence"]."' ";
$rs3=mysql_query($sql3,$conn);
while($property_img=mysql_fetch_array($rs3))
{
?><tr>
<td colspan="2"><img src="http://domain.co.uk/img/property-images/<?php echo $property_img["image"]; ?>" width="80px" height="80px" /></td>
<td colspan="2"><input type="checkbox" name="image1" value="Y" <?php if($property_img["image1"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image2" value="Y" <?php if($property_img["image2"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image3" value="Y" <?php if($property_img["image3"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image4" value="Y" <?php if($property_img["image4"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image5" value="Y" <?php if($property_img["image5"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image6" value="Y" <?php if($property_img["image6"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image7" value="Y" <?php if($property_img["image7"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image8" value="Y" <?php if($property_img["image8"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image9" value="Y" <?php if($property_img["image9"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image10" value="Y" <?php if($property_img["image10"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image11" value="Y" <?php if($property_img["image11"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image12" value="Y" <?php if($property_img["image12"] == 'Y') { echo 'checked="checked"'; } ?> />
<input type="checkbox" name="image13" value="Y" <?php if($property_img["image13"] == 'Y') { echo 'checked="checked"'; } ?> /></td>
</tr><?php
}
?>
each row, has its own image with the columns image1 - image13
i want to update the table with the checkboxes that are checked and unchecked on the form update
how is this possible?
Thanks
If you mean that you want to update the $property_img["imagexx"] val on db depending on the user click on the checkbox you must use ajax.
Fire an event on triggering each checkbox and send the value to a php page that update the php.
Jquery Ajax function can help you on this task.
L
name all your checkboxes images[]
<input type="checkbox" name="images[]" value="1" />
<input type="checkbox" name="images[]" value="2" />
<input type="checkbox" name="images[]" value="3" />
<input type="checkbox" name="images[]" value="4" />
You can then update with PHP :
<?php
$q = "UPDATE property_images SET";
foreach($_POST["images"] as $image) {
$q .= " image" . $image ." = 'Y', ";
}
$q .= " property_seq = '".$property["sequence"]."' WHERE property_seq = '".$property["sequence"]."'";
mysql_query($q);
?>
First of all, mysql_ functions are deprecated, please google mysqli_ or PDO, mysql_ won't be supported on future versions, and is unsafe, etc
Your html output could be much simpler with a loop, also have an eye on putting the sequence number on a hidden field or something first:
<?php
$sql3="SELECT * from property_images where property_seq = '".$property["sequence"]."' ";
$rs3=mysql_query($sql3,$conn);
while($property_img=mysql_fetch_array($rs3)){
?>
<tr>
<td colspan="2"><img src="http://domain.co.uk/img/property-images/<?php echo $property_img["image"]; ?>" width="80px" height="80px" /></td>
<!-- THIS IS VERY IMPORTANT: send the sequence or ID through a hidden field, to know which row you are gonna update later-->
<input type="hidden" name="sequence" value="<?php echo $property_img['sequence']; ?>"/>
<td colspan="2">
<?php for($i = 1; $i <= 13; $i++): ?>
<input type="checkbox" name="images[]>" value="<?php echo $i; ?>" <?php if($property_img["image$i"] == 'Y') { echo 'checked="checked"'; } ?> />
<?php endfor ?>
</td>
</tr>
<?php
}
?>
Then this is the next page where the update is done, have a good look at the comments:
<?php
//Handle as you want the situation when there are no images selected instead of using an exit(), I used it here just for the quickness;
if(count($_POST['images'] < 1) exit('no images where selected to update');
$images = array();
for($i = 1; $i <= 13; $i++){
$string = "image$i = ";
if(in_array($i, $_POST['images'])){
$string .= "'Y'"; //notice the double quoting here, it's important
} else {
//This updates the table so if it was checked when loaded, but unchecked by the user, this makes the change!
$string .= "'N'";
}
}
//This creates a string like: image1 = 'Y', images2 = 'N', etc...
$images = implode(', ', $images );
//try to sanitize the query first ... I won't cos am just showing you your question xD
$sql = "UPDATE property_images SET $images WHERE property_seq = " . mysql_real_escape_string($_POST[sequence]) . ";
mysql_query($sql,$conn);
?>

Checking checkboxes from database values

I post values from check boxes into a database for a user profile. When the user goes to edit his/her profile I want the checkboxes that they selected previously to be checked so they don't lose that info after updating their profile. I have tried many different solutions but with no luck.
The check box values get entered into table name members_teachers into a column called focus and are seperated by a comma for example art,mathematics,dance etc I am not sure how close or far I am away at accomplishing my goal but I greatly appreicate any help or suggestions you can provide. Thank you very much in advance
My code to try and check the values is
<?php
$focusQuery = mysql_query("SELECT focus FROM members_teachers WHERE id = $member_id") or die;
while ($new_row = mysql_fetch_assoc($focusQuery)){
$focusRow = $row['focus'];
$focusValue = explode(',', $focusRow);
foreach($focusValue as $newFocus){
//echo $newFocus;
//echo "<br/>";
$result = mysql_query("SELECT focus FROM members_teachers WHERE focus LIKE '%$focusRow%'") or die;
if(mysql_num_rows($result) > $newFocus){
$checked = 'checked="checked"';
}
else{
$checked = '';
}
}
}
?>
This is my html
<label for="art-focus">Art</label>
<input name="focus[]" type="checkbox" value="Art" <?php echo $checked ?>>
<label for="math-focus">Mathematics</label>
<input name="focus[]" type="checkbox" value="Mathematics" <?php echo $checked ?>>
<label for="dance-focus">Dance</label>
<input name="focus[]" type="checkbox" value="Dance" <?php echo $checked ?>>
<?php
// Create connection
$con=mysqli_connect("hostname","username","pass","dbname");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT focus FROM members_teachers WHERE id = $member_id");
while($row = mysqli_fetch_array($result))
{
$focus=explode(",",$row['focus']);
?>
<input type="checkbox" name="focus[]" value="Art" <?php if(in_array("Art",$focus)) { ?> checked="checked" <?php } ?> >
<input type="checkbox" name="focus[]" value="Mathematics" <?php if(in_array("Mathematics",$focus)) { ?> checked="checked" <?php } ?> >
<input type="checkbox" name="focus[]" value="Dance" <?php if(in_array("Dance",$focus)) { ?> checked="checked" <?php } ?> >
<?php
}
?>
<?php
$focusedValues = array();
$focusQuery = mysql_query("SELECT focus FROM members_teachers WHERE id = $member_id") or die;
while ($row = mysql_fetch_assoc($focusQuery)){
$focusedValues = explode(',', $row['focus']);
}
?>
<label for="art-focus">Art</label>
<input name="focus[]" type="checkbox" value="Art" <?php echo in_array('Art', $checked) ?>>
<label for="math-focus">Mathematics</label>
<input name="focus[]" type="checkbox" value="Mathematics" <?php echo in_array('Mathematics', $checked) ?>
<label for="dance-focus">Dance</label>
<input name="focus[]" type="checkbox" value="Dance" <?php echo in_array('Dance', $checked) ?>
I don't know why you were SELECTing for the second time, that's pointless, you already know what's checked because it's in $focusedValues. Also, in your code, $checked would be empty if nothing was checked and checked="checked" otherwise. You obviously need a variable for each input, no?
<?php $hobby = $row['hobbies'];
$hobbies = explode (' ', $hobby);
?>
<input type="checkbox" name="hobbies[]" value="cricket" <?php echo in_array('cricket', $hobbies?'checked':'') ?> >cricket
<input type="checkbox" name="hobbies[]" value="singing" <?php echo in_array('singing' , $hobbies ?'checked': '') ; ?> >singing
<input type="checkbox" name="hobbies[]" value="football" <?php echo in_array('football', $hobbies ?'checked': '') ; ?> >footballl

Categories