1

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);
?>
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
JK9
  • 370
  • 2
  • 7
  • 26
  • 1
    Are you actually running a local web server, or just opening the HTML file directly in your web browser? Seeing the full PHP code implies that your PHP was never executed. – Michael Berkowski Dec 09 '13 at 15:11
  • 1
    **By building SQL statements with outside variables, you are leaving yourself wide open to SQL injection attacks.** Also, any input data with single quotes in it, like a name of "O'Malley", will blow up your SQL query. Please learn about using parametrized queries, preferably with the PDO module, to protect your web app. http://bobby-tables.com/php has examples to get you started, and [this question](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) has many examples in detail. – Andy Lester Dec 09 '13 at 15:16
  • @MichaelBerkowski i have installed xampp and create a database inside phpMyAdmin – JK9 Dec 09 '13 at 15:28
  • Just having the web server present doesn't make it active. Are you accessing the file via something like `http://localhost/contact.html`? If it looks like `file:///path/to/contact.html` then the web server isn't processing it. – Michael Berkowski Dec 09 '13 at 15:41
  • @MichaelBerkowski opps~it does look like file:///path/to/contact.html.How can imake it to accessing via http://localhost/contact.html? – JK9 Dec 09 '13 at 15:43
  • Don't know- follow the directions for xampp. You need to put your HTML and PHP files in whatever directory xampp expects the web server DocumentRoot to be, but I don't know what that is. When you get them there, access `http://localhost/contact.html`. Meanwhile, try going to `http://localhost` and see if xampp puts a temporary file there with further instructions. – Michael Berkowski Dec 09 '13 at 15:45

1 Answers1

3

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.

Community
  • 1
  • 1
elixenide
  • 44,308
  • 16
  • 74
  • 100
  • i do installed a web server called xampp but it still doesnt work. – JK9 Dec 09 '13 at 15:25
  • Create a new page, `test.php`, and put the following code in it: `` Browse to that page. If you see anything other than `Foo`, then either (1) you aren't accessing the page correctly or (2) xampp isn't running correctly. – elixenide Dec 09 '13 at 15:37
  • I still see the entire code.How can i accessing the page correctly because i think the xampp is running correctly.Maybe is the accessing problem? – JK9 Dec 09 '13 at 15:42
  • Then where should i put the file test.php to? – JK9 Dec 09 '13 at 15:44
  • It needs to be in whatever directory your web server is using as its home directory. What URL are *you* using to access it? – elixenide Dec 09 '13 at 15:45
  • My accessing url is something like this file:///path/to/contact.html – JK9 Dec 09 '13 at 15:47
  • Please see my note above -- the problem is accessing it via `file:///` -- you *must* use `http://` (or `https://` if you have an SSL certificate installed). – elixenide Dec 09 '13 at 15:52
  • Thank you, I have solved the problem and i will use mysqli instead of mysql. I am totally new in this so thank you for all the advices. – JK9 Dec 09 '13 at 15:55