423,822 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,822 IT Pros & Developers. It's quick & easy.

Inserting a value from a drop down list into a mysql database

P: 20
Hi All

I wish to use a drop down list with the values 'Active' and 'Inactive' and select one of those values to go into my table 'users' which has a field called 'status'. I do not want to store the values 'Active' and 'Inactive' into another table but I want to use them from the list directly. In fact I have been able to build the drop down list but I am unable to insert it into the table 'users'. Could anyone please tell me how this can be done since I am new to php and mysql.

Thanks in advance
Jun 18 '07 #1
Share this Question
Share on Google+
5 Replies


ak1dnar
Expert 100+
P: 1,584
Hi All

I wish to use a drop down list with the values 'Active' and 'Inactive' and select one of those values to go into my table 'users' which has a field called 'status'. I do not want to store the values 'Active' and 'Inactive' into another table but I want to use them from the list directly. In fact I have been able to build the drop down list but I am unable to insert it into the table 'users'. Could anyone please tell me how this can be done since I am new to php and mysql.

Thanks in advance
Hi sharmilah,

Please post some codes here,that you made so far.at least you can post your html form.
Then its so easy for us to provide some guidence with your coding.

Thanks,
-Ajaxrand
Jun 18 '07 #2

P: 20
Hi ajaxrand

Part of the code which makes the drop down list in my program is shown below

<?php
....some codes here
<tr>
<td class="hr"><? echo htmlspecialchars("Status")."&nbsp;" ?></td>
<td class="dr"><SELECT name=”Usr_Status”>
<option value="Active">Active</option>
<option value="Inactive">Inactive</option></SELECT>
</td>
</tr>
...code continues
?>

The above creates the drop down list, but I want to know how to post the value chosen into my table user in the field 'Usr_Status' so that I can know whether a user is active or inactive.

Thanks
Jun 19 '07 #3

bonski
P: 53
[PHP]
<?php
....some codes here
<tr>
<td class="hr"><? echo htmlspecialchars("Status")."&nbsp;" ?></td>
<td class="dr"><SELECT name=”Usr_Status”>
<option value="Active">Active</option>
<option value="Inactive">Inactive</option></SELECT>
</td>
</tr>
...code continues
?>[/PHP]

----------------------------------------------------------

hi there...

ill just make this simple...

// this is for you form.. where you input data...
Expand|Select|Wrap|Line Numbers
  1. <html>
  2.  
  3. <form name="form_name" action="form_action_page" method="post">
  4. <table>
  5. <tr>
  6. <td>
  7. Status
  8. </td>
  9. <td>
  10.  
  11. <select name="Usr_Status">
  12. <option value="active">Active</option>
  13. <option value="inactive">Inactive</option>
  14. </select>
  15. </td>
  16. </tr>
  17. <tr>
  18. <td>
  19. submit button.....
  20. </td>
  21. </tr>
  22. </form
  23. </html>
// now here is where you action or processing the data...

[PHP]<?php

// this is how to get the inputted data from form... if the drop down is not set it will just set an automatic value to 'active'.. ok.. ^__^
$usr_status = isset($_POST['Usr_Status']) ? $_POST['Usr_Status'] : 'active';

// now insert this variable to the table..
$sql = "INSERT INTO users(status) VALUES('".$usr_status."')";
$qry = mysql_query($sql);

?>[/PHP]

now just try this one... and if it works, modify it to where you're comfortable with.. ok... have fun... ^__^
Jun 19 '07 #4

P: 20
[PHP]
<? function showroweditor($row)
{
global $conn;
?>
<table class="tbl" border="0" cellspacing="1" cellpadding="5"width="50%">
<tr>
<td class="hr"><? echo htmlspecialchars("User Id")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="Usr_Id" value="<? echo str_replace('"', '&quot;', trim($row["Usr_Id"])) ?>"></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Surname")."&nbsp;" ?></td>
<td class="dr"><textarea cols="35" rows="4" name="Usr_Surname" maxlength="100"><? echo str_replace('"', '&quot;', trim($row["Usr_Surname"])) ?></textarea></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Name")."&nbsp;" ?></td>
<td class="dr"><textarea cols="35" rows="4" name="Usr_Name" maxlength="100"><? echo str_replace('"', '&quot;', trim($row["Usr_Name"])) ?></textarea></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Login")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="Usr_Login" maxlength="10" value="<? echo str_replace('"', '&quot;', trim($row["Usr_Login"])) ?>"></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Password")."&nbsp;" ?></td>
<td class="dr"><input type="text" name="Usr_Passwd" maxlength="16" value="<? echo str_replace('"', '&quot;', sha1(trim($row["Usr_Passwd"]))) ?>"></td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Status")."&nbsp;" ?></td>
<td class="dr"><SELECT name=”Usr_Status”>
<option value="Active">Active</option>
<option value="Inactive">Inactive</option>
</SELECT>
</td>
</tr>
<tr>
<td class="hr"><? echo htmlspecialchars("Role")."&nbsp;" ?></td>
<td class="dr"><select name="Usr_RolId">
<?
$sql = "select `Rol_Id`, `Rol_Name` from `roles`";
$res = mysql_query($sql, $conn) or die(mysql_error());

while ($lp_row = mysql_fetch_assoc($res)){
$val = $lp_row["Rol_Id"];
$caption = $lp_row["Rol_Name"];
if ($row["Usr_RolId"] == $lp_row["Rol_Id"]) {$selstr = " selected"; } else {$selstr = ""; }
?><option value="<? echo $val ?>"<? echo $selstr ?>><? echo $caption ?></option>
<? } ?></select>
</td>
</tr>
</table>
<? } ?>[/PHP]

I have put my codes here which has been generated with a php generator. I want to update the database with the values 'Active' or 'Inactive' in the field 'Usr_Status' - lines 26 to 33 show how I made the drop down list - in the same way as is done for the other items as shown in the code if possible ie. using str_replace rather than using insert. Could you please help

Thanks
Jun 19 '07 #5

bonski
P: 53
i think you can use.. UPDATE instead of INSERT... just get the value.. the same i gave you.. $_POST['Usr_Status']... and this time you have to get the id also of the user..

for example you a field in you user table.. that is user_id.. when you about to go to you edit page.. you should include this id also.. so that its easy for you to update the status of the user..

here..

[PHP]//this where you get the values
//this from the drop down
$usr_status = isset($_POST['Usr_Status']) ? $_POST['Usr_Status'] : 'active';
//make sure this one is pass through a hidden field.. ok <input type="hidden" />
$usr_id = isset($_POST['Usr_ID']) ? $_POST['Usr_ID'] : 0;

// this is how you update...
$sql = "UPDATE table SET status='".$usr_status."' WHERE user_id='".$usr_id."'";[/PHP]


^___^
Jun 19 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.