I have created a html file called contact.html
and connectivity.php
. Inside the contact.html
I set form action="Conectivity.php"
. However,when I run contact.html
and click send button, it should save the record inside my database, but when i clicked the button it only show me the entire code inside connectivity.php
.
Here is my code in connectivity.php
:
<?php
define('DB_HOST', 'localhost');
define('DB_NAME', 'practice');
define('DB_USER','root');
define('DB_PASSWORD','');
$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die("Failed to connect to MySQL: " . mysql_error());
$db=mysql_select_db(DB_NAME,$con) or die("Failed to connect to MySQL: " . mysql_error());
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$query = "INSERT INTO contact (contactName,contactEmail,message)VALUES('$name','$email','$message')";
$result = mysql_query($query);
if($result)
{
echo "Successfully updated database";
}
else
{
die('Error: '.mysql_error($con));
}
mysql_close($con);
?>
You say "it only show me the entire code inside connectivity.php" -- it sounds like you don't have a web server installed and are seeing PHP code, because it's not executing your script. You need to install a web server.
Note: if you access a file like file:///foo/Connectivity.php
, you will also have this problem -- it has to be over HTTP for the web server to execute the PHP. You must access the file via HTTP, like http://localhost/Connectivity.php
or whatever your local server name is instead of localhost.
Please also check your spelling. Your question says, i set form action="conectivity.php"
. Your file is named Connectivity.php
-- please confirm that you spelled it correctly in the <form>
tag.
Also, don't use mysql_* - use MySQLi or PDO instead. You must use prepared statements; your current code is extremely vulnerable to SQL injection attacks.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.