Jump to content

Recommended Posts

this script is supposed to enter data into mysql. The only required imput is first and last name. for some reason, after I submit, the form displays again. the form is only supposed to display if no $_POST data is submitted or the required $_POST data in not submitted. I have been looking at this code for about 4 hours. I need another set of eyes.

 

 

<?php

\\this is my mysqli_connectcode- simple

include("aBookInclude.php");

 

if (!$_POST) {

$display_block = "<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>

 

<p><strong>Address:</strong><br/>

<input type=\"text\" name=\"address\" size=\"30\"></p>

 

<p><strong>City/State/Zip:</strong><br/>

<input type=\"text\" name=\"city\" size=\"30\" maxlength\"50\">

<input type=\"text\" name=\"state\" size=\"30\" maxlength\"2\">

<input type=\"text\" name=\"zip\" size=\"30\" maxlength\"10\">

 

<p><strong>Address Type:</strong><br/>

<input type=\"radio\" name=\"add_type\" value=\"home\"> home

<input type=\"radio\" name=\"add_type\" value=\"work\"> work

<input type=\"radio\" name=\"add_type\" value=\"other\"> other</p>

 

<p><strong>Telephone Number:</strong><br/>

<input type=\"text\" name=\"tel_number\" size=\"30\" maxlength\"25\">

<input type=\"radio\" name=\"tel_type\" value=\"home\"> home

<input type=\"radio\" name=\"tel_type\" value=\"work\"> work

<input type=\"radio\" name=\"tel_type\" value=\"other\"> other</p>

 

<p><strong>Fax Number:</strong><br/>

<input type=\"text\" name=\"fax_number\" size=\"30\" maxlength\"25\">

<input type=\"radio\" name=\"fax_type\" value=\"home\"> home

<input type=\"radio\" name=\"fax_type\" value=\"work\"> work

<input type=\"radio\" name=\"fax_type\" value=\"other\"> other</p>

 

<p><strong>Email Address:</strong><br/>

<input type=\"text\" name=\"email\" size=\"30\" maxlength\"25\">

<input type=\"radio\" name=\"email_type\" value=\"home\"> home

<input type=\"radio\" name=\"email_type\" value=\"work\"> work

<input type=\"radio\" name=\"email_type\" value=\"other\"> other</p>

 

<p><strong>Personal Note:</strong><br/>

<textarea name=\"note\" cols=\"35\" rows=\"3\" wrap=\"virtual\"></textarea></p>

 

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

 

} else if ($_POST) {

if (($_POST["f_name"] == "") || ($_POST["l_name"] == "")) {

header("Location: addentry.php");

exit;

}

 

doDB;

 

$add_master_sql = "INSERT INTO master_name (date_added, date_modified, f_name, l_name) VALUES (now(), now(),

'".$_POST['f_name']."', '".$_POST['l_name']."')";

$add_master_res = mysqli_query($mysqli, $add_master_sql) or die(mysqli_error($mysqli));

 

$master_id = mysqli_insert_id($mysqli);

 

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

$add_address_sql = "INSERT INTO address (master_id, date_added, date_modified, address, city, state, zipcode, type) VALUES

('".$master_id."', now(), now(), '".$_POST['address']."', '".$_POST['city']."', '".$_POST['state']."', '".$_POST["zipcode"]."', '".$_POST["add_type"]."')";

$add_address_res = mysqli_query($mysqli, $add_address_sql) or die(mysqli_error($mysqli));

 

}

 

if ($_POST["tel_number"]) {

$add_tel_sql = "INSERT INTO telephone (master_id, date_added, date_modified, tel_number, type) VALUES ('".$master_id."', now(), now(),'".$_POST["tel_number"]."', '".$_POST["tel_type"]."')";

$add_tel_res = mysqli_query($mysqli,$add_tel_sql) or die(mysqli_error($mysqli));

 

}

 

if ($_POST["fax_number"]) {

$add_fax_sql = "INSERT INTO fax (master_id, date_added, date_modified, fax_number, type) VALUES ('".$master_id."', now(), now(), '".$_POST["fax_number"]."', '".$_POST["fax_type"]."')";

$add_fax_res = mysqli_query($mysqli, $add_fax_sql) or die(mysqli_error($mysqli));

}

if ($_POST["email"]) {

$add_email_sql = "INSERT INTO email (master_id, date_added, date_modified, email, type) VALUES ('".$master_id."', now(), now(),

'".$_POST["email"]."', '".$_POST["email_type"]."')";

$add_email_res = mysqli_query($mysqli, $add_email_sql) or die(mysqli_error($mysqli));

}

if ($_POST["note"]) {

$add_note_sql = "INSERT INTO personal_notes (master_id, date_added, date_modified, note) VALUES ('".$master_id."', now(), now(), '".$_POST["note"]."', '".$_POST["note_type"]."')";

$add_tel_res = mysqli_query($mysqli, $add_note_sql) or die(mysqli_error($mysqli));

 

}

mysqli_close($mysqli);

\\i should see this after submitting

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

 

}

?>

 

<html>

<head>

<title>Add an Entry</title>

</head>

<body><h1>Add an Entry</h1>

<?php echo $display_block; ?>

</body>

</html>

Link to comment
https://forums.phpfreaks.com/topic/270179-record-addition-script-mysql/
Share on other sites

OK, people invented code tags for a reason (and no, it want's so you could not use them)....

your check for the form simply looks to see if the $_POST superglobal has been registered in memory. Superglobals, once populated once, exist (even without data) for the rest of the script (may even always be there, but I don't recall ever trying to check that). Expand your check to see if the form submit button was sent through the $_POST array to check if the form it's self was passed

if(!isset($_POST['submit'])){
//your code here

 

As a rule of thumb, I would say try to be as explicit and verbose as you can when coding. Never assume that the language will be ok at sorting out any ambiguity in your code.

Why, for the love of all that's holy, do you have one table for each input field?!

 

The database schema you've selected defies all logic, I'm afraid. The only data that needs their own table are the zip, city and state fields. Where "city" table has a foreign key to the state ID, and the "zip" table has a foreign key to the city ID. (Not that "city" would be a perfect description in this case, but..)

All the rest can easily be saved into one table, named "users", with the ZIP being a foreign key to the zip table. As all of the information gathered pertains directly to that one user, and no-one else.

 

When sending someone a gift composed of a few small items, you wouldn't pack and ship each of those items individually, would you? ;)

 

PS: If you really need a log of when the different fields have been changed, then define a table for said log and save the relevant data in it.

Edited by Christian F.
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.