在列上插入语句&;缺少数据


Insert statement pushing over a column & missing data

我正试图从用户输入表单将用户信息添加到数据库中。表单中的数据正在被识别,如果我为所有输入的变量测试打印语句,信息就会显示出来。然而,当它执行insert语句时,它缺少第一列数据(lastname),并将所有其他数据推送到错误的列中。

姓氏显示在名字列中,名字显示在联系人类型中,联系人类型显示在联系人信息中,联系人信息显示在城市中,评论不会显示在任何地方,添加的日期是正确的。

我已经添加了最相关的本地代码,但如果您需要通用函数,我也可以添加。底部还有表格结构。

谢谢!

    <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 ;

您对方法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);

我认为调用之间显然不匹配

 insertData($db, $mylastname, $myfirstname, $mycontact_type, $mycontactinfo, $mycity, $mycomments);

和实际签名:

 function insertData($mylastname, $myfirstname, $mycontact_type, $mycontactinfo, $mycity, $mycomments)

从调用中删除第一个参数$db