PDO将信息从一个数据库插入另一个数据库

时间:2014-12-07 00:38:13

标签: php mysql pdo insert fetch

我试图将信息从一个数据库表插入到另一个数据库表中,但它似乎无法正常工作。

这是我的approve.php文件=

<?php
if($_POST['approve']) {
$mysql = "INSERT INTO approved_videos WHERE id = " . $info['id'] . " SET `title` = '" . $info['title'] . "', `description` = '" . $info['description'] . "', `name` = '" . $info['name'] . "', `email` = '" . $info['email'] . "', `country` = '" . $info['country'] . "', `videotmp` = '" . $info['videotmp'] . "', `videotype` = '" . $info['videotype'] . "', `videosize` = " . $info['videosize'] . ", `videopath` = '" . $info['videopath'] . "'";
$dbh->exec($mysql) or die(print_r($dbh->errorInfo(), true));
}
?>

这是显示信息的页面,其中批准或拒绝按钮被批准点击,信息将从一个数据库表转移到另一个数据库表=

<body>
<div id="wrapper">
<div id="navigation">
<ul>
    <li><a href="homelink.com">Home</a></li>
    <li><a href="logoutlink.php">Logout</a></li>
</ul>
</div>
<div id="right-content">
<?php
$dir = dirname("connection/directory");
require($dir . 'thisismyconnectionfile.php');

$dbh = pdo_videos();

$stmt = $dbh->query("SELECT * FROM videos");
$stmt->setFetchMode(PDO::FETCH_ASSOC);

    while ($info = $stmt->fetch()) {
    echo "<table width=\"100%\">";
    echo "<tr>";
    $coverid = $info['id']; 
    echo "<td>" . $info['title'] . "</td>";

    $smalldesc = substr($info['description'], 0, 50) . "...";

    echo "<td>" . $smalldesc . "</td>";
    echo "<td>" . $info['name'] . "</td>";
    echo "<td>" . $info['country'] . "</td>";

    include("process/approve.php");

    echo "<td width='100px'><form action='" . htmlspecialchars($_SERVER["PHP_SELF"]) . "' method='post'><input type='submit' name='approve' value='Approve' /></form></td>";
    echo "<td width='100px'><form action='process/decline.php' method='post'><input type='submit' name='decline' value='Decline' /></form></td>";
    echo "</tr>";
    echo "</table>";
}
?>   
</div>
</div>
</body>

有谁知道我哪里出错了?

修改

我已将查询更改为以下内容 -

"INSERT INTO approved_videos(title,desc,name,email,country,videotmp,videotype,videosize,videopath) SELECT videos.title,videos.desc,videos.name,videos.email,videos.country,videos.videotmp,videos.videotype,videos.videosize,videos.videopath FROM videos WHERE id = videos.id";

在错误日志中,我收到以下错误 -

PHP Fatal error:  Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'desc,name,email,country,videotmp,videotype,videosize,videopath) SELECT videos.ti' at line 1' in /bla/bla/bla/process/approve.php:4

任何想法如何解决这个问题?

更新编辑

好的,所以我正在阅读mysql网站,四处寻找我的代码,并将我的查询编辑到以下内容 -

$mysql = $dbh->prepare("INSERT INTO approved_videos(approved_videos.title,approved_videos.desc,approved_videos.name,approved_videos.email,approved_videos.country,approved_videos.videotmp,approved_videos.videotype,approved_videos.videosize,approved_videos.videopath) SELECT videos.title,videos.desc,videos.name,videos.email,videos.country,videos.videotmp,videos.videotype,videos.videosize,videos.videopath FROM videos WHERE videos.id = :id");

它运行该查询并且我没有错误但是它实际上没有将视频表中的信息插入到approved_videos表中,所以我错过了什么?

固定

好的,所以在玩了一下而不是包括它之后我只是从approve.php文件执行了它然而在我放的动作中

'process/approve.php?id='" . $info['id'] . ">

和我的approve.php文件,其中我有查询我将ID更改为 -

videos.id = :id

但也补充说 -

$mysql->bindParam(":id" ,$_GET['id']);

这解决了我的问题。

1 个答案:

答案 0 :(得分:0)

你的sql错了,尝试这样的事情:

$mysql = "INSERT INTO approved_videos SELECT * FROM videos where id = ".$info['id'];