I'm working on a simple script that connects to a MySQL database and inserts some data from an HTML form.
Everything SEEMS to work. But when I go into mysql and check to see if data is actually being inserted, I find it is not being inserted into the database! I echoed all the variables to make sure they are not empty, and they are being passed from the form to the script just fine. I have multiple (Or Die) statements to stop the script if something doesn't work right. I even manually inserted data into the database through the MySQL command line and it inserts just fine. I know it has to be something silly, stupid, and simple(isn't it always?), so if someone else could peruse this code for me, I'd be very happy and stop crying.
Thanx
------
<body bgcolor="#FFFFFF" text="#000000">
<?php
//DB variables
$dbhost='xxxx';
$dbusername='xxxx';
$dbuserpassword='xxxx';
//stripping any HTML that might be inserted into the forms, protects against malicious code
$lname=HTMLSpecialChars($lname);
$fname=HTMLSpecialChars($fname);
$room=HTMLSpecialChars($room);
$phone=HTMLSpecialChars($phone);
$email=HTMLSpecialChars($email);
$request=HTMLSpecialChars($request);
//make sure all form fields are filled out
if ($lname=="")
{
echo "You must enter in your last name, please press your back button and try again.";
exit;
}
if ($fname=="")
{
echo "You must enter in your first name, please press your back button and try again.";
exit;
}
if($room=="")
{
echo "You must enter in your room number, please press your back button and try again.";
exit;
}
if($phone=="")
{
echo "You must enter in your phone number, please press your back button and try again.";
exit;
}
if($email=="")
{
echo "You must enter in your email address, please press your back button and try again.";
exit;
}
if($type=="")
{
echo "You must select a type of request, please press your back button and try again.";
exit;
}
if($request=="")
{
echo "You must enter in a request, please press your back button and try again.";
exit;
}
//connecting to the DB
$link_id=mysql_connect($dbhost, $dbusername, $dbuserpassword) or DIE("Unable to connect to Server!");
$db=mysql_select_db("rfa", $link_id) or DIE("Unable to select database!");
//insert data into faculty table
$query="INSERT INTO faculty VALUES(NULL, '$fname', '$lname','$room','$phone','$email')" or DIE("Insert Failed!");
$NewID=mysql_insert_id(); //grabs the primary key from the last insert, allowing us to insert it as a foreign key into another table
//insert data into request table
$query="INSERT INTO request VALUES(NULL, NULL, '$request','$type',1,'$NewID')" or DIE("Insert Failed!");
?>
<h2><font face="Arial, Helvetica, sans-serif" size="2" color="#003366">Request
added succesfully! Your request will be reviewed shortly. If you have any questions,
please email <a href=http://www.phpbuilder.com/board/archive/index.php/"mailto:%3Cfont%20face=%22Arial,%20Helvetica,%20sans-serif%22%20size=%222%22%20color=%22#003366%22%[email protected]%3C/font%3E">[email protected]</a>.</font></h2>
<h2><font face="Arial, Helvetica, sans-serif" size="2" color="#003366"><a href="http://www.geology.ohio-state.edu">back</a></font></h2>
<table width="40%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="32%"><font face="Arial, Helvetica, sans-serif">First Name</font></td>
<td width="68%"><?php echo $fname; ?></td>
</tr>
<tr>
<td width="32%"><font face="Arial, Helvetica, sans-serif">Last Name</font></td>
<td width="68%"><?php echo $lname; ?></td>
</tr>
<tr>
<td width="32%"><font face="Arial, Helvetica, sans-serif">Room #</font></td>
<td width="68%"><?php echo $room; ?></td>
</tr>
<tr>
<td width="32%"><font face="Arial, Helvetica, sans-serif">Phone #</font></td>
<td width="68%"><?php echo $phone; ?></td>
</tr>
<tr>
<td width="32%"><font face="Arial, Helvetica, sans-serif">Email</font></td>
<td width="68%"><?php echo $email; ?></td>
</tr>
<tr>
<td width="32%"><font face="Arial, Helvetica, sans-serif">Type</font></td>
<td width="68%"><?php echo $type; ?></td>
</tr>
</table>
Everything SEEMS to work. But when I go into mysql and check to see if data is actually being inserted, I find it is not being inserted into the database! I echoed all the variables to make sure they are not empty, and they are being passed from the form to the script just fine. I have multiple (Or Die) statements to stop the script if something doesn't work right. I even manually inserted data into the database through the MySQL command line and it inserts just fine. I know it has to be something silly, stupid, and simple(isn't it always?), so if someone else could peruse this code for me, I'd be very happy and stop crying.
Thanx
------
<body bgcolor="#FFFFFF" text="#000000">
<?php
//DB variables
$dbhost='xxxx';
$dbusername='xxxx';
$dbuserpassword='xxxx';
//stripping any HTML that might be inserted into the forms, protects against malicious code
$lname=HTMLSpecialChars($lname);
$fname=HTMLSpecialChars($fname);
$room=HTMLSpecialChars($room);
$phone=HTMLSpecialChars($phone);
$email=HTMLSpecialChars($email);
$request=HTMLSpecialChars($request);
//make sure all form fields are filled out
if ($lname=="")
{
echo "You must enter in your last name, please press your back button and try again.";
exit;
}
if ($fname=="")
{
echo "You must enter in your first name, please press your back button and try again.";
exit;
}
if($room=="")
{
echo "You must enter in your room number, please press your back button and try again.";
exit;
}
if($phone=="")
{
echo "You must enter in your phone number, please press your back button and try again.";
exit;
}
if($email=="")
{
echo "You must enter in your email address, please press your back button and try again.";
exit;
}
if($type=="")
{
echo "You must select a type of request, please press your back button and try again.";
exit;
}
if($request=="")
{
echo "You must enter in a request, please press your back button and try again.";
exit;
}
//connecting to the DB
$link_id=mysql_connect($dbhost, $dbusername, $dbuserpassword) or DIE("Unable to connect to Server!");
$db=mysql_select_db("rfa", $link_id) or DIE("Unable to select database!");
//insert data into faculty table
$query="INSERT INTO faculty VALUES(NULL, '$fname', '$lname','$room','$phone','$email')" or DIE("Insert Failed!");
$NewID=mysql_insert_id(); //grabs the primary key from the last insert, allowing us to insert it as a foreign key into another table
//insert data into request table
$query="INSERT INTO request VALUES(NULL, NULL, '$request','$type',1,'$NewID')" or DIE("Insert Failed!");
?>
<h2><font face="Arial, Helvetica, sans-serif" size="2" color="#003366">Request
added succesfully! Your request will be reviewed shortly. If you have any questions,
please email <a href=http://www.phpbuilder.com/board/archive/index.php/"mailto:%3Cfont%20face=%22Arial,%20Helvetica,%20sans-serif%22%20size=%222%22%20color=%22#003366%22%[email protected]%3C/font%3E">[email protected]</a>.</font></h2>
<h2><font face="Arial, Helvetica, sans-serif" size="2" color="#003366"><a href="http://www.geology.ohio-state.edu">back</a></font></h2>
<table width="40%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="32%"><font face="Arial, Helvetica, sans-serif">First Name</font></td>
<td width="68%"><?php echo $fname; ?></td>
</tr>
<tr>
<td width="32%"><font face="Arial, Helvetica, sans-serif">Last Name</font></td>
<td width="68%"><?php echo $lname; ?></td>
</tr>
<tr>
<td width="32%"><font face="Arial, Helvetica, sans-serif">Room #</font></td>
<td width="68%"><?php echo $room; ?></td>
</tr>
<tr>
<td width="32%"><font face="Arial, Helvetica, sans-serif">Phone #</font></td>
<td width="68%"><?php echo $phone; ?></td>
</tr>
<tr>
<td width="32%"><font face="Arial, Helvetica, sans-serif">Email</font></td>
<td width="68%"><?php echo $email; ?></td>
</tr>
<tr>
<td width="32%"><font face="Arial, Helvetica, sans-serif">Type</font></td>
<td width="68%"><?php echo $type; ?></td>
</tr>
</table>