2017-05-05 110 views
1

根据计算机是否在数据库中,我尝试通过if… else语句。我做了一些研究,找到对我有益的东西,但它看起来并不完美,因为它通过我的if ......。并通过我的else在同一时间...。我的问题是我得到了我的警报Hello world,而不是我的function (popup),它将信息插入我的数据库。也许我在代码中犯了一个错误,找不到它。代码在if语句和else语句中传入

我想要什么,如果去了$computername ==计算机数据库($nomComputer as $info),该insert into没有工作,直到popup总得做出肯定的(这是一个有点不同,因为我需要的insert into和我之前做一个update要先调整if else语句)....但如果计算机名称不在数据库中,它将被插入到数据库中。

我也改变了别的if($computername!=$info)为这个结果:我通过第一个if($computername==$info)没有做任何事情,当我把一个类似的数据库中的计算机名称,并直接插入它。

我发现没有什么能够帮助我解决所有我通过的问题(我看到的所有其他PHP/jQuery语言(如Python或C++))。

代码中有什么错误使它在if语句中过去,还有else语句吗?

这是我的验证,其中一个把我foreach包含if语句

//Get informations in the form and insert it into db 
    $location=$_GET['location']; 
    $dept=$_GET['dept']; 
    $jack=$_GET['jack']; 
    $computername=$_GET['computername']; 
    $productkey=$_GET['productkey']; 
    $model=$_GET['model']; 
    $vendor=$_GET['vendor']; 
    $serialnumber=$_GET['serialnumber']; 
    $macaddress=$_GET['macaddress']; 
    $status=$_GET['status']; 
    $starphone=$_GET['starphone']; 
    $intid=$_GET['intid']; 
    $did=$_GET['did']; 
    $software=$_GET['software']; 
    $yes=$_GET['yes']; 

    foreach($nomComputer as $info){ 
     if($computername==$info){ 
      "<script type='text/javascript'>alert(‘Hello World’); 
       ;(function($) { 
        $(function() { 
         e.preventDefault(); 
         $('#element_to_pop_up').bPopup({ 
          appendTo: 'form' 
          , zIndex: 2 
          , modalClose: false 
         }); 
        }); 
       })(jQuery); 
      </script> ";    
    }else{ 
     //Insert Into departement 
     $query='insert into department(
     department, 
     location_id)values(
     "'.$dept.'", 
     "'.$location.'")'; 
     if(valid_query($query)){ 
     $message .= "Enregistrement a ete mis a jour! Informations has been saved for department!<br>"; 
     }else{ 
       $message .= "Enregistrement n a pas ete mis a jour! Informations has not been saved for department<br>"; 
     } 
     //Insert Into computer 
     $query='insert into computer(
     computer_name, 
     product_key, 
     model, 
     serial_number, 
     status_id, 
     starphone, 
     inst_id, 
     did, 
     macaddress, 
     software_id, 
     hidden 
     )values(
     "'.$computername.'", 
     "'.$productkey.'", 
     "'.$model.'", 
     "'.$serialnumber.'", 
     "'.$status.'", 
     "'.$starphone.'", 
     "'.$intid.'", 
     "'.$did.'", 
     "'.$macaddress.'", 
     "'.$software.'", 
     0 
     )'; 
     if(valid_query($query)){ 
     $message .= "Enregistrement a ete mis a jour! Informations has been saved!<br>"; 
     }else{ 
       $message .= "Enregistrement n a pas ete mis a jour! Informations has not been saved<br>"; 
     } 
     $query="select dept_id from department where department='".$dept."' and location_id='".$location."'"; 
     $deptid=select_query($query); 
     $query="select computer_id from computer where macaddress='".$macaddress."'"; 
     $computerid=select_query($query); 
     //Insert Into jack 
     $query='insert into jack(
     jack_number, 
     dept_id, 
     location_id, 
     computer_id 
     )values(
     "'.$jack.'", 
     "'.$deptid.'", 
     "'.$location.'", 
     "'.$computerid.'")'; 
     if(valid_query($query)){ 
     $message .= "Enregistrement a ete mis a jour! Informations has been saved for jack!<br>"; 
     }else{ 
       $message .= "Enregistrement n a pas ete mis a jour! Informations has not been saved for jack<br>"; 
     } 
     //Insert Into computer_vendor 
     $query='insert into computer_vendor(
     computer_id, 
     vendor_id 
     )values(
     "'.$computerid.'", 
     "'.$vendor.'")'; 
     if(valid_query($query)){ 
     $message .= "Enregistrement a ete mis a jour! Informations has been saved for vendor!<br>"; 
     }else{ 
       $message .= "Enregistrement n a pas ete mis a jour! Informations has not been saved for vendor<br>"; 
     } 
     $location=""; 
     $dept=""; 
     $jack=""; 
     $computername=""; 
     $productkey=""; 
     $model=""; 
     $vendor=""; 
     $serialnumber=""; 
     $macaddress=""; 
     $status=""; 
     $starphone=""; 
     $intid=""; 
     $did=""; 

     $messageValide="Enregistrement a ete mis a jour! Informations has been saved";//Display went validate 
     } 
    } 
    break; 
} 

