Ganesh Ganesh - 3 months ago 15
MySQL Question

Form values are not storing in database by using mvc architecture?

This is my code and it is not storing the values into the database... I am new to this mvc and i don't how to overcome that problem.. please help me to find solution for this.

Here is my HTML code:

<?PHP


Here i Included those files which is useful to connect and store values in database

require_once('Models/UsersModel.php');
require_once('DataObjects/User.php');
require_once('GenericClasses/GenericCollectionClass.php');


//If you are not submitting the form HTML will be directly shown
if (!isset($_POST['submit']))
{
?>

<body>
<table>

<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt"> First Name:</td>
<td bgcolor="#FFFFFF"> <input type='text' name='fname' id='fid' size="50" style="height:18px;" value='' maxlength="100" ></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt"> Last Name:</td>
<td bgcolor="#FFFFFF" > <input type='text' name='lname' id='lid' size="50" style=" height:18px; "value='' maxlength="100" ></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt"> Gender:</td>
<td bgcolor="#FFFFFF" > <input type='radio' name='gend' id='m' value='M' checked>Male <input type='radio' name='gend' id='f' value='F'>Female</td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt"> Phone Number:</td>
<td bgcolor="#FFFFFF" > <input type='number_format' name='phone' id='phno'size="50" style=" height:18px; " value=''></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt"> Work Experiance:</td>
<td bgcolor="#FFFFFF" >
<select name="exp" >
<option selected="" value="Default"> Select One </option>
<option value="Fresher"> Fresher </option>
<option value="Experiance"> Experiance</option>
</select>
</td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt">User Name:</td><td bgcolor="#FFFFFF"> <input type='txt' name='uname' id='uid'size="50" style=" height:18px; " value=''></td>
</tr>
<tr>
<td bgcolor="#FFFFFF" height="28" class="form_txt">Password:</td><td bgcolor="#FFFFFF"> <input type='password' name='pwd' id='pid' size="50" style=" height:18px; " value=''></td>
</tr>
</table>
<table align="center">
<tr>
<td><input name="enter" class="btn_login" type="submit" value="Submit" align="center"><input name="cancle" class="btn_login" type="reset" value="Cancle" valign="right" /></td>
</tr>
</table>
</div>
</form>
</div>
</div>


</body>
</html> <!--HTML Ended-->


Here i am getting values from form and post to db

<?PHP
}
else
{

If you are submitting the form insert the details into database

$fname=$_POST['fname'];
$lname=$_POST['lname'];
$gend =$_POST['gend'];
$phone=$_POST['phone'];
$exp=$_POST['exp'];
$uname=$_POST['uname'];
$pwd=$_POST['pwd'];

If (!(empty($fname) && empty($lname) && empty($gend) && empty($phone) && empty($exp) && empty($uname) && empty($pwd)))
{

$usermodel = new UsersModel();

$rowsCount = $usermodel->insertUserDetails($fname,$lname,$gend,$phone,$exp,$uname,$pwd);

echo $rowsCount;
if ($rowsCount == 0)
{
echo 'Not inserted';
} else
{
echo 'Inserted';
}
}

}


Here is DB connection Program **

<?php

defined('DATABASE_HOST') ? NULL : define('DATABASE_HOST', 'localhost');
defined('DATABASE_NAME') ? NULL : define('DATABASE_NAME', 'project');
defined('DATABASE_USER') ? NULL : define('DATABASE_USER', 'root');
defined('DATABASE_PASSWORD') ? NULL : define('DATABASE_PASSWORD', '');

?>


Here is my function for inserting data into the database

This is the funciton for inserting values into database in UserModel.php

public function insertUserDetails($fname,$lname,$gend,$phone,$exp,$uname,$pwd){
//Establising database connection to get all the users informatio'
$databse= new Database();
echo 'hai';
//Open the connection
$databse->openConnection();


// Build database query
$sql = "insert into users(fname,lname,gend,phone,exp,uname,pwd) VALUES('$fname','$lname','$gend','$phone','$exp','$uname','$pwd')";
echo $sql;
$rowsCount = $databse->executeDml($sql);

//Close the connection
$databse->closeConnection();

return $rowsCount;


}

Answer

First, add form tags and set your method to POST. After that you should change the name of your button into 'submit' instead of enter, or change the if statement into:

if (!isset($_POST['enter'])) 

Now it should work.