0

我正在尝试将用户信息从用户输入表单添加到数据库中。表单中的数据正在被识别,如果我为所有输入的变量测试打印语句,则会显示信息。但是,当它通过插入语句时,它会丢失第一列数据(姓氏)并将所有其他数据推入错误的列。

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

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

谢谢!

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

2 回答 2

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);
于 2012-08-21T23:09:33.177 回答
0

我认为很明显电话之间存在不匹配:

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

和实际签名:

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

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

于 2012-08-21T23:09:47.633 回答