insert data to mysql db from user USING php script

Discussion in 'PHP' started by progfrog, Dec 11, 2008.

  1. #1
    Hi!
    I would like to insert data in to table through user inter face.

    basic, right?


    I have two text boxes and i need to insert the data frsend the submitted info(that is written by the user) directly to database.

    Is it possible to connect to the server directly from the form (that contain the text boxes)?
    In the command:
     form action="process.php" method="post"
    HTML:
    The file-process.php is made to create the connection.

    But, Is it that the submitted info has to pass through another process file? can't the form action can be
     form action="mysql databse"
    
    HTML:
    thanx
    progfrog
     
    progfrog, Dec 11, 2008 IP
  2. Lexiseek

    Lexiseek Banned

    Messages:
    2,499
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    0
    #2
    No, that won't work. You need to run the PHP script to handle the MySQL tasks.
     
    Lexiseek, Dec 11, 2008 IP
  3. newgenservices

    newgenservices Well-Known Member

    Messages:
    862
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    105
    Digital Goods:
    1
    #3
    You can try to send data to the same file if you do not like to send data to other file(s).
     
    newgenservices, Dec 11, 2008 IP
  4. SornSoft

    SornSoft Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    on the page process.php you'll need to do something like this.

    Lets say for example your form as FName, LName, Age and Sex then your could do this.

    
    <?
        include_once("dbConnect.php"); //This file should hold all your DB connection information (User,Pass, Host, DBName)
    
         $fname = $_POST['FName'];
         $lname = $_POST['LName'];
         $age = $_POST['Age'];
         $sex = $_POST['Sex'];
    
         UpdateDB();
    
         function UpdateDB(){
              global $fname, $lname, $age, $sex;
              $tbl = "TABLENAME"; // Change this to your table name in your DB
              mysql_query("UPDATE $tbl SET FName='$fname', LName='$lname', Age='$age', Sex='$sex'") or die();
         }
    
    mysql_close($con);	// Closes the DB Connection (Provided you set $Con to the DB Information in the file dbConnect.php)
    ?>
    
    Code (markup):
    I went ahead and threw in the dbConnect.php code as well.
    
    	//Please Change the values below to reflect your servers settings!
    	$dbUser = "admin";
    	$dbPass = "mypass";
    	$dbName = "myDBName";
    	
    	// Handles DB Connection DO NOT CHANGE CODE BELOW THIS LINE!!!
    	$con = mysql_connect("localhost", $dbUser, $dbPass) or die(mysql_error());
    	mysql_select_db($dbName,$con) or die(mysql_error());
    
    Code (markup):
    Obviously this could be more complex and involved but this is just a simple example showing how to update a mysql record. There are plenty of tutorials on the subject and the php manual is a invaluable resource. Simply type "PHP [COMMAND YOUR LOOKING FOR] Tutorial" in google and it should help you tremendously. In this case you would have searched for "PHP MYSQL UPDATE Tutorial"

    I hope this helps a bit
     
    SornSoft, Dec 11, 2008 IP