这是我的循环在数据库中已获得计算机名

header('content-type: text/html; charset=utf-8'); 
require_once("cnx_db_inventory.php"); 
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS,DB_NAME); 
    //Get number of rows 
    $sql="SELECT computer_name FROM computer"; 
    $result=mysqli_query($con, $sql); 
    $i=1; 
    while($row=mysqli_fetch_assoc($result)){ 
     $nomComputer[$i] = $row['computer_name']; 
     $i++; 
    } 

// Loop through the results from the database 
for ($i = 1; $i <=count($nomComputer); $i++){ 
     $nomComputer[$i]; 
} 

popup我尝试插入(现在,它在一个按钮上,但想要它在if statement(我试了一下))

;(function($) { 
    $(function() { 
     $('#my-button').bind('click', function(e) { 
      e.preventDefault(); 
      $('#element_to_pop_up').bPopup({ 
       appendTo: 'form' 
       , zIndex: 2 
       , modalClose: false 
      }); 
     }); 
    }); 
})(jQuery); 

而且popup内容:

<!-- Button that triggers the popup --> 
<button id="my-button">POP IT UP</button> 
<!-- Element to pop up --> 
<div id="element_to_pop_up"> 
    <h3>ATTENTION</h3> 
    <p>This computer is already active in the database.</p> 
    <p>Did you want to deactive the computer in the database and insert the new one?</p> 
    <div align="right"><a id="yes" name="yes">Yes</a> &nbsp;&nbsp;&nbsp; <a class="b-close">No</a> &nbsp;&nbsp;&nbsp; </div> 
</div> 

database

编辑 任何计算机名称为例得到了主意?我尝试了很多东西(使用===而不是==,推迟alert,将$function置于注释中(if中的那个)),但没有成功。我的代码仍然通过if声明和else声明!也许这是别的,但我不能指责它!

回答

1

我的问题是,我把我的ifelse声明中foreach,所以我做了foreach第一和if ... else

foreach($nomComputer as $info){ 
     if($computername==$info){ 
      $foundIt = $computername; 
     } 
    } 
     if($computername==$foundIt){ 
      echo "<script type='text/javascript'> 
       ;(function($) { 
        $(function() { 
         $('#element_to_pop_up').bind('click', function(e) { 
          e.preventDefault(); 
          $('#element_to_pop_up').bPopup({ 
           appendTo: 'form' 
           , zIndex: 2 
           , modalClose: false 
          }); 
         }); 
        }); 
       })(jQuery); 
      </script> "; 
     }else{ 
      ... 
     } 
0

您必须使用echo来输出脚本。现在你只有字符串。

if($computername==$info){ 
     echo "<script type='text/javascript'>alert(‘Hello World’); 
      ;(function($) { 
       $(function() { 
        e.preventDefault(); 
        $('#element_to_pop_up').bPopup({ 
         appendTo: 'form' 
         , zIndex: 2 
         , modalClose: false 
        }); 
       }); 
      })(jQuery); 
     </script> ";    
}else{ 
... 
+0

后它插入新记录? – inarilo

+0

我试过了,但是没有给我结果 – Nexis

+0

不行了,但是脚本没有显示 – Nexis