libURLFormData and PHP (and MySQL)

Nicolas Cueto nicolas_cueto at yahoo.com
Sat Sep 3 08:50:24 CDT 2005


Hello List,

I'm trying to pass form variables from a stack to
a MySQL database via a set of PHP pages --
one page contains an HTML form with a PHP script,
the other contains the PHP script that receives
the form variables via an URL request string (?) and
adds them to the database. (And please don't suggest
using Rev's sql commands. My webhost only allows
access to databases via a PHP interface.)

The problem is, I don't see how to use Rev to send
form variables to a PHP script which usually relies
on the info being typed into the input fields of the
HTML form. (The problem, too, is that I've only a
hazy understanding of PHP and HTML forms.)

Hope that was clear... If it helps any, I've also appended
below the outlines of the two PHP pages.

Confused yet grateful,

Nicolas Cueto
niconiko language school


## PHP PAGE FOR ENTERING TYPED DATA ##
<html> <body>
<?php
 // get the id from the URL request
   $id = $_REQUEST['id'];
   if( $id ) {   // connect to the server
      mysql_connect( 'localhost', 'low_test', 'test' )
         or die( "Error! Could not connect to database: " .
mysql_error() );
      // select the database
      mysql_select_db( 'low_test' )
        or die( "Error! Could not select the database: " .
mysql_error() );
      // retrieve the row from the database
      $query = "SELECT * FROM `contacts` WHERE `id`='$id'";
      $result = mysql_query( $query );
      if( $result && $contact = mysql_fetch_object( $result ) )
      { // set our variables
         $lastName = $contact -> lastName;
         $firstName = $contact -> firstName;
         $email = $contact -> email;}}
   // print out the form ?>
<form action="saveitem.php" method="get">
<input type="hidden" name="id" value="<?php echo($id) ?>">

<table>
<tr>
<th align="left">First Name</th>
<td align="left">
<input name="firstName" type="text" value="<?php echo($firstName) ?>"
/>
</td>
</tr>
<snip>
</table>
   <br>   <input type="submit" value="Save Entry">   <br>
</form>
</body>
</html>

### PHP PAGE FOR RECEIVING AND PASSING THE FORM
## DATA TO THE DATABASE (a.k.a. "saveitem.php")
<html>
<body>
<?php // saving script
  // connect to the server
   mysql_connect( 'localhost', 'username', 'password' )
      or die( "Error! Could not connect to database: " .
mysql_error() );
   // select the database
   mysql_select_db( 'database_name' )
      or die( "Error! Could not select the database: " .
mysql_error() );
   // get the variables from the URL request string
   $id = $_REQUEST['id'];
   $firstName = $_REQUEST['firstName'];
   $lastName = $_REQUEST['lastName'];
   $email = $_REQUEST['email'];
   // if $id is not defined, we have a new entry, otherwise update the
old entry
   if( $id )
   { $query = "UPDATE `contacts` SET `firstName`='$firstName',
`lastName`='$lastName',
         `email`='$email' WHERE `id`='$id'"; }
   else
   { $query = "INSERT INTO `contacts` (
`firstName`,`lastName`,`email` )
         VALUES ( '$firstName','$lastName','$email' )"; }
   // save the info to the database
   $results = mysql_query( $query );
   // print out the results
   if( $results )
   { echo( "Successfully saved the entry." ); }
   else
   { die( "Trouble saving information to the database: " .
mysql_error() ); } ?>
</body>
</html>




More information about the use-livecode mailing list