简体   繁体   English

PHP - 不能在数据库中存储记录

[英]PHP - Cant store record inside database

I have created a html file called contact.html and connectivity.php . 我创建了一个名为contact.htmlconnectivity.php的html文件。 Inside the contact.html I set form action="Conectivity.php" . contact.html里面我设置了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 . 但是,当我运行contact.html并单击“发送”按钮时,它应该将记录保存在我的数据库中,但是当我单击该按钮时,它只会向我显示connectivity.php的整个代码。

Here is my code in connectivity.php : 这是我在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. 你说“它只显示了连接内部的整个代码。”这听起来好像你没有安装Web服务器并且看到PHP代码,因为它没有执行你的脚本。 You need to install a web server. 您需要安装Web服务器。

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. 注意:如果您访问file:///foo/Connectivity.php这样的file:///foo/Connectivity.php ,您也会file:///foo/Connectivity.php此问题 - 必须通过HTTP才能让Web服务器执行PHP。 You must access the file via HTTP, like http://localhost/Connectivity.php or whatever your local server name is instead of localhost. 您必须通过HTTP访问该文件,例如http://localhost/Connectivity.php或您的本地服务器名称而不是localhost。

Please also check your spelling. 还请检查你的拼写。 Your question says, i set form action="conectivity.php" . 你的问题是, i set form action="conectivity.php" Your file is named Connectivity.php -- please confirm that you spelled it correctly in the <form> tag. 您的文件名为Connectivity.php - 请确认您在<form>标记中拼写正确。

Also, don't use mysql_* - use MySQLi or PDO instead. 另外, 不要使用mysql_ * - 而是使用MySQLi或PDO。 You must use prepared statements; 必须使用准备好的陈述; your current code is extremely vulnerable to SQL injection attacks. 您当前的代码极易受到SQL注入攻击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM