MySql Insert problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vfpguru
Forum Newbie
Posts: 6
Joined: Mon Sep 13, 2010 12:20 pm

MySql Insert problem

Post by vfpguru »

Hello all;
I am running a simple insert statement and for some unknown reason when it executes, it inserts 101 rows! here is the statement:
*********************************************
$sql = "insert into test_maint_employee(mnt_id_num, f_name, l_name, hr_rate, active_cd) values ";
$sql .= "(0,'$_nfname', '$_nlname', '$_nhrrate', 'A')";
mysql_query($sql);
*********************************************
The column mnt_id_num is the primary key, not null and autoincrement as well. I have tried omitting the field in the insert, sending in a null in place of the value... nothing works. when I submit, it hangs and then comes up with:
CGI Error
The specified CGI application misbehaved by not returning a complete set of HTTP headers.

anyone run into this or have a clue on how to get around this?? I am running php ver. 5.2.5 and mysql ver. 5.1.35 on IIS
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: MySql Insert problem

Post by mikosiko »

post the code surrounding that ... no enough info from what you posted.
vfpguru
Forum Newbie
Posts: 6
Joined: Mon Sep 13, 2010 12:20 pm

Re: MySql Insert problem

Post by vfpguru »

ok, here it is:
******************************************
$page_title = "Edit Maint Employees";
session_start();
include_once("functions.php");
include_once("mysql.php");
CheckUser("$PHP_SELF",10);
db_connect();

if($addnewempl) {
if($_nfname == '') {
echo "<strong>Error: The First Name can not be blank.</strong><br>";
echo "Use your browsers Back button to input the First Name and resubmit.\n";
exit;
}
if($_nlname == '') {
echo "<strong>Error: The Last Name can not be blank.</strong><br>";
echo "Use your browsers Back button to input the Last Name and resubmit.\n";
exit;
}
if($_nhrrate == '') {
echo "<strong>Error: The Hourly Rate can not be blank.</strong><br>";
echo "Use your browsers Back button to input the Hourly Rate and resubmit.\n";
exit;
}


$sql = "insert into test_maint_employee(mnt_id_num, f_name, l_name, hr_rate, active_cd) values ";
$sql .= "(0,'$_nfname', '$_nlname', '$_nhrrate', 'A')";

mysql_query($sql);

if(mysql_errno() == 1062) {
echo "<strong>Error: An Employee with that Employee Code already exists in the database.</strong><br>";
echo "Use your browsers Back button to change the Employee Code and resubmit.\n";
exit;
} elseif(mysql_errno()) {
echo "An error happened. Transaction NOT processed.<br>\n";
echo mysql_errno().": ".mysql_error()."<br>\n";
PrintFooter();
exit;
}
unset($addnewempl);
header("Location: $PHP_SELF?");
exit;
}


if($_mnt_idnum && !$_doupdate) {
PrintHeader("$page_title");
PrintNavBar();
$sql = "select * from maint_employee where mnt_id_num='$_mnt_idnum'";
$qrslt=db_query($sql);
if(!db_numrows($qrslt)) {
echo "<strong>can't connect to database to get the employee.</strong>";
PrintFooter();
exit;
}
$row=db_fetch_array($qrslt);
echo "<form action=\"$PHP_SELF\" method=\"post\">\n";
echo "<input type=\"hidden\" name=\"_doupdate\" value=\"1\">\n";
echo "<input type=\"hidden\" name=\"_mnt_idnum\" value=\"$_mnt_idnum\">\n";
echo "<div align=\"center\"><table border=\"0\" width=\"85%\">\n";
echo "<tr><td class=\"tictit\">First Name</td>\n";
echo "<td class=\"ticdat\">";
echo "<input type=\"text\" size=\"25\" maxlength=\"25\" name=\"_fname\" value=\"$row[f_name]\"></td></tr>\n";
echo "<tr><td class=\"tictit\">Last Name</td>\n";
echo "<td class=\"ticdat\">";
echo "<input type=\"text\" size=\"25\" maxlength=\"25\" name=\"_lname\" value=\"$row[l_name]\"></td></tr>\n";
echo "<tr><td class=\"tictit\">Hourly Rate</td>\n";
echo "<td class=\"ticdat\">";
echo "<input type=\"text\" size=\"10\" maxlength=\"10\" name=\"_hrrate\" value=\"$row[hr_rate]\"></td></tr>\n";
echo "<tr><td class=\"tictit\">Status</td>\n";
echo "<td class=\"ticdat\"><select name=\"_active_cd\">\n";
echo "<option value=\"A\"";
echo $row[active_cd]=="A" ? " selected" : "";
echo ">Active</option><option value=\"I\"";
echo $row[active_cd]=="I" ? " selected" : "";
echo ">Inactive</option></select>\n";
echo "</td></tr>\n";
echo "<tr><td class=\"ticdat\" colspan=\"2\" align=\"center\">\n";
echo "<input type=\"submit\" value=\"Update\">&nbsp;&nbsp;&nbsp;";
echo "<input type=\"reset\" value=\"Reset\"></td></tr></table></div></form><br>\n";
PrintFooter();
exit;
}

