我有一个简单的邮件列表,它只是一个电子邮件地址,但我决定添加一个名称列,但无法弄清楚出了什么问题。
当用户提交表单时,电子邮件地址会添加到电子邮件列中,但在名称列中它只是空白。此外,它每次都添加一个新的空白表格行。
这是表单的代码....
<form id="addressForm" action="index.php" method="get">
<p>
<input class="formname" type="text" name="name" id="name" value="Your Name" onfocus="this.value = this.value=='Your Name'?'':this.value;" onblur="this.value = this.value==''?'Your Name':this.value;">
<input class="formemail" type="text" name="address" id="address" value="Your E-mail" onfocus="this.value = this.value=='Your E-mail'?'':this.value;" onblur="this.value = this.value==''?'Your E-mail':this.value;">
<input class="formsubmit" type="submit" value="Sign Up" />
</p>
<p id="response"><?php echo(storeAddress()); ?></p>
</form>
这里是 php...
<?
require_once("dbConstants.php");
function storeAddress() {
$message = " ";
// Check for an email address in the query string
if( !isset($_GET['address']) ){
// No email address provided
}
else {
// Get details from the query string
$name = $_GET['name'];
$address = $_GET['address'];
// Validate Address
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$/i", $address)) {
$message = "<strong>Error</strong>: An invalid email address was provided.";
}
else {
// Connect to database
$con = mysql_connect(DBHOST ,DBUSER, DBPASS);
mysql_select_db(DBNAME, $con);
// Insert email address into mailinglist table
$result = mysql_query("INSERT INTO mailinglist SET name='" . $name . "'");
$result = mysql_query("INSERT INTO mailinglist SET email='" . $address . "'");
if(mysql_error()){
$message = "<strong>Error</strong>: There was an error storing your email address.";
}
else {
$message = "Thanks for signing up!";
}
}
}
return $message;
}
?>
将新行添加到 phpmyadmin 时,我使用了 SQL 查询
ALTER TABLE mailinglist ADD column_name name;
谁能指出创建额外行的代码有什么问题?