473,386 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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
5 21956
ak1dnar
1,584 Expert 1GB
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
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
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
[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
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

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

Similar topics

1
by: Jonny Tango | last post by:
Hello everyone. Q. How do I create a dynamically-generated drop-down list for use in an array. I'm using PHP with a MySQL database (through phpMyAdmin) My database table is called...
1
by: jonnytansey2 | last post by:
Can anyone out there give me a pointer regarding creating a dynamically-generated drop-down list connected to an array? And is that question as clear as chocolate spread? Here's what I've got....
5
by: Vigneshwar Pilli via DotNetMonster.com | last post by:
string connectionString1 = "server=(local); user=sa;password=sa; database=sonic"; System.Data.SqlClient.SqlConnection dbConnection1 = new System.Data.SqlClient.SqlConnection(connectionString1);...
3
by: cpptutor2000 | last post by:
Could some PHP guru please help me? I am have a drop down list, whose options are read in dynamically from a table in a MySQL database. one of the items being read in is an URL. I am unable to...
5
by: ashok893 | last post by:
I'm using two drop down list ina form. I have generated the first drop down list from MySQL database. When i select an option from first drop down list, i have to generate second drop down list...
0
by: jaeden99 | last post by:
I have a two drop down list box. The first contains district name(district id is the value) and the the second will contain the user name based on the district selected in the first drop down list....
1
by: student2008 | last post by:
Sorry about the title its a tricky one. I have a form which allows me to add a question and answers into a mysql database via a combination of, if a certain option is chosen and the reset button...
4
by: hdhouse3rd | last post by:
Greetings: A newbie question. I have a form that is completed and every time we save the form the value of the drop down list is reset to default. I presume you need to do a query of the database...
0
by: StarLavender | last post by:
Hi. I want to create a search form using the drop down list and checkbox. First, the users have to select from drop down list whether they want to search by date or id. After that, for example, a new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.