Whats wrong with this insert records php codes
Posted by: black85 (---.ipt.aol.com)
Date: July 14, 2006 06:42PM

Hi people,
I'm trying to insert record into database table, but to no avail. addentry.php is supposed to allow me to enter information on a html form, but unfortunately no form is displayed on the browser. Also, the message 'entry added' should be displayed on the browser as well after values have been inserted into form fields. Can anyone help unravel the problems with these scripts below:

addentry.php - insert record script
------------------------------------------------

<?php
if ($_POST[op] != "add"winking smiley {
//haven't seen the form, so show it
$display_block = "<h1>Add an Entry</h1>
<form method=\"post\" action=\"$_SERVER[PHP_SELF]\">
<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>

<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>Address Type:</strong><br>
<input type=\"radio\" name=\"add_type\" value=\"home\" checked> home
<input type=\"radio\" name=\"add_type\" value=\"work\"> work
<input type=\"radio\" name=\"add_type\" value=\"other\"> other

<P><strong>Telephone Number:</strong><br>
<input type=\"text\" name=\"tel_number\" size=30 maxlength=25>
<input type=\"radio\" name=\"tel_type\" value=\"home\" checked> home
<input type=\"radio\" name=\"tel_type\" value=\"work\"> work
<input type=\"radio\" name=\"tel_type\" value=\"other\"> other

<P><strong>Fax Number:</strong><br>
<input type=\"text\" name=\"fax_number\" size=30 maxlength=25>
<input type=\"radio\" name=\"fax_type\" value=\"home\" checked> home
<input type=\"radio\" name=\"fax_type\" value=\"work\"> work
<input type=\"radio\" name=\"fax_type\" value=\"other\"> other

<P><strong>Email Address:</strong><br>
<input type=\"text\" name=\"email\" size=30 maxlength=150>
<input type=\"radio\" name=\"email_type\" value=\"home\" checked> home
<input type=\"radio\" name=\"email_type\" value=\"work\"> work
<input type=\"radio\" name=\"email_type\" value=\"other\"> other

<P><strong>Personal Note:</strong><br>
<textarea name=\"note\" cols=35 rows=5 wrap=virtual></textarea>
<input type=\"hidden\" name=\"op\" value=\"add\">

<p><input type=\"submit\" name=\"submit\" value=\"Add Entry\"></p>
</FORM>";

} else if ($_POST[op] == "add"winking smiley {
//time to add to tables, so check for required fields
if (($_POST[f_name] == ""winking smiley || ($_POST[l_name] == ""winking smiley) {
header("Location: addentry.php"winking smiley;
exit;
}

//connect to database
$conn = mysql_connect("localhost", "root", "olu1bal"winking smiley or die(mysql_error());
mysql_select_db("testDB",$conn) or die(mysql_error());

//add to master_name table
$add_master = "insert into master_name values (null, 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();
if (($_POST[address]) || ($_POST[city]) || ($_POST[state]) || ($_POST[zipcode])) {
//something relevant, so add to address table
$add_address = "insert into address values (null, $master_id, now(), now(), '$_POST[address]', '$_POST[city]', '$_POST[state]', '$_POST[zipcode]', '$_POST[add_type]')";
mysql_query($add_address) or die(mysql_error());
}

if ($_POST[tel_number]) {
//something relevant, so add to telephone table
$add_tel = "insert into telephone values (null, $master_id, now(), now(), '$_POST[tel_number]', '$_POST[tel_type]')";
mysql_query($add_tel) or die(mysql_error());
}

if ($_POST[fax_number]) {
//something relevant, so add to fax table
$add_fax = "insert into fax values (null, $master_id, now(), now(), '$_POST[fax_number]', '$_POST[fax_type]')";
mysql_query($add_fax) or die(mysql_error());
}

if ($_POST) { //something relevant, so add to email table $add_email = &quot;insert into email values (null, $master_id, now(), now(), '$_POST

Options: ReplyQuote
Re: Whats wrong with this insert records php codes
Posted by: CyberSpatium (67.170.181.---)
Date: July 15, 2006 12:00PM

-----------------------------------------------------------------

<?php
if ($_POST['op'] != "add"winking smiley {
//haven't seen the form, so show it
$display_block = " ?>

<h1>Add an Entry<h1>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
<P><strong>FirstLast Names:<strong><br>
<input type="text" name="f_name" size=30 maxlength=75>
<input type="text" name="l_name" size=30 maxlength=75>
<P><strong>Address:<strong><br>
<input type="text" name="address" size=30>
<P><strong>CityStateZip:<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>Address Type:<strong><br>
<input type="radio" name="add_type" value="home" checked> home
<input type="radio" name="add_type" value="work"> work
<input type="radio" name="add_type" value="other"> other
<P><strong>Telephone Number:<strong><br>
<input type="text" name="tel_number" size=30 maxlength=25>
<input type="radio" name="tel_type" value="home" checked> home
<input type="radio" name="tel_type" value="work"> work
<input type="radio" name="tel_type" value="other"> other
<P><strong>Fax Number:<strong><br>
<input type="text" name="fax_number" size=30 maxlength=25>
<input type="radio" name="fax_type" value="home" checked> home
<input type="radio" name="fax_type" value="work"> work
<input type="radio" name="fax_type" value="other"> other
<P><strong>Email Address:<strong><br>
<input type="text" name="email" size=30 maxlength=150>
<input type="radio" name="email_type" value="home" checked> home
<input type="radio" name="email_type" value="work"> work
<input type="radio" name="email_type" value="other"> other
<P><strong>Personal Note:<strong><br>
<textarea name="note" cols=35 rows=5 wrap=virtual><textarea>
<input type="hidden" name="op" value="add">
<p><input type="submit" name="submit" value="Add Entry"><p>
<FORM>

<?php ";


} else if ($_POST['op'] == "add"winking smiley {
//time to add to tables, so check for required fields
if ((is_null($_POST['f_name'])) || (is_null($_POST['l_name']))) {
header("Location: addentry.php"winking smiley;
exit;
}


//connect to database
$conn = mysql_connect("localhost", "root", "olu1bal"winking smiley or die(mysql_error());
mysql_select_db("testDB",$conn) or die(mysql_error());


//add to master_name table
############################
## you are going to run into problems here, you sql syntax invaid.
## Also, for the id column, you are trying to insert the value of
## null. that will not work becauswe when you created that table,
## you set it to not null. you need to insert a validinteger
## instead of null in this query
##
## your query:
## "insert into master_name values (null, now(), now(), '$_POST['f_name']', '$_POST['l_name']')";
##
############################
$add_master = "INSERT INTO master_name (id, date_added, date_modified, f_name, l_name) VALUES (, now(), now(), " . $_POST['f_name'] . ", " . $_POST['l_name'] . "winking smiley";
mysql_query($add_master) or die(mysql_error());


//get master_id for use with other tables
############################
## you are trying to use mysql_insert_id() here. it will cause
## an error in your script, because you are setting all your
## database tables id column values to null. all of your
## invaid querys I changing for you will fix that problem
## and not cause an error
############################
$master_id = mysql_insert_id();


if (($_POST['address']) || ($_POST['city']) || ($_POST['state']) || ($_POST['zipcode'])) {

//something relevant, so add to address table

############################
## once again your sql is invalid and you are again trying to set
## the column id to null. change the column id to a valid integer
##
## your query:
## "insert into address values (null, $master_id, now(), now(), '$_POST['address']', '$_POST['city']', '$_POST['state']', '$_POST['zipcode']', '$_POST['add_type']')";
##
############################
$add_address = "INSERT INTO address (id, master_id, date_added, date_modified, address, city, state, zipcode, type) VALUES (_CHANGE THIS TO A VALID INTEGER_, " . $master_id . ", now(), now(), " . $_POST['address'] . ", " . $_POST['city'] . ", " . $_POST['state'] . ", " . $_POST['zipcode'] . ", " . $_POST['add_type'] . "winking smiley";
mysql_query($add_address) or die(mysql_error());
}


if ($_POST['tel_number']) {

//something relevant, so add to telephone table
############################
## once again your sql is invalid and you are again trying to set
## the column id to null. change the column id to a valid integer
##
## your query:
## "insert into telephone values (null, $master_id, now(), now(), '$_POST['tel_number']', '$_POST['tel_type']')";
##
############################
$add_tel = "INSERT INTO telephone (id, master_id, date_added, date_modified, tel_number, type) VALUES (_CHANGE THIS TO A VALID INTEGER_, " . $master_id . ", now(), now(), " . $_POST['tel_number'] . ", " . $_POST['tel_type'] . "winking smiley";
mysql_query($add_tel) or die(mysql_error());
}


if ($_POST['fax_number']) {

//something relevant, so add to fax table

############################
## once again your sql is invalid and you are again trying to set
## the column id to null. change the column id to a valid integer
##
## your query:
## "insert into fax values (null, $master_id, now(), now(), '$_POST['fax_number']', '$_POST['fax_type']')";
##
############################

$add_fax = "INSERT INTO fax (id, master_id, date_added, date_modified, fax_number, type) VALUES (_CHANGE THIS TO A VALID INTEGER_, " . $master_id . ", now(), now(), ". $_POST['fax_number'] . ", " . $_POST['fax_type'] . "winking smiley";
mysql_query($add_fax) or die(mysql_error());
}

################
## this below is an incorrect sytax, I assume you mean ($_POST['email'])
##
## you wrote:
## if ($_POST) {
##
## change it to:
if ($_POST['email']) {

//something relevant, so add to email table

############################
## once again your sql is invalid and you are again trying to set
## the column id to null. change the column id to a valid integer
##
## your query:
## "insert into email values (null, $master_id, now(), now(), '$_POST', '$_POST['email_type']')";
##
############################
$add_email = "INSERT INTO email (id, master_id, date_added, date_modified, email, type) VALUES (_CHANGE THIS TO A VALID INTEGER_, ". $master_id . ", now(), now(), " . $_POST['email'] . ", " . $_POST['email_type'] . "winking smiley";
mysql_query($add_email) or die(mysql_error());
}


if ($_POST['note']) {

//something relevant, so add to notes table

############################
## once again your sql is invalid and you are again trying to set
## the column id to null. change the column id to a valid integer
##
## your query:
## "insert into personal_notes values (null, $master_id, now(), now(), '$_POST['note']')";
##
############################
$add_note = "INSERT INTO personal_notes (id, master_id, date_added, date_modified, note) VALUES (_CHANGE THIS TO A VALID INTEGER_, " . $master_id . ", now(), now(), " . $_POST['note'] . "winking smiley";
mysql_query($add_note) or die(mysql_error());
}

$display_block = "?> <h1>Entry Added</h1>
<P>Your entry has been added. Would you like to
<a href="addentry.php">add another</a>?</p> <?php ";
}


?>
<HEAD>
<TITLE>Add an Entry</TITLE>
</HEAD>
<BODY>
<?php echo $display_block; ?>
</BODY>
</html>

-----------------------------------------------------------------

I just quickly edited your code, I did not test it myself, so there maybe a typo or small error or two.



Post Edited (07-15-06 12:31)

CyberSpatium
----------------------
WAMP Forum Admin

Web Development for Newbie's Blog - Check out my new blog. It is for web developers, and especially tailored for the web development newbie. If you are not fluent in “geek speak”, then this incredible resource is just you. And even if you are a web development pro, this is a great resource to check out some of the latest web development tips, news, tutorials, codes and more.

Options: ReplyQuote
Re: Whats wrong with this insert records php codes
Posted by: black85 (---.ipt.aol.com)
Date: July 15, 2006 09:32PM

@ CyberSpatium

I have made changes like you suggested and it works perfectly now. Thanks for assistance man.

Black85

Options: ReplyQuote


Sorry, only registered users may post in this forum.