2014-12-07 119 views
0

我试图从一个数据库表插入信息到另一个,但它似乎没有工作。PDO从一个数据库插入信息到另一个

这里是我的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表中,所以我错过了什么?

FIXED

好了,所以周围有点打它包括更多,而不是后,我只是执行其从approve.php文件然而,在我把

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

的动作和我approve.php文件,其中我有查询我改变ID来 -

videos.id = :id 

而且还增加 -

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

哪个修复了我的问题。

+0

['INSERT'(http://dev.mysql.com/doc/refman/5.6/en/insert.html)没有按”没有'WHERE'子句,['UPDATE'](http://dev.mysql.com/doc/refman/5.6/en/update.html)。另外,在哪里/如何从中填充'$ info'? – 2014-12-07 00:50:43

+0

我刚刚更新了代码,因为我意识到我还没有包含该文件,所以我怎样才能从表中插入一个特定行信息到另一个 – 2014-12-07 00:52:33

+0

http://dev.mysql.com/doc/refman/5.0/en /insert-select.html - INSERT SELECT不支持'WHERE'子句;这是你最需要使用的。 – 2014-12-07 00:55:54

回答

0

你的SQL是错误的,尝试这样的事情:

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

没有,似乎无法正常工作,但无论如何感谢 – 2014-12-07 09:05:09

+0

看看错误日志记录了这个 - 'PHP致命错误:未捕获带有消息'SQLSTATE [23000]的异常'PDOException':完整性约束违规:1062 /bla/bla/bla/process/approve.php中的密钥'PRIMARY'的重复条目'1':4' – 2014-12-07 09:10:06

+0

将查询更改为您的桌子的领域,但没有汽车incr。 prim键。例如:INSERT INTO表(field1,field2,field3)SELECT field1,field2,field3 FROM table WHERE id =:id' – DarkBee 2014-12-07 10:09:51

相关问题