this assignmet call for inserting data into the database and also retrieve. at one time it was inserting into the database but for strange reason it just stop inserting, i have search every noook and cranny within the code to find the error. I know that there is an error there that cause it to stop inserting. but what it is I really dont know. I am almost to the point of pulling out my hair. and another thing error is that I have set it to redirect it to the login page(this is the registration page am talking about) one mine it giving an error underfined index: gender in C, but I dont know how to fix it. ah lots of things is going on with this php codes and i cant seem to find what is worng and now am trying to validate it using js and cant get it to do it.

here is my html

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" 
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <head>
        <title> Register </title>
        <link rel="stylesheet" type="text/css" href="../css/stylesheet.css" />
    </head>
    <body>
        <div id="container" >
            <div id="header">
                <img src="../images/header.jpg" alt="header" />
            </div>
            <div id="navbar">
                <ul>
                    <li><a href="../index.html" id="tlinks" class="toplinks" >Homepage</a></li>
                    <li><a href="featureNationalWonders.html" id="thold" class="toplinks">Feature National Wonders</a></li>
                    <li><a href="contact.html" id="linkhold" class="toplinks">Contact Us</a></li>
                    <li><a href="register.html" id="phold" class="toplinks">Sign Up</a></li>
                    <li><a href="login_user.html" id="loglinks" class="toplinks">Login</a></li>
                    <li><a href="aboutus.html" id="panlinks" class="toplinks">About Us</a></li>

                </ul>   
                    <form method="post" id="search"   action="" >
                        <div>
                            <input type="text" name="search" value=""/>
                            <input type="submit" value="Search"/>
                        </div>                      
                    </form> 
            </div>
            <br/><br/>
            <div id="contentArea">
                <form method="post" action="../php/register_user1.php" name="frmRegister" onsubmit="return validateForm()";>

                        <p>
                            <label> User Personal Information</label>
                        </p>
                                <p>
                                     <label>Title</label>
                                    <select id="title" name="title">
                                        <option value="" selected="selected" > Select a Title</option>
                                        <option value="Ms">Ms.</option>
                                        <option value="Mrs">Mrs.</option>
                                        <option value="Mr">Mr.</option>
                                        <option value="Dr">Dr.</option>
                                    </select>
                                </p>

                            <p> 
                                 <label>First Name:</label>
                                <input type="text" name="firstname" id="firstname" size="47"/>

                            </p>
                            <p>
                                <label>Last Name:</label>
                                <input type="text" name="lastname" id="lastname" size="47"/>
                            </p>
                            <p>
                                <label> UserName:</label>
                                <input type="text" name="username" id="username"  size="44"/>
                            </p>
                            <p>
                                <label> Email Address:</label>                      
                                <input type="text" id="email" name="email" size="43"/>
                            </p>
                            <p>
                                <label>Gender:</label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                                <input type="radio" name="gender" id="m_male" value="male"/>Male 
                                <input type="radio" name="gender" id="f_female" value="Female"/>Female
                            </p>
                            <p>
                                <label> Services:</label>
                                Magical Mermaid Hike: <input type="checkbox" name="services"/> 
                                Kayaking:<input type="checkbox" name="services"/>
                                Turtle Watching:<input type="checkbox" name="services"/>
                                Bus Tours:<input type="checkbox" name="services"/>
                                Sightseeing:<input type="checkbox" name="services"/>
                            </p>
                            <p>
                                <label>Address:</label>
                                <textarea name="address" id="address" cols="46" rows="9"></textarea>
                            </p>
                            <p>
                                <label> Password: </label>
                                <input type="password" id="mypwd" name="mypwd" size="47"/>
                            </p>
                            <p>
                                 <label>Confirm Password:</label>
                                <input type="password" name="confmypwd" id="confmywd" size="39"/>
                            </p>

                                <input type="submit" name="enter" value="Register" class="btnEnter"/>
                                <input type="reset" name="reset" value="RESET"/>

                </form>

                <p>
                    <a href="http://validator.w3.org/check?uri=referer"><img
                      src="http://www.w3.org/Icons/valid-xhtml11" alt="Valid XHTML 1.1" height="31" width="88" /></a>
                </p>
                <p>
                <a href="http://jigsaw.w3.org/css-validator/check/referer">
                    <img style="border:0;width:88px;height:31px"
                    src="http://jigsaw.w3.org/css-validator/images/vcss-blue"
                    alt="Valid CSS!" />
                </a>
            </p>

            </div>
                <div id="footerFix"></div>
                <div id="footerbox">
                    <div id="footer" >
                        <div id="footerholder">
                        <div>
                            <ul>
                                <li><a href="../index.html" class="toplinks" >Homepage</a></li>
                                <li><a href="featureNationalWonders.html"  class="toplinks">Feature National Wonders</a></li>
                                <li><a href="contact.html" class="toplinks">Contact Us</a></li>
                                <li><a href="register_user.html" class="toplinks">Sign Up</a></li>
                                <li><a href="login_user.html" class="toplinks">Login</a></li>
                                <li><a href="aboutus.html" class="toplinks">About Us</a></li>
                            </ul>
                        </div>
                        </div>
                    </div>            
                </div>

        </div>

    </body>
