php - 在 HTML 表单上插入数据库提交

标签 php html mysql sql sql-insert

我有一个允许用户输入数据的表单。最后,可以按下一个提交按钮,将输入的数据提交到数据库中。几周前我就开始工作了,但不得不绕道去做其他几个项目。我现在回来了,插入似乎不起作用,我不知道为什么。

所以我主要关注代码底部的 SQL 语句,但提供了其他 HTML 供您引用。您是否发现我的 SQL 语句中可能存在任何问题?

另请注意,如果这部分 sql 代码未注释掉,则页面本身不会加载:

INSERT INTO Stage_Rebate_Agreements 
(
    Terms, 
    Tier, 
    `Tier Minimum`, 
    Multiplier, 
    UOM, 
    Retro, 
    Guaranteed, 
    Pay
) 
VALUES 
(
    '$_POST[rows[0][0][term]]',
    '$_POST[tier]',
    '$_POST[rows[0][0][purchase_minimum]]',
    '$_POST[rows[0][0][multiplier]]',
    '$_POST[rows[0][0][uom]]',
    '$_POST[rows[0][0][retro]]',
    '$_POST[rows[0][0][guaranteed]]',
    '$_POST[rows[0][0][paid]]'
)

但是如果没有注释掉,页面会由于某种原因无法加载。

如果我们能以某种方式解决这个问题,我们将不胜感激!

<?php
$host="xxxxxxxx";
    $dbName="xxxxxxxxx";
    $dbUser="xxxxxxx";
    $dbPass="xxxxxxxxxxxxx";
    $dbh = new PDO("sqlsrv:Server=$host;Database=$dbName", $dbUser, $dbPass);

    $usersQuery = "SELECT MR_Name, MR_POC_N, MR_POC_E, MR_POC_P FROM Stage_Rebate_Master";

    $users = $dbh->query($usersQuery);

?>

<html>
<body>

    <div class="wrapper">

      <header>
          <h2>test</h2>
      </header>

    <div class="container">

<form name="form1" action="confirmation.php" method="POST">

<!-- Vendor -->

<section class="desc-block-left0" align="left">
<div>
<div id="vendor">
<strong>Vendor:</strong>
</div> 

<div class="align">
<select name="vendor_dropdown" id="ven" onChange="updateinput();">
    <option value="">Choose a Vendor</option>
        <?php foreach($users->fetchAll() as $user): ?>
            <option
                    data-name="<?php echo $user['MR_POC_N'];?>"
                    data-email="<?php echo $user['MR_POC_E'];?>"
                    data-phone="<?php echo $user['MR_POC_P'];?>"
            >
                <?php echo $user['MR_Name'];?>
            </option>
        <?php endforeach; ?>
</select>
</div>

</div>
</section>

<!-- Program Name -->

<section class="desc-block-right0">
<div>
<div id="program_name">
<strong>Program Name:</strong><br>
</div>

<div class="align1">
<input class="textbox" type="text" id="program_name1" name="program_name" style="width: 50%">
</div>

</div>
</section>

<!-- Program Period -->

<section class="desc-block-left1">
<div>
<div id="program_period">
<strong>Program Period:</strong>
</div>

<div class="align">
<input type="text" name="program_period_dropdown" id="period" value="<?php echo date("Y");?>" readonly style="width: 50px">
</div>

</div>
</section>

<!-- Terms -->

<section class="desc-block-right1">
<div>
<div id="terms">
<strong>Terms:</strong><br>
</div>

<div class="align1">
<input type="text" id="year" name="term" style="width: 50px">
<label for="percent">%</label>
<input type="text" id="year" name="term" style="width: 50px">
<label for="percent">Net</label>
<input type="text" id="year" name="term" style="width: 50px">
</div>

</div>
</section>

<!-- Vendor Type -->

<section class="desc-block-left2">
<div>
<div id="vendor_type">
<strong>Vendor Type:</strong><br>
</div>

<div class="align">
<table align="left">
    <tr align="center">
        <td><input type="radio" name="ven_type" value="Coded" id="type" checked="true"></td>
        <td align="left">Coded</td>
    </tr>
    <tr align="center">
        <td><input type="radio" name="ven_type" value="Non-Coded" id="type"></td>
        <td>Non-Coded</td>
    </tr>
</table>
</div>

</div>
</section>

<!-- Contact Information -->

<section class="desc-block-right2" align="center">
<div>
<div id="contact_info">
<strong>Contact Information:</strong><br>
</div>

<div class="align1">
<table align="left" id="contact">
<tr align="left">
    <th>Name</th>
    <th>Email</th>
    <th>Phone Number</th>
