473,396 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Data not going to db

nomad
664 Expert 512MB
Hello everyone:
Once again I need help. I'm trying to make a address book and it's not working. The data is not displaying and is not going to the db. I think it has to do with the id, master_id or the date_added, date_modified fields

Here is my code for the address
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if (($_POST[op] != "add") || ($_GET[master_id] != "")) {
  3.     //haven't seen the form, so show it
  4.     $display_block = "
  5.     <h1>Add an Entry</h1>
  6.     <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">";
  7.  
  8.     if ($_GET[master_id] != "") {
  9.         //connect to database
  10.         $conn = mysql_connect("localhost", "nomad", "nomad") 
  11.         or die(mysql_error());
  12.         mysql_select_db("teamvelo",$conn)  or die(mysql_error());
  13.  
  14.         //get first, last names for display/tests validity
  15.         $get_names = "select concat_ws(' ', f_name, l_name) as 
  16.         display_name from master_name where id = $_GET[master_id]";
  17.         $get_names_res = mysql_query($get_names) or die(mysql_error());
  18.  
  19.         if (mysql_num_rows($get_names_res) == 1) {
  20.             $display_name = mysql_result($get_names_res,0,'display_name');
  21.         }
  22.     }
  23.  
  24.     if ($display_name != "") {
  25.         $display_block .= "<P>Adding information for
  26.                        <strong>$display_name</strong>:</p>";
  27.     } else {
  28.         $display_block .= "
  29.         <P><strong>First/Last Names:</strong><br>
  30.         <input type=\"text\" name=\"f_name\" size=30 maxlength=75>
  31.         <input type=\"text\" name=\"l_name\" size=30 maxlength=75>";
  32.     }
  33.     $display_block .= "<P><strong>Address:</strong><br>
  34.     <input type=\"text\" name=\"address\" size=30>
  35.  
  36.     <P><strong>City/State/Zip:</strong><br>
  37.     <input type=\"text\" name=\"city\" size=30 maxlength=50>
  38.     <input type=\"text\" name=\"state\" size=5 maxlength=2>
  39.     <input type=\"text\" name=\"zipcode\" size=10 maxlength=10>
  40.  
  41.     <P><strong>Telephone Number:</strong><br>
  42.     <input type=\"text\" name=\"tel_number\" size=30 maxlength=25>
  43.  
  44.     <P><strong>Cell Number:</strong><br>
  45.     <input type=\"text\" name=\"cell_number\" size=30 maxlength=25>
  46.  
  47.     <P><strong>Email Address:</strong><br>
  48.     <input type=\"text\" name=\"email\" size=30 maxlength=150>
  49.  
  50.     <P><strong>Emergency Name:</strong><br>
  51.     <input type=\"text\" name=\"contact_name\" size=30 maxlength=75>
  52.     <P><strong>Emergency Phone:</strong><br>
  53.     <input type=\"text\" name=\"contact_phone\" size=25 maxlength=25>
  54.  
  55.     <p><input type=\"submit\" name=\"submit\" value=\"Add Entry\"></p>
  56.     </FORM>";
  57.  
  58. } else if ($_POST[op] == "add") {
  59.     //time to add to tables, so check for required fields
  60.     if ((($_POST[f_name] == "") || ($_POST[l_name] == "")) && ($_POST[master_id] == "")) {
  61.         header("Location: addentry2.php");
  62.         exit;
  63.     }
  64.  
  65.     //connect to database
  66.         $conn = mysql_connect("localhost", "nomad", "nomad") or die(mysql_error());
  67.         mysql_select_db("teamvelo",$conn)  or die(mysql_error());
  68.  
  69.     if ($_POST[master_id] == "") {
  70.         //add to master_name table
  71.         $add_master = "insert into master_name values ('', now(), now(), '$_POST[f_name]', '$_POST[l_name]')";
  72.         mysql_query($add_master) or die(mysql_error());
  73.  
  74.         //get master_id for use with other tables
  75.         $master_id = mysql_insert_id();
  76.     } else {
  77.          $master_id = $_POST[master_id];
  78.     }
  79.  
  80.     if (($_POST[address]) || ($_POST[city]) || ($_POST[state]) || ($_POST[zipcode])) {
  81.         //something relevant, so add to address table
  82.         $add_address = "replace into address values ('', $master_id, now(), now(), '$_POST[address]', '$_POST[city]', '$_POST[state]', '$_POST[zipcode]')";
  83.         mysql_query($add_address) or die(mysql_error());
  84.     }
  85.  
  86.     if ($_POST[tel_number]) {
  87.         //something relevant, so add to telephone table
  88.         $add_tel = "replace into telephone values ('', $master_id, now(), now(), '$_POST[tel_number]')";
  89.         mysql_query($add_tel) or die(mysql_error());
  90.     }
  91.  
  92.     if ($_POST[cell_number]) {
  93.         //something relevant, so add to fax table
  94.         $add_cell = "replace into cell values ('', $master_id, now(), now(), '$_POST[cell_number]')";
  95.         mysql_query($add_cell) or die(mysql_error());
  96.     }
  97.  
  98.     if ($_POST[email]) {
  99.         //something relevant, so add to email table
  100.         $add_email = "replace into email values ('', $master_id, now(), now(), '$_POST[email]')";
  101.         mysql_query($add_email) or die(mysql_error());
  102.     }
  103.  
  104.     if ($_POST[emergency]) {
  105.         //something relevant, so add to notes table
  106.         $add_emergency = "replace into emergency values ('', $master_id, now(), now(), '$_POST[contact_name]', '$_POST[contact_phone]')";
  107.         mysql_query($add_emergency) or die(mysql_error());
  108.     }
  109.  
  110.     $display_block = "<h1>Entry Added</h1>
  111.     <P>Your entry has been added.  Would you like to
  112.     <a href=\"addentry2.php\">add another</a>?</p>";
  113. }
  114. ?>
  115. <HTML>
  116. <HEAD>
  117. <TITLE>Add an Entry</TITLE>
  118. </HEAD>
  119. <BODY>
  120. <?php echo $display_block; ?>
  121. </BODY>
  122. </HTML>
  123.  
  124.  
