填充下拉列表,传递两个变量

时间:2017-01-02 06:18:09

标签: php mysql

我想从另一个mysql表中填充下拉列表,然后将两个列中的值分配给变量 - 即“select employee,eid,perc from employee”。 “John Doe”将是$ eid = 1234和$ perc = 20.

非常感谢任何帮助!

谢谢 - 马特

以下是我一直在使用的代码:

PHP

<?php

   //session_start();

   $page_title = 'New invoice';
include ('includes/header.html');

// Check for form submission:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    require ('mysqli_connect.php'); // Connect to the db.

    /*$errors = array(); // Initialize an error array. */

    // Invoice number is automatic

    if (empty($_POST['op1'])) {
        $errors[] = 'Operation needs to be entered.';
        } else {
        $op1 = mysqli_real_escape_string($dbc, trim($_POST['op1']));
        }


    // Amount:
    if (empty($_POST['amount1'])) {
        $errors[] = 'Amount to be charged.';
        } else {
        $amount1 = mysqli_real_escape_string($dbc, trim($_POST['amount1']));
        }


    // percentage:
    if (empty($_POST['perc'])) {
        $errors[] = 'Select a percentage.';
        } else {
        $perc = mysqli_real_escape_string($dbc, trim($_POST['perc']));
        }

    // eid:
    if (empty($_POST['eid'])) {
        $errors[] = 'Enter a techician.';
        } else {
        $eid = mysqli_real_escape_string($dbc, trim($_POST['eid']));
        }

    // Stocknum:
    if (empty($_POST['stocknum'])) {
        $errors[] = 'Need a stock number.';
        } else {
        $stocknum = mysqli_real_escape_string($dbc, trim($_POST['stocknum']));
        }

    // Stocknum:
    if (empty($_POST['myear'])) {
        $errors[] = 'Enter vehicle year.';
        } else {
        $myear = mysqli_real_escape_string($dbc, trim($_POST['myear']));
        }       

    if (empty($_POST['make'])) {
        $errors[] = 'Enter vehicle make.';
        } else {
        $make = mysqli_real_escape_string($dbc, trim($_POST['make']));
        }

    if (empty($_POST['model'])) {
        $errors[] = 'Enter vehicle model.';
        } else {
        $model = mysqli_real_escape_string($dbc, trim($_POST['model']));
        }   

    if (empty($_POST['vin'])) {
        $errors[] = 'Enter last 6 of the VIN.';
        } else {
        $vin = mysqli_real_escape_string($dbc, trim($_POST['vin']));
        }   

    if (empty($_POST['mileage'])) {
        $errors[] = 'Enter current mileage.';
        } else {
        $mileage = mysqli_real_escape_string($dbc, trim($_POST['mileage']));
        }

    if (empty($errors)) { // If everything's OK.
        $q = "INSERT INTO `mwcc`.`wp` (`tdate`, `stocknum`, `myear`, `make`, `model`,`vin`, `eid`, `op1`, `amount1`,`mileage`,`ecomm`) VALUES (CURRENT_DATE(), '$stocknum', '$myear', '$make', '$model','$vin', '$eid', '$op1', '$amount1','$mileage', ($amount1*$perc));";
        $r = @mysqli_query ($dbc, $q); // Run the query.
        //echo ($q); 
        if ($r) { // If it ran OK.

            // Print a message:
            echo '<h1>Success!</h1>
        <p>Invoice has been created!<br /></p>';    

        } else { // If it did not run OK.

            // Public message:
            echo '<h1>System Error</h1>
            <p class="error">Uh oh. There has been an error. We apologize for any inconvenience.</p>'; 

            // Debugging message:
            echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';

        } // End of if ($r) IF.

        mysqli_close($dbc); // Close the database connection.

        exit();

    } else { // Report the errors.

        echo '<h1>Error!</h1>
        <p class="error">The following error(s) occurred:<br />';
        foreach ($errors as $msg) { // Print each error.
            echo " - $msg<br />\n";
        }
        echo '</p><p>Please try again.</p><p><br /></p>';

    } // End of if (empty($errors)) IF.

    mysqli_close($dbc); // Close the database connection.


} // End of the main Submit conditional.

?>

HTML:

 <form action="newinv.php" method="post">
        <p>Stock #
        <input type="text" name="stocknum" size="15" maxlength="20" value="<?php if (isset($_POST['stocknum'])) echo $_POST['stocknum']; ?>" />
        Last 6 of VIN 
        <input type="text" name="vin" size="15" maxlength="6" value="<?php if (isset($_GET['vin'])) echo $_POST['vin']; ?>" /> </p>
        <p>Year 
        <input type="text" name="myear" size="4" maxlength="4" value="<?php if (isset($_POST['myear'])) echo $_POST['myear']; ?>" />
        Make 
        <input type="text" name="make" size="30" maxlength="20" value="<?php if (isset($_POST['make'])) echo $_POST['make']; ?>" />
        Model 
        <input type="text" name="model" size="30" maxlength="20" value="<?php if (isset($_POST['model'])) echo $_POST['model']; ?>" /></p>
        Mileage
        <input type="text" name="mileage" sizesize="15" maxlength="6" value="<?php if (isset($_POST['mileage'])) echo $_POST['mileage']; ?>" /> </p>
        <p>Operation <input type="text" name="op1" size="60" maxlength="250" value="<?php if (isset($_POST['op1'])) echo $_POST['op1']; ?>" />
            Amount <input type="text" name="amount1" size="8" maxlength="20" value="<?php if (isset($_POST['amount1'])) echo $_POST['amount1']; ?>" /></p>
        <br>
        <input type="radio" name="eid" value="1767">Alex H<br>
        <input type="radio" name="eid" value="1688">Blake S<br>
        <input type="radio" name="eid" value="1506">Brian M<br>
        <input type="radio" name="eid" value="1898">Chris V<br>
        <input type="radio" name="eid" value="3000">Kim R<br>
        <input type="radio" name="eid" value="1916">Jorden U<br>
        <input type="radio" name="eid" value="1931">Tina M<br>
        <input type="radio" name="eid" value="1506">Tanner C<br>    
        <br>
        <input type="radio" name="perc" value=".35">35%
        <br>
        <input type="radio" name="perc" value=".40">40%
        <p><input type="submit" name="submit" value="Add" /></p>
        </form>

2 个答案:

答案 0 :(得分:0)

我对你问题的理解。

按照您的提及获取查询结果。select name, eid, perc from employee

对于前端,如果你想在单一选择中传递两个值,那么使用一些独特的分隔符,比如我使用双下划线__

<?php foreach($result as $user): ?>
     <select name="eid__perc" >
      <option value="<?php  $user->eid . '__' . $user->perc?>">
        <?php $user->name; //in array case $user['name'];?>
      <option>
     <select>
<?php endforeach;?>

当您保存信息时,请使用相同的分隔符来爆炸数据,如

list($eid, $perc) = explode('__', $_POST['eid__per'])

答案 1 :(得分:-1)

您需要使用WHERE条件:

SELECT name, eid, perc FROM employee WHERE eid = ? AND perc = ?

使用mysqli_stmt_bind_param($stmt, 'ss', $eid, $perc);绑定参数。