Jump to content

Need help with this PHP code, popup alert appears twice


cottonbuds2005

Recommended Posts

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>';
   }
}
?>

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
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>';
}
?>

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.