if($_mnt_idnum && $_doupdate) {
$sql = "update maint_employee set f_name='$_fname', l_name='$_lname', ";
$sql .= "hr_rate='$_hrrate', active_cd='$_active_cd' ";
$sql .= "where mnt_id_num='$_mnt_idnum'";
db_query($sql);
if(mysql_errno()) {
PrintHeader("$page_title");
PrintNavBar();
echo "Problem updating the employee";
unset($_mnt_idnum); unset($_doupdate);
exit;
}
unset($_mnt_idnum); unset($_doupdate);
header("Location: mainteditempl.php?");
exit;
}


// $qrslt=db_query("select * from maint_employee order by l_name, f_name"); Original statement
$qrslt=db_query("select * from maint_employee where active_cd = 'A' order by l_name, f_name");
if(!db_numrows($qrslt)) {
#should never happen
echo "<strong>can't connect to database to get employees.</strong>";
PrintFooter();
exit;
}

PrintHeader("$page_title");
PrintNavBar();
echo "<!-- begin bldg list -->\n";
echo "<form method=\"post\" action=\"$PHP_SELF\">";
echo "<input type=\"hidden\" name=\"addnewempl\" value=\"1\">";
echo "<div align=\"center\"><table border=\"0\" cellspacing=\"1\" cellpadding=\"2\">\n";
echo "<tr><td class=\"tictit\">Empl ID</td><td class=\"tictit\">First Name</td><td class=\"tictit\">Last Name</td>";
echo "<td class=\"tictit\">Hourly Rate</td><td class=\"tictit\">Active Code</td><tr>";

while($row=db_fetch_array($qrslt)) {
echo "<tr><td class=\"ticdat\">$row[mnt_id_num]</td>";
echo "<td class=\"ticdat\">$row[f_name]</td>";
echo "<td class=\"ticdat\">";
echo "<a href=\"$PHP_SELF?_mnt_idnum=$row[mnt_id_num]\">" . htmlentities($row[l_name]) . "</a>";
echo "</td>";
echo "<td class=\"ticdat\">$row[hr_rate]</td>";
echo "<td class=\"ticdat\" align=\"center\">$row[active_cd]";
echo "</td></tr>\n";
}
echo "<tr>";
echo "<td class=\"ticdat\">&nbsp;</td>";
echo "<td class=\"ticdat\"><input type=\"text\" name=\"_nfname\" size=\"25\" maxlength=\"25\"></td>";
echo "<td class=\"ticdat\"><input type=\"text\" name=\"_nlname\" size=\"25\" maxlength=\"25\"></td>";
echo "<td class=\"ticdat\"><input type=\"text\" name=\"_nhrrate\" size=\"10\" maxlength=\"10\"></td>";
echo "<td class=\"ticdat\"><input type=\"submit\" value=\"Add\"></td></tr>";



echo "</table></div></form>\n";
PrintFooter();
# vim: set cin:ts=8:sts=2:sw=2:
?>
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: MySql Insert problem

Post by mikosiko »

I didn't read the code completely but looks like your multiples insert are due to the fact that the page is executing several REFRESH... not sure what your function CheckUser("$PHP_SELF,10") is doing of if can be causing the behavior... also could be that the unset and & header that you are sending after the insert is not working as you expect.
vfpguru
Forum Newbie
Posts: 6
Joined: Mon Sep 13, 2010 12:20 pm

Re: MySql Insert problem

Post by vfpguru »

ok, that makes sense... I may have to rewrite it because when it was written, it was running on a linyx server and has since been moved over to IIS. I believe it was also written in an older version of php. Thanks for the tip :)
Post Reply