if if语句中的PHP if语句

时间:2015-04-29 19:11:44

标签: php mysql if-statement

我正在构建一个php网站,我希望用户创建他的公司。

脚本正在检查用户是否已经注册了任何公司,然后它应该显示是否存在。

如果他没有注册公司,他应该看一张他可以注册的表格。

如果他选择注册公司,脚本将检查任何具有相同名称的公司或插入该行。

我唯一的问题是,当已经有一个具有该名称的公司时,回声不显示。

我已经在代码中写了问题所在。

<?php
        $con=mysqli_connect("mysql","USER","PASS","DB");
        if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); }

            $result_get_companies = mysqli_query($con,"SELECT * FROM companies WHERE userid='". $login_session ."' ORDER BY companyid ASC") or die(mysqli_error());

            if (mysqli_num_rows($result_get_companies) >= 1) {

                while($row_companies = mysqli_fetch_array( $result_get_companies )) {

                $result_get_company_owner = mysqli_query($con,"SELECT username FROM users WHERE userid='". $login_session ."'")                 or die(mysqli_error());

                $company_owner = mysqli_fetch_assoc($result_get_company_owner);

                    echo 'THIS WORKS';
                }

            } else {

                if (isset($_POST['create_first_company']) && !empty($_POST['company_name'])) {

                    $company_name_unsafe = mysqli_real_escape_string($con, $_POST['company_name']);
                    $company_name = preg_replace("/[^a-zA-Z0-9\s]/","",$company_name_unsafe );

                    $check_companies = "SELECT companyid FROM companies WHERE company_name='". $company_name ."'";
                    $what_to_do_companies = mysqli_query($con,$check_companies);

                    if (mysqli_num_rows($what_to_do_companies) != 0) {

                        echo 'THIS DOESNT WORK
                                It does register that is should go here
                                because it does not insert new row.
                                and when the value is = 0 it does go
                                to else ELSE below and insert row.';

                    } else {

                        $result_create_company = mysqli_query($con,"INSERT INTO companies (companyname) 
                                                                    VALUES ('". $login_session ."')") 
                                                                    or die(mysqli_error());

                        echo 'THIS WORKS';  

                    }

                } else {

                    echo 'THIS WORKS!';

                }

            }

?>

0 个答案:

没有答案