I am trying to add user info into a database from a user input form. The data from the form is being recognized and if I do test print statements for the variables for all inputs the information is showing up. When it goes through the insert statement though, it is missing the first column data (lastname) and pushing all the other data into the wrong columns.
The lastname ends up in the firstname column, firstname ends up in contact type, contact type ends up in contact info, contact info ends up in city, comments don’t show up anywhere, and date added is correct.
I have added the local code that is most relevant, but if you need the common functions I could also add. There is also the table structure at the bottom.
THANKS!
<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>
Table Structure:
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 ;
Your call to the method
insertData()is passing the parameters incorrectly:You’re passing
$dbas the first argument, but the function’s first argument should be the last name. Simply update the call to remove$dband you should be good: