Submit HTML pick / dropdown form to MySQL database

I am trying to send an HTML <form> and have input and select information entered into a MySQL database table. My code enters data <input type="text"> into the table, but does not enter data into <select> .

This is the <form> that I used:

 <form enctype="multipart/form-data" action="send.php" method="post"> <select name="gender"> <option value="Male"> Male</option> <option value="Female">Female</option> </select> <input type="text" name="name"> <input type="submit" value="Add"> </form> 

And this is the PHP that I used to enter the form into the table:

 <?php $gender=$_POST['gender']; // This is the select/dropdown $name=$_POST['name']; // This is the text input mysql_connect("", "", "") or die(mysql_error()) ; mysql_select_db("") or die(mysql_error()) ; mysql_query("INSERT INTO `table1` VALUES ('$gender', '$name')") ; ?> 
+4
source share
4 answers

Hmmm ... If this does not work, try:

 mysql_query("INSERT INTO table1 VALUES ('".$gender."','".$name."');"); 
+1
source
  • try printing the results of your form.
  • try typing the SQL statement instead of executing
  • check approval by eye
  • copy the statement and try to execute it on the server manually.
  • add the parameter log=query.log in the [mysqld] section of the MySQL server configuration file (possibly my.cnf ), restart the server and look in query.log for the latest queries - do they reach the server?
+2
source

Try:

 mysql_query("INSERT INTO `table1` (genderCol,nameCol) VALUES ('{$gender}','{$name}')"); 

Although, as @Joe Stefanelli said, you really have to misinform the input - never trust the user. Take a look at the prepared statements

0
source

You can try

 <?php $gender=$_POST['gender']; $name=$_POST['name']; mysql_connect("", "", "") or die(mysql_error()) ; mysql_select_db("") or die(mysql_error()) ; mysql_query("INSERT INTO `table1` SET `fieldgender`='{$gender}', `fieldname`='{$name}'") ; ?> 

With field player AND field name are colums in your mysql1 table

0
source

All Articles