【问题标题】:Insert into function with multiple dynamic input elements插入具有多个动态输入元素的函数
【发布时间】:2019-07-30 08:38:20
【问题描述】:

我目前正在尝试在我的数据库中的两个不同表之间创建一些动态内容。

首先,“table1”将包含这些值。此表可能会动态扩展。

这些条目创建动态表单就像创建动态输入标签一样多,可以在 table1 中找到。

我非常想做的是,每当我提交表单时,我都想在 table2 中传递这些动态条目。

这是该项目的完整示例。甚至创建表格和示例条目。

基本上我不知道用什么代替“???”在“insert_into_table2”函数中。

从现在开始非常感谢。

<?php
check_tables();
insert_tables();
if(isset($_GET['new']) && isset($_GET['new']) == ''){
    $somevalue = 'Some Value';
    insert_into_table2($somevalue);
}
function check_tables(){
    $connection = mysqli_connect("localhost", "testuser", "testpass", "test");
    $sql = "CREATE TABLE IF NOT EXISTS `table1` (
            `table1_ID` int(11) NOT NULL AUTO_INCREMENT,
            `table1_no` int(3) NOT NULL,
            `table1_title` varchar(255) NOT NULL,
            `table1_description` varchar(255) NOT NULL,
            PRIMARY KEY (`table1_ID`)
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
    if ($connection->query($sql) === TRUE) {}
    $connection->close();
    $connection = mysqli_connect("localhost", "testuser", "testpass", "test");
    $sql = "CREATE TABLE IF NOT EXISTS `table2` (
            `table2_ID` int(11) NOT NULL AUTO_INCREMENT,
            `table2_row1` varchar(255) NOT NULL, 
            `name` varchar(255) DEFAULT NULL,
            `lastname` varchar(255) DEFAULT NULL, 
            PRIMARY KEY (`table2_ID`)
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
    if ($connection->query($sql) === TRUE) {}
    $connection->close();
}
function insert_tables(){
    $connection = mysqli_connect("localhost", "testuser", "testpass", "test");
    $sql = "SELECT * FROM table1";
    $result = $connection->query($sql);
    if (mysqli_num_rows($result) < 1) {
        $sql = "INSERT INTO `table1` (`table1_ID`, `table1_no`, `table1_title`, `table1_description`) VALUES
                (1, 1, 'Name', 'name'),
                (2, 2, 'Lastname', 'lastname');";
        if ($connection->query($sql) === TRUE) {}
        $connection->close();
    }
}
function insert_into_table2($somevalue){
    $connection = mysqli_connect("localhost", "testuser", "testpass", "test");
    $sql = "INSERT INTO `table2` (`table2_row1`, '???') VALUES ('$somevalue', '???');";
    if ($connection->query($sql) === TRUE) {}
    $connection->close();
}
?>
<form method="post" action="?new" enctype="multipart/form-data">
    <?php
    $connection = mysqli_connect("localhost", "testuser", "testpass", "test");
    $sql = "SELECT * FROM table1 ORDER BY table1_no ASC";
    $result = $connection->query($sql);
    if (mysqli_num_rows($result) > 0) { 
        while($row = mysqli_fetch_array($result)){
            echo '<input type="text" name="name[]" value="" placeholder="'.$row['table1_title'].'"/><br>';
        }
    }
    ?>
    <input type="submit"/>
</form>

【问题讨论】:

  • 很抱歉,我不得不对代码进行一些更改,因为我注意到我遗漏了一些东西。

标签: php mysqli sql-insert


【解决方案1】:

几天后没有其他人的帮助。我想通了,只是想在这里分享,如果其他人有兴趣。

首先,我为提交按钮指定了一个名称,以使触发器工作。 而不是这个;

<input type="submit"/>

我用过这个;

<input type="submit" name="new_entry"/>

当然,还要更改触发器。来自;

isset($_GET['new']) && isset($_GET['new']) == ''

到;

isset($_POST['new_entry'])

最后我能够从列和输入中获取值并在下面添加代码;

function insert_into_table2($somevalue){
    $connection = mysqli_connect("localhost", "testuser", "testpass", "test");

代码是;

$sqlfc = "SELECT * FROM table1 ORDER BY table1_no ASC";
    $result = $connection->query($sqlfc);
    $columns_ = array();
    if (mysqli_num_rows($result) > 0) {
        $values = implode("', '", $_POST["name"]);
        while($row = mysqli_fetch_array($result)){
            $columns_[] = $row['table1_description'];
        }
        $columns = implode(", ",$columns_);
    }

对于决赛,我能够更改问题标记区域。比如来自;

$sql = "INSERT INTO `table2` (`table2_row1`, '???') VALUES ('$somevalue', '???');";

到;

$sql = "INSERT INTO table2 (table2_row1, $columns) VALUES ('$somevalue', '$values');";

现在一切正常。我以为我可以从这里得到帮助,但这次我没有那么幸运。希望这至少可以对其他人有所帮助。

【讨论】:

    猜你喜欢
    • 2019-01-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 2017-07-11
    • 2014-10-10
    • 2020-09-18
    相关资源
    最近更新 更多