Form Data Won't Insert

Codes here !

Moderators: egami, macek, gesf

Post Reply
saved4106
New php-forum User
New php-forum User
Posts: 5
Joined: Mon Jan 29, 2018 1:27 am

Mon Jan 29, 2018 1:48 am

I've been bui;ding a website for the first time. i have a database on phpmyadmin and i have created a table. i wrote the php code and it wont insert my form data to the table. can someone please help with what im doing wrong. i'm very new and after two days of troubleshooting i have no idea where to go next. below is my html code and php.

<html>
<body>
<head>
<title>GlidewellFitness </title>
<link rel="stylesheet" type="text/css"
href="css/homepage-css.css" />
<h1><img src="images/Glidewell-01.png"alt="" width="500" height="200" /></h1>
</head>
<body>
<p><a href="index.html">home</a> <a href="sign-up.html">sign up</a> <a href="plans.html">plans</a> <a href="contact trainer.html">contact</a>
<form action="clientinfo.php" method="post" name="client info">
<div>
<label for="*First Name" class="inputs"><b>*First Name:</b></label>
<input type="text" id="*First Name" name="FirstName" size="15" maxlength="35" class="boxes"/>
</div>
<div>
<label for="M.I." class="inputs"><b>M.I.:</b></label>
<input type="text" id="M.I." name="MI" size="15" maxlength="5" class="boxes" />
</div>
<div>
<label for="LastName" class="inputs"><b>*Last Name:</b></label>
<input type="text" name="*Last Name" size="15" maxlength="40" class="boxes"/>
</div>
<div>
<label for="Suffix" class="inputs"><b>Suffix:</b></label>
<input type="text" name="Suffix" size="15" maxlength="30" class="boxes" />
</div>
<div>
<label for="gender" class="inputs"><b>Gender:</b></label>
<input type="radio" name="M/F" value="M" /> M
<input type="radio" name="M/F" value="F" /> F
</div>
<div>
<label for="Age" class="inputs"><b>Age:</b></label>
<select name="age" class="boxes">
<option value="12-20">16-20</option>
<option value="21-30">21-30</option>
<option value="31-40">31-40</option>
<option value="41-49">41-49</option>
<option value="50+">50+</option>
</select>
</div>
<div>
<label for="weight" class="inputs"><b>Weight:</b></label>
<input type="text" name="Weight" size="15" maxlength="40" class="boxes" />
</div>
<div>
<label for="Exercise Experience" class="inputs"><b>Exercise Experience:</b></label>
<select name="ExerciseExperience" class="boxes">
<option value="none">none</option>
<option value="moderate">moderate</option>
<option value="extensive">exstensive</option>
</select>
</div>
<div>
<label for="FitnessGoal" class="inputs"><b>Fitness Goal:</b></label>
<select name="Fitness Goal" class="boxes">
<option value="weight loss"> weight loss</option>
<option value="strength increase"> strength increase</option>
<option value="muscle gain"> muscle gain</option>
</select>
</div>
<div>
<label for="Email" class="inputs"><b>Email:</b></label>
<input type="text" name="Email" size="15" maxlength="40" class="boxes"/>
</div>
<input type="submit" name="client info" value="Submit">
</form>
</html>



<?php

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

include('clientinfo.php' ) ;

$FirstName=$_POST['FirstName'];
$MI=$_POST['MI'];
$LastName=$_POST['LastName'];
$Suffix=$_POST['Suffix'];
$Gender=$_POST['Gender'];
$Age=$_POST['Age'];
$Weight=$_POST['Weight'];
$ExerciseExperience=$_POST['ExerciseExperience'];
$FitnessGoal=$_POST['FitnessGoal'];
$Email=$_POST['Email'];

$db_host='****';
$db_username='********';
$db_password='******';
$db_name='********';
$con = mysqli_connect('$db_host','db_username','$db_password','$db_name' )
or
die("ERROR" . mysqli_error($con));

$sqlinsert = "INSERT INTO Client Info (FirstName, MI, LastName, Suffix, Gender, Age, Weight, ExerciseExperience, FitnessGoal, Email) VALUES ( '$FirstName', '$MI', '$LastName', '$Suffix', '$Gender', '$Age', '$Weight', '$ExerciseExperience', '$FitnessGoal', '$Email' )" ;

if(!validEmail($email))
{
echo "<p>Please Go Back and enter a valid e-mail address</p>\n";
$badinput = "<p>we need your valid email!l</p>\n";
echo $badinput;
die ("Use the BACK button ! ");
}

if(empty($FirstName) || (empty($LastName) ||empty($Gender) ||empty($Age) ||empty($Weight) || empty($ExerciseExperience) ||empty($FitnessGoal)))
{
echo "<p>You have not completed all fields, please Go Back and fill out all the fields</p>\n";
die ("Use the BACK button ! ");
}

mysql_query($sqlinsert);


}
?>

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 719
Joined: Mon Feb 22, 2016 5:52 pm

Mon Jan 29, 2018 12:17 pm

