Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

php and data base

Posted on 2003-11-11
2
Medium Priority
?
301 Views
Last Modified: 2013-12-16
hai !!
i'm a novice in web programming , i wanted to build a html form so that the user can input his data and i can store his request in the data base . i have tried using php for catching the variables from the form and  insert these values into my sql database but i was unsuccessful in doing that . can anyone please help me ..here is what i have done

form.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>FORM</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="post" action="my.html">
  <p>ID:
    <input name="id" type="text" id="id">
  </p>
  <p>FIRST NAME:
    <input name="first_name" type="text" id="first_name">
  </p>
  <p>LAST NAME:
    <input name="surname" type="text" id="surname">
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit">
    <input name="reset" type="reset">
  </p>
</form>

</body>
</html>

my.html

<html>
      <head>
            <title>Welcome to Adobe GoLive 4</title>
      </head>
      
<?php
      $username = "root";
      $password = "root";
      $hostname = "localhost";      
      //$dbh = mysql_connect($hostname, $username, $password) or

die("Unable to connect to MySQL");
      //print "Connected to MySQL<br>";
      // you're going to do lots more here soon


$id = $HTTP_GET_VARS["id"];
$first_name = $HTTP_GET_VARS["fist_name"];
$surname = $HTTP_GET_VARS["surname"] ;
$selected = mysql_select_db("first_test",$dbh) or die("Could not

select first_test");
//      if (mysql_query("insert into people

values('$id','$first_name','$surname')")) {
        //print "successfully inserted record";
//      }
//      else {
         //print "Failed to insert record";
//      }
//mysql_close($dbh);
print(" id: {0},firstname {1},surname{2}",$id,$first_name,$surname");

?>
<body>
<p> thank you </p>
      </body>
</html>


i also have a database "first_test" with id , first_name and surname as the attributes in the table
people
...i don't know what i'm doing wrong ...any help will be grealty appreciated
thank you

raghu
0
Comment
Question by:Raghunandan44
2 Comments
 
LVL 13

Accepted Solution

by:
lozloz earned 80 total points
ID: 9725189
first you have commented out a lot of the code - that's what // means so for example the mysql_connect isn't being executed. secondly, in order for your php file to be processed, it needs to have the extension .php. also you've made a typo with $HTTP_GET_VARS["fist_name"]. in your form, your method action is "POST", which means that you are sending the information via POST data which is stored in $HTTP_POST_VARS (or $_POST past version 4.1.0). so you need to either change your method to GET in the form or change your variables to $HTTP_POST_VARS FROM $HTTP_GET_VARS. incidentally, if you php version is higher than 4.1.0, it's best to use $_POST, $_GET, $_SESSION etc. instead of $HTTP_POST_VARS, $HTTP_GET_VARS, $HTTP_SESSION_VARS etc. here's how your my.php should look like, you'll need to change the action of the form to point to my.php instead of my.html

<html>
     <head>
          <title>Welcome to Adobe GoLive 4</title>
     </head>
     
<?php
     $username = "root";
     $password = "root";
     $hostname = "localhost";    
     $dbh = mysql_connect($hostname, $username, $password) or

die("Unable to connect to MySQL");
     //print "Connected to MySQL<br>";
     // you're going to do lots more here soon


$id = $HTTP_GET_VARS["id"];
$first_name = $HTTP_GET_VARS["first_name"];
$surname = $HTTP_GET_VARS["surname"] ;
$selected = mysql_select_db("first_test",$dbh) or die("Could not select first_test");
     if (mysql_query("insert into people values('$id','$first_name','$surname')")) {
       print "successfully inserted record";
     }
     else {
        print "Failed to insert record";
     }
mysql_close($dbh);
print(" id: {0},firstname {1},surname{2}",$id,$first_name,$surname");

?>
<body>
<p> thank you </p>
     </body>
</html>

also, you need to change the three variableshere to the appropriate parameters to connect to your database:

     $username = "root";
     $password = "root";
     $hostname = "localhost";

tell me how it goes - hope you're not too confused by that, feel free to ask any more questions

loz
0
 

Author Comment

by:Raghunandan44
ID: 9727190
hai loz...

thanx a lot ..i have been able to do what i wanted ..thanx  a lot once again ..

raghu
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The purpose of this video is to demonstrate how to integrate Mailchimp with Facebook. This will be demonstrated using a Windows 8 PC. Mailchimp and Facebook will be used. Log into your Mailchimp account. : Click on your name. Go to Account Setti…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

876 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question