我正在向我的 PHP 文件发送变量以向数据库中添加内容,但由于某种原因,我的数据库要么被空白填充,要么根本不添加到数据库中。
客户.php
<script type="text/javascript">
function addCustomerFunc(add_LN,add_FN,add_PN,add_DOB)
{
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("show_label").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("POST","addCustomer.php",true);
xmlhttp.send("add_LN="+add_LN+"&add_FN="+add_FN+"&add_PN="+add_PN+"&add_DOB="+add_DOB);
}
</script>
<p>Add New Customer:</p>
<div align="center">
<table width="337" border="1">
<tr>
<td width="154"><p>Last Name:</p>
<p>First Name:</p>
<p>Phone Number:</p>
<p>Date of Birth:</p>
<p> </p></td>
<td width="167"><p align="center">
<form>
<input type="text" name="add_LN" id="add_LN" />
<br/><br/>
<input type="text" name="add_FN" id="add_FN" />
<br /><br />
<input type="text" name="add_PN" id="add_PN" />
<br /><br />
<input type="text" name="add_DOB" id="add_DOB" />
<br /><br />
<input type="submit" name="add_Customer" id="New_Customer_Form" value="Add Customer" onClick="addCustomerFunc(this.add_LN, this.add_FN, this.add_PN, this.add_DOB); return false;"/>
</form>
<div id="show_label"/>
</p>
</td>
</tr>
</table>
</div>
在 addCustomer.php
<?php
$username="*****";
$password="*****";
$database="*****";
if (isset ($_POST['add_LN']))
$lastName=$_POST['add_LN'];
else
die("Last Name not passed in POST");
if (isset ($_POST['add_FN']))
$firstName=$_POST['add_FN'];
else
die ("First Name not passed in POST");
if (isset ( $_POST['add_PN']))
$phone=$_POST['add_PN'];
else
die("Phone Number not passed in POST");
if (isset ($_POST['add_DOB']))
$dob=$_POST['add_DOB'];
else
die("Date of Birth not passed in Post");
mysql_connect("dbs4.cpsc.ucalgary.ca",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query = "INSERT INTO customer (last_name, first_name, phone_no, date_of_birth, membership) VALUES ('$lastName', '$firstName', '$phone', '$dob', 'T')";
if (mysql_query($query)){
echo "Thanks";
} else
{
echo "Failed to insert customer into database";
}
mysql_close();
?>
我被告知要使用 ISSET 函数,但变量似乎永远不会更改为非空值。我正在使用 PHP 5.3.5。
编辑:我在 show_label 中不断收到“Last Name not pass in POST”,因为它一直将 add_LN 视为空白。