Here are my tables

Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `teamvelo`.`master_name` (
  2. `id` INT NOT NULL AUTO_INCREMENT ,
  3. `date_added` DATETIME NOT NULL ,
  4. `date_modified` DATETIME NOT NULL ,
  5. `f_name` VARCHAR( 75 ) NOT NULL ,
  6. `l_name` VARCHAR( 75 ) NOT NULL ,
  7. PRIMARY KEY ( `id` )
  8. ) ENGINE = MYISAM ;
  9.  
  10.  
  11. CREATE TABLE `teamvelo`.`address` (
  12. `id` INT NOT NULL AUTO_INCREMENT ,
  13. `user_id` INT( 11 ) NOT NULL ,
  14. `date_added` DATETIME NOT NULL ,
  15. `date_modified` DATETIME NOT NULL ,
  16. `address` VARCHAR( 255 ) NOT NULL ,
  17. `city` VARCHAR( 50 ) NOT NULL ,
  18. `state` CHAR( 2 ) NOT NULL ,
  19. `zipcode` VARCHAR( 10 ) NOT NULL ,
  20. `enum` ENUM( 'home', 'work', 'other' ) NOT NULL ,
  21. PRIMARY KEY ( `id` )
  22.  
  23.  
  24. CREATE TABLE `teamvelo`.`telephone` (
  25. `id` INT NOT NULL AUTO_INCREMENT ,
  26. `master_id` INT( 11 ) NOT NULL ,
  27. `date_added` DATETIME NOT NULL ,
  28. `date_modified` DATETIME NOT NULL ,
  29. `tel_number` VARCHAR( 25 ) NOT NULL ,
  30. `type` ENUM( 'home', 'work', 'other' ) NOT NULL ,
  31. PRIMARY KEY ( `id` )
  32. ) ENGINE = MYISAM ;
  33.  
  34. CREATE TABLE `teamvelo`.`cell` (
  35. `id` INT NOT NULL AUTO_INCREMENT ,
  36. `master_id` INT( 11 ) NOT NULL ,
  37. `date_added` DATETIME NOT NULL ,
  38. `date_modified` DATETIME NOT NULL ,
  39. `cell_number` VARCHAR( 25 ) NOT NULL ,
  40. `type` ENUM( 'home', 'work', 'other' ) NOT NULL ,
  41. PRIMARY KEY ( `id` )
  42. ) ENGINE = MYISAM ;
  43.  
  44. CREATE TABLE `teamvelo`.`emergency` (
  45. `id` INT NOT NULL AUTO_INCREMENT ,
  46. `master_id` INT( 11 ) NOT NULL ,
  47. `date_added` DATETIME NOT NULL ,
  48. `date_modified` DATETIME NOT NULL ,
  49. `contact_name` VARCHAR( 75 ) NOT NULL ,
  50. `contact_phone` VARCHAR( 25 ) NOT NULL ,
  51. `type` ENUM( 'home', 'work', 'other' ) NOT NULL ,
  52. PRIMARY KEY ( `id` )
  53. ) ENGINE = MYISAM ;
Any help or direction would be great.
Thanks
Damon
Jan 21 '10 #1
4 1737
johny10151981
1,059 1GB
Hello
you have entered a big program. I didnt read it. I will suggest you, if connecting and database choosing is success full but db read or write is making trouble. Then
first echo the query to the browser then copy the output and directly try from database client. see if it work other wise if there is a error you will be able to figure it out.

Regards,
Johny
Jan 21 '10 #2
nomad
664 Expert 512MB
no echo return I even deleted all the input fields to just to fields f_name and l_names, I know the info is to going to the db.

damon
Jan 21 '10 #3
Atli
5,058 Expert 4TB
Hey.

On line #58 you check for the value of $_POST[op], but I don't see a op field in your form. Did you forget to add a hidden field there?

Also, a couple of things that you should check out:
  • Your code is wide open to SQL Injection. You should never use raw user input in SQL queries, or you make it easy for malicious users to do all sorts of nasty things to your database. (And yes, they are out there, and they will attack you. Be paranoid, be veeery paranoid!)

  • You should always quote the names of array indexes. That is:
    Expand|Select|Wrap|Line Numbers
    1. // This is INCORRECT. Will cause a notice.
    2. $value = $_POST[value]
    3.  
    4. // It should be:
    5. $value = $_POST['value'];
    6.  
    Associative array elements; elements that have a name, rather than a number; should always be quoted. They are strings, and strings need to be quoted. Otherwise PHP will consider them constants. - PHP does give you a bit of rope here, and converts non-existing constant names into strings when called, but that does not mean it is OK to do so. (It's sloppy! :P)

  • When you check the value of an array element that may not exist - which includes ALL values in the $_POST and $_GET arrays - you should make sure they exist before you check their values. Otherwise you may get an error notice. (Production servers suppress them, but they are still there.)
    Expand|Select|Wrap|Line Numbers
    1. // To check for an empty value do:
    2. if(isset($_POST['elem']) && empty($_POST['elem']));
    3.  
    4. // To check for a specific value do:
    5. if(isset($_POST['elem']) && $_POST['elem'] == 'my value');
    6.  
    The main thing here is to always use the isset function on an unknown array element before using it.

    A somewhat questionable, yet sometimes useful, alternative is to manually suppress the error by prepend the value with @.
    Expand|Select|Wrap|Line Numbers
    1. $value = @$_POST['value'];
    You should go very carefully about using this, and only do so when absolutely needed

  • You should always try to have an else clause on if statements that are crucial to how the page is displayed. In your case, if both the if and else if are false, then nothing is displayed.
    Expand|Select|Wrap|Line Numbers
    1. /** Instead of doing **/
    2. if(!isset($_POST['op']) || empty($_POST['op'])) {
    3.     // Show form
    4. }
    5. else if($_POST['op'] == 'add') {
    6.    // Add stuff
    7. }
    8. else if($_POST['op'] == 'edit') {
    9.    // Edit stuff
    10. }
    11.  
    12. /** It would be better to do **/
    13. if(isset($_POST['op']) && $_POST['op'] == 'add') {
    14.    // Add stuff
    15. }
    16. else if(isset($_POST['op']) && $_POST['op'] == 'edit') {
    17.    // Edit stuff
    18. }
    19. else {
    20.     // Show form
    21. }
Jan 22 '10 #4
nomad
664 Expert 512MB
As always Atli you are a great help

damon
Jan 22 '10 #5

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

Similar topics

10
by: Zap | last post by:
Widespread opinion is that public data members are evil, because if you have to change the way the data is stored in your class you have to break the code accessing it, etc. After reading this...
20
by: sandSpiderX | last post by:
Hi, I have to develop a program to compute range of all data types at run time... meaning say char c; now I have to see how many characters char can represent,, one logic i think is to...
7
by: Neil Ginsberg | last post by:
I'm having some problems with an Access 2000 MDB file with a SQL Server 7 back end, using ODBC linked tables. I previously wrote about this, but am reposting it with some additional information and...
19
by: Johnny Google | last post by:
Here is an example of the type of data from a file I will have: Apple,4322,3435,4653,6543,4652 Banana,6934,5423,6753,6531 Carrot,3454,4534,3434,1111,9120,5453 Cheese,4411,5522,6622,6641 The...
11
by: hoopsho | last post by:
Hi Everyone, I am trying to write a program that does a few things very fast and with efficient use of memory... a) I need to parse a space-delimited file that is really large, upwards fo a...
1
by: Sam Berry | last post by:
I am in need of some guidance on asscessing the same data from multiple forms. I tried using a collection, but everytime that I try to access the collection, I have to use the new word. colStuff...
5
by: George Durzi | last post by:
I have a company form that I need to web-enable. The form has about 50 data items with some of these data items able to have N sub data items. I'm considering ways to store this form in my...
5
by: tshad | last post by:
Is there a way to carry data that I have already read from the datagrid from page to page? I am looking at my Datagrid that I page through and when the user says get the next page, I have to go...
0
by: Hannibal111111 | last post by:
I found this code on a site for doing string encryption/decryption. The string will encrypt fine, but I get this error when I try to decrypt. Any idea why? I posted the code below. The error...
20
by: TC | last post by:
I need an automated procedure to copy data from an Access table to a SQL Server table. Speed is important. What is the recommended technique? I can export the data from Access, copy it via FTP,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.