Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Insert record in PHP

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Insert record in PHP

Postby Sunny » Sun Oct 27, 2013 10:33 pm

hi

In the below code it is storing emp_name as Blank & Date as 00-0000-00 . Why is it so.

<html>
<head>
<title>Add New Record in MySQL Database</title>
</head>
<body>
<?php
if(isset($_POST['add']))
{
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
die('Could not connect: ' . mysql_error());
}

if($_POST['emp_name'] = "")
{
echo "Name Cannot Be Blank\n";
}
else
{
$emp_name = $_POST['emp_name'];
$emp_age = $_POST['emp_age'];
$emp_dob = $_POST['emp_dob'];
$emp_email = $_POST['emp_email'];

echo $_POST['emp_name'];
echo $emp_name ;

$sql="INSERT INTO emp_master (emp_name,emp_age,emp_dob,emp_email) VALUES ('$emp_name','$emp_age','$emp_dob','$emp_email');";
mysql_select_db('test');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
}
}
?>

<form method="post" action="<?php $_PHP_SELF ?>">
<table width="500" border="1" cellspacing="1" cellpadding="2">
<tr>
<td width="150">Employee Name</td>
<td><input name="emp_name" type="text" id="emp_name"></td>
</tr>
<tr>
<td width="150">Employee Age</td>
<td><input name="emp_age" type="text" id="emp_age"></td>
</tr>
<tr>
<td width="150">Employee DOB</td>
<td><input name="emp_dob" type="text" id="emp_dob"></td>
</tr>
<tr>
<tr>
<td width="150">Employee Email</td>
<td><input name="emp_email" type="text" id="emp_email"></td>
</tr>
<tr>

<td width="150"> </td>
<td> </td>
</tr>
<tr>
<td width="150"> </td>
<td>
<input name="add" type="submit" id="add" value="Add Employee">
</td>
</tr>
</table>
</form>
</body>
</html>

Thanks
Sunny
New php-forum User
New php-forum User
 
Posts: 23
Joined: Wed Nov 30, 2011 6:51 am

Re: Insert record in PHP

Postby tiger » Sun Oct 27, 2013 10:43 pm

Hello Sunny

I think you forgot to put the `` before and after the table name

try this :

Code: Select all
$sql="INSERT INTO `emp_master` (`emp_name`, `emp_age`, `emp_dob`, `emp_email`) VALUES ('$emp_name','$emp_age','$emp_dob','$emp_email');";
tiger
New php-forum User
New php-forum User
 
Posts: 16
Joined: Thu Jun 23, 2011 9:28 pm

Re: Insert record in PHP

Postby Sunny » Sun Oct 27, 2013 10:53 pm

Hi

It is still storing emp_name as Blank & Date as 00-0000-00

Thanks
Sunny
New php-forum User
New php-forum User
 
Posts: 23
Joined: Wed Nov 30, 2011 6:51 am

Re: Insert record in PHP

Postby tiger » Sun Oct 27, 2013 11:09 pm

check this one

Code: Select all
if($_POST['emp_name'] = "")


it should be

Code: Select all
if($_POST['emp_name'] == "")


if that did not fix it, then sorry for not being able to help :)
tiger
New php-forum User
New php-forum User
 
Posts: 16
Joined: Thu Jun 23, 2011 9:28 pm

Re: Insert record in PHP

Postby Sunny » Sun Oct 27, 2013 11:26 pm

Hi Tiger

Done . Why it is storing Data as 0000-00-00 . I want it in dd-mm-yyyy
What is the difference between = AND ==

Thanks
Sunny
New php-forum User
New php-forum User
 
Posts: 23
Joined: Wed Nov 30, 2011 6:51 am

Re: Insert record in PHP

Postby tiger » Sun Oct 27, 2013 11:50 pm

ok good

I'm not sure about the date. You may want to check on the date() function here http://php.net/manual/en/function.date.php


as for the difference between = and ==

= means that you assign a variable. For example:
$name = "Tiger";
here I assigned Tiger to the $name variable.

== means equals to something. For example:
if ($name == "Tiger")
here I check if the value of the variable whether or not it is equal to "Tiger".
tiger
New php-forum User
New php-forum User
 
