Hi, I recently started learning PHP and have been reading a tutorial about posting to databases and retrieving data and displaying data from databases. I copied the code from the tutorial EXACTLY as it is (except for filenames and passwords etc.). The code is for a simple contact list. You enter data into form, it sends it to a page to be processed, then you look on another page and it displays the results.

However, the code does not seem to be submitting any data to the database. The correct number of entries are displayed, but the fields are left blank. I will post all of the code...

Creating The Table (test1.php):

<html>
<head>
</head>
<body>
<?
$user="fbacall";
$password="*********";
$database="fbacall_uk_db";
mysql_connect(localhost,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="CREATE TABLE contacts (id int(6) NOT NULL auto_increment,first varchar(15) NOT NULL,last varchar(15) NOT NULL,phone varchar(20) NOT NULL,mobile varchar(20) NOT NULL,fax varchar(20) NOT NULL,email varchar(30) NOT NULL,web varchar(30) NOT NULL,PRIMARY KEY (id),UNIQUE id (id),KEY id_2 (id))";
mysql_query($query);
mysql_close();
?>
Database Created
</body>
</html>

The Form (test2.php):

<html>
<head>
</head>
<body>
<form action="test3.php" method="get">
First Name: <input type="text" name="first"><br>
Last Name: <input type="text" name="last"><br>
Phone: <input type="text" name="phone"><br>
Mobile: <input type="text" name="mobile"><br>
Fax: <input type="text" name="fax"><br>
E-mail: <input type="text" name="email"><br>
Web: <input type="text" name="web"><br>
<input type="Submit">
</form>
</body>
</html>

The "processing page" (test3.php):

<html>
<head>
</head>
<body>
<?
$username="fbacall";
$password="********";
$database="fbacall_uk_db";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "INSERT INTO contacts VALUES ('','$first','$last','$phone','$mobile','$fax','$email','$web')";
mysql_query($query);

mysql_close();
?>
Data inserted
</body>
</html>

Displaying the Results (test4.php):

<html>
<head>
</head>
<body>
<?
$user="fbacall";
$password="********";
$database="fbacall_uk_db";
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM contacts";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {

$first=mysql_result($result,$i,"first");
$last=mysql_result($result,$i,"last");
$phone=mysql_result($result,$i,"phone");
$mobile=mysql_result($result,$i,"mobile");
$fax=mysql_result($result,$i,"fax");
$email=mysql_result($result,$i,"email");
$web=mysql_result($result,$i,"web");

echo "<b>$first $last</b><br>Phone: $phone<br>Mobile: $mobile<br>Fax: $fax<br>E-mail: $email<br>Web: $web<br><hr><br>";

++$i;
}

?>
</form>
</body>
</html>

Any help would be much appreciated πŸ™‚

-Fuzz

PS. I am hosting my PHP on lycos (yes it does support php and mysql)

    mysql_connect(localhost,$username,$password);

    localhost may be shld have $locahost,$username,$paswd

    declare these first;
    localhoat= '$localhost';
    etc
    etc

    then connect stuff ant where below. i tinkπŸ˜‰

      An alternative would be:

      $user="fbacall"; 
      $password="*********"; 
      $database="fbacall_uk_db";
      $Host = "localhost";
      
      if(!($link_id = mysql_connect($Host, $user, $password))) 
      {
      die(mysql_error());
      }
      mysql_select_db($database);
      

      Note the exclamation mark at the start of the if loop.
      What this loop is basicaly saying is: "Connect to the host defined in the $Host variable using the username defined in the $user variable and the password defined in the $password variable. If for any reason, you cannot connect, tell me why. If there are no problems ( the contents of the loop never gets executed), select the database defined in the $database variable.

      The exclamation mark means NOT. So instead of the contents of the loop executing when it is true, the contents execute when the result is false (there's a problem with the connection). die(mysql_error()); is the function that will give you details of the problem.

      Hope that helps πŸ™‚

        Originally posted by Calahan
        An alternative would be:

        $user="fbacall"; 
        $password="*********"; 
        $database="fbacall_uk_db";
        $Host = "localhost";
        
        if(!($link_id = mysql_connect($Host, $user, $password))) 
        {
        die(mysql_error());
        }
        mysql_select_db($database);
        

        Note the exclamation mark at the start of the if loop.
        What this loop is basicaly saying is: "Connect to the host defined in the $Host variable using the username defined in the $user variable and the password defined in the $password variable. If for any reason, you cannot connect, tell me why. If there are no problems ( the contents of the loop never gets executed), select the database defined in the $database variable.

        The exclamation mark means NOT. So instead of the contents of the loop executing when it is true, the contents execute when the result is false (there's a problem with the connection). die(mysql_error()); is the function that will give you details of the problem.

        Hope that helps πŸ™‚ [/B]

        and the propper less intensive solution to the above...

        $link_id = mysql_connect($Host, $user, $password) or die(mysql_error());
        mysql_select_db($database);
        

        a whopping 2 lines of code and no if check to slow down processing πŸ™‚

          Thank you all, it worked πŸ˜ƒ πŸ˜ƒ πŸ˜ƒ
          Now I'm off to create my first php based website.. bye! πŸ™‚

            dont forget to use the "resolved" link below before you go!

              Write a Reply...