</tr>
    <tr>
        <td><input type="text" id="name" class="name" name="name"></td>
        <td><input type="email" id="email" class="email" name="email"></td>
        <td><input type="tel" id="tel" class="tel" name="number"></td>
        <td><input type="button" class="save" name="edit" value="Save"></td>
    </tr>
</table>
</div>

</div>
</section>

<!-- 400 Rebate Tables -->
<!-- 400P -->

<div align="center" id="border">
<div id="rebate_400p">
<strong>400P</strong><br>
</div>

<table id="tables" cellspacing="5">
    <tr align="center" class="table_titles">
        <td>Tier</td>
        <td>Purchase Minimum</td>
        <td>Multiplier</td>
        <td>UOM</td>
        <td>Retro</td>
        <td>Guaranteed</td>
        <td>Paid</td>
        <td>Delete?</td>
        <td>Add Row</td>
    </tr>
    <tr>
            <td align="center" name="tier" id="tier">1</td>
            <td><input type="text" class="rebate_tables" id="purchase_min" data-name="purchase_minimum" name="rows[0][0][purchase_minimum]"></td>
            <td><input type="text" class="rebate_tables" id="multiplier" data-name="multiplier" name="rows[0][0][multiplier]"></td>
            <td><input type="text" class="rebate_tables" id ="uom" data-name="uom" name="rows[0][0][uom]"></td>
            <td><input type="text" class="rebate_tables" id="retro" data-name="retro"  name="rows[0][0][retro]"></td>
            <td><input type="text" class="rebate_tables" id="guaranteed" data-name="guaranteed" name="rows[0][0][guaranteed]"></td>
            <td><input type="text" class="rebate_tables" id="paid" data-name="paid" name="rows[0][0][paid]"></td>
            <td><input type="button" class="delRow" value="Delete" onclick="deleteRow(this)"></td>
            <td><input type="button" class="addmoreRowsbutton" value="Add row" onclick="insRow()"></td>
        </tr>
</table>

<!-- 400M -->
<div id="rebate_400m">
<strong>400M</strong><br>
</div>

<table cellspacing="5" id="tables1" style="display: none;">
    <tr align="center" class="table_titles">
        <td>Tier</td>
        <td>Purchase Minimum</td>
        <td>Multiplier</td>
        <td>UOM</td>
        <td>Retro</td>
        <td>Guaranteed</td>
        <td>Paid</td>
        <td>Delete?</td>
        <td>Add Rows</td>
    </tr>
    <tr>
            <td align="center" name="tier" id="tier1">1</td>
            <td><input type="text" class="rebate_tables" id="purchase_min1" data-name="purchase_minimum" name="rows[1][0][purchase_minimum]"></td>
            <td><input type="text" class="rebate_tables" id="multiplier1" data-name="multiplier" name="rows[1][0][multiplier]"></td>
            <td><input type="text" class="rebate_tables" id ="uom1" data-name="uom" name="rows[1][0][uom]"></td>
            <td><input type="text" class="rebate_tables" id="retro1" data-name="retro"  name="rows[1][0][retro]"></td>
            <td><input type="text" class="rebate_tables" id="guaranteed1" data-name="guaranteed" name="rows[1][0][guaranteed]"></td>
            <td><input type="text" class="rebate_tables" id="paid1" data-name="paid" name="rows[1][0][paid]"></td>
            <td><input type="button" class="delRow" value="Delete" onclick="deleteRow(this)"></td>
            <td><input type="button" class="addmoreRowsbutton" value="Add row" onclick="insRow1()"></td>
        </tr>
</table>
<!-- Button to display table for 400M -->
<input type="button" name="row" value="+" onclick="show2();"/>

<!-- 400D -->

<div id="rebate_400d">
<strong>400D</strong><br>
</div>

