Solved

php and data base

Posted on 2003-11-11
2
277 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 20 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The purpose of this video is to demonstrate how to Test the speed of a WordPress Website. Site Speed is an important metric of a site’s health. Slow site speed can result in viewers leaving your site quickly and not seeing your content. This…
The purpose of this video is to demonstrate how to integrate Mailchimp with WordPress, by placing a Mailchimp signup form on a WordPress Page or Post. This will be demonstrated using a Windows 8 PC. Mailchimp will be used. Log into your Mailchi…

863 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now