Sorry, but I've got bad news for you; the script that you have is pretty much all wrong and not worth saving:

your html is badly formed and incorrect, a browser can normally handle to a greater or lesser extent much of it, but you will probably have a few issues when looking at it using different browsers

you're mixing MySQL and MySQLi, this is the computer equivalent of using a chisel as a screwdriver - it's not the same thing although it may look similar from some angles

When you are using database data, you need to have it before you fill your html out; what you have done is try to print a form out and then fill it out so that you can print it (you may need to read that a couple of times!).

How to proceed, that's up to you and why you are doing this:

are doing it for your own company (in which case is speed an issue)
out of interest
as a means to learn web stuff
or something else

?

I will offer what I think is your best course of action and what help I can offer depending on your answer.

saved4106
New php-forum User
New php-forum User
Posts: 5
Joined: Mon Jan 29, 2018 1:27 am

Mon Jan 29, 2018 12:26 pm

Hyper,

Im working on this for 2 reasons 1 i love learning new things and this is something ive always been interested in. I am also wprking on a startup business and saw an opportunity to kill two birds with one stone.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 719
Joined: Mon Feb 22, 2016 5:52 pm

Mon Jan 29, 2018 12:32 pm

What urgency do you have to complete the project?

saved4106
New php-forum User
New php-forum User
Posts: 5
Joined: Mon Jan 29, 2018 1:27 am

Mon Jan 29, 2018 12:44 pm

Not with too much urgency

User avatar
zaim
New php-forum User
New php-forum User
Posts: 29
Joined: Sat Jan 06, 2018 9:05 am
Location: Kedah, Malaysia.

Mon Jan 29, 2018 12:51 pm

Hello, there.

I am agreed with hyper on some matters but I also believe people need support and assist in order to improve.

Well, I want to be the guy who wanted to help you improve. However, I'm afraid that I would not be able to help you to explain everything. I would simplify this;

1) You are mixing a lot of stuff in a single code, making it look unnecessarily a mess.
2) You should learn step-by-step without jumping into a complicated coding just because it looks cool. Cool things takes time.

For your coding, I have reviewed it. It is good to be honest, just need some CSS ( HTML and PHP is priority, no need to rush for CSS ).

1) Your 'navigation bar' is bad, I would suggest to use nav tag which will consist of ul and li tag. You can search for it.
2) You gave your form an action but at the same time you also use 'isset' for your submit button inside the HTML file. Choose either one, I would use 'action' rather than 'isset'. Makes my code looks smooth and simple.
3) What those div tags are used for? Why it is so many like hell?!
4) Inputs using checkbox or radio are not same like a text input. Bear that in mind.
5) Instead of using label tag I would recommend paragraph <p> tag. Well, I love how paragraph tag is so easy to manipulate.
6) Instead of using echo to check if input or email is not valid. Why not use required attribute instead? It is more flexible!
7) Like hyper said, use mysqli_* instead
8. I hate to say this but when I used to be a beginner, people always told me this. You should sanitize your inputs, against SQL Injection.
9) Like I said about the inputs, checkbox and radio button values, I would recommend you using 'enum' inside the database.

Overall, I would say, most of your problem is pretty easy (if you search, mainly StackOverflow forum) and the answers for all of your problem actually has already been answered, not once, thousand times. :)
Sorry if my answers aren't top notch but I will try my best to help anyone in need. :oops:

saved4106
New php-forum User
New php-forum User
Posts: 5
Joined: Mon Jan 29, 2018 1:27 am

Mon Jan 29, 2018 1:05 pm

Ziam, thank you ive perused stack overflow before but knowing my big issues will help a lot.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 719
Joined: Mon Feb 22, 2016 5:52 pm

Mon Jan 29, 2018 1:15 pm

Ok, lesson number one:

Learn to use a html validator:

Copy your html code from this [ code ] block and paste it into the box on the above link above -

Code: Select all

