Form and do not connect the mysql

i have a form that has a HTML <select> data i do not know how to send these data to mysql.

<?php
        $months=range(1,12);
        $days=range(1,31);
        $years=range(2010,1900);
        echo'<select name="year">';
        foreach($years as $key=>$value){
            echo"<option value=\\"$key\\">$value</option>\
";
        }
        echo'</select>';
                echo'<select name="month">';
        foreach($months as $key=>$value){
            echo"<option value=\\"$key\\">$value</option>\
";
        }
        echo'</select>';
                echo'<select name="day">';
        foreach($days as $key=>$value){
            echo"<option value=\\"$key\\">$value</option>\
";
        }
        echo'</select>';
        ?>

Sending the values to a MySQL database is very simple PHP

mysql_query("INSERT INTO `table_name` (`year`,`month`,`day`) VALUES('".$_POST['year']."','".$_POST['year']."','".$_POST['year']."')", $con);

Make sure you read about variable sanitizing and protection to prevent MySQL injection attacks

[B]mysql_real_escape_string/B - http://php.net/manual/en/function.mysql-real-escape-string.php

in addition to SgtLegend’s post your form must have an attribute called “action” and the value of action will be the name of the server side script that will receive the form data when submit is clicked and process it as SgtLegend described.

Or if it is a single field/column to store the date then you just concatenate the values posted to a single variable according to your date format in the table. Assuming the format YYYY-MM-DD:


$date = mysql_real_escape_string($_POST['year']);
$date .= '-' . mysql_real_escape_string($_POST['month']);
$date .= '-' . mysql_real_escape_string($_POST['day']);
// then insert the date into the table.
$sql = "INSERT INTO SET `date`='" . $date . "'";

I do not understand

$sql = “INSERT INTO SET date='” . $date . “'”;

and it’s does not work still.

What rajug posted is example code, to run the query you need to already have a MySQL connection setup and then use the [B]mysql_query/B function to run it. Personally i would recommend using MySQLi if your server supports it as its a lot better and easier to understand

I see the only question is i can get the data that selected but i do not know how to format the data that selected then store the data

Sorry the query I prepared was wrong. It should be something like this:


$sql = "INSERT INTO tblname SET `date`='" . $date . "'";

The table name is missed.

Can you post the table structure so that we can give you a real example/query?

ok
this is the table structure
CREATE TABLE IF NOT EXISTS mst_userinfo (
ID int(11) NOT NULL auto_increment,
userID varchar(16) NOT NULL,
password varchar(16) NOT NULL,
user_name varchar(20) NOT NULL,
user_sex tinyint(4) NOT NULL,
user_birthday date NOT NULL,
user_mail varchar(50) NOT NULL,
ins_userID varchar(16) NOT NULL,
ins_date date NOT NULL,
upd_userID varchar(16) NOT NULL,
upd_date date NOT NULL,
PRIMARY KEY (ID)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=85 ;

only the birthday i do not know how to save

I assume the field to store your selected date is user_birthday:


$date = mysql_real_escape_string($_POST['year']);
$date .= '-' . mysql_real_escape_string($_POST['month']);
$date .= '-' . mysql_real_escape_string($_POST['day']);
// then insert the date into the table.
$sql = "INSERT INTO `mst_userinfo` SET `user_birthday`='" . $date . "'";

Setting rest of the fields are as usual I hope you know that.

thx a lot