How to execute insert query without actually opening the php file and
showing a blank page?
I have a main form which has a submit button that when pressed it inserts
employee data using an insert query, but when I press the submit button in
the form it opens a blank page because there is no visible content in the
resulting form. How can i get it so that when I press the submit button it
just enters the data but remains on the same form and doesn't open a blank
page. Is this even possible? Will I have to simply copy the style of
index.php into the process.php file? Please help I am new and want to
learn.
Here is my code (index.php)
<html>
<head>
</head>
<body>
<form action="process.php" method="post" style="position:relative;
top:200px;left:150px">
First Name: <input type="text"
style="position:relative;left:14px"name="firstName"><br>
Last Name: <input type="text"
style="position:relative;left:15px"name="lastName"><br>
<input
type="submit"style="position:relative;left:88px"name="submitButton"
><br>
</form>
</body>
</html>
Here is the resulting php file where the query is executed (process.php)
<html>
<body>
<?php
$myServer = "MyComputerName\SQLEXPRESS";
$myUser = "Username";
$myPass = "password";
$myDB = "Northwind";
//connection to the database
$dbhandle = mssql_connect($myServer, $myUser, $myPass)
or die("Couldn't connect to SQL Server on $myServer");
//select a database to work with
$selected = mssql_select_db($myDB, $dbhandle)
or die("Couldn't open database $myDB");
$query = "Insert into Employees values
('".$_POST["firstName"]."','".$_POST["lastName"]."')";
mssql_query($query);
mssql_close();
?>
</body>
</html>
No comments:
Post a Comment