PHP 将数据从一张表插入到另一张表 [英] PHP Insert data from one table to another

查看:336
本文介绍了PHP 将数据从一张表插入到另一张表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将数据从一个表插入到另一个表,这是我收到的错误消息:

I was trying to insert the data from one table to another, and this is the error message I get:

错误:INSERT INTO content2 (d1, d2, d3) VALUES (John, Doo, 24);

Error: INSERT INTO content2 (d1, d2, d3) VALUES (John, Doo, 24);

字段列表"中的未知列约翰"错误:INSERT INTO content2 (d1,d2, d3) 值(玛丽,萌,36 岁);

Unknown column 'John' in 'field list'Error: INSERT INTO content2 (d1, d2, d3) VALUES (Mary, Moe, 36);

字段列表"中的未知列玛丽"错误:INSERT INTO content2 (d1, d2, d3) VALUES (Julie, Dooley, 42)

Unknown column 'Mary' in 'field list'Error: INSERT INTO content2 (d1, d2, d3) VALUES (Julie, Dooley, 42)

字段列表"中的未知列朱莉"错误:INSERT INTO content2(d1, d2, d3) 值 (John, Doo, 24);

Unknown column 'Julie' in 'field list'Error: INSERT INTO content2 (d1, d2, d3) VALUES (John, Doo, 24);

字段列表"中的未知列约翰"错误:INSERT INTO content2 (d1, d2, d3) VALUES (Mary, Moe, 36);

Unknown column 'John' in 'field list'Error: INSERT INTO content2 (d1, d2, d3) VALUES (Mary, Moe, 36);

字段列表"中的未知列玛丽"错误:INSERT INTO content2 (d1,d2, d3) 价值观(朱莉,杜利,42 岁);

Unknown column 'Mary' in 'field list'Error: INSERT INTO content2 (d1, d2, d3) VALUES (Julie, Dooley, 42);

字段列表"中的未知列朱莉"

Unknown column 'Julie' in 'field list'

这是我的 php 代码:

//Get Connection
require_once('connect.php');

//Get Data
$sql = "SELECT d1, d2, d3 FROM content";
$result = mysqli_query($con, $sql);

if (mysqli_num_rows($result) > 0) {
// output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $row1 = $row["d1"];
        $row2 = $row["d2"];
        $row3 = $row["d3"];

//Insert Data to another table
            $sql = "INSERT INTO content2 (d1, d2, d3)
            VALUES ($row1, $row2, $row3);";
            if (mysqli_multi_query($con, $sql)) {
                echo "New records created successfully";
            } else {
                echo "Error: " . $sql . "<br>" . mysqli_error($con);
            }
        //echo "id: " . $row["d1"]. " - Name: " . $row["d2"]. " " . $row["d3"]. "<br>";
    }
} else {
    echo "0 results";
}

我无法弄清楚问题是什么.请帮忙

I couldn't figure out what the problem are. Please Help

推荐答案

当 PHP 将您的插入查询发送到 MySQL 时,它最终看起来像这样:

When PHP sends your insertion query to MySQL, it ends up looking like this:

INSERT INTO content2 (d1, d2, d3) VALUES (John, Mary, Julie);

因为John"、Mary"和Julie"周围没有引号,MySQL 认为您指的是其他列名.快速而肮脏的解决方案是在您的查询中添加引号,但正如@tadman 所说,您不应该使用这种查询样式,而应该使用 bind_param 将变量添加到查询中.

Because there are no quotation marks around "John", "Mary", and "Julie", MySQL thinks you're referring to other column names. The quick and dirty solution would be to add quotation marks to your query, but as @tadman says, you should not ever be using this style of query, and should instead use bind_param to add your variables to the query.

但是,如果您只想从一个表复制到另一个表,如@Dan Bracuk 所说,您可以通过一个查询来完成:

However, if all you want to do is copy from one table to another, as @Dan Bracuk says, you can do this with a single query:

INSERT INTO content2 (d1, d2, d3)
SELECT d1, d2, d3
FROM content

这篇关于PHP 将数据从一张表插入到另一张表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