Data not going to db

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nomad
    Recognized Expert Contributor
    • Mar 2007
    • 664

    Data not going to db

    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
    Code:
    <?php
    if (($_POST[op] != "add") || ($_GET[master_id] != "")) {
        //haven't seen the form, so show it
        $display_block = "
        <h1>Add an Entry</h1>
        <form method=\"post\" action=\"$_SERVER[PHP_SELF]\">";
    
        if ($_GET[master_id] != "") {
            //connect to database
            $conn = mysql_connect("localhost", "nomad", "nomad") 
    		or die(mysql_error());
            mysql_select_db("teamvelo",$conn)  or die(mysql_error());
    
            //get first, last names for display/tests validity
            $get_names = "select concat_ws(' ', f_name, l_name) as 
    		display_name from master_name where id = $_GET[master_id]";
            $get_names_res = mysql_query($get_names) or die(mysql_error());
    
            if (mysql_num_rows($get_names_res) == 1) {
                $display_name = mysql_result($get_names_res,0,'display_name');
            }
        }
    
        if ($display_name != "") {
            $display_block .= "<P>Adding information for
                           <strong>$display_name</strong>:</p>";
        } else {
            $display_block .= "
            <P><strong>First/Last Names:</strong><br>
            <input type=\"text\" name=\"f_name\" size=30 maxlength=75>
            <input type=\"text\" name=\"l_name\" size=30 maxlength=75>";
        }
        $display_block .= "<P><strong>Address:</strong><br>
    	<input type=\"text\" name=\"address\" size=30>
    
    	<P><strong>City/State/Zip:</strong><br>
    	<input type=\"text\" name=\"city\" size=30 maxlength=50>
    	<input type=\"text\" name=\"state\" size=5 maxlength=2>
    	<input type=\"text\" name=\"zipcode\" size=10 maxlength=10>
    
    	<P><strong>Telephone Number:</strong><br>
    	<input type=\"text\" name=\"tel_number\" size=30 maxlength=25>
    
    	<P><strong>Cell Number:</strong><br>
    	<input type=\"text\" name=\"cell_number\" size=30 maxlength=25>
    
    	<P><strong>Email Address:</strong><br>
    	<input type=\"text\" name=\"email\" size=30 maxlength=150>
    
    	<P><strong>Emergency Name:</strong><br>
    	<input type=\"text\" name=\"contact_name\" size=30 maxlength=75>
    	<P><strong>Emergency Phone:</strong><br>
    	<input type=\"text\" name=\"contact_phone\" size=25 maxlength=25>
    
    	<p><input type=\"submit\" name=\"submit\" value=\"Add Entry\"></p>
    	</FORM>";
    
    } else if ($_POST[op] == "add") {
        //time to add to tables, so check for required fields
        if ((($_POST[f_name] == "") || ($_POST[l_name] == "")) && ($_POST[master_id] == "")) {
            header("Location: addentry2.php");
            exit;
        }
    
        //connect to database
            $conn = mysql_connect("localhost", "nomad", "nomad") or die(mysql_error());
            mysql_select_db("teamvelo",$conn)  or die(mysql_error());
    
        if ($_POST[master_id] == "") {
            //add to master_name table
            $add_master = "insert into master_name values ('', now(), now(), '$_POST[f_name]', '$_POST[l_name]')";
            mysql_query($add_master) or die(mysql_error());
    
            //get master_id for use with other tables
            $master_id = mysql_insert_id();
        } else {
             $master_id = $_POST[master_id];
        }
    
    	if (($_POST[address]) || ($_POST[city]) || ($_POST[state]) || ($_POST[zipcode])) {
    		//something relevant, so add to address table
    		$add_address = "replace into address values ('', $master_id, now(), now(), '$_POST[address]', '$_POST[city]', '$_POST[state]', '$_POST[zipcode]')";
    		mysql_query($add_address) or die(mysql_error());
    	}
    
    	if ($_POST[tel_number]) {
    		//something relevant, so add to telephone table
    		$add_tel = "replace into telephone values ('', $master_id, now(), now(), '$_POST[tel_number]')";
    		mysql_query($add_tel) or die(mysql_error());
    	}
    
    	if ($_POST[cell_number]) {
    		//something relevant, so add to fax table
    		$add_cell = "replace into cell values ('', $master_id, now(), now(), '$_POST[cell_number]')";
    		mysql_query($add_cell) or die(mysql_error());
    	}
    
    	if ($_POST[email]) {
    		//something relevant, so add to email table
    		$add_email = "replace into email values ('', $master_id, now(), now(), '$_POST[email]')";
    		mysql_query($add_email) or die(mysql_error());
    	}
    
    	if ($_POST[emergency]) {
    		//something relevant, so add to notes table
    		$add_emergency = "replace into emergency values ('', $master_id, now(), now(), '$_POST[contact_name]', '$_POST[contact_phone]')";
    		mysql_query($add_emergency) or die(mysql_error());
    	}
    
    	$display_block = "<h1>Entry Added</h1>
    	<P>Your entry has been added.  Would you like to
    	<a href=\"addentry2.php\">add another</a>?</p>";
    }
    ?>
    <HTML>
    <HEAD>
    <TITLE>Add an Entry</TITLE>
    </HEAD>
    <BODY>
    <?php echo $display_block; ?>
    </BODY>
    </HTML>
    Here are my tables

    Code:
    CREATE TABLE `teamvelo`.`master_name` (
    `id` INT NOT NULL AUTO_INCREMENT ,
    `date_added` DATETIME NOT NULL ,
    `date_modified` DATETIME NOT NULL ,
    `f_name` VARCHAR( 75 ) NOT NULL ,
    `l_name` VARCHAR( 75 ) NOT NULL ,
    PRIMARY KEY ( `id` )
    ) ENGINE = MYISAM ;
    
    
    CREATE TABLE `teamvelo`.`address` (
    `id` INT NOT NULL AUTO_INCREMENT ,
    `user_id` INT( 11 ) NOT NULL ,
    `date_added` DATETIME NOT NULL ,
    `date_modified` DATETIME NOT NULL ,
    `address` VARCHAR( 255 ) NOT NULL ,
    `city` VARCHAR( 50 ) NOT NULL ,
    `state` CHAR( 2 ) NOT NULL ,
    `zipcode` VARCHAR( 10 ) NOT NULL ,
    `enum` ENUM( 'home', 'work', 'other' ) NOT NULL ,
    PRIMARY KEY ( `id` )
    
    
    CREATE TABLE `teamvelo`.`telephone` (
    `id` INT NOT NULL AUTO_INCREMENT ,
    `master_id` INT( 11 ) NOT NULL ,
    `date_added` DATETIME NOT NULL ,
    `date_modified` DATETIME NOT NULL ,
    `tel_number` VARCHAR( 25 ) NOT NULL ,
    `type` ENUM( 'home', 'work', 'other' ) NOT NULL ,
    PRIMARY KEY ( `id` )
    ) ENGINE = MYISAM ;
    
    CREATE TABLE `teamvelo`.`cell` (
    `id` INT NOT NULL AUTO_INCREMENT ,
    `master_id` INT( 11 ) NOT NULL ,
    `date_added` DATETIME NOT NULL ,
    `date_modified` DATETIME NOT NULL ,
    `cell_number` VARCHAR( 25 ) NOT NULL ,
    `type` ENUM( 'home', 'work', 'other' ) NOT NULL ,
    PRIMARY KEY ( `id` )
    ) ENGINE = MYISAM ;
    
    CREATE TABLE `teamvelo`.`emergency` (
    `id` INT NOT NULL AUTO_INCREMENT ,
    `master_id` INT( 11 ) NOT NULL ,
    `date_added` DATETIME NOT NULL ,
    `date_modified` DATETIME NOT NULL ,
    `contact_name` VARCHAR( 75 ) NOT NULL ,
    `contact_phone` VARCHAR( 25 ) NOT NULL ,
    `type` ENUM( 'home', 'work', 'other' ) NOT NULL ,
    PRIMARY KEY ( `id` )
    ) ENGINE = MYISAM ;
    Any help or direction would be great.
    Thanks
    Damon
    Last edited by Atli; Jan 22 '10, 07:14 AM. Reason: Fixed the [code] tags.
  • johny10151981
    Top Contributor
    • Jan 2010
    • 1059

    #2
    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

    Comment

    • nomad
      Recognized Expert Contributor
      • Mar 2007
      • 664

      #3
      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

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        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:
          [code=php]
          // This is INCORRECT. Will cause a notice.
          $value = $_POST[value]

          // It should be:
          $value = $_POST['value'];
          [/code]
          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.)
          [code=php]
          // To check for an empty value do:
          if(isset($_POST['elem']) && empty($_POST['elem']));

          // To check for a specific value do:
          if(isset($_POST['elem']) && $_POST['elem'] == 'my value');
          [/code]
          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 @.
          [code=php]$value = @$_POST['value'];[/code]
          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.
          [code=php]
          /** Instead of doing **/
          if(!isset($_POS T['op']) || empty($_POST['op'])) {
          // Show form
          }
          else if($_POST['op'] == 'add') {
          // Add stuff
          }
          else if($_POST['op'] == 'edit') {
          // Edit stuff
          }

          /** It would be better to do **/
          if(isset($_POST['op']) && $_POST['op'] == 'add') {
          // Add stuff
          }
          else if(isset($_POST['op']) && $_POST['op'] == 'edit') {
          // Edit stuff
          }
          else {
          // Show form
          }[/code]

        Comment

        • nomad
          Recognized Expert Contributor
          • Mar 2007
          • 664

          #5
          As always Atli you are a great help

          damon

          Comment

          Working...