Jump to content

data is to writing to the db


nomadrw

Recommended Posts

Hello Everyone:

 

I'm trying to figure out why I not getting a display for my code and why the info is not going to the db.

This code is based on a Book called "PHP, MYSQL and Apache" By C Meloni.

I modified a little. The db and user has full grant privileges.

 

<?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 is my db and tables

 

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` MEDIUMINT( 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` MEDIUMINT( 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` MEDIUMINT( 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 ;

 

 

Any help would be great.

Damon

PS I trying to learn PHP

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.