我正在尝试从用户输入表单将用户信息添加到数据库中。表单中的数据正在被识别,如果我测试所有输入的变量的打印语句,则显示信息。当它通过insert语句时,它缺少第一列数据(lastname)并将所有其他数据推入错误的列。
姓氏列在firstname列中,firstname以联系人类型结束,联系人类型以联系人信息结束,联系人信息最终在城市,评论不会显示在任何地方,添加日期是正确的。
我添加了最相关的本地代码,但如果您需要常用功能,我也可以添加。底部还有桌子结构。
谢谢!
<html>
<head>
<title>Submitted Guestbook Info</title>
<link rel="stylesheet" type="text/css" href="css/king.css" />
</head>
<body>
<img src="images/KingLogo.jpg"><br>
<div id="guestlist">
<?php
include "king_common_functions.php";
//*******************************************************
//Variables for Guestbook Submissions
//*******************************************************
//*******************************************************
//Call Functions
//*******************************************************
setUserInfo();
?>
<?php
function setUserInfo()
{
//******************************************************
//Validate Input Fields, Define Variables
//******************************************************
$mycontact_type = $_POST['mycontact'];
$mycity = $_POST['city'];
if (isset($_POST['mylastname']))
{
$mylastname = trim($_POST['mylastname']);
} else {
$mylastname ='';
}
if (isset($_POST['myfirstname']))
{
$myfirstname = trim($_POST['myfirstname']);
} else {
$myfirstname ='';
}
if (isset($_POST['mycontactinfo']))
{
$mycontactinfo = trim($_POST['mycontactinfo']);
} else {
$mycontactinfo ='';
}
if (isset($_POST['comments']))
{
$mycomments = trim($_POST['comments']);
} else {
$mycomments ='';
}
$rtnmsg = doGuestValidation($myfirstname, $mylastname, $mycontactinfo, $mycity);
if ($rtnmsg == '')
{
$rtncode = insertData($db, $mylastname, $myfirstname, $mycontact_type, $mycontactinfo, $mycity, $mycomments);
} else {
$rtncode = '';
print $rtnmsg;
print '<br />Go <a href ="assignment_6_guest_calc.php">BACK</a> and try again!';
}
}
function insertData($mylastname, $myfirstname, $mycontact_type, $mycontactinfo, $mycity, $mycomments)
{
$mydate = date("Y-m-d");
$db = connectDatabase();
$statement = "insert into u1585_guestbook (lastname, firstname, contact_type, contact_info, city, comments, date_added)
values ('".$mylastname."', '".$myfirstname."', '".$mycontact_type."', '".$mycontactinfo."', '".$mycity."', '".$mycomments."', '".$mydate."')";
$result = mysql_query($statement);
print "TEST: $mylastname"
if ($result)
{
print "<h4>Thank You! A representative will contact you soon.</h4>";
print "Information Submitted for: ".$myfirstname." ".$mylastname;
print "<br/><br/>Your name ".$mycontact_type. " is " .$mycontact_info;
print "<br/>and you are interested in living in ".$mycity;
print "<br/><br/>Our representative will review your comments below:";
print "<br/><br/><textarea name='comments' rows='7' cols='60' disabled='disabled' class='textdisabled'>";
print $mycomments;
print "</textarea>";
} else {
$errno = mysql_errno($db);
if ($errno == '1062') {
echo "<br>User is already in Guestbook: <br />".$mylastname.", ".$myfirstname;
} else {
echo("<h4>MySQL No: ".mysql_errno($result)."</h4>");
echo("<h4>MySQL Error: ".mysql_error($result)."</h4>");
echo("<h4>SQL: ".$statement."</h4>");
echo("<h4>MySQL Affected Rows: ".mysql_affected_rows($result)."</h4>");
}
return 'NotAdded';
}
}
?>
</div>
</body>
</html>
表格结构:
CREATE TABLE u1585_Guestbook (
guest_id int(11) NOT NULL AUTO_INCREMENT,
lastname varchar(40) NOT NULL,
firstname varchar(30) NOT NULL,
contact_type varchar(30) NOT NULL,
contact_info varchar(40) NOT NULL,
city varchar(40) NOT NULL,
comments varchar(200) NOT NULL,
date_added date NOT NULL,
PRIMARY KEY (guest_id)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
答案 0 :(得分:1)
您对方法insertData()
的调用是错误地传递参数:
$rtncode = insertData($db, $mylastname, $myfirstname, $mycontact_type, $mycontactinfo, $mycity, $mycomments);
...
function insertData($mylastname, $myfirstname, $mycontact_type, $mycontactinfo, $mycity, $mycomments) {
你传递$db
作为第一个参数,但函数的第一个参数应该是姓氏。只需更新通话即可删除$db
,您应该做得很好:
$rtncode = insertData($mylastname, $myfirstname, $mycontact_type, $mycontactinfo, $mycity, $mycomments);
答案 1 :(得分:0)
我认为很明显电话之间存在不匹配:
insertData($db, $mylastname, $myfirstname, $mycontact_type, $mycontactinfo, $mycity, $mycomments);
和实际签名:
function insertData($mylastname, $myfirstname, $mycontact_type, $mycontactinfo, $mycity, $mycomments)
从通话中删除第一个参数$db
。