2014-12-07 134 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

相關問題