简体   繁体   English

使用 HTML 表格将数据插入 MySQL 表

[英]Inserting Data into MySQL table using HTML form

I am currently trying to insert rows into a MySQL database, and most of the code is there but I'm having a few issues I can't diagnose.我目前正在尝试将行插入 MySQL 数据库,大部分代码都在那里,但我遇到了一些我无法诊断的问题。 I know the database connection is good, and every time the submit button is pressed it runs the correct php script.我知道数据库连接良好,每次按下提交按钮时,它都会运行正确的 php 脚本。 The issue I'm having is that it always adds 2 records to the database table and fails to carry though any of the form data (it inserts two completely blank rows.)我遇到的问题是它总是向数据库表中添加 2 条记录,并且无法携带任何表单数据(它插入两个完全空白的行。)

Here's the code for the form (with a little extra code for the wordpress page)这是表单的代码(wordpress 页面的一些额外代码)

  <div class="main-container">
<div class="content-area">
    <div class="middle-align">
        <div class="site-main" id="sitefull">
            <?php while ( have_posts() ) : the_post(); ?>
                <?php get_template_part( 'content', 'page' ); ?>
            <div>
                <form method="POST" name="cutting tool" action="add-tool-script.php">
                    <table style="width:auto;">
                        <tr>
                            <th width="50%"><h2><ul>Tool Information</ul></h2><br></th>
                            <th width="50%"><ul><h2>Storage Information</h2></ul><br></th>
                        </tr>
                        <tr>
                            <td width="50%">
                                <h3>Tooling Name</h3><br>
                                <input type="text" name="name" placeholder="e.g. ShearHog"><br><br>

                                <h3>Tooling Type</h3><br>
                                <select name="type">
                                    <option selected disabled hidden style='display: none' value=''></option>
                                    <option value="Ballnose Endmill">Ballnose Endmill</option>
                                    <option value="Bullnose Endmill">Bullnose Endmill</option>
                                    <option value="Boring Bar">Boring Bar</option>
                                    <option value="Brush">Brush</option>
                                    <option value="Burnishing">Burnishing</option>
                                    <option value="Chamfer Mill">Chamfer Mill</option>
                                    <option value="Countersink">Countersink</option>
                                    <option value="Drill">Drill</option>
                                    <option value="Drill/Mill">Drill/Mill</option>
                                    <option value="Engraver">Engraver</option>
                                    <option value="Face Mill">Face Mill</option>
                                    <option value="Flat Endmill">Flat Endmill</option>
                                    <option value="High Feed Mill">High Feed Mill</option>
                                    <option value="Reamer">Reamer</option>
                                    <option value="Slitting Saw">Slitting Saw</option>
                                    <option value="Spot Drill">Spot Drill</option>
                                    <option value="Tap">Tap</option>
                                    <option value="Threadmill">Threadmill</option>
                                    <option value="Woodruff">Woodruff</option>
                                    <option value="Other">Other</option>
                                </select><br><br>

                                <h3>Tooling Brand</h3><br>
                                <input type="text" name="brand" placeholder="e.g. Lakeshore Carbide"><br><br>

                                <h3>Part Number</h3><br>
                                <input type="text" name="part_number" placeholder="e.g. 360014X"><br><br>

                                <h3>Price</h3><br>
                                <input type="text" name="price" placeholder="e.g. 24.95"><br><br>

                                <h3>Overall Length</h3><br>
                                <input type="text" name="oal" placeholder="e.g. 2.5"><br><br>

                                <h3>Tooling Material</h3><br>
                                <select name="material">
                                    <option selected disabled hidden style='display: none' value=''></option>
                                    <option value="Carbide">Carbide</option>
                                    <option value="Ceramic">Ceramic</option>
                                    <option value="Diamond">Diamond</option>
                                    <option value="HSS">HSS</option>
                                    <option value="Powdered Metal">Powdered Metal</option>
                                </select><br><br>

                                <h3>Cutting Diameter</h3><br>
                                <input type="text" name="cutting_diam" placeholder="e.g. 0.250"><br><br>

                                <h3>Shank Diameter</h3><br>
                                <input type="text" name="shank_diam" placeholder="e.g. .250"><br><br>

                                <h3>Number of Flutes</h3><br>
                                <input type="text" name="flutes" placeholder="e.g. 3"><br><br>

                                <h3>Length of Cut (Flute Length)</h3><br>
                                <input type="text" name="loc" placeholder="e.g. .750"><br><br>

                                <h3>Corner Radius</h3><br>
                                <input type="text" name="corner_rad" placeholder="e.g. .004"><br><br>
                            </td>
                            <td width="50%">
                                <h3>Quantity in Stock</h3><br>
                                <input type="text" name="qty" placeholder="e.g. 37"><br><br>

                                <h3>Minimum Trigger Quantity</h3><br>
                                <input type="text" name="trigger_qty" placeholder="e.g. 4"><br><br>

                                <h3>Reorder Link</h3><br>
                                <input type="text" name="reorder_link" placeholder="e.g. example.com"><br><br>

                                <h3>Toolbox Number</h3><br>
                                <input type="text" name="toolbox_no" placeholder="e.g. 1"><br><br>

                                <h3>Drawer Number</h3><br>
                                <input type="text" name="drawer_no" placeholder="e.g. 1"><br><br>

                                <h3>Bin Number</h3><br>
                                <input type="text" name="bin_no" placeholder="e.g. 1"><br><br>

                                <h3>Product</h3><br>
                                <input type="text" name="product" placeholder="e.g. Widget #2"><br><br>

                                <input type="submit" value="Add to Tool Crib" name="submitbutton" action="submit"/>
                            </td>
                        </tr>
                    </table>

                </form>
            </div>
                <?php
                //If comments are open or we have at least one comment, load up the comment template
                    if ( comments_open() || '0' != get_comments_number() )
                    comments_template();
                ?>
            <?php endwhile; // end of the loop. ?>
        </div>
        <div class="clear"></div>
    </div>
