<select> Default Value [duplicate] - php

This question already has answers here:
How can I set the default value for an HTML <select> element?
(34 answers)
Closed 6 years ago.
I have an issue where by I want to save a user input and then allow them to edit it later.
when i list the results for the edit their is no default.
<select id="edit_group" name="edit_group" class="form-control">
<?php Types("") ?>
</select>
types function is
function Types($x){
global $mysqli;
$type = " SELECT type FROM types ";
if ($type_results = $mysqli->query($type)) {
while ($row = $type_results->fetch_assoc()) {
printf ("<option>%s</option>", $row["type"]);
}
$type_results->free();
}
}
if anyone could point me in the right direction of how i could set the default value so its the one stored in the db, it would really help a beginner out. or better still point me in the right direction of documentation as my google searches have returned nothing but json and java options.

You don't use an argument $x of your Types function which as i guess is a selected value. Try to replace your function with this code:
function Types($x) {
global $mysqli;
$type = " SELECT type FROM types ";
if ($type_results = $mysqli->query($type)) {
while ($row = $type_results->fetch_assoc()) {
$selected = ($row["type"] == $x) ? 'selected="selected"' : '';
printf ("<option %s>%s</option>", $selected, $row["type"]);
}
$type_results->free();
}
}
and of course provide selected value to function Types in your html code.

Related

Accidental infinite while loop in PHP [duplicate]

