How to do simple Contact Form to mysql database

Discussion in 'PHP' started by Wrighteq, Mar 25, 2007.

  1. #1
    I have a site and I want to put in a contact with. When the user submits that form I want it to put the entries into a secure mysql database, and to email me the form contents.

    I got the email part down, but what would be some simple code to store everything in a mysql database.


    Thanks
     
    Wrighteq, Mar 25, 2007 IP
  2. mikemotorcade

    mikemotorcade Peon

    Messages:
    645
    Likes Received:
    49
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here is some code. You would have to replace the $host, $username, $password, $database with the appropriate info.

    also, in the query, you would replace "field1" etc, with the names of the db fields, and "field1data" etc, with the data itself. Probably like $_POST['formfield1'] or whatever.

    //open database connection
    $db = mysql_connect ("$host", "$username", "$password") or die ('I cannot connect to the database because: ' . mysql_error());
    
    //select the database
    mysql_select_db ("$database");
    
    //build the query to store the data
    $query = 'INSERT INTO `contactTable` (`field1`, `field2`, `field3`) VALUES ("$field1data", "$field2data", "$field3data")';
    
    //run the query
    $result = mysql_query($query);
    
    //check to make sure everything was successful
    if ($result) {
    	//data successfully inserted into database. Execute whatever code you want next.
    }else{
    	//there was an error.
    }
    
    //close database connection
    mysql_close($db);
    PHP:
     
    mikemotorcade, Apr 18, 2007 IP