cottonbuds2005 Posted December 9, 2009 Share Posted December 9, 2009 Hi all, Im a newbie at PHP / MySQL stuff so please bear with me. This is a simple "insert data" code and I have a popup alert box indicating successful data insertion into my MySQL table. My problem is that the popup alert appears twice. How do I correct this problem? Many many thanks. <?php require_once('../Connections/ITInventory.php'); ?> <?php if (!function_exists("GetSQLValueString")) { function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") { if (PHP_VERSION < 6) { $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue; } $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue); switch ($theType) { case "text": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "long": case "int": $theValue = ($theValue != "") ? intval($theValue) : "NULL"; break; case "double": $theValue = ($theValue != "") ? doubleval($theValue) : "NULL"; break; case "date": $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL"; break; case "defined": $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue; break; } return $theValue; } } $editFormAction = $_SERVER['PHP_SELF']; if (isset($_SERVER['QUERY_STRING'])) { $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']); } if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) { $insertSQL = sprintf("INSERT INTO weloan (hostname, location, floor, type, assetno, serialno, model, ram, hdd, warranty, remarks, securitytag) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)", GetSQLValueString($_POST['hostname'], "text"), GetSQLValueString($_POST['location'], "text"), GetSQLValueString($_POST['floor'], "text"), GetSQLValueString($_POST['type'], "text"), GetSQLValueString($_POST['assetno'], "text"), GetSQLValueString($_POST['serialno'], "text"), GetSQLValueString($_POST['model'], "text"), GetSQLValueString($_POST['ram'], "text"), GetSQLValueString($_POST['hdd'], "text"), GetSQLValueString($_POST['warranty'], "text"), GetSQLValueString($_POST['remarks'], "text"), GetSQLValueString($_POST['securitytag'], "text")); mysql_select_db($database_ITInventory, $ITInventory); $Result1 = mysql_query($insertSQL, $ITInventory); if($Result1!=1){ echo '<script language="JavaScript" type="text/javascript">alert("'.mysql_error().'")</script>'; }else if($Result1==1){ echo '<script language="JavaScript" type="text/javascript">alert("Record created into database")</script>'; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/184492-need-help-with-this-php-code-popup-alert-appears-twice/ Share on other sites More sharing options...
mrMarcus Posted December 9, 2009 Share Posted December 9, 2009 is this chunk of code part of a larger process where it's nested within a loop perhaps? this bit here doesn't make sense .. mysql_query() returns boolean on INSERT, so checking its return against a numeric value of 1 is irrelevant. <?php if (!$Result1) { echo '<script language="JavaScript" type="text/javascript">alert("'.mysql_error().'")</script>'; }else if($Result1) { echo '<script language="JavaScript" type="text/javascript">alert("Record created into database")</script>'; } ?> makes more sense. are you getting two records inserted into the db? and to be more clear, you are receiving this javascript alert twice: <script language="JavaScript" type="text/javascript">alert("Record created into database")</script> ? you didn't expand much. Quote Link to comment https://forums.phpfreaks.com/topic/184492-need-help-with-this-php-code-popup-alert-appears-twice/#findComment-973916 Share on other sites More sharing options...
oni-kun Posted December 9, 2009 Share Posted December 9, 2009 My problem is that the popup alert appears twice. How do I correct this problem? Many many thanks. $Result will almost always not be (string)'1', and be (boolean)1. The previous code that mrMarcus stated should be more correct, although I'd use this to nullify the other possibilities: <?php IF ($Result1) { echo '<script language="JavaScript" type="text/javascript">alert("Record created into database")</script>'; } ELSE { echo '<script language="JavaScript" type="text/javascript">alert("'.mysql_error().'")</script>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/184492-need-help-with-this-php-code-popup-alert-appears-twice/#findComment-973919 Share on other sites More sharing options...
cottonbuds2005 Posted December 9, 2009 Author Share Posted December 9, 2009 Hi mrMarcus and oni-kun, I will take your suggestions into consideration asap. yes mrMarcus, I correctly insert one record to the DB but I receive this twice. <script language="JavaScript" type="text/javascript">alert("Record created into database")</script> Ill let you guys know later. Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/184492-need-help-with-this-php-code-popup-alert-appears-twice/#findComment-973931 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.