我发现了类似的问题,但无法将其与我的示例联系起来。我是PHP的新手,完全是自学的。


  目前,我有一个输入新客户的表格。以这种形式,我希望用户能够选择一个现有的数据库项目(业务)并将该BusinessID插入到CUSTOMER表中。我的问题是我可以获取BusinessID,但是随后我无法将其与其他字段输入一起发布。下面的代码


<!DOCTYPE html>

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8" />


    <title>New Contact</title>

    <!--Declare CSS and JavaScript-->
    <link rel="stylesheet" type="text/css" href="RealtyCRM_Style.css">
    <script type="text/javascript" src="http://code.jquery.com/jquery-latest.js"></script>
    <script type="text/javascript" src="jquery.resmenu.min.js"></script>

</head>
<body>
    <script>
        $(document).ready(function () {
            $('.toresponsive').ReSmenu();
        });
    </script>
    <!--Begin Header Code-->
    <!--Begin Header Code-->
    <div class="BodyHeader">
    <div>
        <a href='index.php'><img src='RealEstate(1).jpg' alt='LeasingLog_Logo' class="LeasingLog_Logo"></a>
    </div>
    </div>
        <!--Begin Menu Code-->
        <div class="menu_container" style="position:relative; z-index:11;">
            <ul class="toresponsive">
                <li><a href="logIn.php">Log In</a></li>
                <li><a href="contact.php">Contact</a></li>
                <li><a href="news.php">News</a></li>
                <li class="current-menu-item"><a href="dashboard.php">Dashboard</a>
                    <ul>
                        <li><a href="dataEntry.php">Add New Data</a></li>
                        <li><a href="updatedata.php">Update Data</a></li>
                        <li><a href="search.php">Search</a></li>
                        <li><a href="reports.php">Report</a></li>
                        <li><a href="adminPage.php">Admin Page</a></li>
                        <li><a href="logInteraction.php">Log Interaction</a></li>
                    </ul>
                </li>
            </ul>
        </div>

    <br>

    <!--Begin Dashboard Buttons Code-->
    <div class="DashboardButtonsTop">
        <a href="retailerdataentry.php"><h1 class="centeredDashBoardButtonInactive">New Retailer</h1></a>
        <a href="contactdataentry.php"><h1 class="centeredDashBoardButton">New Contact</h1></a>
        <a href="propertydataentry.php"><h1 class="centeredDashBoardButtonInactive">New Property</h1></a>
    </div>
    <hr style="width:700px; height:5px;">
    <br>
    <br>
    <!--END Dashboard Buttons Code-->
      <?php
         if(isset($_POST['add']))
         {
            $dbhost = 'localhost';
            $dbuser = 'leasingl_dbwrite';
            $dbpass = 'password';
            $conn = mysql_connect($dbhost, $dbuser, $dbpass);

            if(! $conn )
            {
               die('Could not connect: ' . mysql_error());
            }

            if(! get_magic_quotes_gpc() )
            {
               $contactFirstName = addslashes ($_POST['contactFirstName']);
               $contactLastName = addslashes ($_POST['contactLastName']);
            }
            else
            {
               $contactFirstName = $_POST['contactFirstName'];
               $contactLastName = $_POST['contactLastName'];
            }

            $contactPhoneNumber = $_POST['contactPhoneNumber'];
            $contactEmail = $_POST['contactEmail'];
            $Business = $_POST['BusinessID'];


            $sql = "INSERT INTO Contact ". "(ContactFName,ContactLName, ContactMobilePhone, contactEmail, BusinessID, CreatedDate) ". "VALUES('$contactFirstName','$contactLastName','$contactPhoneNumber', '$contactEmail', '$Business', NOW())";

            mysql_select_db('applicationDatabase');
            $retval = mysql_query( $sql, $conn );

            if(! $retval )
            {
               die('Could not enter data: ' . mysql_error());
            }

            echo "<div style='text-align:center;'>Entered data successfully\n</div>";
            echo "<br><div><a href='contactdataentry.php' class='redirectButton'>Add More Contacts</a>\n</div>";
            echo "<br><div><a href='dashboard.php' class='redirectButton'>Return to Dashboard</a></div>";

            mysql_close($conn);
         }
         else
         {
            ?>

               <div class="Form_container">
                <form method="post" action="<?php $_PHP_SELF ?>">
                    Contact First Name<br>
                    <input class="largeInput" type="text" name="contactFirstName" ID="contactFirstName"><br>
                    Contact Last Name<br>
                    <input class="largeInput" type="text" name="contactLastName" ID="contactLastName"><br>
                    Contact Phone Number<br>
                    <input class="largeInput" type="text" name="contactPhoneNumber" placeholder="### - ### - ####" ID="contactPhoneNumber"><br>
                    Contact Email<br>
                    <input class="largeInput" type="text" name="contactEmail"><br>
                    Business<br>
                <?php
                    $servername = "localhost";
                    $username = "leasingl_dbread";
                    $password = "password";
                    $dbname = "applicationDatabase";

                    // Create connection
                    $conn = new mysqli($servername, $username, $password, $dbname);
                    // Check connection
                    if ($conn->connect_error) {
                        die("Connection failed: " . $conn->connect_error);
                    }

                    $sql = "SELECT RetailerID, RetailerName FROM Retailer ORDER BY RetailerName DESC";
                    $result = $conn->query($sql);
                    ?>
                        <select style='text-align:center;' class='largeInput' name='Business' ID='Business'>
                    <?php
                        if ($result->num_rows > 0) {
                            // output data of each row
                            while($row = $result->fetch_assoc()) {
                                echo "<option value='". $row["RetailerID"]. "'>" . $row["RetailerName"]. " - " . $row["RetailerID"]. "</option><br><br>";
                            }
                        } else {
                            echo "0 results";
                        }
                    ?>
                        </select><br><br>
                    <?php
                        $conn->close();
                    ?>
                    <input name="add" type="submit" id="add" value="Add Contact" class="button">
                </form>
                <br>
                <hr style="width:400px; height:10px;">
            </div>

            <?php
         }
      ?>

   </body>
