PHP/MySQL Input Form

  • tommya
  • Graduate
  • Graduate
  • tommya
  • Posts: 221
  • Loc: United Kingdom

Post 3+ Months Ago

Hi all,

How can I amend the code below so that after returning an error message after form input, it doesnt just return the error message but also goes back to the input form

For example, if I input something that is already in the MySQL database, and it recognises this, it then displays a duplication error message, telling me it already exists. But then the variables are not cleared so if I refresh the page, it just reloads the error message.
I've tried using unset() in various places to counteract this but this is wrong, I know, can anybody advise

Cheers
Tommy

-------------------------------------------------

Code: [ Select ]

<?php

if ( isset( $contact ) && isset( $abdial ) )

    {
    
    //check user input here!

    $dberror = "";
    $ret = add_to_database( $contact, $abdial, $dberror );

    if ( ! $ret )
        print "Error: $dberror<br>";        
    
    else
        print "You have successfully added the contact: $contact to the database, with an abdial number of: $abdial";
    
    }

else    
    
    {

    write_form();

    }


function add_to_database( $contact, $abdial, &$dberror )

    {

    $user = "administrator";
    $pass = "password";
    $db = "PhoneDirectory";
    $link = mysql_connect("localhost",$user,$pass);

    if ( ! $link )
    
        {

        $dberror = "Couldnt connect to MySQL server";
        return false;

        }

    if ( ! mysql_select_db( $db, $link ) )

        {

        $dberror = mysql_error();
        return false;

        }

$query = "INSERT INTO Abdials ( contactName, abdialNumber )
    values( '$contact','$abdial' )";

    if ( ! mysql_query( $query, $link ) )

        {

        $dberror = mysql_error();
        return false;

        }

    return true;

    }


function write_form()

    {
    
    global $PHP_SELF;
    print "<form action=\"$PHP_SELF\" method=\"POST\">\n";
    print "<input type=\"text\" name=\"contact\"> ";
    print "Contact Name to be added<p>\n";
    print "<input type=\"text\" name=\"abdial\"> ";
    print "Abdial Number<p>\n";
    print "<input type=\"submit\" value=\"Add To Database\">\n</form>\n";

    }

?>
  1. <?php
  2. if ( isset( $contact ) && isset( $abdial ) )
  3.     {
  4.     
  5.     //check user input here!
  6.     $dberror = "";
  7.     $ret = add_to_database( $contact, $abdial, $dberror );
  8.     if ( ! $ret )
  9.         print "Error: $dberror<br>";        
  10.     
  11.     else
  12.         print "You have successfully added the contact: $contact to the database, with an abdial number of: $abdial";
  13.     
  14.     }
  15. else    
  16.     
  17.     {
  18.     write_form();
  19.     }
  20. function add_to_database( $contact, $abdial, &$dberror )
  21.     {
  22.     $user = "administrator";
  23.     $pass = "password";
  24.     $db = "PhoneDirectory";
  25.     $link = mysql_connect("localhost",$user,$pass);
  26.     if ( ! $link )
  27.     
  28.         {
  29.         $dberror = "Couldnt connect to MySQL server";
  30.         return false;
  31.         }
  32.     if ( ! mysql_select_db( $db, $link ) )
  33.         {
  34.         $dberror = mysql_error();
  35.         return false;
  36.         }
  37. $query = "INSERT INTO Abdials ( contactName, abdialNumber )
  38.     values( '$contact','$abdial' )";
  39.     if ( ! mysql_query( $query, $link ) )
  40.         {
  41.         $dberror = mysql_error();
  42.         return false;
  43.         }
  44.     return true;
  45.     }
  46. function write_form()
  47.     {
  48.     
  49.     global $PHP_SELF;
  50.     print "<form action=\"$PHP_SELF\" method=\"POST\">\n";
  51.     print "<input type=\"text\" name=\"contact\"> ";
  52.     print "Contact Name to be added<p>\n";
  53.     print "<input type=\"text\" name=\"abdial\"> ";
  54.     print "Abdial Number<p>\n";
  55.     print "<input type=\"submit\" value=\"Add To Database\">\n</form>\n";
  56.     }
  57. ?>
[/code]
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Okay the solution was pretty simple, you just needed to call your write_form function

PHP Code: [ Select ]
 
<?php
 
 
 
if ( isset( $contact ) && isset( $abdial ) ){
 
   
 
   //check user input here!
 
 
 
   $dberror = "";
 
   $ret = add_to_database( $contact, $abdial, $dberror );
 
 
 
   if ( ! $ret ){
 
      print "Error: $dberror<br>";
 
   }else{
 
      print "You have successfully added the contact: $contact to the database, with an abdial number of: $abdial";
 
   write_form(); // call the write_form function to display the info below the error
 
   }
 
}else{
 
   write_form();
 
}
 
 
 
function add_to_database( $contact, $abdial, &$dberror ){
 
   $user = "administrator";
 
   $pass = "password";
 
   $db = "PhoneDirectory";
 
   $link = mysql_connect("localhost",$user,$pass);
 
   if ( ! $link ){
 
      $dberror = "Couldnt connect to MySQL server";
 
      return false;
 
   }
 
   if ( ! mysql_select_db( $db, $link ) ) {
 
      $dberror = mysql_error();
 
      return false;
 
   }
 
 
 
$query = "INSERT INTO Abdials ( contactName, abdialNumber ) values( '$contact','$abdial' )";
 
   if ( ! mysql_query( $query, $link ) ){
 
      $dberror = mysql_error();
 
      return false;
 
   }
 
   return true;
 
}
 
 
 
function write_form(){
 
   global $PHP_SELF;
 
   print "<form action=\"$PHP_SELF\" method=\"POST\">\n";
 
   print "<input type=\"text\" name=\"contact\"> ";
 
   print "Contact Name to be added<p>\n";
 
   print "<input type=\"text\" name=\"abdial\"> ";
 
   print "Abdial Number<p>\n";
 
   print "<input type=\"submit\" value=\"Add To Database\">\n</form>\n";
 
}
 
 
 
?>
 
 
  1.  
  2. <?php
  3.  
  4.  
  5.  
  6. if ( isset( $contact ) && isset( $abdial ) ){
  7.  
  8.    
  9.  
  10.    //check user input here!
  11.  
  12.  
  13.  
  14.    $dberror = "";
  15.  
  16.    $ret = add_to_database( $contact, $abdial, $dberror );
  17.  
  18.  
  19.  
  20.    if ( ! $ret ){
  21.  
  22.       print "Error: $dberror<br>";
  23.  
  24.    }else{
  25.  
  26.       print "You have successfully added the contact: $contact to the database, with an abdial number of: $abdial";
  27.  
  28.    write_form(); // call the write_form function to display the info below the error
  29.  
  30.    }
  31.  
  32. }else{
  33.  
  34.    write_form();
  35.  
  36. }
  37.  
  38.  
  39.  
  40. function add_to_database( $contact, $abdial, &$dberror ){
  41.  
  42.    $user = "administrator";
  43.  
  44.    $pass = "password";
  45.  
  46.    $db = "PhoneDirectory";
  47.  
  48.    $link = mysql_connect("localhost",$user,$pass);
  49.  
  50.    if ( ! $link ){
  51.  
  52.       $dberror = "Couldnt connect to MySQL server";
  53.  
  54.       return false;
  55.  
  56.    }
  57.  
  58.    if ( ! mysql_select_db( $db, $link ) ) {
  59.  
  60.       $dberror = mysql_error();
  61.  
  62.       return false;
  63.  
  64.    }
  65.  
  66.  
  67.  
  68. $query = "INSERT INTO Abdials ( contactName, abdialNumber ) values( '$contact','$abdial' )";
  69.  
  70.    if ( ! mysql_query( $query, $link ) ){
  71.  
  72.       $dberror = mysql_error();
  73.  
  74.       return false;
  75.  
  76.    }
  77.  
  78.    return true;
  79.  
  80. }
  81.  
  82.  
  83.  
  84. function write_form(){
  85.  
  86.    global $PHP_SELF;
  87.  
  88.    print "<form action=\"$PHP_SELF\" method=\"POST\">\n";
  89.  
  90.    print "<input type=\"text\" name=\"contact\"> ";
  91.  
  92.    print "Contact Name to be added<p>\n";
  93.  
  94.    print "<input type=\"text\" name=\"abdial\"> ";
  95.  
  96.    print "Abdial Number<p>\n";
  97.  
  98.    print "<input type=\"submit\" value=\"Add To Database\">\n</form>\n";
  99.  
  100. }
  101.  
  102.  
  103.  
  104. ?>
  105.  
  106.  


Okay the reason you are getting errors when you refresh is because the information cannot be unset.

It has to do with the browser resubmitting the information, not php resubmitting the info
//Please if I am wrong correct me!

Cool hope it helps
  • tommya
  • Graduate
  • Graduate
  • tommya
  • Posts: 221
  • Loc: United Kingdom

Post 3+ Months Ago

Cheers Dog, worked a treat

I knew it was a matter of calling the form, but I'd been putting it in the wrong place. I assumed that because it was displaying an error message and falling over at the first part of the IF statement, that it was then trying to exit that statement. So I was putting the write_form(); call underneath the print error line, rather than where u suggested

It just never occured to me to put it there

Probably basic I know but hey, I only started learning it recently, its left my head in bits!! :shock:

Cheers again mate, you may here from me again

Tommy
  • Rabid Dog
  • Web Master
  • Web Master
  • User avatar
  • Posts: 3245
  • Loc: South Africa

Post 3+ Months Ago

Glad I could help.

Post Information

  • Total Posts in this topic: 4 posts
  • Users browsing this forum: No registered users and 77 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.