How to Insert Into a Database With a Drop-down Menu With PHP

A drop-down menu lets your users choose one value, but you must first make a connection to the database before inserting that value into a table. The PHP and MySQL languages work natively together, so the functions to connect them are already included with the PHP library. You can then insert the user's selected value into a MySQL table using the PHP programming language.

Instructions

    • 1

      Right-click the PHP page you want to edit and select "Open With." Select your PHP editor from the list of programs.

    • 2

      Add a connection to the server and choose a database where you want to insert the new value. The following code shows you how to connect to MySQL from PHP:

      mysql_connect("localhost", "myname", "mypass") ;
      mysql_select_db("mydatabasename") or die(mysql_error());

      Replace your MySQL server name and username and password in the first set of parameters. In the MySQL select function, type your database name.

    • 3

      Create the query string that inserts a record using the selected value from a drop-down box. The following code shows you how to create an insert query:

      $sql = "insert into orders (product) values ('" + $_POST['productList'] + "')";

      The "productList" is the name of the drop-down on your page. Change this name with the name of your own drop-down box.

    • 4

      Send the query to the MySQL server. After the query is set up, you must send it to the database server. Use the following code to send to the user:

      $insertresult= mysql_fetch_array( $sql);

Related Searches:

References

Comments

Related Ads

Featured