<table cellspacing="5" id="tables2" style="display: none;">
    <tr align="center" class="table_titles">
        <td>Tier</td>
        <td>Purchase Minimum</td>
        <td>Multiplier</td>
        <td>UOM</td>
        <td>Retro</td>
        <td>Guaranteed</td>
        <td>Paid</td>
        <td>Delete?</td>
        <td>Add Rows</td>
    </tr>
    <tr>
            <td align="center" name="tier" id="tier2">1</td>
            <td><input type="text" class="rebate_tables" id="purchase_min2" data-name="purchase_minimum" name="rows[2][0][purchase_minimum]"></td>
            <td><input type="text" class="rebate_tables" id="multiplier2" data-name="multiplier" name="rows[2][0][multiplier]"></td>
            <td><input type="text" class="rebate_tables" id ="uom2" data-name="uom" name="rows[2][0][uom]"></td>
            <td><input type="text" class="rebate_tables" id="retro2" data-name="retro"  name="rows[2][0][retro]"></td>
            <td><input type="text" class="rebate_tables" id="guaranteed2" data-name="guaranteed" name="rows[2][0][guaranteed]"></td>
            <td><input type="text" class="rebate_tables" id="paid2" data-name="paid" name="rows[2][0][paid]"></td>
            <td><input type="button" class="delRow" value="Delete" onclick="deleteRow(this)"></td>
            <td><input type="button" class="addmoreRowsbutton" value="Add row" onclick="insRow2()"></td>
        </tr>
</table>
<!-- Button to adisplay table for 400D -->
<input type="button" name="row" value="+" id="plus-button" onclick="show3();"/>
</div>

<!-- Products -->

<div id="align">
<p>
<div id="products">
<strong>Products:</strong><br>
</div>

<input type="radio" name="product" value="All" onclick="hide();"/ checked> All 
<input type="radio" name="product" value="Category" onclick="show();"/> Category 
<input type="radio" name="product" value="Sku" onclick="hide();"/> Sku
    <select multiple id="area" name="category" style="display: none;">
        <option value="Example A" align ="center">Example A</option>
        <option value="Example B" align ="center">Example B</option>
        <option value="Example C" align ="center">Example C</option>
        <option value="Example D" align ="center">Example D</option>
        <option value="Example E" align ="center">Example E</option>
        <option value="Example F" align ="center">Example F</option>
    </select>
</p>

<!-- Submit Button -->

<br>
<input type="submit" value="Submit" id="btn" name="submit">
</div>

</form>

    </div>

    <div class="push"></div>
    </div>
    <div class="footer">
    <footer>
        <h3>test</h3>
    </footer>
    </div>

</body>
</html>


<?php

if(isset($_POST['submit']))

  {

  $host="xxxxxxxxxxx"; 
  $dbName="xxxxxx"; 
  $dbUser="xxxxxxxxxxxxxx"; 
  $dbPass="xxxxxxxxxx";

  $pdo = new PDO("sqlsrv:server=".$host.";Database=".$dbName, $dbUser, $dbPass);

  $sql = "INSERT INTO Stage_Rebate_Master (MR_Name, MR_POC_N, MR_POC_E, MR_POC_P) VALUES ('$_POST[vendor_dropdown]','$_POST[name]','$_POST[email]','$_POST[number]')";

  $sql1 = "INSERT INTO Stage_Rebate_Programs (Program_Year) VALUES ('$_POST[program_period_dropdown]')";

  $sql2 = "INSERT INTO Stage_Rebate_Agreements (Terms, Tier, `Tier Minimum`, Multiplier, UOM, Retro, Guaranteed, Pay) VALUES ('$_POST[rows[0][0][term]]','$_POST[tier]','$_POST[rows[0][0][purchase_minimum]]','$_POST[rows[0][0][multiplier]]','$_POST[rows[0][0][uom]]','$_POST[rows[0][0][retro]]','$_POST[rows[0][0][guaranteed]]','$_POST[rows[0][0][paid]]')";

  $sql3 = "INSERT INTO Stage_Rebate_Programs (Program_Name) VALUES ('$_POST[program_name]')";

  $stmt = $pdo->prepare($sql);
  $stmt1 = $pdo->prepare($sql1);
  $stmt2 = $pdo->prepare($sql2);
  $stmt3 = $pdo->prepare($sql3);

  $result = $stmt->execute();
  $result1 = $stmt1->execute();
  $result2 = $stmt2->execute();
  $result3 = $stmt3->execute();

  echo json_encode($result);
  echo json_encode($result1);
  echo json_encode($result2);
  echo json_encode($result3);


}

?>

最佳答案

删除form中的action=confirmation.php属性,导致底部提交代码不起作用

<form name="form1" method="POST">

够了

关于php - 在 HTML 表单上插入数据库提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42093220/

相关文章:

javascript - PHP MySQL : count views per month for jQuery charts

php - 购物车中不显示总金额

javascript - 如何根据用户输入更改背景图像 | HTML、CSS、JS

php - PDO 准备好的语句有多安全

下拉列表值更改时 PHP 更新文本框

php: array_replace_recursive 替代

javascript - ajax post 不工作 php mysql

PHP 将 csv 列读入数组

html - 如何创建波浪形CSS?

html - 如何制作 :hover pseudoclass affect the parent element?