Jump to content

Archived

This topic is now archived and is closed to further replies.

proctk

validating data

Recommended Posts

I'm trying create a system where a user adding information to a mysql table but if if there is matching information found in the table then a message will appear.  the message to display depnds on who is entering the information. If it the orginal person that entered the information then the message is "you already entered....." the second message tells the user how entered the information.

If there are no matches found then the information on the form is to be entered into the table.

the code I posted displays the correct message but if there is no match it still displays the message and does not add the information

Steps to validate

if the user in the table master is found and there is a match to the child_id then message " you entered...."

if user not found in master but child ID id found then messge "user already entered....." A link will be offered to add current user_id and child_id to table master.

If no matches are found then enter the child

I hope this makes sense.
thank you for the help

[code=php:0]

<? session_start();  // Start Session?>

<?

include 'db.php';

// Define post fields into simple variables
$childfirstname = $_POST['childfirstname'];
$childlastname  = $_POST['childlastname'];
$childdob = $_POST['childdob'];
$childsex = $_POST['childsex'];
$user_id = $_SESSION['user_id'];

/* Let's strip some slashes in case the user entered
any escaped characters. */

$childfirstname = stripslashes($childfirstname);
$childlastname  = stripslashes($childlastname);
$childdob = stripslashes($childdob);
$childsex = stripslashes($childsex);

if((!$childfirstname) || (!$childlastname) || (!$childdob) || (!$childsex)) {
$errorMsg .= '<div style="width:325px" id= "formmessage">'; 

    if(!$childfirstname){
        $errorMsg .=  "Enter child's first name.<br />";
    }
    if(!$childlastname){
        $errorMsg .=  "Enter child's lastname.<br />";
    }
    if(!$childdob){
        $errorMsg .=  "Enter child's Date of Birth. <br />";
    }
    if(!$childsex){
        $errorMsg .= "Select child's sex.<br />";

}

$errorMsg .= '</div>';

include 'addchildren.php';

    exit(); // if the error checking has failed, we'll exit the script!
}

// checking to see if child was entered already

if(($owner_id == $user_id) || ($owner_id != $user_id) || ($owner_id = 0) || ($user_id = 0)|| ($child_id != childMaster_id)) {

$query4 = "SELECT child_id FROM children";
$result4 = mysql_query($query4) or die(mysql_error());
$row4 = mysql_fetch_array($result4) or die(mysql_error());

$child_id= $row4['child_id'];

$query7 = "SELECT children_id, user_id FROM master WHERE master.user_id = '$user_id' ";
$result7 = mysql_query($query7) or die(mysql_error());
$row7 = mysql_fetch_array($result7) or die(mysql_error());

$childMaster_id= $row7['children_id'];

$query5 = "SELECT user_id FROM master";
$result5 = mysql_query($query5) or die(mysql_error());
$row5 = mysql_fetch_array($result5) or die(mysql_error());

$owner_id= $row5['user_id'];

$query6 = "SELECT last_name, first_name FROM users";
$result6 = mysql_query($query6) or die(mysql_error());
$row6 = mysql_fetch_array($result6) or die(mysql_error());

$owner_last= $row6['last_name'];
$owner_first= $row6['first_name'];

echo $child_id;

if($owner_id == $user_id) {
 
$msg .= '<div style="width:325px" id= "formmessage">';
$msg .= "You entered $childfirstname $childlastname already<br>";
$msg .= '</div>';
}

if($owner_id != $user_id) {

$msg .= '<div style="width:325px" id= "formmessage">';
$msg .= $owner_first .' '.$owner_last .' '. " entered" .' '.$childfirstname. ' '.  $childlastname. " already<br>";

$msg .="<a href='codeAddchildLink.php?childid=$child_id&userid=$user_id'>Link Child</a>";

$msg .= '</div>';
}

if($owner_id) {

}

if ($user_id) {

}

if($child_id != childMaster_id) {
}

include 'addchildren.php';

    exit(); // if the error checking has failed, we'll exit the script!
}
// Enter info into the Database.
$info2 = htmlspecialchars($info);
$sql = mysql_query("INSERT INTO children (childfirstname,
childlastname, childdob, childsex)
VALUES('$childfirstname', '$childlastname', '$childdob', '$childsex')")
or die (mysql_error());

if(!$sql){

}

$children_id = mysql_insert_id();

mysql_query("INSERT INTO master (user_id, children_id) VALUE ('$user_id', '$children_id')") or die (mysql_error());

$msg .='<div style="width:325px" id= "formmessage">'; 
$msg .= $childfirstname. ' '. $childlastname. " has been added.";
$msg .= '</div>';


include 'getchildren.php';

?>
[/code]

Share this post


Link to post
Share on other sites

×

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.