PHP代码无法正常工作

时间:2014-10-28 19:31:51

标签: php

我正在创建一个表单,用户可以在其中输入联系人号码,并从数据库中提取数据。现在同一个电话号码可以属于多个人,这段代码只是一次获取一个人的详细信息。我应该做什么改变,以便它可以为所有人获取数据。

<?php

if (preg_match("/^[0-9]+/", $_POST['name'])) {

    $name = $_POST['name'];

    //connect  to the database
    $db = mysql_connect("localhost", "root", "") or die ('I cannot connect to the database  because: ' . mysql_error());
    //-select  the database to use
    $mydb = mysql_select_db("trsv_data");


    //-query  the database table to find person_id
    $sql_search = "SELECT  person_id FROM contactnumbers WHERE contact_number = " . $name;
    // $sql_search="SELECT  person_id FROM Email WHERE Email LIKE '%" . $name .  "%' ";

    //-run  the query against the mysql query function
    $result = mysql_query($sql_search);
    //-create  while loop and loop through result set
    while ($row = mysql_fetch_array($result)) {
        $person_id = $row['person_id'];
        //-display the result of the array
        echo "<ul>\n";
        //echo "Person Id: <a  href=\"trilliumsearch.php?id=$ID\">" . $person_id . "</a>\n";
        echo "Person Id: <a  href=\"person_search.php\">" . $person_id . "</a>\n";
        //http://localhost:8080/Trillium_Emarketing/Trillium/output/person_search.php
        echo "</ul>";


        //-query  the database table to find Person_FirstName
        $sql_Person = "SELECT  FirstName, company_id FROM person WHERE person_id =" . $person_id;

        //-run  the query against the mysql query function
        $result = mysql_query($sql_Person);
        //-create  while loop and loop through result set
        while ($row = mysql_fetch_array($result)) {
            $FirstName = $row['FirstName'];
            $company_id = $row['company_id'];
            //-display the result of the array
            echo "<ul>\n";
            echo "<b>First Name:</b> " . $FirstName;

        }


        //-query  the database table to find Person_MiddleName
        $sql_Person = "SELECT MiddleName FROM person WHERE person_id =" . $person_id;

        //-run  the query against the mysql query function
        $result = mysql_query($sql_Person);
        //-create  while loop and loop through result set
        while ($row = mysql_fetch_array($result)) {

            $MiddleName = $row['MiddleName'];

            //-display the result of the array
            echo "&nbsp";
            echo "<b>Middle Name:</b> " . $MiddleName;

        }

        //-query  the database table to find Person_LastName
        $sql_Person = "SELECT  LastName FROM person WHERE person_id =" . $person_id;

        //-run  the query against the mysql query function
        $result = mysql_query($sql_Person);
        //-create  while loop and loop through result set
        while ($row = mysql_fetch_array($result)) {

            $LastName = $row['LastName'];

            //-display the result of the array
            echo "&nbsp";
            echo "<b>Last Name:</b> " . $LastName;
            echo "</ul>";
        }

        echo "<p>";

        //-query  the database table to find Emails 
        $sql_Email = "SELECT  Email FROM email WHERE person_id =" . $person_id;

        //-run  the query against the mysql query function
        $result = mysql_query($sql_Email);
        //-create  while loop and loop through result set
        while ($row = mysql_fetch_array($result)) {
            $Email = $row['Email'];
            //-display the result of the array
            echo "<ul>";
            echo "<b>Email: </b> " . $Email;

        }


        //-query  the database table to find Email_type
        $sql_Email = " SELECT Email_types FROM email_type,email WHERE email_type.email_type_id = email.email_type_id AND person_id =" . $person_id;

        //-run  the query against the mysql query function
        $result = mysql_query($sql_Email);
        //-create  while loop and loop through result set
        while ($row = mysql_fetch_array($result)) {

            $Email_type = $row['Email_types'];

            //-display the result of the array
            echo "&nbsp";
            echo "<b>Email Type: </b> " . $Email_type;

        }


        //-query  the database table to find Email_status
        $sql_Email = "SELECT email_status FROM email_status,email WHERE email_status.email_status_id = email.email_status_id AND person_id =" . $person_id;

        //-run  the query against the mysql query function
        $result = mysql_query($sql_Email);
        //-create  while loop and loop through result set
        while ($row = mysql_fetch_array($result)) {
            $Email_status = $row['email_status'];
            //-display the result of the array
            echo "&nbsp";
            echo "<b>Email Status:</b> " . $Email_status;
            echo "</ul>";
        }

        echo "<p>";

        //-query  the database table to find contact Number
        $sql_contactnumber = "SELECT  contact_number  FROM contactnumbers WHERE person_id =" . $person_id;

        //-run  the query against the mysql query function
        $result = mysql_query($sql_contactnumber);
        //-create  while loop and loop through result set
        while ($row = mysql_fetch_array($result)) {
            $contact = $row['contact_number'];

            //-display the result of the array
            echo "<ul>";
            echo "<b>Contact: </b>" . $contact;

        }

        //-query  the database table to find contact Number Type
        $sql_contactnumber = "SELECT contact_number_types FROM contact_number_types,contactnumbers WHERE contact_number_types.contact_num_types_id = contactnumbers.contact_num_type_id AND contactnumbers.person_id  = " . $person_id;

        //-run  the query against the mysql query function
        $result = mysql_query($sql_contactnumber);
        //-create  while loop and loop through result set
        while ($row = mysql_fetch_array($result)) {

            $contact_type = $row['contact_number_types'];
            //-display the result of the array
            echo "&nbsp";
            echo "<b>Contact type: </b> " . $contact_type . "\n";
            echo "</ul> ";
        }
        echo "<p>";

        //-query  the database table to find Company
        $sql_company = "SELECT  company_name FROM company WHERE company_id =" . $company_id;

        //-run  the query against the mysql query function
        $result = mysql_query($sql_company);
        //-create  while loop and loop through result set
        while ($row = mysql_fetch_array($result)) {
            $company_name = $row['company_name'];

            //-display the result of the array
            echo "<ul>";
            echo "<b>Company Name: </b>" . $company_name;

        }

        //-query  the database table to find Company Type
        $sql_company = "SELECT company_type FROM company_type,company WHERE company_type.company_type_id = company.company_type_id AND company_id =" . $company_id;

        //-run  the query against the mysql query function
        $result = mysql_query($sql_company);
        //-create  while loop and loop through result set
        while ($row = mysql_fetch_array($result)) {

            $company_type = $row['company_type'];
            //-display the result of the array
            echo "&nbsp";
            echo "<b>Company Type: </b>" . $company_type;
            echo "</ul>";
        }

        //-query  the database table to find Product blast

        $sql_product_blast = "SELECT product_name FROM product,product_blast WHERE product.product_id = product_blast.product_id AND product_blast.person_id =  " . $person_id;


        //-run  the query against the mysql query function

        $result = mysql_query($sql_product_blast);

        //-create  while loop and loop through result set

        while ($row = mysql_fetch_array($result)) {
            $product_name = $row['product_name'];


            //-display the result of the array

            echo "<ul>\n";
            echo "<b>Product Blasted: </b>" . $product_name . "\n";
            echo "</ul>";

        }

    }
}
}

?>

1 个答案:

答案 0 :(得分:2)

问题在于,您始终使用$row = mysql_fetch_array($result),因此您始终会重写$result$row。所以当你上次调用它时,它会给你最后一个循环中的最后一行,并且在你的主循环中将终止。

试试这个:

$sql_search = "SELECT  person_id FROM contactnumbers WHERE contact_number = " . mysqli_real_escape_string($name);
$id_result = mysqli_query($link, $sql_search);
while ($id_row = mysqli_fetch_array($id_result)) {
    //....
}

1)避免sql注射

2)不要使用mysql函数。使用mysqli或PDO函数代替mysql_ *函数。