<html>
<body>
<head>
<title>GlidewellFitness </title>
<link rel="stylesheet" type="text/css"
href="css/homepage-css.css" />
<h1><img src="images/Glidewell-01.png"alt="" width="500" height="200" /></h1>
</head>
<body>
<p><a href="index.html">home</a> <a href="sign-up.html">sign up</a> <a href="plans.html">plans</a> <a href="contact trainer.html">contact</a>
<form action="clientinfo.php" method="post" name="client info">
<div>
<label for="*First Name" class="inputs"><b>*First Name:</b></label>
<input type="text" id="*First Name" name="FirstName" size="15" maxlength="35" class="boxes"/>
</div>
<div>
<label for="M.I." class="inputs"><b>M.I.:</b></label>
<input type="text" id="M.I." name="MI" size="15" maxlength="5" class="boxes" />
</div>
<div>
<label for="LastName" class="inputs"><b>*Last Name:</b></label>
<input type="text" name="*Last Name" size="15" maxlength="40" class="boxes"/>
</div>
<div>
<label for="Suffix" class="inputs"><b>Suffix:</b></label>
<input type="text" name="Suffix" size="15" maxlength="30" class="boxes" />
</div>
<div>
<label for="gender" class="inputs"><b>Gender:</b></label>
<input type="radio" name="M/F" value="M" /> M
<input type="radio" name="M/F" value="F" /> F
</div>
<div>
<label for="Age" class="inputs"><b>Age:</b></label>
<select name="age" class="boxes">
<option value="12-20">16-20</option>
<option value="21-30">21-30</option>
<option value="31-40">31-40</option>
<option value="41-49">41-49</option>
<option value="50+">50+</option>
</select>
</div>
<div>
<label for="weight" class="inputs"><b>Weight:</b></label>
<input type="text" name="Weight" size="15" maxlength="40" class="boxes" />
</div>
<div>
<label for="Exercise Experience" class="inputs"><b>Exercise Experience:</b></label>
<select name="ExerciseExperience" class="boxes">
<option value="none">none</option>
<option value="moderate">moderate</option>
<option value="extensive">exstensive</option>
</select>
</div>
<div>
<label for="FitnessGoal" class="inputs"><b>Fitness Goal:</b></label>
<select name="Fitness Goal" class="boxes">
<option value="weight loss"> weight loss</option>
<option value="strength increase"> strength increase</option>
<option value="muscle gain"> muscle gain</option>
</select>
</div>
<div>
<label for="Email" class="inputs"><b>Email:</b></label>
<input type="text" name="Email" size="15" maxlength="40" class="boxes"/>
</div>
<input type="submit" name="client info" value="Submit">
</for>
</html>
Although there are a lot of errors, don't be despondent, you're learning; just take note that this is html code only - a html validator only checks html, not PHP or SQL.

saved4106
New php-forum User
New php-forum User
Posts: 5
Joined: Mon Jan 29, 2018 1:27 am

Mon Feb 05, 2018 3:41 pm

hyper,
Thank you for showing me the HTML validator. that really helped with a lot of issues i didnt see or even realize i had. i also revised my php code as follows. Now however i get still get the Error statement echoed from th MYSQLI_query at the end. What am i Missing? all help is much appreciated.

Code: Select all

 <?php 

    $conn = mysqli_connect('**********', '********', '*******' );
   if (!$conn)
   {
        echo 'Not Connected To Server';
   }
   $db_select = mysqli_select_db($conn, 'glidewellfitness');
   if (!$db_select)
   {
       echo 'Database Not Selected';
   }
$FirstName=$_POST['FirstName'];
$MI = $_POST['MI'];
$LastName=$_POST['LastName'];
$Suffix=$_POST['Suffix'];
$Gender=$_POST['Gender'];
$Age=$_POST['Age'];
$Weight=$_POST['Weight'];
$ExerciseExperience=$_POST['ExerciseExperience'];
$FitnessGoal=$_POST['FitnessGoal'];
$Email=$_POST['Email'];  
        
$sql = ("INSERT INTO 'Client Info' (`FirstName`, `MI`, `LastName`, `Suffix`, `Gender`, `Age`, `Weight`, `ExerciseExperience`, `FitnessGoal`, `Email`)  VALUES ( '$FirstName', '$MI', '$LastName', '$Suffix', '$Gender', '$Age', '$Weight', '$ExerciseExperience', '$FitnessGoal', '$Email' )"); 

mysqli_query($conn, $sql);

 if(!mysqli_query($conn, $sql))
   {
    echo 'ERROR';
} 
else
{
    echo 'success';   
}

$conn->close();
?>

User avatar
zaim
New php-forum User
New php-forum User
Posts: 29
Joined: Sat Jan 06, 2018 9:05 am
Location: Kedah, Malaysia.

Mon Feb 05, 2018 7:51 pm

Code: Select all

$sql = ("INSERT INTO 'Client Info' (`FirstName`, `MI`, `LastName`, `Suffix`, `Gender`, `Age`, `Weight`, `ExerciseExperience`, `FitnessGoal`, `Email`)  VALUES ( '$FirstName', '$MI', '$LastName', '$Suffix', '$Gender', '$Age', '$Weight', '$ExerciseExperience', '$FitnessGoal', '$Email' )"); 
You do not need to put a bracket before and after the double-quotes.

Code: Select all

$sql = "INSERT INTO 'Client Info (`FirstName`, `MI`, `LastName`, `Suffix`, `Gender`, `Age`, `Weight`, `ExerciseExperience`, `FitnessGoal`, `Email`)  VALUES ( '$FirstName', '$MI', '$LastName', '$Suffix', '$Gender', '$Age', '$Weight', '$ExerciseExperience', '$FitnessGoal', '$Email' )"; 
You can save your query as a variable.

Code: Select all

$query = mysqli_query( $conn, $sql );

if( !$query )
{
	echo "Error";
}
else
{
	echo "Success";
}
Sorry if my answers aren't top notch but I will try my best to help anyone in need. :oops:

Post Reply