简体   繁体   English

如何将值从PHP表单插入另一个SQL查询

[英]How to insert a value from a PHP form to another SQL query

I have created a PHP form with radio buttons so on submit it will produce a roomCode based on the one I have selected. 我创建了一个带有单选按钮的PHP表单,因此在提交时,它将基于我选择的表单生成一个roomCode。 I am struggling to show the other SQL statement once the submit button has been pressed. 按下提交按钮后,我正努力显示其他SQL语句。

PHP: PHP:

<?php 
    if( isset( $_POST['submit'] ) )
    {
        if(isset($_POST['proj_check']))
            $proj_check = $_POST['proj_check'];
        else
            $proj_check = "N";

        $numeroOption= $_POST['numero'];
        $roomtype= $_POST['roomtype'];
        $selectOption = $_POST['parkname'];
        $query = "SELECT * FROM `ROOMS` 
            WHERE `Capacity` < '$numeroOption' 
            AND `Park` LIKE '$selectOption%'
            AND `dataProjector` LIKE '$proj_check%' 
            AND `Whiteboard` LIKE '$white_check%' 
            AND `OHP` LIKE '$ohp_check%' 
            AND `WheelchairAccess` LIKE '$wheel_check%' 
            AND `lectureCapture` LIKE '$cap_check%' 
            AND `Style` LIKE '$roomtype%'"; 
        $result = mysql_query($query);
        if ($result == FALSE) die ("could not execute statement $query<br />");
                echo "<form action='' method='post'>";
        echo "<table>"; 

        while($row = mysql_fetch_array($result)){                           
                echo "<tr><td>" . $row['roomCode'] . "</td></tr>";
                echo "<td><input type='radio' name='radioSelect' value= '". $row['roomCode']."'></td>";
                }
        echo "<input type='submit' name='ttroom' id='ttroom' name='ttroom'>";
        echo "</tr>";
        echo "</table>"; 
        echo "</form>";
    }

    if( isset( $_POST['ttroom'] ) )
    {
            $roomcode = $_POST['ttroom'];
            $try = "SELECT * FROM 'ROOMBOOKING' WHERE 'roomCode' = '$roomcode';";
            $ttres = mysql_query($try);
            if ($ttres == FALSE) die ("could not execute statement $try<br />");

                echo "<table>"; 

            while($ttrow = mysql_fetch_array($ttres)){                          
                echo "<tr><td>" . $ttrow['roomCode'] . "</td>";
            }
            echo "</tr>";
            echo "</table>"; 
    }               
    mysql_close();              
?>

Here's an example of something that I assume you're trying to do: 这是我假设您要尝试执行的操作的示例:

Website: 网站:

http://www.w3schools.com/php/php_form_complete.asp http://www.w3schools.com/php/php_form_complete.asp

What the above link does is basically creates a form, sets some form validation and asks the user to complete the form. 上面的链接所做的基本上是创建一个表单,设置一些表单验证并要求用户完成表单。 Once the user has completed the form and submitted the form, it echoes the results entered. 用户填写完表格并提交表格后,它将回显输入的结果。 Now I'm assuming that is what you're looking for. 现在,我假设这就是您想要的。 And if you're worried about communicating with the database, you could basically place all the form field names into individual variables and then run an insert query at the same time. 而且,如果您担心与数据库通信,则可以将所有表单字段名称放入单个变量中,然后同时运行插入查询。 So you can echo based on your insert query. 因此,您可以根据插入查询进行回显。 Now I noticed in your code, you used a table... forms, tables.. the concept is the same. 现在我在您的代码中注意到,您使用了表格...表格,表格..概念是相同的。 Give it a go, let me know what you get! 放手,让我知道您得到什么!

Hope this helps, 希望这可以帮助,

Sohail 索海尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM