By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,906 Members | 1,771 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,906 IT Pros & Developers. It's quick & easy.

One form, two different tables that data needs to be entered in

P: 8
Hello Folks, It's your new mysql and php user looking for some more help.

Background: I'm attempting to develop an online SCUBA Diving Log application. My plans are to put it together in pieces while at the same time learning on what I've done on a previous section.

The first part I have decided to start is the dive buddy section. This section will contain all your standard contact information (ie name, address, city, state, zip, phone numbers, email addresses and a photo).

I've broken this down even further to just wanting to enter a first name, last name, address line one and address line two.

The below code 'works' meaning no errors are produced just no data is being populated in the tables. the buddy table record count never increases were the address table does increase but no data is passed, it is just a blank entry.

The below code was butchered from several different examples from books I'm using. Connection to the database works fine BTW.

Thank you for any advise!!


Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
  5. <title>Dive Buddy Entry Page</title>
  6. </head>
  7.  
  8. <body>
  9. <?php require_once('../Connections/scuba.php'); ?>
  10. <?php
  11. $Fname =$POST['Fname'];
  12. $Lname =$POST['Lname'];
  13. $address_1 =$POST['address_1'];
  14. $address_2 =$POST['address_2'];
  15.  
  16. $sql = "INSERT into scubalog.buddy SET
  17.     Fname='$Fname',
  18.     Lname='$Lname'";
  19. $sql = "INSERT into scubalog.address SET
  20.     address_1='$address_1',
  21.     address_2='$address_2'";
  22.  
  23. if  (@mysql_query($sql)) {
  24. echo '<p> New Buddy Added<p>';
  25. } else {
  26. echo '<p>Error adding new buddy<p> ' .
  27. mysql_error() . '<p>';
  28. }
  29. ?>
  30.  
  31. <p><a href="<?php echo $_server['PHP_SELF'];  ?>">Add another buddy</a></p>
  32.  
  33. <FORM ACTION="<?php echo $_server['PHP SELF']; ?>" method="post">
  34.  
  35.     <table align="left">
  36.     <tr valign="baseline">
  37.       <td nowrap align="right">First Name:</td>
  38.       <td><input type="text" name="Fname"  size="32"></td>
  39.     </tr>
  40.     <tr valign="baseline">
  41.       <td nowrap align="right">Last Name:</td>
  42.       <td><input type="text" name="Lname"  size="32"></td>
  43.     </tr>
  44.         <tr valign="baseline">
  45.           <td nowrap align="right">Address_1:</td>
  46.       <td><input type="text" name="address_1"  size="32"></td>
  47.     </tr>
  48.           <td nowrap align="right">Address_2</td>
  49.       <td><input type="text" name="address_2" size="32"></td>
  50.     </tr>
  51.     <tr valign="baseline">
  52.     <tr valign="baseline">
  53.       <td nowrap align="right">&nbsp;</td>
  54.       <td><input type="submit" value="Insert record"></td>
  55.     </tr>
  56.   </table>
  57.   <input type="hidden" name="MM_insert" value="form1">
  58. </form>
  59.  
  60.  
  61.  
  62.  
  63. </body>
  64. </html>
  65.  
Please use code tags when you post to the Forum. Read the Instruction on the Right side pane to your Editor.
Aug 13 '07 #1
Share this Question
Share on Google+
5 Replies


ak1dnar
Expert 100+
P: 1,584
So no Errors ! shall we try this first! please.may be in your php.ini it has disabled.(Error Reporting)
Aug 13 '07 #2

gregerly
Expert 100+
P: 192
Expand|Select|Wrap|Line Numbers
  1. <?php require_once('../Connections/scuba.php'); ?>
  2. <?php
  3. $Fname =$POST['Fname'];
  4. $Lname =$POST['Lname'];
  5. $address_1 =$POST['address_1'];
  6. $address_2 =$POST['address_2'];
  7.  
  8. $sql = "INSERT into scubalog.buddy SET
  9.     Fname='$Fname',
  10.     Lname='$Lname'";
  11. $sql = "INSERT into scubalog.address SET
  12.     address_1='$address_1',
  13.     address_2='$address_2'";
  14.  
  15. if  (@mysql_query($sql)) {
  16. echo '<p> New Buddy Added<p>';
  17. } else {
  18. echo '<p>Error adding new buddy<p> ' .
  19. mysql_error() . '<p>';
  20. }
  21. ?>
  22.  
The first thing that jumps out at me is your $sql. You have two $sql variables being declared, so only the last one is ever going to run. Your basically saying:

$sql equals This query.

then

Just kidding, $sql equals this other query. Your resetting the value of the variable.

Your actual SQL looks funky also. To know for sure I need to know is scubalog the table name or the database name? If it's the table name, your query should be rewritten as below:
Expand|Select|Wrap|Line Numbers
  1. $sql = "INSERT INTO scubalog (fname,lname,address_1,address_2) VALUES ('$fname','$lname','$address1','$address2');
I think that is a start to fixing your problem. I'm also assuming that your include at the top of the page is including a database connection and that works properly.

Good Luck

Greg
Aug 13 '07 #3

P: 8
The first thing that jumps out at me is your $sql. You have two $sql variables being declared, so only the last one is ever going to run. Your basically saying:

$sql equals This query.

then

Just kidding, $sql equals this other query. Your resetting the value of the variable.

Your actual SQL looks funky also. To know for sure I need to know is scubalog the table name or the database name? If it's the table name, your query should be rewritten as below:

$sql = "INSERT INTO scubalog (fname,lname,address_1,address_2) VALUES ('$fname','$lname','$address1','$address2');

I think that is a start to fixing your problem. I'm also assuming that your include at the top of the page is including a database connection and that works properly.

Good Luck

Greg

Hi Greg,

Thanks for the reply.

The Database name is SCUBALOG and the tables are buddy and address.

Thanks for the heads up on the variable name. I'll change one of them to something different unless I can use one variable name to input the 4 values into the two different tables. Look forward to anymore help you can provide to me.
Aug 13 '07 #4

pbmods
Expert 5K+
P: 5,821
Heya, Greg.

Mark's INSERT syntax, while 'funky', actually is valid. Check this out.

P.S. Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.
Aug 13 '07 #5

gregerly
Expert 100+
P: 192
Heya, Greg.

Mark's INSERT syntax, while 'funky', actually is valid. Check this out.

P.S. Please use CODE tags when posting source code. See the REPLY GUIDELINES on the right side of the page next time you post.
Good call pbmods. Ya learn something new everyday! I thought I did use code tags (at least I meant to go back and add code tags after I typed my post...whoops)

Thanks for the update to the post pbmods.

Greg
Aug 14 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.