/    Sign up×
Community /Pin to ProfileBookmark

How to get results in the same page

Can php be embedded within html?
Is the following program correct?

[code=php]<html>
<head>
<title>add.php</title>
</head>
<body>

<form action=”add.php” method=”POST”>
Enter emp id: <input type=”text” name=”eid”><br>
Enter emp name: <input type=”text” name=”enm”><br>
Enter salary: <input type=”text” name=”sal”><br>
<input type=”submit” value=”Enter”><br>
</form>
<?php
extract($_REQUEST);
$con=mysql_connect(‘localhost’,’root’,”);
if(!$con)
{
die(“Error:”.mysql_error());
}
$db=mysql_select_db(‘db1’,$con)
or die(“error:”.mysql_error());
$sql=”insert into emp(emp_id,ename,salary) values(‘$eid’,’$enm’,’$sal’)”;
if(!mysql_query($sql,$con))
{
die(‘Error:’.mysql_error());
}
else
{
echo “Record Saved”;
}
?>
</body>
</html>[/code]

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 02.2012 — Yes, that sort of thing is often done. The file will normally need to have a ".php" suffix instead of ".html", however.
Copy linkTweet thisAlerts:
@jitcauthorJan 02.2012 — No you are not correct @Nogdog.I already saved the file as "add.php" [Hope u have seen the title section].

But still the problem when it reloads it automatically enters a duplicate data or a blank data to the table "emp".

How to resolve that? :eek:
Copy linkTweet thisAlerts:
@NogDogJan 02.2012 — You should add some logic to process the form data only if the relevant $_POST elements exist (and are not empty if they are required).
Copy linkTweet thisAlerts:
@jitcauthorJan 02.2012 — Ur answer is so vague.
Copy linkTweet thisAlerts:
@DasherJan 02.2012 — In simplest form;

[code=php]if ($_POST['submit']) == "Enter" {

//rest of your php code here....

}[/code]
Copy linkTweet thisAlerts:
@jitcauthorJan 03.2012 — Well Dasher I tried this code as per ur suggestions-
[code=php]<html>
<head>
<title>Emp Add</title>
</head>
<body>

<form action="add.php" method="POST">
Enter emp id: <input type="text" name="eid"><br>
Enter emp name: <input type="text" name="enm"><br>
Enter salary: <input type="text" name="sal"><br>
<input type="submit" value="Enter"><br>
</form>
<?php
if (($_POST['submit']) == "Enter") {

if(!empty( $_POST['eid'] ) && !empty( $_POST['enm'] ) && !empty( $_POST['sal'] )){
$eid = mysql_real_escape_string( $_POST['eid'] );
$enm = mysql_real_escape_string( $_POST['enm'] );
$sal = mysql_real_escape_string( $_POST['sal'] );

$con=mysql_connect('localhost','root','');
if(!$con)
{
die("Error:".mysql_error());
}
$db=mysql_select_db('db1',$con)
or die("error:".mysql_error());

$sql="insert into emp(emp_id,ename,salary) values('$eid','$enm','$sal')";

if(!mysql_query($sql,$con))
{
die('Error:'.mysql_error());
}
else
{
echo "Record Saved";
}
mysql_close($con);
}
?>
</body>
</html>[/code]

The error I get is this-

( ! ) Parse error: syntax error, unexpected $end in D:wampwww...add2.php on line 43
Copy linkTweet thisAlerts:
@hamburglarJan 03.2012 — You did not close the IF statement started on line 14.
Copy linkTweet thisAlerts:
@LaffinTooJan 07.2012 — Corrections:

No, PHP can not be embedded into HTML

HTML can be embedded into PHP, even if there is no PHP code in the HTML (You need the php tags to drop in and out of php code)

if($_POST['submit']
if they load this page directly, you will get an undefined variable error.
if(isset($_POST['submit']))
is a better solution
×

Success!

Help @jitc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...