Link to home
Start Free TrialLog in
Avatar of MrTV
MrTVFlag for Thailand

asked on

php code stop when no result, Plese pro tect it to run the following code

if (Recordset_Inserting($fieldList)) {
        $sSql2 = "SELECT PCode  FROM prddtl WHERE SerialNo1 = $x_AOLcode ";
        $result01 = "mysql_query($sSql2, $conn)" ;
                   if(!$result01)
        {
        echo "serial number not exist" ;
          die();
        }


How can i make the code stop not continue to run the following line if the SerialNo1  generate no Pcode

the code above when i put SerialNo1  that not axist it still run the following line

The full code is below



The full code is  below

function AddData($conn)
{
      global $x_idComSetDetail;
      global $x_ComSetHeadId;
      global $x_AOLcode;
      $sFilter = ewSqlKeyWhere;

      // Check for duplicate key
      $bCheckKey = true;
      if ((@$x_idComSetDetail == "") || (is_null(@$x_idComSetDetail))) {
            $bCheckKey = false;
      } else {
            $sFilter = str_replace("@idComSetDetail", AdjustSql($x_idComSetDetail), $sFilter); // Replace key value
      }
      if ($bCheckKey) {
            $sSqlChk = ewBuildSql(ewSqlSelect, ewSqlWhere, ewSqlGroupBy, ewSqlHaving, ewSqlOrderBy, $sFilter, "");
            $rsChk = phpmkr_query($sSqlChk, $conn) or die("Failed to execute query at line " . __LINE__ . ": " . phpmkr_error($conn) . '<br>SQL: ' . $sSqlChk);
            if (phpmkr_num_rows($rsChk) > 0) {
                  $_SESSION[ewSessionMessage] = "Duplicate value for primary key";
                  phpmkr_free_result($rsChk);
                  return false;
            }
            phpmkr_free_result($rsChk);
      }

      // Field ComSetHeadId
      $theValue = ($GLOBALS["x_ComSetHeadId"] != "") ? intval($GLOBALS["x_ComSetHeadId"]) : "NULL";
      $fieldList["`ComSetHeadId`"] = $theValue;

      // Field AOLcode
      $theValue = (!get_magic_quotes_gpc()) ? addslashes($GLOBALS["x_AOLcode"]) : $GLOBALS["x_AOLcode"];
      $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
      $fieldList["`AOLcode`"] = $theValue;

      // Field ProductCussification
      $theValue = (!get_magic_quotes_gpc()) ? addslashes($GLOBALS["x_ProductCussification"]) : $GLOBALS["x_ProductCussification"];
      $theValue = ($theValue != "") ? " '" . $theValue . "'" : "NULL";
      $fieldList["`ProductCussification`"] = $theValue;

      // Field RemovefromSet
      $theValue = (!get_magic_quotes_gpc()) ? addslashes($GLOBALS["x_RemovefromSet"]) : $GLOBALS["x_RemovefromSet"];
      $theValue = (strtoupper($theValue) != "Y") ? " 'N'" : " 'Y'";
      $fieldList["`RemovefromSet`"] = $theValue;

      // Inserting event
      if (Recordset_Inserting($fieldList)) {


        $sSql2 = "SELECT PCode  FROM prddtl WHERE SerialNo1 = $x_AOLcode ";

        $result01 = "mysql_query($sSql2, $conn)" ;
       
            if(!$result01)
        {
        echo "serial number not exist" ;
          die();
        }
            
        $sSql1 = "UPDATE prdmas SET onhand = onhand - 1 WHERE Code = (SELECT PCode  FROM prddtl WHERE SerialNo1 = $x_AOLcode )  ";

            mysql_query($sSql1, $conn) or die("fail to deduct stock " . __LINE__ . ": " . phpmkr_error($conn) . '<br>SQL: ' . $sSql1);




            // Insert
            $sSql = "INSERT INTO `ComSetDetail` (";
            $sSql .= implode(",", array_keys($fieldList));
            $sSql .= ") VALUES (";
            $sSql .= implode(",", array_values($fieldList));
            $sSql .= ")";      
            phpmkr_query($sSql, $conn) or die("Failed to execute query at line " . __LINE__ . ": " . phpmkr_error($conn) . '<br>SQL: ' . $sSql);
            $fieldList["`idComSetDetail`"] = phpmkr_insert_id($conn);
            $result = (phpmkr_affected_rows($conn) > 0);

            // Inserted event
            if ($result) Recordset_Inserted($fieldList);
      } else {
            $result = false;
      }
      return $result;
}


Avatar of Cornelia Yoder
Cornelia Yoder
Flag of United States of America image

if (Recordset_Inserting($fieldList)) {
        $sSql2 = "SELECT PCode  FROM prddtl WHERE SerialNo1 = $x_AOLcode ";
        $result01 = "mysql_query($sSql2, $conn)" ;
                   if(!$result01)
        {
        echo "serial number not exist" ;
          die();
        }
 

The statement
                   if(!$result01)
only checks if the query itself failed, not if there were no rows selected.

Instead, you can use

$numrows = mysql_num_rows($result01);
if ($numrows == 0)
        {
        echo "serial number not exist" ;
          die();
        }
Avatar of MrTV

ASKER

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\manager\ComSetDetailadd.php on line 361
serial number not exist
Avatar of MrTV

ASKER

Hi yodercm

when i change it show above

if (Recordset_Inserting($fieldList)) {


        $sSql2 = "SELECT PCode  FROM prddtl WHERE SerialNo1 = $x_AOLcode ";

        $result01 = "mysql_query($sSql2, $conn)" ;
             $numrows = mysql_num_rows($result01);
        if ($numrows == 0)
        {
        echo "serial number not exist" ;
          die();
        }
      
            
        $sSql1 = "UPDATE prdmas SET onhand = onhand - 1 WHERE Code = (SELECT PCode  FROM prddtl WHERE SerialNo1 = $x_AOLcode )  ";

            mysql_query($sSql1, $conn) or die("fail to deduct stock " . __LINE__ . ": " . phpmkr_error($conn) . '<br>SQL: ' . $sSql1);




            // Insert
            $sSql = "INSERT INTO `ComSetDetail` (";
            $sSql .= implode(",", array_keys($fieldList));
            $sSql .= ") VALUES (";
            $sSql .= implode(",", array_values($fieldList));
            $sSql .= ")";      

ASKER CERTIFIED SOLUTION
Avatar of Cornelia Yoder
Cornelia Yoder
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of AlexanderR
AlexanderR
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial