drxp993551 2016-01-03 14:27
浏览 26

PHP POST和GET在同一语句中

I've found similar questions, but have been unable to tie them into my example. I am very new to PHP and completely self teaching.

At present I have a form for entering a new customer. In that form I want the user to be able to select an existing DB item (business) and insert that BusinessID into the CUSTOMER table. My problem is that I can GET the BusinessID, but then I can't POST that same ID with the other field inputs. Code below

<!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
</div>";
            echo "<br><div><a href='contactdataentry.php' class='redirectButton'>Add More Contacts</a>
</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>'

So being able to insert the value from my drop down is the main issue. Additionally, I'm sure there is unnecessary / incorrect code in what I posted, I've been piecing together examples one at a time.

Thank you for all the help, if I can get this working I can have a functioning basic version of my application

EDIT

I have successfully prepopulated my drop down, and the user then chooses from that list. I want to pass that choice via my INSERT statement. I worry that the two different CONNECTIONS which I establish are part of the reason my INSERT won't recognize $Business

  • 写回答

1条回答 默认 最新

  • duanrongpai9556 2016-01-03 14:36
    关注

    It appears you are referring to GET in a confusing way. In PHP there is $_GET and $_POST variables, and when you mention GET in all-caps, in implies you are using $_GET - which in fact you are not.

    The solution - as I understand the question - is actually fairly straightforward.

    Inside your form, add a hidden input that stores (and then passes) the BusinessID variable, like so:

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

    As you mention you are just learning, here's some additional tips:

    1. Name your variables consistently throughout. If the name of the database column is BusinessID, then name your variable $businessID" and your inputBusinessID".
    2. Kudos to you for good indenting / formatting! That will save you gobs of time when troubleshooting / reading your own code!

    EDIT
    If what you are trying to do is pre-select the record in the dropdown, then alter your loop like so:

    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>";   
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错