代码传入if语句和else语句

时间:2017-05-05 20:40:38

标签: php jquery mysqli

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

我想要的是,如果$computername ==数据库($nomComputer as $info)中的计算机insert intopopupupdate以某种方式作出肯定之后就不起作用了(这是一个有点不同,因为我需要在insert into之前执行if else并且我想先调整if($computername!=$info)语句)....但如果计算机名称不在数据库中,它将会插入数据库。

我也通过else if($computername==$info)更改了其他结果:当我在数据库中放入一个类似于计算机名称的计算机名称并直接插入时,我传递了第一个if而没有做任何事情。

我找不到任何帮助我的问题(我看到的所有其他语言都是PHP / jQuery的不同语言(如python或C ++))。

foreach语句和else语句中,代码是否有任何错误?

这是我的验证,其中我的//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; } 包含if语句

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语句和Products语句!也许这是别的东西,但我不能指责它!

2 个答案:

答案 0 :(得分:1)

我的问题是我将ifelse语句放在foreach中,所以我先foreachif ... else <之后/ p>

    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{
            ...
        }

答案 1 :(得分: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{
...