Wordpress正在将数据提交给我的sql


Wordpress submiting data to my sql

我已经设计了向数据库表提交数据的接口,但我不确定如何进行。这里是我的一些代码,大部分都是一样的,所以一旦我知道如何进行,我就可以复制它。

<form method="post" action="input.php">
<?php wp_nonce_field('update-options'); ?>
<table width="600">
<tr valign="top>">
    <th width="92" scope="row">Enter New Track</th>
    <th width="92" scope="row">Enter New Position</th>
    <th width="92" scope="row">Enter New Driver</th>
    <th width="92" scope="row">Enter New Class</th>
</tr>
<tr valign="top>">
    <td><input type="text" name="track" /></td>
    <td><input type="text" name="position" /></td>
    <td><input type="text" name="driver" /></td>
    <td><input type="text" name="class" /></td>
</tr>
<tr valign="top">
    <td><input type="submit" value="Submit" /></td>
    <td><input type="submit" value="Submit" /></td>
    <td><input type="submit" value="Submit" /></td>
    <td><input type="submit" value="Submit" /></td>
</tr>
</table>

我想做的是将这些值中的每一个单独输入到表格中。我仔细研究了这个问题,没有发现任何有帮助的东西,任何想法都得到了赞赏。

这里有许多问题需要解决。

首先,您只需要一个类型为submit<input>。那可以放在桌子外面。其次,您的valign属性包含一个额外的>字符。

    <table width="600">
    <tr valign="top">
        <th width="92" scope="row">Enter New Track</th>
        <th width="92" scope="row">Enter New Position</th>
        <thwidth="92" scope="row">Enter New Driver</th>
        <th width="92" scope="row">Enter New Class</th>
    </tr>
    <tr valign="top">
        <td><input type="text" name="track" /></td>
        <td><input type="text" name="position" /></td>
        <td><input type="text" name="driver" /></td>
        <td><input type="text" name="class" /></td>
    </tr>
</table>
<br />
<input type="submit" value="Submit" />

上面的代码应该放在<form>标签中。

提交表单后,数据将被POST编辑到input.php,其目的是处理数据并将任何新数据插入到表中(为什么要这样做仍然是个谜,但我会先回答这个问题)。

input.php应该看起来像这样:

<?php
    // I will ignore error handling here, but if you want a good tutorial on PHP PDOs, try http://www.phpro.org/tutorials/Introduction-to-PHP-PDO.html
    $pdoObj = new PDO("mysql:host=[yourhost];dbname=[yourdbname]", [db_username], [db_password]);
    // keep in mind that this code is WAY OPEN to SQL injection attacks, but this will at least give you an idea of how to get your code to function, if not securely.
    if(isset($_POST['track'])) {
        $pdoObj->exec("INSERT INTO tracks (name) VALUE ('" . $_POST['track'] . "')");
    }
    if(isset($_POST['position'])) {
        $pdoObj->exec("INSERT INTO positions (name) VALUE ('" . $_POST['position'] . "')");
    }
    if(isset($_POST['driver'])) {
        $pdoObj->exec("INSERT INTO drivers (name) VALUE ('" . $_POST['driver'] . "')");
    }
    if(isset($_POST['class'])) {
        $pdoObj->exec("INSERT INTO classes (name) VALUE ('" . $_POST['class'] . "')");
    }
    // some code here that will either display some copy or redirect the user to another page

注意:

看起来您对web开发还是个新手,所以我要告诉您:SQL注入可不是闹着玩的。你必须*防御它,否则你可能会失去数据库中的一切。

挑选一些关于PHP、Wordpress和web开发的书籍或教程。与社区中的人谈论最佳实践,并尝试获得更多关于如何完成你觉得舒服的基本任务的建议。然后转到更复杂的场景来测试你的能力。有了足够的经验,你会惊讶于你能让你的网络应用程序做什么。