php - 插入语句推过列并丢失数据

标签 php mysql sql

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

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

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

谢谢!

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

关于php - 插入语句推过列并丢失数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12064269/

相关文章:

SQL 逻辑中的 Java 插入或更新

php - 使用mysql和php抓取没有订单的用户,简化了这个

sql - 创建存储过程

php - 如果关系不存在,Laravel 会得到默认值的 Eloquent 关系吗?

php - 如何在 Yii Framework 中获取当前路由器?

mysql - 将 MySQL Server 从版本 5.6.14 修补到 5.6.21

sql - 使用数据库字段值与 cakephp 中的保存字段相加

php - 在 sql 中多次使用相同的准备语句参数

php - 寻找最近的数组集

python - SQLAlchemy + MySQL 加入奇怪的行为