Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
HELP: How to Insert a select input data into a database Using Php
#1
Am having a problem in trying to insert the gender of a user(which is in a select input form). when ever i click submit the input clears, saying its empty instead of send the selected data to my database. Pls i want to know why this is happening and how i can fix this.
MY PHP CODE;
$gender = mysqli_real_escape_string($db, $_POST['gender'];
if ($gender = "") {
$err_msg = "your gender is required";
}
if ($err_msg = "") {
// my insert query...
}

MY HTML CODE
<select name="gender">
<option></option>
<option name="male" value="male">Male</option>
<option name="female" value="female">Female</option>
</select>


@Gift101 @Webohost @Aphatheology @Horlahcoded @Hackinubee
I am a proud member of NIgerian Coders Team
[-] The following 1 user Likes Aaron's post:
  • Ahmad
#2
@Dealwap
I am a proud member of NIgerian Coders Team
Reply
#3
OK do something like


<?php
if(isset($_POST['submit'])){
$gender = trim($_POST['user_gender']);

if(empty($gender)){
#echo error
}
else{
include("connection.php");

mysqli_query($connectvariable,"INSERT INTO user(gender) VALUES('$gender')");

if(mysqli_affected_rows($connectvairable) == 1){
#sucess
}
else{
#not_successful

}
}
}
?>

<form method="post" action="<?php echo $_SERVER ['PHP_SELF']; ?>">
<select name="user_gender">
<option value="male">Male</option>


<option value="female">Female</option>


<option value="unspecify">None</option>
</select>
</form>


Nothing only the select tag has the name attribute
I am a proud member of NIgerian Coders Team
[-] The following 2 users Like abcheats's post:
  • Aaron, Ahmad
Reply
#4
(04-13-2018, 07:42 PM)abcheats Wrote:
OK do something like


<?php
if(isset($_POST['submit'])){
$gender = trim($_POST['user_gender']);

if(empty($gender)){
#echo error
}
else{
include("connection.php");

mysqli_query($connectvariable,"INSERT INTO user(gender) VALUES('$gender')");

if(mysqli_affected_rows($connectvairable) == 1){
#sucess
}
else{
#not_successful

}
}
}
?>

<form method="post" action="<?php echo $_SERVER ['PHP_SELF']; ?>">
<select name="user_gender">
<option value="male">Male</option>


<option value="female">Female</option>


<option value="unspecify">None</option>
</select>
</form>


Nothing only the select tag has the name attribute


your code is not secure.... always use the string escape function
Reply
#5
use this

if (isset($_POST["Submit"])) {
if(!empty($_POST["gender"])) {
$Gender = mysqli_real_escape_string($db, $_POST["gender"]);
} else {
$GenderError = "Please select your gender";
}
$insert = mysqli_query($db, "INSERT INTO user(gender) VALUES('$Gender')");
mysqli_close($db);

if ($insert) {
$success = "Your form has been submitted";
}
}

<?php echo $GenderError ?>
<?php echo $success ?>


MY HTML CODE
<select>
<option></option>
<option name="gender" value="male">Male</option>
<option name="gender" value="female">Female</option>
</select>
[-] The following 1 user Likes Cavdy's post:
  • Aaron
Reply
#6
Nice work Fam...

[Image: JKUgy6R.jpg]
Reply
#7
am still having the same problem... pls i need help
I am a proud member of NIgerian Coders Team
Reply
#8

Code:
<?php
    // variables
    $msg = $error = $error_class = $error0 = $error0_class = $error1 = $error1_class = $error2 = $error2_class = $error3 = $error3_class = $set_uname = $displayname = "";

        if(isset($_POST['reg_submit'])) {

            // firstname validation
             $firstname = mysqli_real_escape_string($db, $_POST["firstname"]);
            if(empty($_POST["firstname"])) {
                $error = '<font color="#CF1433">Your first name is required</font>';
                $error_class = 'style="border-color:#CF1433;color:#CF1433"';
            } elseif(strlen($firstname) < 3) {
                $error = '<font color="#CF1433">Your first name must be greater than 3 letters</font>';
                $error_class = 'style="border-color:#CF1433;color:#CF1433"';
            } elseif(strlen($firstname) > 25) {
                $error = '<font color="#CF1433">Your first name is too long</font>';
                $error_class = 'style="border-color:#CF1433;color:#CF1433"';
            } elseif(!preg_match("/^[A-Za-z]+$/ ", $firstname)) {
                $error = '<font color="#CF1433">Your first name must be letters only e.g. Aaron</font>';
                $error_class = 'style="border-color:#CF1433;color:#CF1433"';
            }

            // middlename validation
            $middlename = mysqli_real_escape_string($db, $_POST['middlename']);
            if(empty($_POST["middlename"])) {
                // just proceed...
            } elseif(strlen($middlename) < 3) {
                $error0 = '<font color="#CF1433">Your middle name must be greater than 3 digits</font>';
                $error0_class = 'style="border-color:#CF1433;color:#CF1433"';
            } elseif(strlen($middlename) > 25) {
                $error0 = '<font color="#CF1433">Your middle name is too long</font>';
                $error0_class = 'style="border-color:#CF1433;color:#CF1433"';
            } elseif(!preg_match("/^[A-Za-z]+$/ ", $middlename)) {
                $error0 = '<font color="#CF1433">Your middle name must be letters only e.g. Aaron</font>';
                $error0_class = 'style="border-color:#CF1433;color:#CF1433"';
            }

            // lastname validation
            $lastname = mysqli_real_escape_string($db, $_POST['lastname']);
            if(empty($_POST["lastname"])) {
                $error1 = '<font color="#CF1433">Your last name is required</font>';
                $error1_class = 'style="border-color:#CF1433;color:#CF1433"';
            } elseif(strlen($lastname) < 3) {
                $error1 = '<font color="#CF1433">Your last name must be greater than 3 digits</font>';
                $error1_class = 'style="border-color:#CF1433;color:#CF1433"';
            } elseif(strlen($lastname) > 25) {
                $error1 = '<font color="#CF1433">Your last name is too long</font>';
                $error1_class = 'style="border-color:#CF1433;color:#CF1433"';
            } elseif(!preg_match("/^[A-Za-z]+$/ ", $lastname)) {
                $error1 = '<font color="#CF1433">Your last name must be letters only e.g. Aaron</font>';
                $error1_class = 'style="border-color:#CF1433;color:#CF1433"';
            }

            // email validation
            $email = mysqli_real_escape_string($db, $_POST['email']);
            if(empty($_POST["email"])) {
                $error2 = '<font color="#CF1433">Your email address is required</font>';
                $error2_class = 'style="border-color:#CF1433;color:#CF1433"';
            } elseif(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {
                $error2 = '<font color="#CF1433">Invalid email address</font>';
                $error2_class = 'style="border-color:#CF1433;color:#CF1433"';
            } else {
                $query = "SELECT `email` FROM `users` WHERE `email` = '$email'";
                if(!$result = mysqli_query($db, $query)) {
                    exit(mysqli_error($db));
                }
                if(mysqli_num_rows($result) > 0) {
                    $error2 = '<font color="#CF1433">'.$email.' is already in use</font>';
                    $error2_class = 'style="border-color:#CF1433;color:#CF1433"';
                } else {
                    $error2_class = 'style="border-color:green"';
                }
            }        

            // gender validation
            $gender = mysqli_real_escape_string($db, $_POST['gender']);

            // password validation
            $password = mysqli_real_escape_string($db, $_POST["password"]);
            if(empty($_POST["password"])) {
                $error3 = '<font color="red">Your password is required</font>';
                $error3_class = 'style="border-color:red;color:red"';
            } elseif(strlen($password) < 6) {
                $error3 = '<font color="#CF1433">Password must be greater than 6 digits</font>';
                $error3_class = 'style="border-color:#CF1433;color:#CF1433"';
            } elseif(strlen($password) > 100) {
                $error3 = '<font color="#CF1433">Your password is to long</font>';
                $error3_class = 'style="border-color:#CF1433;color:#CF1433"';
            } else {
                $error3_class = 'style="border-color:green"';
            }

            // check errors, if null submit form to database
            if($error == "" && $error0 == "" && $error1 == "" && $error2 == "" && $error3 = "") {

                // grab user ip address
                $ip = mysqli_real_escape_string($db, $_SERVER['REMOTE_ADDR']);

                // encrypt password
                $password = ($password);

                // generate username
                if ($middlename == "") {
                    $set_uname = $firstname.".".$lastname;
                } else {
                    $set_uname = $firstname.".".$middlename.".".$lastname;
                }

                // check if generate username exits
                $select = "SELECT `username` FROM `users` WHERE `username` = '$set_uname'";
                if(!$result = mysqli_query($db, $query)) {
                    exit(mysqli_error($db));
                }
                if(mysqli_num_rows($result) > 0) {
                    // add random digits to username if existing
                    $username = $set_uname.rand(0,1000);
                } else {
                    $username = $set_uname;
                }

                // generate display name
                if ($middlename == "") {
                    $displayname = $firstname." ".$lastname;
                } else {
                    $displayname = $firstname." ".$middlename." ".$lastname;
                }

                // insert form data to the following database tables
                // user name, email, gender and password stored in table `users`
                $query = mysqli_query($db, "INSERT INTO users(username, email, gender, password) VALUES('$username','$email','$gender','$password');");

                // user name, email and gender stored in table `users_profile`, username will be set as default user 'display_name'
                $query0 = mysqli_query($db, "INSERT INTO users_profile(display_name, first_name, middle_name, last_name, username, gender, email) VALUES('$displayname','$firstname','$middlename','$lastname','$username','$gender','$email');");

                // user name stored in table `users_lastlogin_dt` to save user last login date
                $query1 = mysqli_query($db, "INSERT INTO users_lastlogin_dt(username) VALUES('$username')");

                // user name stored in table `users_lastlogin_ip` to save user last login ip address
                $query2 = mysqli_query($db, "INSERT INTO users_lastlogin_ip(username) VALUES('$username')");

                // user name, signup date and time stored in table `users_signup_dt` to save user signup date and time
                $query3 = mysqli_query($db, "INSERT INTO users_signup_dt(username, NOW()) VALUES('$username', '$date_n_time')");

                // user name and signup ip address stored in table `users_signup_dt`
                $query4 = mysqli_query($db, "INSERT INTO users_signup_ip(username, ip) VALUES('$username', '$ip')");

                // user name stored in table `users_level`. This is done to check if user will be a normal or verified user, moderators are for workers and admin for the owner
                $query5 = mysqli_query($db, "INSERT INTO users_level(username) VALUES('$username')");

                // report message (success)
                $msg = '<div class="alert a-success alert-dismissable" id="alert"><a href="#" class="close" data-dismiss="alert" aria-label="close">×</a><b>Signup was successful, check your mail to confirm your email address</b></div>';

                // clear form input when submitted to db
                $firstname = $middlename = $lastname = $email = $gender = $password = "";

            } else {
                // report message (failure)
                $msg = '<div class="alert a-danger alert-dismissable" id="alert"><a href="#" class="close" data-dismiss="alert" aria-label="close">×</a><b>Signup was unsuccessful, please try again</b></div>';
            }

        }
?>


<form id="canabody" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"])?>" enctype="multipart/form-data">
                        <div class="form-group" <?php echo $error_class; ?>>
                            <b>First name</b>
                            
                            <input class="form-control" id="fname" type="text" name="firstname" maxlength="25" value="<?php if(isset($firstname)) echo $firstname; ?>" placeholder="3 - 25 characters" autocomplete="off" title = "Please enter your first name" required>
                            <span id="firstname_val"><?php echo $error; ?></span>
                        </div>

                        <div class="form-group" <?php echo $error0_class; ?>>
                            <b>Middle name(optional)</b>
                            
                            <input class="form-control" id="mname" type="text" name="middlename" maxlength="25" value="<?php if(isset($middlename)) echo $middlename; ?>" placeholder="3 - 25 characters" autocomplete="off" title = "Please enter your middle name">
                            <span id="middlename_val"><?php echo $error0; ?></span>
                        </div>

                        <div class="form-group" <?php echo $error1_class; ?>>
                            <b>Last name</b>
                            
                            <input class="form-control" id="lname" type="text" name="lastname" maxlength="25" value="<?php if(isset($lastname)) echo $lastname; ?>" placeholder="3 - 25 characters" autocomplete="off" title = "Please enter your last name" required>
                            <span id="lastname_val"><?php echo $error1; ?></span>
                        </div>

                        <div class="form-group" <?php echo $error2_class; ?>>
                            <b>Email address</b>
                            
                            <input class="form-control"  id="usrem"  type="email" name="email" value="<?php if(isset($email)) echo $email; ?>" placeholder = "yourname@domain.com" title="Please enter your email address" required>
                            <span id="email_val"><?php echo $error2; ?></span>
                        </div>

                        <div class="form-group" <?php echo $error3_class; ?>>
                            <b>Gender</b>
                            
                            <select class="form-control" name="gender" id="ugender" title="Please select your gender" required>
                                <option value="">Select...</option>
                                <option name="male" <?php if (isset($gender) && $gender=="male") echo "selected";?> value="male">Male</option>
                                <option name="female" <?php if (isset($gender) && $gender=="female") echo "selected";?> value="female">Female</option>
                            </select>
                            <?php echo $error3; ?>
                        </div>

                        <div class="form-group" <?php echo $error3_class; ?>>
                            <b>Password</b>
                            
                            <input class="form-control" id="usrpass" type="password" name="password" maxlenght="100" value="<?php if(isset($password)) echo $password; ?>" placeholder="6 - 100 characters" title = "Please enter a valid password" required>
                            <span id="password_val"><?php echo $error3; ?></span>
                            <input type="checkbox" onclick="showpass()"> Show Password
                        </div>
                        <p>By clicking on the button below, you have accepted our <a href="#">terms & conditions</a> and you have also read our <a href="#">privacy policy</a>.</p>
                        <button id="canabut" class="btn btn-success" type="submit" name="reg_submit">Create account</button>
                    </form>

this is my full code else
I am a proud member of NIgerian Coders Team
Reply
#9
help pls
I am a proud member of NIgerian Coders Team
Reply
#10
the problem is not actually on the select input of a thing because i print_r($_POST); and all data's where intact... but but i do get the error msg... "signup unsuccessful, try again"
(my else message after failure in signing up)
I am a proud member of NIgerian Coders Team
Reply
#11
I couldnt go through your codes, it is too loaded.... about your error alert, why not use session..... it will reduce your code..... and it won't ask you to resubmit if your refresh the page

and I noticed that your if statements comes after you render the code, that is not right....

in your code, you submit the $firstname before it even run the if statement.... seeing your code, after the if and elseif statement..... use else to run your $firstname code... which means if all these conditions are met, then run these else statement....
Reply
#12
I hope you understand?
Reply
#13
(04-16-2018, 08:31 PM)Cavdy Wrote:
I couldnt go through your codes, it is too loaded.... about your error alert, why not use session..... it will reduce your code..... and it won't ask you to resubmit if your refresh the page

and I noticed that your if statements comes after you render the code, that is not right....

in your code, you submit the $firstname before it even run the if statement.... seeing your code, after the if and elseif statement..... use else to run your $firstname code... which means if all these conditions are met, then run these else statement....


i don't understand
I am a proud member of NIgerian Coders Team
Reply
#14
the $firstname u saw below my codes, i am trying to generate users username using their firstname, middlename and lstname... thats working fine... but my form data is not sending to database, i used print_r($_POST); to see if the datas where sending, and yes it was but i still get the same problem
I am a proud member of NIgerian Coders Team
Reply
#15
u can delete some of my query codes and leave only 1, and their are other uneccessary codes, or i will just do them... here;
I am a proud member of NIgerian Coders Team
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  HELP: How To Upload Multiple Images In Mysql Database abcheats 6 163 02-18-2018, 06:24 AM
Last Post: Laive
  HELP: Php Input Problem (help Needed Urgently) Aaron 5 148 12-14-2017, 11:05 PM
Last Post: Aaron
  SHARED: How To Run Php And Mysql Database On Any Andriod Devine Like Xampp For Computer sedenu4u 1 186 10-08-2017, 07:40 PM
Last Post: Deelino
  HELP: How To Validate A Html Input Form Php If And Else Conditions Aaron 19 431 08-01-2017, 09:49 AM
Last Post: Gift101
  TUTORIAL: Php Login Without Using Database bfaschat 8 236 06-15-2017, 08:25 AM
Last Post: Horlahcoded
  Auto-Refresh HTML-PHP Page Data using PHP Cookie 2 1,641 02-06-2016, 07:01 AM
Last Post: Aphatheology
  Cleaning Database Input With PHP Dealwap 0 313 01-25-2016, 01:36 PM
Last Post: Dealwap



Users browsing this thread: Laive, 1 Guest(s)

About Nigeria's Creative Talents

Nigeria's Creative Talents is an online community where every creative Nigerians can share ideas with themselves.

For any more information, please use our contact form.

              User Links

              Advertise