将值复制到另一个mysql数据库

时间:2015-06-04 17:49:08

标签: php mysql

我有一个表格,显示从表单发送的字段。有些按钮可以通过选择id来编辑或删除所选行。我想添加一个按钮,将所选行插入另一个表。我无法让它发挥作用。

这是表格的代码:

    <?php
/* 
  VIEW.PHP
  Displays all data from 'players' table
*/

  // connect to the database
  include('config2.php');

  // get results from database
  $result = mysql_query("SELECT * FROM articles") 
    or die(mysql_error());  

  // display data in table
  echo "<table border='1' cellpadding='10'>";
  echo "<tr> <th>Author</th> <th>Email</th> <th>Title</th> <th>Poem</th> <th>id</th>";

  // loop through results of database query, displaying them in the table
  while($row = mysql_fetch_array( $result )) {

    // echo out the contents of each row into a table
    echo "<tr>";
    echo '<td>' . $row['Name'] . '</td>';
    echo '<td>' . $row['Email'] . '</td>';
    echo '<td>' . $row['title'] . '</td>';
    echo '<td>' . $row['content'] . '</td>';
    echo '<td>' . $row['id'] . '</td>';
    echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
    echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
    echo '<td><a href="publish.php?id=' . $row['id'] . '">Publish</a></td>';
    echo "</tr>"; 
  } 

  // close table>
  echo "</table>";
?>

以下是删除功能的代码:

 // connect to the database
 include('config2.php');

 // check if the 'id' variable is set in URL, and check that it is valid
 if (isset($_GET['id']) && is_numeric($_GET['id']))
 {
 // get id value
 $id = $_GET['id'];

 // delete the entry
 $result = mysql_query("DELETE FROM stories WHERE id=$id")
 or die(mysql_error()); 

 // redirect back to the view page
 header("Location: secret.php");
 }
 else
 // if id isn't set, or isn't valid, redirect back to view page
 {
 header("Location: secret.php");
 }

以下是我认为将行插入到其他表中的函数应该看起来像但是它不起作用

// connect to the database
 include('config2.php');

 // check if the 'id' variable is set in URL, and check that it is valid
 if (isset($_GET['id']) && is_numeric($_GET['id']))
 {
 // get id values
 $id = $_GET['id'];
 $name = $_GET['name'];
 $email = $_GET['email']; 
 $title = $_GET['title'];
 $content = $_GET['content'];


 //upload
 $result = mysql_query("INSERT into publish (name, email, title, content)
  VALUES WHERE name=$name, email=$email, title=$title, content=$content")
 or die(mysql_error()); 

 // redirect back to the view page
 header("Location: secret.php");
 }
 else
 // if id isn't set, or isn't valid, redirect back to view page
 {
 header("Location: secret.php");
 }

我是新手,所以不确定在这种情况下正确的语法是什么

3 个答案:

答案 0 :(得分:0)

    Using select query  

            $id = $_GET['id'];
   $result = mysql_query("select *stories WHERE id=$id")
             or die(mysql_error()); 
            $row = mysql_fetch_array( $result );
$query= mysql_query("INSERT INTO publish (name, email, title, content)
                     VALUES ('$row['Name']','$row['Email']',$row['title'],$row['content'])"); 

答案 1 :(得分:0)

我不确定任何PHP相关的僵硬,但你看过你的INSERT声明。这是完全错误的。您无法在WHERE语句中使用INSERT条件,如下所示

INSERT into publish (name, email, title, content)
  VALUES WHERE name=$name, ....
          ^------ Here

您是否打算使用INSERT INTO .. SELECT FROM构建

INSERT into publish (name, email, title, content)
  SELECT name, email, title, content
FROM Article
WHERE name=$name, email=$email, title=$title, content=$content"

(或)只是一个INSERT陈述

INSERT into publish (name, email, title, content)
  VALUES($name, $email, $title, $content)

答案 2 :(得分:0)

如果你想要使用sub select,但我不知道大表中的性能

Insert into tablea (name,xx,xxx) value ('select name  from table b where id=x' ,'select xx  from table b where id=x ', 'select xxx  from table b where id=x') not tested but it shoild work
相关问题