Insert records into mysql database from a form

Discussion in 'PHP' started by Kyriakos, Apr 14, 2008.

  1. #1
    hi everyone,

    i'm very new with PHP. i use asp in my job but for my site i want to use php (with wamp server 2.0). i don'to know how to insert some records into mysql database. it's so simply for me in asp but i have no idea in php. can any from you convert this code for me? this is my asp script:
    <%
    addEmail=request.Form("addEmail")
    company=request.Form("company")
    subject=request.Form("subject")
    remarks=request.Form("remarks")
    
    Set Con = Server.CreateObject( "ADODB.Connection" )
    Con.Open "storeDSN","username","password"
    %>
    <% if addEmail = "1" then %>
    <%sqlString = "INSERT INTO emails (name,subject,remarks) VALUES ( '"&name&"','"&subject&"','"&remarks&"')"
    		Con.Execute sqlString
    		response.Write("record ok!")
    		%>
    		<%
    Con.Close
    %><% else %>error<% end if %>
    HTML:
    username of mysql database is "root" and password is blank (by default).

    thanks in advance.
     
    Kyriakos, Apr 14, 2008 IP
  2. Xtrm2Matt

    Xtrm2Matt Active Member

    Messages:
    129
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #2
    
    <?php
    if( $_POST["do"] == 1 )
    {
    	// Database configuration
    	$host = "localhost";
    	$user = "root";
    	$pass = "";
    	$database = "database";
    	$table = "emails";
    	
    	// Connect!
    	$connection = mysql_connect( $host, $user, $pass );
    	
    	// Select the database
    	mysql_select_db( $database, $connection );
    	
    	// Catch the form values and assign them variables
    	$name = $_POST["name"];
    	$subject = $_POST["subject"];
    	$remark = $_POST["remark"];
    	
    	// Insert the database
    	mysql_query( "INSERT INTO $table (name, subject, remark) VALUES ($name, $subject, $remark)" );
    	
    	// All done!
    	echo "Done!";
    }
    ?>
    
    <form method="post" action="">
    <input type="hidden" name="do" value="1">
    Name: <input type="text" name="name" />
    Subject: <input type="text" name="subject" />
    Remark: <input type="text" name="remark" />
    <input type="submit" value="Submit" />
    </form>
    
    PHP:
    Hope that helps :)
     
    Xtrm2Matt, Apr 14, 2008 IP
  3. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #3
    thank you very match. i appreciate it.
     
    Kyriakos, Apr 14, 2008 IP
  4. Xtrm2Matt

    Xtrm2Matt Active Member

    Messages:
    129
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #4
    No problem. If it doesn't work or you need any more help then let me know.
     
    Xtrm2Matt, Apr 14, 2008 IP