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

104 225
    • 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);

Source...

Leave A Reply

Your email address will not be published.