</html>'


因此,能够从下拉菜单中插入值是主要问题。另外,我确定我发布的内容中没有不必要的/不正确的代码,我一次将示例拼凑在一起。

感谢您提供的所有帮助,如果我可以正常工作,那么我的应用程序可以正常运行

编辑


  我已经成功地预填充了下拉菜单,然后用户从该列表中进行选择。我想通过我的INSERT语句传递该选择。我担心我建立的两个不同的连接是我的插入无法识别$ Business的部分原因

最佳答案

看来您是在以混乱的方式指代GET。在PHP中,有$_GET$_POST变量,当您在大写形式中提到GET时,就意味着您正在使用$_GET-实际上,您不是。

就我所理解的问题而言,解决方案实际上非常简单。

在表单内部,添加一个隐藏的输入来存储(然后传递)BusinessID变量,如下所示:

<input type="hidden" name="BusinessID" value="<?php echo $Business; ?>">


正如您提到的,您只是在学习,这里还有一些其他提示:


始终为变量命名。如果数据库列的名称为BusinessID,则将变量命名为$businessID" and your input BusinessID”。
感谢您提供良好的缩进/格式化功能!这样可以节省故障排除/读取自己的代码时的时间!


编辑
如果您要尝试的是在下拉列表中预先选择记录,则可以像这样更改循环:

while($row = $result->fetch_assoc()) {
    // Note: I've removed the <br> tags here, they don't belong in a select dropdown
    echo "<option value='". $row["RetailerID"]. "'";
    // If the ID matches, make this the selected option
    // NOTE: Per my tip above, I'd strongly recommend changing the variable name $Business to match the field name - $RetailerID in this case
    echo ($row['RetailerID'] == $Business) ? ' selected' : '';
    echo ">" . $row["RetailerName"]. " - " . $row["RetailerID"]. "</option>";
}

10-06 09:44