3

how i can insert my db information into a list?

<?php

$db_host = 'localhost';
$db_name= 'site';
$db_table= 'tablesite';
$db_user = 'root';
$db_pass = '';




$con = mysql_connect($db_host,$db_user,$db_pass) or die("خطا در اتصال به پايگاه داده");
$selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");
mysql_query("SET CHARACTER SET  utf8");
$dbresult=mysql_query("SELECT * FROM  $db_table WHERE job<>''",$con);
while($amch=mysql_fetch_assoc($dbresult))
{?>

<select>

<option value="$amch['job_name']"></option>

</select>

for example i have 15 jobs in my db, now i want a drop list that has all 15 works.

5
  • so I assume every thing is working but you need to make a dropdown with 15 job working? is that correct? Commented Oct 29, 2015 at 7:42
  • @maytham-ɯɐɥıλɐɯ 15 or more or less, may i want remove some jobs or i want to have more that 15. its possible? Commented Oct 29, 2015 at 7:44
  • btw mysql is deprecated and not suggest to be used longer. use mysqli or pdo Commented Oct 29, 2015 at 7:44
  • yes it is, I will try to answer your question, let me have some minuttes Commented Oct 29, 2015 at 7:45
  • @maytham-ɯɐɥıλɐɯ: many thanks... Commented Oct 29, 2015 at 7:46

5 Answers 5

1

You need to change your while loop like this:

$dbresult=mysql_query("SELECT * FROM  $db_table WHERE job<>''",$con);
echo '<select>';
echo '<option></option>'; // add a empty select as first dropdown element, you can use <option value="">-Select-</option> OR <option value="">-Not Applicable-</option>
while($amch=mysql_fetch_assoc($dbresult))
{
   echo '<option value="'.$amch['job_name'].'">'.$amch['job_name'].'</option>';
}
echo '</select>';

Here value attribute is not required as option text is same.

Sign up to request clarification or add additional context in comments.

6 Comments

that was true, i forgot a thing, i need a empty record at the first of jobs because some people do not have job. its possible?
thanks friend thanks! my reputation is below 15 so i cant vote you, u helped me too much :)
friend a question, why i cant use of <br/> here? echo "توانمندی مورد نظر خود را انتخاب نمایید: "; <br/> echo '<select>';
i got myself, cuz <br/> was in php form :p
@sajad: Correct. Either put br in echo (echo '<br />';) OR close-re-open php tag(?> <br /> <?php).
|
1
<?php
$db_host = 'localhost';
$db_name= 'site';
$db_table= 'tablesite';
$db_user = 'root';
$db_pass = '';

$con = mysql_connect($db_host,$db_user,$db_pass) or die("خطا در اتصال به پايگاه داده");
$selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");
mysql_query("SET CHARACTER SET  utf8");
$dbresult=mysql_query("SELECT * FROM  $db_table WHERE job<>''",$con);
echo "<select name=\"dropdown\">";
while($amch=mysql_fetch_array($dbresult))
{
    echo "<option value=".$amch['job_name'].">".$amch['job_label']."</option>";
}
echo "</select>";
?>

Try this, where job_label would be the name of the actual job in the database which shows as value in the dropdown. And job_name the actual value you want it to have to recognize which job was selected in the dropdown (the value of $_POST['dropdown'] really).

1 Comment

friend, it just printed all records, i need a drop down list that i can choose a job. :(
1

you should put while loop inside the select block and also use mysqli(improved) -

<?php

$db_host = 'localhost';
$db_name= 'site';
$db_table= 'tablesite';
$db_user = 'root';
$db_pass = '';




$con = mysqli_connect($db_host,$db_user,$db_pass,$db_name) or die("خطا در اتصال به پايگاه داده");
mysqli_query("SET CHARACTER SET  utf8");
$dbresult=mysqli_query("SELECT * FROM  $db_table WHERE job<>''",$con);
?>

<select>

<?php while($amch=mysqli_fetch_assoc($dbresult))
{?>

<option value="<?php echo $amch['job_name']?>"><?php echo $amch['job_name']?></option>

<?php } ?>

</select>

Comments

1
$con = mysql_connect($db_host,$db_user,$db_pass) or die("خطا در اتصال به پايگاه داده");
$selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");
mysql_query("SET CHARACTER SET  utf8");
$dbresult=mysql_query("SELECT * FROM  $db_table WHERE job<>''",$con);
echo "<select>";
while($amch=mysql_fetch_assoc($dbresult))
{
    ?>
    <option value="<?php echo $amch['job_name']; ?>"><?php echo $amch['job_name']; ?></option>
    <?php
}
echo "</select>";

2 Comments

that was true, i forgot a thing, i need a empty record at the first of jobs because some people do not have job. its possible?
That is possible, just add this under your <select>: echo "<option value=""></option>";
1

I know this comes late, but I wrote in my comments earlier that mysql is deprecated and it is not good to used it and most answer is based on deprecated method.

The risk of using mysql is that when you filter the jobs you can risk some body inject sql statement and return all job list, and that might overload your server in this case.

The other thing is your variable names can get you confused in long term, I will use understandable names.

Here is how your code should look like:

<?php

$db_hostname = 'localhost';
$db_database = 'site';
$db_username = 'root';
$db_password = '';

$db_table = 'tablesite';
$job = '';

// Create connection
$conn = new mysqli($db_hostname, $db_username, $db_password, $db_database);

// Check connection
if ($conn->connect_error)
    die("خطا در اتصال به پايگاه داده" . $conn->connect_error);

$job = mysqli_real_escape_string($conn, $job);

//since user and password is not blank, find user info using the email and password entered by user
$sql = "SELECT * FROM `$db_table` WHERE job <> ?";
$stmt = $conn->prepare($sql);

$stmt->bind_param("s", $job);
$stmt->execute();

$output = $stmt->get_result();

echo "<select>";
while ($row = $output->fetch_array(MYSQLI_NUM))
{
    echo "<option value=" . $row[0] . ">" . $row[1] . "</option>";
}
echo "</select>";

4 Comments

um, it seems that you spent too much time to solve my problem so you have my special thanks...
You are welcome I am happy for that. It is important you learn form it. I hope any way you re-think about checking this as answer
hi میثم: can u please look on this post too?i think u can help me: stackoverflow.com/questions/33460844/…
sure I will do @sajad

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.