Posts: 16
Joined: Thu Jun 23, 2011 9:28 pm

Re: Insert record in PHP

Postby Sunny » Mon Oct 28, 2013 12:12 am

Thanks
Sunny
New php-forum User
New php-forum User
 
Posts: 23
Joined: Wed Nov 30, 2011 6:51 am

Re: Insert record in PHP

Postby simplypixie » Mon Oct 28, 2013 12:17 am

You cannot store dates as dd-mm-yyyy in the database as the database date format is yyyy-mm-dd. Therefore you need to convert dates from dd-mm-yyyy to yyyy-mm-dd before you insert or update your db and vice versa to display the date in your required format when displaying.

Based on the way you are coding your PHP the easiest way to do this is

To convert for database
Code: Select all
$emp_dob = date('Y-m-d', strtotime($_POST['emp_dob']));


To convert from database to required display (using $row as the variable your query result is assigned to so may need chaning for your actual requirements)
Code: Select all
$emp_dob = date('d-m-Y', strtotime($row['emp_dob']));


As @tiger has pointed out, take a look at the PHP date functions to see all the ways to format dates as you need. I am not sure if you use or understand OOP but there is actually an in-built date class with PHP which you can use to format dates as well http://php.net/manual/en/datetime.construct.php
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire

Re: Insert record in PHP

Postby johnj » Mon Oct 28, 2013 12:18 am

print the emp_name and dob to see if it has a value.
johnj
php-forum Super User
php-forum Super User
 
Posts: 1805
Joined: Thu Mar 10, 2011 5:07 pm

Re: Insert record in PHP

Postby tiger » Mon Oct 28, 2013 12:32 am

simplypixie wrote:You cannot store dates as dd-mm-yyyy in the database as the database date format is yyyy-mm-dd. Therefore you need to convert dates from dd-mm-yyyy to yyyy-mm-dd before you insert or update your db and vice versa to display the date in your required format when displaying.

Based on the way you are coding your PHP the easiest way to do this is

To convert for database
Code: Select all
$emp_dob = date('Y-m-d', strtotime($_POST['emp_dob']));


To convert from database to required display (using $row as the variable your query result is assigned to so may need chaning for your actual requirements)
Code: Select all
$emp_dob = date('d-m-Y', strtotime($row['emp_dob']));


As @tiger has pointed out, take a look at the PHP date functions to see all the ways to format dates as you need. I am not sure if you use or understand OOP but there is actually an in-built date class with PHP which you can use to format dates as well http://php.net/manual/en/datetime.construct.php


Great info to know. Thanks simplypixie :D
tiger
New php-forum User
New php-forum User
 
Posts: 16
Joined: Thu Jun 23, 2011 9:28 pm

Re: Insert record in PHP

Postby Sunny » Mon Oct 28, 2013 1:12 am

Hi Tiger

When i enter value as 20/05/1978 and then use the below code
$emp_dob = date('Y-m-d', strtotime($_POST['emp_dob']));
it shows as 1970-01-01 . Why is it so . It should display 20/05/1978. I have DataType as Date in Database.

Thanks
Sunny
New php-forum User
New php-forum User
 
Posts: 23
Joined: Wed Nov 30, 2011 6:51 am

Re: Insert record in PHP

Postby simplypixie » Mon Oct 28, 2013 1:26 am

Sorry, it is so long since I have formatted dates this way (i.e. without using the inbuilt php datetime class) that I forgot that if you use slashes in a date that strtotime will see it as a US date format and therefore will think your format of dd/mm/yyyy is actually mm/dd/yyyy and therefore return an incrrect time string.

In order to get your dd/mm/yyyy date to be reformatted by strtotime it first needs to be changed to dd-mm-yyyy