This question already has answers here:
The 3 different equals
(5 answers)
Closed 6 years ago.
Hi I have created a while loop but cannot understand why it loops forever. Could somebody please explain why?
while ($i<3){
if ($i=1){
$x='psu';
}else if ($i=2){
$x='cases';
}
$sqlcpu = "SELECT * FROM $x WHERE name LIKE '%{$term}%'";
$query = mysqli_query($db, $sqlcpu);
while ($row = mysqli_fetch_array($query)){
?><br /> Name: <?php echo $row['name'];?>
<?php
echo ' Price: £'.$row['price'];
}
$i++;
}
There are other problems with this code but they are not my main concern right now as this loop seems like it should be simple. The variables $x and $i never change after $i=1
if ($i=1) You're assinging 1 to $i here, instead of comparing 1 and $i, use
if ($i==1)
instead.
Instead of the for/if-elseif construct you could also use
foreach( array('psu', 'cases') as $table ) {
$sqlcpu = "SELECT * FROM $table WHERE name LIKE '%{$term}%'";

PHP MySQL Get Specific Value When Result Show [duplicate]

This question already has answers here:
Extracting Twitter hashtag from string in PHP
(7 answers)
Closed 8 years ago.
How to get specific value of query in MySQL.
My code :
$q = mysqli_query("SELECT * FROM tb_post WHERE post LIKE '%#%'");
while($d = mysqli_fetch_array($q))
{
$post = $d['post'];
}
Example data result like this :
I'm groot #me
What are you doing #what
Sometimes I felt like superman #me
I don't know what to do #confuse
Now, my question is : is it possible to get just hashtag? Example : #me, #what, #confuse.
So I do not need another value.
Please help!
This should work for you:
$post = $d['post'];
$whatIWant = substr($post, strpos($post, "#") + 1);
echo "#" . $whatIWant . "<br />";
Output:
#me
#what
#me
#confuse

zero not disappearing [duplicate]

This question already has answers here:
Why does non-equality check of one variable against many values always return true?
(3 answers)
Closed 8 years ago.
$i=0;
$sql = $dbh->prepare('SELECT kappaleen_nimi, levy_id, kesto FROM kappaleet');
$ok = $sql->execute();
if(!$ok) { print_r( $sql->errorInfo() ) ; }
while($row = $sql->fetch(PDO::FETCH_ASSOC) ) {
foreach($row as $value) {
if($value!=null || $value!=0){
$haku2[$i] = $value;
$i=$i+1;
}
}
}
I'm working with this sql database, where i'm fetching kappaleen_nimi (name of the track), levy_id (record_id) and kesto (length) from the sql table kappaleet. I'm trying to remove fields containing null or 0. However, the final zero gets through anyway through this filter: "if($value!=null || $value!=0)".
When printing my table the following way:
foreach($haku2 as $value){
echo $value.', ';
}
I get this kind of result (last few fields): "Sateen tango, 7, 3.11, 0, "
The final zero is still there, and i can't get my head around it, why the he** it passes the if condition... I know that the final zero is levy_id (record_id).
I think you are looking for an AND conditional instead of an OR. To fix this, replace the following line
if($value!=null || $value!=0){
with this one
if($value!=null && $value!=0){

Data insertion using array in PHP

I have a php script for conducting online test. while taking test, I am using post method to get the answers in the action page.The answers are passed as an array with question id and the selected option value(true or false/the answers the candidate entered like that).Then I have to insert the answers marked by the candidate in the database. The code is as shown below:
$student_id=$_POST['name'];
$survey_id=$_POST['survey_id'];
$store = array();
if (isset($_POST['question_id'])) {
foreach ($_POST['question_id'] as $key => $option) {
$option1 = array_filter($option);
print_r($option1);
if (count($option1) > 1) {
$option2 = implode("^", $option1);
$store[] = $option2;
} else {
$value = $option1;
$i = implode(null, $option1);
$store[] = $i;
}
}
print_r($store);
}
$t = new DateTime();
$t->setTimestamp($time = time());
$t->setTimeZone(new DateTimeZone("Asia/Singapore"));
$date = $t->format(DateTime::RFC850);
$SQL = "INSERT INTO answer_table(student_id ,survey_id, ans_1, ans_2, ans_3, ans_4, ans_5, ans_6, ans_7, ans_8, ans_9, ans_10, timestamp) VALUES ('$student_id','$survey_id', '$store[0]', '$store[1]', '$store[2]', '$store[3]', '$store[4]', '$store[5]', '$store[6]', '$store[7]', '$store[8]', '$store[9]', '$date')";
$result = mysql_query($SQL);
If the student answers the test in sequential order(from question number 1 to 10) the code works fine. But when the candidate answers in random manner(first 10 then 5 like that),the table field named,ans_1 will get inserted with answer of question number 10. I need to insert fields with corresponding answers ,(ans_1 with answer of question 1 like that) what ever pattern,the candidate takes test.
Can anyone help me to solve this issue. Thanks in advance.
For achieving you goal.you should store question_id with answers or create another table which store question_id and corresponding answers, with id of answer table.
If you have the question id in the $key variable, you could use
$store[$key] = $option2;
instead of
$store[] = $option2;
With that the first question is the first entry in the array regardless of the order of the answers in the post array.
You can replace $store[] = $option2; with:
$store[$_POST['question_id']] = $option2;

PHP: form select box doesn't write changed selection to DB

Okay, so I have this form that is set to preload a DB record for editing if you add a ?edit=1 to the url, it will load record #1 for editing into the form. I have a box that is like this-
<select class="field select addr">
<option value="no"<?php if($row['has_amenities'] == "no") {echo ' selected=\"selected\"'; } ?>>No</option>
<option value="yes"<?php if($row['has_amenities'] == "yes") {echo 'selected=\"selected\"'; } ?>>Yes</option>
</select>
Now, let's say that $row['has_amenities'] is "yes" so when the form loads, the select box is showing "Yes".
BUT, if I change the select box to "No" and click save, it doesn't write "no" to the DB, but it does wipe out that record's "yes" with nothing.
What am I doing wrong?
Here's the update code--
$sql = "UPDATE venues SET microsite_title = '$_POST[microsite_title]',
microsite_city_title = '$_POST[microsite_city_title]', logo = '$_POST[logo]', photo1 =
'$_POST[photo1]', photo2 = '$_POST[photo2]', photo3 = '$_POST[photo3]', photo4 =
'$_POST[photo4]', photo5 = '$_POST[photo5]', photo6 = '$_POST[photo6]', photo7 =
'$_POST[photo7]', photo8 = '$_POST[photo8]', website_primary = '$_POST[website_primary]',
website_secondary = '$_POST[website_secondary]', paragraph_1_title =
'$_POST[paragraph_1_title]', paragraph_1 = '$_POST[paragraph_1]', paragraph_2_title =
'$_POST[paragraph_2_title]', paragraph_2 = '$_POST[paragraph_2]', paragraph_3_title =
'$_POST[paragraph_3_title]', paragraph_3 = '$_POST[paragraph_3]', paragraph_4_title =
'$_POST[paragraph_4_title]', paragraph_4 = '$_POST[paragraph_4]', paragraph_5_title =
'$_POST[paragraph_5_title]', paragraph_5 = '$_POST[paragraph_5]', paragraph_6_title =
'$_POST[paragraph_6_title]', paragraph_6 = '$_POST[paragraph_6]', top10_1 =
'$_POST[top10_1]', top10_2 = '$_POST[top10_2]', top10_3 = '$_POST[top10_3]', top10_4 =
'$_POST[top10_4]', top10_5 = '$_POST[top10_5]', top10_6 = '$_POST[top10_6]', top10_7 =
'$_POST[top10_7]', top10_8 = '$_POST[top10_8]', top10_9 = '$_POST[top10_9]', top10_10 =
'$_POST[top10_10]', top10_locale = '$_POST[top10_locale]', contact_title =
'$_POST[contact_title]', contact_street_addr = '$_POST[contact_street_addr]',
contact_street_addr2 = '$_POST[contact_street_addr2]', contact_city =
'$_POST[contact_city]', contact_state = '$_POST[contact_state]', contact_zip =
'$_POST[contact_zip]', contact_phone = '$_POST[contact_phone]', contact_tollfree =
'$_POST[contact_tollfree]', latitude = '$_POST[latitude]', longitude = '$_POST[longitude]',
testimonial = '$_POST[testimonial]', sidebar_title = '$_POST[sidebar_title]',
sidebar_content = '$_POST[sidebar_content]', has_amenities = '$_POST[has_amenities]'
WHERE id = '$_POST[query]'";
Also, I know it's not a good idea to write $_POST values without cleaning them first, but this is an internal form behind a firewall, etc. I'll clean it up later after it's working :o)
Thanks!
It looks like the <select> element has no name or id--is that the case in your code? If so, I believe $_POST[has_amenities] won't be set--there would be no has_amenities value in $_POST. You'd get an empty string instead.
Wrap all of the instances of $_POST[] in {} (curly braces) so it looks like this
'{$_POST['key']}'
The curly braces are need to force PHP to evaluate $_POST as a variable when it's inside a double-quoted string.
Also, quote your $_POST array keys like this
$_POST['key']
You want to get in the habit of this even though $_POST[key] will usually work. PHP is treating key as an constant which, if it's undefined, is automatically turned into a the string "key" so you get the behavior you're expecting.
However, if key already exists as a constant (via the define()) function, you'll get the value of the key constant which is not what you want.
Take a look at the Array do's and don'ts section.
Try wrapping your array variables with curly braces like so:
'$_POST[paragraph_3]' = '{$_POST[paragraph_3]}'
You'll need to specify a name for this select tag. I also see you're escaping your double-quotes with backslashes, which is unnecessary (it will literally use \" so the output would look like: selected=\"selected\" which is bad html).
Try using this:
<select name="has_amenities" class="field select addr">
<option value="no"<?php if($row['has_amenities'] == "no") {echo ' selected="selected"'; } ?>>No</option>
<option value="yes"<?php if($row['has_amenities'] == "yes") {echo 'selected="selected"'; } ?>>Yes</option>
</select>
Your SQL statement will work the way it is, but not that if a single quote is entered by the user, it will break the statement...possibly causing a huge security hole. Check out "SQL injection" on the google.

Categories