</html>

and here is my php codes for it

<?php

        $register=$_POST['register_user']; 
        if(isset($_POST['Register']))
        {

            //capture the variable from the form and store in php variables
            $title=$_POST['title'];
            $firstname=$_POST['firstname'];

<?php

        $register=$_POST['register_user']; 
        if(isset($_POST['Register']))
        {

            //capture the variable from the form and store in php variables
            $title=$_POST['title'];
            $firstname=$_POST['firstname'];
            $lastname=$_POST['lastname'];
            $username=$_POST['username'];
            $email=$_POST['email'];
            $gender= isset($_POST['gender']) && !empty($_POST['gender']);
            $address=$_POST['address'];
            $mypwd=$_POST['mypwd'];

            include'db_server.php';

            //Query the database
            $sql="SELECT * FROM members WHERE username='$username'";

            $result= mysqli_query($conn, $sql) or die ("ERROR:" .mysqli_error());

            $rowcount=mysqli_num_rows($result);

            //checking to see if username is already exist
            if($rowcount >= 1) 
            {
                echo "<script type=\"text/javascript\">
                      alert('Welcome!! Firstname Lastname, you are now a member of the Caribbean Nature Seekers Institute TT(CNSITT)');
                      window.location=\"../login_user.html\";
                       </script>";

            }
            else
            {
                //insert data into table

                $sql = "INSERT INTO members
                VALUES('$title', '$firstname','$lastname', '$username', '$email', '$gender', '$address', md5('$mypwd'))";

                if(mysqli_query($conn,$sql))
                {
                    session_start();
                    $_SESSION['user']=$username;                    
                    header("location:login_user.html");
                    mysqli_close($conn);

                }
                else
                {
                    echo "Error inserting values into database";
                }
                //end of line

            }
        }   

?>

            $lastname=$_POST['lastname'];
            $username=$_POST['username'];
            $email=$_POST['email'];
            $gender= isset($_POST['gender']) && !empty($_POST['gender']);
            $address=$_POST['address'];
            $mypwd=$_POST['mypwd'];

            include'db_server.php';

            //Query the database
            $sql="SELECT * FROM members WHERE username='$username'";

            $result= mysqli_query($conn, $sql) or die ("ERROR:" .mysqli_error());

            $rowcount=mysqli_num_rows($result);

            //checking to see if username is already exist
            if($rowcount >= 1) 
            {
                echo "<script type=\"text/javascript\">
                      alert('Welcome!! Firstname Lastname, you are now a member of the Caribbean Nature Seekers Institute TT(CNSITT)');
                      window.location=\"../login_user.html\";
                       </script>";

            }
            else
            {
                //insert data into table

                $sql = "INSERT INTO members
                VALUES('$title', '$firstname','$lastname', '$username', '$email', '$gender', '$address', md5('$mypwd'))";

                if(mysqli_query($conn,$sql))
                {
                    session_start();
                    $_SESSION['user']=$username;                    
                    header("location:login_user.html");
                    mysqli_close($conn);

                }
                else
                {
                    echo "Error inserting values into database";
                }
                //end of line

            }
        }   

