GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 14-Aug-2008, 09:04
TabbeAIPC TabbeAIPC is offline
New Member
 
Join Date: Aug 2008
Posts: 3
TabbeAIPC is on a distinguished road

My First SQL Database and PHP Form


SQL Gurus I Need help! Below is the code for my php form and my process that is supposed to enter the info in the database.
I keep getting "Couldn't execute query." Can anyone tell me what I've done wrong?

PHP Form:
PHP Code:

<?php
/*  
 *  Description:  Script displays a form that asks for the
 *                customer phone number.
 */
  echo "<html>
        <head><title>Customer Info</title></head>
        <body>";
  $labels = array ( "first_name" => "First Name",
                    "last_name" => "Last Name",
                    "phone" => "Phone");
  echo "<h3>Please enter your phone number below.</h3>";
  echo "<form action='processform.php' method='POST'>
        <table>\n";
  /* Loop that displays the form fields */
  foreach($labels as $field => $label)
  {
    echo "<tr>
           <td style='text-align: right;
                     font-weight: bold'> $label</td>
           <td><input type='text' name='$field' size='65'
                     maxlength='65' ></td>
          </tr>";
  }
  echo "<tr>
       <td colspan='2' style='text-align: center'>
            <input type='submit'
                   value='Submit'>";
  echo "</td></tr></table>
        </form>";
?>
</body></html>



Process to insert into DB:
PHP Code:

<?php
/*  
 *  Description:  Program checks all the form fields for
 *                blank fields and incorrect format. Saves the
 *                correct fields in a database.
 */
?>
<html>
<head><title>Member Phone Number</title></head>
<body>
<?php
  /* set up array of field labels */
  $labels = array( "first_name" => "First Name","last_name" => "Last Name","phone" => "Phone");
/* Check information from form */
  foreach($_POST as $field => $value)
  {
    /* check each field for blank fields */
    if( $value == "" )
    {
      $blank_array[] = $field;
    }
    /* check format of each field */
    elseif( ereg("(name)",$field) )
    {
      if(!ereg("^[A-Za-z' -]{1,50}$",$value) )
      {
          $bad_format[] = $field;
      }
    }
    elseif($field == "phone")
    {
      if(!ereg("^[0-9)( -]{7,20}(([xX]|(ext)|(ex))?[ -]?[0-9]{1,7})?$",$value) )
      {
          $bad_format[] = $field;
      }
    }
  } // end of foreach for $_POST
  /* if any fields were not okay, display error message and form */
  if(@sizeof($blank_array) > 0 or @sizeof($bad_format) > 0)
  {
    if(@sizeof($blank_array) > 0)
    {
        /* display message for missing information */
        echo "<b>You didn't fill in one or more required fields.
                 You must enter:</b><br>";
        /* display list of missing information */
        foreach($blank_array as $value)
        {
           echo "&nbsp;&nbsp;&nbsp;{$labels[$value]}<br>";
        }
    }
    if(@sizeof($bad_format) > 0)
    {
        /* display message for bad information */
        echo "<b>One or more fields have information that appears to
                 be incorrect. Correct the format for:</b><br>";
        /* display list of bad information */
        foreach($bad_format as $value)
        {
           echo "&nbsp;&nbsp;&nbsp;{$labels[$value]}<br>";
        }
    }
    /* redisplay form */
    echo "<p><hr />";
    echo "<h3>Please enter your phone number below.</h3>";
    echo "<form action='processform.php' method='POST'>
          <table>";
    foreach($labels as $field => $label)
    {
      $good_data[$field]=strip_tags(trim($_POST[$field]));
      echo "<tr>
             <td style='text-align: right; font-weight: bold'>
                  $label</td>
             <td><input type='text' name='$field' size='65'
                 maxlength='65' value='$good_data[$field]'></td>
            </tr>";
    }
    echo "<tr>
           <td colspan='2' style='text-align: center'>
              <input type='submit' value='Submit'>";
    echo "</td></tr></table>
          </form>";
    exit();
  }
  else   //if data is okay
  {
    $user="Trey";
    $host="localhost";
    $password="abbe12";
    $database = "PartyData";
    $cxn = mysql_connect($host,$user,$password)
           or die ("couldn't connect to server");
    mysql_select_db($database);
    $fields_all = array_keys($labels);
    foreach($fields_all as $field)
    {
      $good_data[$field] = strip_tags(trim($_POST[$field]));
      if($field == "phone")
      {
         $good_data[$field] = ereg_replace("[)( .-]","",$good_data[$field]);
      }
      $good_data[$field] = mysql_real_escape_string($good_data[$field]);
    }

    $query = "INSERT INTO Phone (Lst_Nm,Frst_Nm,Ph_Num)
              VALUES ('$good_data[last_name]','$good_data[first_name]',
                      '$good_data[phone]')";
    $result = mysql_query($query)
              or die ("Couldn't execute query.");
    echo "<h4>New Member added to database</h4>";
  }
?>
</body></html>
Last edited by admin : 14-Aug-2008 at 18:25. Reason: Please insert your example PHP codes between [PHP] and [/PHP] tags
  #2  
Old 14-Aug-2008, 09:25
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,140
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: My First SQL Database and PHP Form


Add the mysql error to the die message:
PHP Code:

or die("Couldn't execute querey. Reason:" . mysql_error() . "\n"); 


That should provide more info about the query failure. Also check this post for an alternative debug check.
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 14-Aug-2008, 09:31
TabbeAIPC TabbeAIPC is offline
New Member
 
Join Date: Aug 2008
Posts: 3
TabbeAIPC is on a distinguished road

Re: My First SQL Database and PHP Form


Pk I've ran the check and it says no database selected but isn't that what this code is for:
$user="Trey";
$host="localhost";
$password="abbe12";
$database = "PartyData";
$cxn = mysql_connect($host,$user,$password)
or die ("couldn't connect to server");
mysql_select_db($database);
???? Im sorry I'm such a NOOB
  #4  
Old 14-Aug-2008, 09:43
TabbeAIPC TabbeAIPC is offline
New Member
 
Join Date: Aug 2008
Posts: 3
TabbeAIPC is on a distinguished road

Re: My First SQL Database and PHP Form


Ok now I feel like a total idiot. I had the wrong DB name. thanks for your help that solved it!!!!!!!!!!!!!!!
 
 

Recent GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
make a website using Php and SQL oggie Web Design Forum 8 19-Aug-2008 08:57
automatically assigning a number into the database from php markyp MySQL / PHP Forum 2 01-May-2007 10:12
Unlimited Domain Hosting – Reselling with Windows 2003| Linux | CF MX | SQL | ASP.NET JodoHost Web Hosting Advertisements & Offers 0 04-Apr-2007 23:08
Form Validation Issue petenyce405 MySQL / PHP Forum 3 11-Jan-2005 22:35
Parsing PHP code that's stored in a database. JdS MySQL / PHP Forum 3 13-May-2004 10:15

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 07:19.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.