将数据从一个表传输到另一个表时出现PHP错误

时间:2015-08-28 20:02:18

标签: php mysqli

我正在将数据从一个表传输到另一个表,如下所示:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

include 'config.php';

if(isset($_GET['ID'])){
    $ID=$_GET['ID'];
}

$sql = "SELECT * FROM story WHERE uIDStory=".$ID;
$result = mysqli_query($con, $sql);
mysqli_query ($con,"set character_set_results='utf8'"); 
$row = mysqli_fetch_array($result);

$uIDStory = $_POST["uIDStory"];
$title = $_POST['titleStory'];
$excerpt = $_POST['excerptStory'];
$story = $_POST['storyStory'];
$catagory = $_POST['catagory'];
$tags = $_POST['tagsStory'];
//$author = $_POST['authorStory'];
$date = $_POST['dateStory'];
$thumbStory = $row['thumbStory'];
$topimageStory = $row['topimageStory'];
$imageStory = $row['imageStory'];
$imagecaption = $_POST['imagecaptionStory'];
$postreachStory = $row['postreachStory'];
$catagory2 = implode(",", $catagory);

    $sql="INSERT INTO storydraft (uIDStory,titleStory,excerptStory,storyStory,catagory,tagsStory,dateStory,imagecaptionStory,thumbStory,topimageStory,imageStory,postreachStory) VALUES ('". mysqli_real_escape_string($con,$uIDStory) ."','". mysqli_real_escape_string($con,$title) ."','". mysqli_real_escape_string($con,$excerpt) ."','". mysqli_real_escape_string($con,$story) ."','". mysqli_real_escape_string($con,$catagory2) ."','". mysqli_real_escape_string($con,$tags) ."','". mysqli_real_escape_string($con,$date0 ."','". mysqli_real_escape_string($con,$imagecaption) ."','". mysqli_real_escape_string($con,$thumbStory) ."','". mysqli_real_escape_string($con,$topimageStory) ."','". mysqli_real_escape_string($con,$imageStory) ."','". mysqli_real_escape_string($con,$postreachStory) ."')";

   mysqli_query($con,$sql);
        $sql2="DELETE FROM story WHERE uIDStory=".$uIDStory;
mysqli_query($con,$sql2);
    header("location: ../story-edit-draft.php?ID=".$uIDStory);

但是这个脚本给了我那个错误:

  

解析错误:语法错误,意外';'在第30行的C:\ wamp \ www \ UP \ Front-end \ admin \ extraphp \ publishtodraft.php

我不知道问题出在哪里,因为相同的脚本可以在数据库中插入新鲜的故事。

或者当我从第30行删除;时,会发生另一个错误:

  

解析错误:第31行的C:\ wamp \ www \ UP \ Front-end \ admin \ extraphp \ publishtodraft.php中的语法错误,意外的'mysqli_query'(T_STRING)

4 个答案:

答案 0 :(得分:2)

你错过了一个括号。

    $sql="INSERT INTO storydraft (uIDStory,titleStory,excerptStory,storyStory,catagory,tagsStory,dateStory,imagecaptionStory,thumbStory,topimageStory,imageStory,postreachStory) VALUES ('". mysqli_real_escape_string($con,$uIDStory) ."','". mysqli_real_escape_string($con,$title) ."','". mysqli_real_escape_string($con,$excerpt) ."','". mysqli_real_escape_string($con,$story) ."','". mysqli_real_escape_string($con,$catagory2) ."','". mysqli_real_escape_string($con,$tags) ."','". mysqli_real_escape_string($con,$date0) ."','". mysqli_real_escape_string($con,$imagecaption) ."','". mysqli_real_escape_string($con,$thumbStory) ."','". mysqli_real_escape_string($con,$topimageStory) ."','". mysqli_real_escape_string($con,$imageStory) ."','". mysqli_real_escape_string($con,$postreachStory) ."')";

答案 1 :(得分:2)

问题出在您的查询中) {/ 1}} {}}

mysqli_real_escape_string($con,$date0 ."','".

应该是

    $sql="INSERT INTO storydraft (uIDStory,titleStory,excerptStory,storyStory,catagory,tagsStory,dateStory,imagecaptionStory,thumbStory,topimageStory,imageStory,postreachStory) VALUES ('". mysqli_real_escape_string($con,$uIDStory) ."','". mysqli_real_escape_string($con,$title) ."','". mysqli_real_escape_string($con,$excerpt) ."','". mysqli_real_escape_string($con,$story) ."','". mysqli_real_escape_string($con,$catagory2) ."','". mysqli_real_escape_string($con,$tags) ."','". mysqli_real_escape_string($con,$date0 ."','". mysqli_real_escape_string($con,$imagecaption) ."','". mysqli_real_escape_string($con,$thumbStory) ."','". mysqli_real_escape_string($con,$topimageStory) ."','". mysqli_real_escape_string($con,$imageStory) ."','". mysqli_real_escape_string($con,$postreachStory) ."')";

答案 2 :(得分:2)

您可以使用一个查询来完成。您不需要两次调用数据库并在应用程序级别处理行。

insert into table1
select * from table2

这个更快更安全。

如果是自定义列,您可以这样做:

insert into table_2(`col1`)
select col1 from table_1

这只会将table_1的col1插入table_2,所以如果你需要一些特定的列,只需在查询中写下它们即可。

答案 3 :(得分:0)

更改此行

$sql="INSERT INTO storydraft (uIDStory,titleStory,excerptStory,storyStory,catagory,tagsStory,dateStory,imagecaptionStory,thumbStory,topimageStory,imageStory,postreachStory) VALUES ('". mysqli_real_escape_string($con,$uIDStory) ."','". mysqli_real_escape_string($con,$title) ."','". mysqli_real_escape_string($con,$excerpt) ."','". mysqli_real_escape_string($con,$story) ."','". mysqli_real_escape_string($con,$catagory2) ."','". mysqli_real_escape_string($con,$tags) ."','". mysqli_real_escape_string($con,$date0 ."','". mysqli_real_escape_string($con,$imagecaption) ."','". mysqli_real_escape_string($con,$thumbStory) ."','". mysqli_real_escape_string($con,$topimageStory) ."','". mysqli_real_escape_string($con,$imageStory) ."','". mysqli_real_escape_string($con,$postreachStory) ."')";

到这个

$sql="INSERT INTO storydraft (uIDStory,titleStory,excerptStory,storyStory,catagory,tagsStory,dateStory,imagecaptionStory,thumbStory,topimageStory,imageStory,postreachStory) VALUES ('". mysqli_real_escape_string($con,$uIDStory)."','". mysqli_real_escape_string($con,$title) ."','". mysqli_real_escape_string($con,$excerpt) ."','". mysqli_real_escape_string($con,$story) ."','". mysqli_real_escape_string($con,$catagory2) ."','". mysqli_real_escape_string($con,$tags) ."','". mysqli_real_escape_string($con,$date0 ."','". mysqli_real_escape_string($con,$imagecaption) ."','". mysqli_real_escape_string($con,$thumbStory) ."','". mysqli_real_escape_string($con,$topimageStory) ."','". mysqli_real_escape_string($con,$imageStory) ."','". mysqli_real_escape_string($con,$postreachStory) )."')";