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.

sammy
  • 717
  • 4
  • 13

5 Answers5

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).

L Ja
  • 1,384
  • 1
  • 11
  • 22
  • friend, it just printed all records, i need a drop down list that i can choose a job. :( – sammy Oct 29 '15 at 07:53
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.

Adarsh Rajput
  • 1,246
  • 14
  • 24
  • 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? – sammy Oct 29 '15 at 07:59
  • thanks friend thanks! my reputation is below 15 so i cant vote you, u helped me too much :) – sammy Oct 29 '15 at 08:14
  • friend a question, why i cant use of
    here? echo "توانمندی مورد نظر خود را انتخاب نمایید: ";
    echo '
    – sammy Oct 29 '15 at 08:22
  • i got myself, cuz
    was in php form :p
    – sammy Oct 29 '15 at 08:31
  • @sajad: Correct. Either put **br** in echo (`echo '
    ';`) OR close-re-open php tag(`?>
    – Adarsh Rajput Oct 29 '15 at 08:53
  • can you take a look on my new problem?!!! http://stackoverflow.com/questions/33425375/send-information-of-a-drop-down-list-to-database – sammy Oct 29 '15 at 22:21
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>
Manoj Salvi
  • 2,639
  • 1
  • 17
  • 21
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>";
Shailesh Katarmal
  • 2,757
  • 1
  • 12
  • 15
  • 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? – sammy Oct 29 '15 at 08:01
  • That is possible, just add this under your – L Ja Oct 29 '15 at 08:07
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>";
Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137
  • 1
    um, it seems that you spent too much time to solve my problem so you have my special thanks... – sammy Oct 29 '15 at 21:42
  • 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 – Maytham Fahmi Oct 29 '15 at 21:48
  • hi میثم: can u please look on this post too?i think u can help me: http://stackoverflow.com/questions/33460844/stopping-duplicate-of-printing-name-of-user-php-sql?noredirect=1#comment54708178_33460844 – sammy Nov 01 '15 at 10:30
  • sure I will do @sajad – Maytham Fahmi Nov 01 '15 at 10:31