</div>
</div>

<?php get_footer(); ?>

And here's the code for the php script to add form data to the database:这是 php 脚本的代码,用于将表单数据添加到数据库:

   //MySQL Database
    $servername = "url.com";
    $username = "user_login";
    $password = "user_password";
    $datab = "database_name";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $datab);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
  }

$name = $_REQUEST['name'];
$type = $_REQUEST['type'];
$brand = $_REQUEST['brand'];
$part_number = $_REQUEST['part_number'];
$price = $_REQUEST['price'];
$oal = $_REQUEST['oal'];
$material = $_REQUEST['material'];
$cutting_diam = $_REQUEST['cutting_diam'];
$shank_diam = $_REQUEST['shank_diam'];
$flutes = $_REQUEST['flutes'];
$loc = $_REQUEST['loc'];
$corner_rad = $_REQUEST['corner_rad'];
$qty = $_REQUEST['qty'];
$trigger_qty = $_REQUEST['trigger_qty'];
$reorder_link = $_REQUEST['reorder_link'];
$toolbox_no = $_REQUEST['toolbox_no'];
$drawer_no = $_REQUEST['drawer_no'];
$bin_no = $_REQUEST['bin_no'];
$product = $_REQUEST['product'];
$username = $user_login;

$sql = "INSERT INTO `cutting tools` (`name`, `type`, `brand`, `part_number`, `reorder_link`, `oal`, `price`, `material`, `cutting_diam`, `shank_diam`, `flutes`, `loc`, `corner_rad`, `qty`, `trigger_qty`, `user`, `drawer_no`, `bin_no`, `toolbox_no`) 
        VALUES ('$name', '$type', '$brand', '$part_number', '$reorder_link', '$oal', '$price', '$material', '$cutting_diam', '$shank_diam', '$flutes', '$loc', '$corner_rad', '$qty', '$trigger_qty', '$username', '$drawer_no', '$bin_no', '$toolbox_no')";


if(mysqli_query($conn, $sql)){
    echo "Record added successfully.";
} else{
    echo "ERROR: Could not execute $sql. " . mysqli_error($conn);
}

// Close connection
mysqli_close($conn);
?>

Also I know my database is vulnerable to injection, that was a change I planned on making once the form was up and running.另外我知道我的数据库容易受到注入,这是我计划在表单启动并运行后进行的更改。

Use WordPress to your advantage.使用 WordPress 对您有利。 Instead of defining your own connection, use global $wpdb, and then use the insert command.不要定义自己的连接,而是使用 global $wpdb,然后使用 insert 命令。

global $wpdb;

$success = $wpdb->insert('tbl_name', array(<br>
   'field1_name' => $_REQUEST['field1'],<br>
   'field2_name' => $_REQUEST['field2'],<br>
));<br>
if($success){<br>
   echo "Inserted correctly";<br>
} else {<br>
   echo "Something went awry!";<br>
}

Here's a prepared statement, a more secure way of creating a MYSQL record in your table.这是一个准备好的语句,这是一种在表中创建 MYSQL 记录的更安全的方法。

<?php    
//MySQL Database
$servername = "url.com";
$username = "user_login";
$password = "user_password";
$datab = "database_name";

// Create connection
$con = new mysqli($servername, $username, $password, $datab);
global $con;

// Post form data
$name = $_POST['name'];
$type = $_POST['type'];
$brand = $_POST['brand'];
$part_number = $_POST['part_number'];
$price = $_POST['price'];
$oal = $_POST['oal'];
$material = $_POST['material'];
$cutting_diam = $_POST['cutting_diam'];
$shank_diam = $_POST['shank_diam'];
$flutes = $_POST['flutes'];
$loc = $_POST['loc'];
$corner_rad = $_POST['corner_rad'];
$qty = $_POST['qty'];
$trigger_qty = $_POST['trigger_qty'];
$reorder_link = $_POST['reorder_link'];
$toolbox_no = $_POST['toolbox_no'];
$drawer_no = $_POST['drawer_no'];
$bin_no = $_POST['bin_no'];
$product = $_POST['product'];

// Prepared statement
$insert = mysqli_prepare($con, "insert into cutting tools (name,type,brand,part_number,reorder_link,oal,price,material,cutting_diam,shank_diam,flutes,loc,corner_rad,qty,trigger_qty,user,drawer_no,bin_no,toolbox_no) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
mysqli_stmt_bind_param($insert, "sssssssssssssssssss", $name,$type,$brand,$part_number,$reorder_link,$oal,$price,$material,$cutting_diam,$shank_diam,$flutes,$loc,$corner_rad,$qty,$trigger_qty,$product,$drawer_no,$bin_no,$toolbox_no);
mysqli_stmt_execute($insert);

if ($insert) { echo "success"; mysqli_close($con); } else { echo "error"; mysqli_close($con); }
?>

Pay close attention to the order of your columns and the data you are submitting to those columns.请密切注意列的顺序以及您提交给这些列的数据。 I have edited this post because your order was incorrect in several places.我编辑了这篇文章,因为您的订单在几个地方不正确。

The column names and the data variables being uploaded to them have to be in the exact same order if you want data created correctly.如果您希望正确创建数据,则上传到它们的列名和数据变量的顺序必须完全相同。

By the way the variable $product does not seem to match the column name of user, you may want to check this.顺便说一句,变量 $product 似乎与用户的列名不匹配,您可能需要检查一下。

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

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