Code: Select all
$format_date = str_replace('/', '-', $_POST['emp_dob']);
$emp_dob = date('Y-m-d', strtotime($format_date);
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire

Re: Insert record in PHP

Postby Sunny » Mon Oct 28, 2013 1:33 am

Hi

Sorry , Date is entered in this format 20-05-1978

Thanks
Sunny
New php-forum User
New php-forum User
 
Posts: 23
Joined: Wed Nov 30, 2011 6:51 am

Re: Insert record in PHP

Postby simplypixie » Mon Oct 28, 2013 1:43 am

In which case my first line of code to convert for the database should work unless the date is not being passed through correctly in the first instance
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire

Re: Insert record in PHP

Postby Sunny » Mon Oct 28, 2013 1:51 am

Hi

I have field with DataType Date . I want when user enters date in HTML form it should be stored in mysql

Thanks
Sunny
New php-forum User
New php-forum User
 
Posts: 23
Joined: Wed Nov 30, 2011 6:51 am

Re: Insert record in PHP

Postby simplypixie » Mon Oct 28, 2013 2:00 am

I know that and my answers are exactly what you need to do - I don't understand the problem?
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire

Re: Insert record in PHP

Postby tiger » Mon Oct 28, 2013 2:01 am

simplypixie wrote:Sorry, it is so long since I have formatted dates this way (i.e. without using the inbuilt php datetime class) that I forgot that if you use slashes in a date that strtotime will see it as a US date format and therefore will think your format of dd/mm/yyyy is actually mm/dd/yyyy and therefore return an incrrect time string.

In order to get your dd/mm/yyyy date to be reformatted by strtotime it first needs to be changed to dd-mm-yyyy

Code: Select all
$format_date = str_replace('/', '-', $_POST['emp_dob']);
$emp_dob = date('Y-m-d', strtotime($format_date);



I just noticed that there is a missing ) in the code above.

Code: Select all
$format_date = str_replace('/', '-', $_POST['emp_dob']);
$emp_dob = date('Y-m-d', strtotime($format_date));
tiger
New php-forum User
New php-forum User
 
Posts: 16
Joined: Thu Jun 23, 2011 9:28 pm

Re: Insert record in PHP

Postby simplypixie » Mon Oct 28, 2013 2:13 am

@tiger - you are correct, thank you for spotting
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire

Re: Insert record in PHP

Postby Sunny » Mon Oct 28, 2013 2:21 am

Hi Simplypixie

I have field with DataType as Date in mysql . I want if i enter 1970/04/10 in HTML form it should be saved in that field with Insert Statement . Please guide me what will be the insert Statement.

Thanks
Sunny
New php-forum User
New php-forum User
 
Posts: 23
Joined: Wed Nov 30, 2011 6:51 am

Re: Insert record in PHP

Postby simplypixie » Mon Oct 28, 2013 2:44 am

Sunny, I have answered this twice for you now and you keep changing your date format - so far you have said
I want it in dd-mm-yyyy
,
When i enter value as 20/05/1978
,
Date is entered in this format 20-05-1978
and
I want if i enter 1970/04/10
. That is 3 different date formats!

What EXACT format is the date when it is input into and posted from your form?

Once you clarify this I can help you apply it to the re-formatting code I have already provided you with
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire

Re: Insert record in PHP

Postby Sunny » Mon Oct 28, 2013 3:08 am

Hi simplypixie

I enter date in format 1978-04-12 . I want this value to be stored in Database. Please forget about other posts regarding this issue. I simple want that Date is entered in HTML form and it should be saved . Database field has DataType Date.

Thanks
Sunny
New php-forum User
New php-forum User
 
Posts: 23
Joined: Wed Nov 30, 2011 6:51 am

Re: Insert record in PHP

Postby simplypixie » Mon Oct 28, 2013 3:14 am

OK, is that the 4th December or the 12th April?
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire

Re: Insert record in PHP

Postby Sunny » Mon Oct 28, 2013 3:26 am

Hi

04 is the month
12 is the date

Thanks
Sunny
New php-forum User
New php-forum User
 
Posts: 23
Joined: Wed Nov 30, 2011 6:51 am

Re: Insert record in PHP

Postby simplypixie » Mon Oct 28, 2013 4:42 am

In which case I really don't understand your problem as it is already in the correct format for the date field in your database - you can just update without doing any changes or formatting.
User avatar
simplypixie
php-forum Active User
php-forum Active User
 
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot], Majestic-12 [Bot] and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron