I am *very* new to PHP & am trying to write a tool where a user can add books to a 
library MySQL database. 

The user fills out an HTML form. I do some form validation & if the form entries are 
ok, I need to run some SQL queries using the form variables.

I have the HTML form posting back to itself to do the error checking. The part I am 
unclear on is how to handle the SQL queries. 

I was thinking that if the user filled out the form correctly, I could redirect to a 
new php script/page to handle the SQL queries. The problem is that if I do so, I do 
not have access to the form variables that I need. The only way I know to pass 
variables to a new script is through a form posting & it doesn't make sense to have 2 
form postings for one form.

I suppose I could run the SQL queries from the same script although when I use my 
below logic, the SQL queries print out underneath the HTML form which looks pretty bad.

I was thinking that if I approach it this way, I need some way to loop back through 
all my "if" statements so that I could catch the last one only ($REQUEST_METHOD == 
"post" && $validated == "yes")
and therefore wouldn't get the HTML form again. I tried putting a while(true){ } loop 
around the entire thing which resulted in printing my HTML table infinity times :)

I included a pseudocode form of my logic here & my actual code below.

If anyone has any thoughts, suggestions, ideas, or comments, I'd really appreciate it! 
Also, is there a good IRC channel for php users?

Laura
[EMAIL PROTECTED]
Instant Messenger: lefindley

if ($REQUEST_METHOD != "POST"){

   include("./form.html");

} else if ($REQUEST_METHOD == "POST" && $validated == "no"){

   perform error checking
   display errors at top of page
   include("./form.html");

} else if ($REQUEST_METHOD == "POST" && $validated
== "yes") {

**here is where I need to run the SQL queries**
 
}

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

<?

$err = "";
$validated = "no";

// display form for user to fill out

if ($REQUEST_METHOD != "POST") {

   include("./form.html");


} else if ($REQUEST_METHOD == "POST" && $validated == "no") {

// if user is submitting the form, do error 
// checking. if there are errors, display them at
// the beginning of the form

   if ($book_title == ""){
      $err .= "<LI><font color="red">Book title cannot be blank!</font><br>";
   }
   if ($author == "") {
      $err .= "<LI><font color="red">Author cannot be left blank!</font><br>";   }
   if ($author != ""){
   if (!ereg('[a-zA-Z]', $author)){
        $err .= "<LI><font color="red">Author name must be " .
                                      "letters!</font><br>";
      }
   }
   if ($price == ""){
      $err .= "<LI><font color="red">Price cannot be left blank!</font><br>";
   }
   if ($price != ""){
      if (!is_numeric($price)){
         $err .=  "<LI><font color="red">Price must be numbers!</font><br>";
      }
   }
   if ($isbn == ""){
      $err .= "<LI><font color="red">ISBN cannot be left blank!</font><br>";
   }
   if ($isbn != ""){
      if (!is_numeric($isbn)){
         $err .= "<LI><font color="red">ISBN must be numbers!</font><br>";

      }
   }
   if ($num_copies != ""){
      if (!is_numeric($num_copies)){
         $err .= "<LI><font color="red"># of copies must be ".
                                        " numbers!</font><br>";
      }
   }
   if ($checked_out != ""){
      if (!is_numeric($checked_out)){
         $err .= "<LI><font color="red"># of checked out copies must be " .

  if ($checked_out != ""){
      if (!is_numeric($checked_out)){
         $err .= "<LI><font color="red"># of checked out copies must be " .
                                          " numbers!</font><br>";
      }
   }
   if (is_numeric($checked_out) && is_numeric($num_copies)){
      if ($checked_out > $num_copies){
         $err .= "<LI><font color="red"># of copies checked out cannot " .
                 "exceed number of copies in library!</font><br>";
      }
   }
   include("./form.html");

   if ($err == ""){
      $validated = "yes";
      break;
    }

    print "Validated: $validated";
  }  // end of else if


if ($REQUEST_METHOD == "POST" && $validated == "yes"){

// if user has correctly filled out the form, I 
// need to run some MySQL queries using the form // variables - not sure if it is best 
to do this // on the same page or redirect to another.

// if i run the SQL queries from the same page,
// i need to be able to not display the HTML form

// if i redirect to another script, I need a way
// to pass the form variables

      print "do some SQL stuff";
   }




?>








-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to