Php 在HTML表单提交时插入数据库

Php 在HTML表单提交时插入数据库,php,html,mysql,sql,sql-insert,Php,Html,Mysql,Sql,Sql Insert,我有一个表单,允许用户输入数据。最后,可以按下提交按钮,将输入的数据提交到数据库中。我在几周前就开始工作了,但在其他一些项目上不得不绕道而行。我现在回到它,插入似乎不起作用,我不知道为什么 因此,我主要关注SQL语句的代码底部,但提供了其他HTML供您参考。您是否看到我的SQL语句中可能存在的任何问题 还请注意,如果未注释掉这段sql代码,则不会加载页面本身: INSERT INTO Stage_Rebate_Agreements ( Terms, Tier, `T

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

因此,我主要关注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);


}

?>

测试
供应商:
选择供应商
程序名:
节目时段:
删除
表单
中的
action=confirmation.php
属性,其结果是底部的提交代码不起作用

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


这就足够了

由于使用的语法,我将您的问题重新标记为
MySQL
,而不是
SQL Server
。您对SQL注入非常开放……在bobby tables访问之前,您需要阅读、理解并开始使用参数化查询。是的,这是一个内部使用的东西…我会去做的,但是要先解决这个问题,你需要检查错误---你的问题太多了,我们无法检查和调试。插入现在可以工作了…所以现在我只需要在单击提交后进入确认页面。我仍然需要在单击提交后进入确认页面。为什么不在确认页。。。或者在插入查询后重定向到confirmation.php我该怎么做重定向…听起来是更好的选择…我在网上看到的不是workingheader('Location:confirmation.php');