?>

Dani AI

Generated

Several distinct issues are likely preventing inserts. Common culprits here are mismatched form names vs what the PHP checks (the submit button is named differently from the server-side test), duplicated/fragmented PHP blocks that cause parse or logic errors, assigning the gender variable to a boolean instead of the posted value, using an INSERT without an explicit column list (which fails if table structure changes), and relying on MD5 for passwords. As pointed out, the gender value must come from the submitted data (or be set to null) rather than being used as a boolean flag. The pasted PHP also looks duplicated and contains stray PHP open/close tags that will break execution.

Practical checklist to fix the form:

  • Make the submit-name checks match the form (or prefer checking request method).
  • Use null coalescing or safe checks to read posted values and set defaults for missing fields.
  • Give checkbox groups a name like services[] so multiple selections arrive as an array.
  • Always list columns in INSERT and use prepared statements to avoid SQL errors and injection.
  • Replace MD5 with PHP's password_hash() and verify with password_verify().
  • Remove duplicate PHP blocks and ensure there is no output before any header() calls (or call exit immediately after redirect).
  • Enable error reporting and inspect var_dump($_POST) while debugging.

Small, safe example (adapt to the actual table column names and to the db connection variable):

<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['enter'])) {
    $title = trim($_POST['title'] ?? '');
    $firstname = trim($_POST['firstname'] ?? '');
    $lastname = trim($_POST['lastname'] ?? '');
    $username = trim($_POST['username'] ?? '');
    $email = trim($_POST['email'] ?? '');
    $gender = $_POST['gender'] ?? null;
    $address = trim($_POST['address'] ?? '');
    $passHash = password_hash($_POST['mypwd'] ?? '', PASSWORD_DEFAULT);

    $stmt = $conn->prepare('INSERT INTO members (title, firstname, lastname, username, email, gender, address, password) VALUES (?, ?, ?, ?, ?, ?, ?, ?)');
    $stmt->bind_param('ssssssss', $title, $firstname, $lastname, $username, $email, $gender, $address, $passHash);
    $stmt->execute() or die($conn->error);
    header('Location: ../login_user.html');
    exit;
}
?>

Quick debugging tips: turn on errors with error_reporting(E_ALL) and ini_set('display_errors',1), var_dump($_POST) to confirm sent fields, and check webserver/PHP error logs. For long-term quality, use prepared statements, server-side validation, and avoid echoing HTML before redirects. This addresses the main gaps raised by and the clarification from .

Recommended Answers

All 2 Replies

This is a LOT of code for us to read and understand. If it's giving you an error message saying undefined index 'gender', that probably means that $_POST['gender'] is not defined. However, you have $gender= isset($_POST['gender']) && !empty($_POST['gender']); and I don't understand what you're trying to do there. It looks like you're setting the $gender variable to a boolean (true or false) as to whether $_POST['gender'] exists or not, instead of setting it to the value of $_POST['gender'] if it does exist. You would want to do:

$gender= (isset($_POST['gender']) && !empty($_POST['gender'])) ? $_POST['gender'] : null;

What that does is it sets $gender to $_POST['gender'] if it exists, and NULL if it doesn't. Can you please be more specific as to the actual error messages you're receiving and what isn't working as intended?

hi dani

that was just something that I was trying to do just to see if it will work but it didnt, kinda fix it but still it is not inserting. yesterday i was out all day with a friend of mine, trying to get it to insert but to no avail. will post the new code later on today

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.