我是 PHP 和使用 mysql 的完全菜鸟,所以你知道我在 HMTL 和 CSS 方面有丰富的经验。我只需要在我的网站上创建一个表格,将表格中的信息上传到我的数据库。问题是单击“提交”按钮只会打开一个空白选项卡,其中包含我的 .php 文件的地址,并显示一个空白的白色屏幕。.php 如下。
<?php
$hostname = "myHostName";
$username = "PreRegCustomers";
$dbname = "PreRegCustomers";
$password = "myPassword";
$usertable = "CustomerInfo";
mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname);
$sql = "INSERT INTO $usertable (firstName, lastName, streetAddress, city, state, zip, country, email, phone, badgeName)
VALUES ('$firstName', '$lastName', '$streetAddress', '$city', '$state', '$zip', '$country', '$email', '$phone', '$badgeName')";
$sql="INSERT INTO $usertable (firstName, lastName, streetAddress, city, state, zip, country, email, phone, badgeName)
VALUES ('".$_POST[firstName]."', '".$_POST[lastName]."', '".$_POST[streetAddress]."', '".$_POST[city]."', '".$_POST[state]."', '".$_POST[zip]."', '".$_POST[country]."', '".$_POST[email]."', '".$_POST[phone]."', '".$_POST[badgeName]."')";
?>
现在从我读到的内容来看,这通常是由代码中的某种错误引起的。这对我来说很难,因为我不太了解 PHP,而且页面中的几乎所有内容都是从其他人的代码中获取的。大部分来自代码的帮助来自 godaddy.com(托管网站和数据库的地方)。
我已经测试以确保支持并启用 PHP,并且确实如此。我有一个已经可以正常工作的表单邮件程序。我已经设置了一个 DNS,我尝试了多种不同的语法,我已经打电话给技术支持,看看他们是否有什么问题,我已经将我的网站从 windows 迁移到了 linux,我所做的每一件事都会导致完全相同的空白屏幕。我毫不怀疑,毕竟这将是一个很容易修复或明显显而易见的事情,但如果有人可以看看我错过了什么,我将非常感激。
我接受了一些发布的答案后的新代码。我仍然收到通知,它仍然没有在我的数据库中插入任何内容。
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$hostname = "myHostName";
$username = "PreRegCustomers";
$dbname = "PreRegCustomers";
$password = "myPassword";
$usertable = "CustomerInfo";
//connect to mysql
$link_id = mysql_connect($hostname, $username, $password);
if (!$link_id) {
die("Unable to connect to database! Please try again later. error:".mysql_errno());
}
//make sure your DB exists
if (!mysql_select_db($dbname)) die ("Connected to mysql but could not connect to the DB. error:".mysql_errno());
//avoid sql_injection
$firstName = mysql_real_escape_string($_POST['firstName']);
$lastName = mysql_real_escape_string($_POST['lastName']);
$streetAddress = mysql_real_escape_string($_POST['streetAddress']);
$city = mysql_real_escape_string($_POST['city']);
$state = mysql_real_escape_string($_POST['state']);
$zip = mysql_real_escape_string($_POST['zip']);
$country = mysql_real_escape_string($_POST['country']);
$email = mysql_real_escape_string($_POST['email']);
$phone = mysql_real_escape_string($_POST['phone']);
$badgeName = mysql_real_escape_string($_POST['badgeName']);
//write the query
$sql = "INSERT INTO $usertable
(firstName, lastName, streetAddress, city, state, zip, country, email, phone, badgeName)
VALUES ('$firstName', '$lastName', '$streetAddress', '$city', '$state', '$zip', '$country', '$email', '$phone', '$badgeName')";
//then you'll need to execute the query :)
mysql_query($sql);
?>