如何使用php,msqli从一个表提交行到另一个表

时间:2015-09-25 13:00:13

标签: php mysql

我有以下php搜索脚本。

<?php
    $query = $_GET['query']; 
    // gets value sent over search form

    $min_length = 3;
    // you can set minimum length of the query if you want

    if(strlen($query) >= $min_length){ // if query length is more or equal minimum length then

        $query = htmlspecialchars($query); 
        // changes characters used in html to their equivalents, for example: < to &gt;

        $query = mysql_real_escape_string($query);
        // makes sure nobody uses SQL injection

        $raw_results = mysql_query("SELECT * FROM florinvtable
            WHERE (`Brand` LIKE '%".$query."%') OR (`Description` LIKE '%".$query."%') OR (`Category` LIKE '%".$query."%')
            ORDER BY Category, Brand, Price") or die(mysql_error());


        if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

            while($results = mysql_fetch_array($raw_results)){
            // $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop

                echo "<div style='position:relative; font-size:18px; width:500px;'><span style='font-size:14px'>".$results['Category']."</span> - <strong>".$results['Brand']."</strong> - ".$results['Description']." - <span style='color:red;'>$".$results['Price']."</span> ".$results['Size']."</div>";
                // posts results gotten from database(title and text) you can also show id ($results['id'])
            }

        }
        else{ // if there is no matching rows do following
            echo "No results";
        }

    }
    else{ // if query length is less than minimum
        echo "Minimum length is ".$min_length;
    }
?>

我想为显示的每一行添加一个提交按钮,我可以单击该按钮将行复制到相同的单独表中。您可能会问,为什么要保留两个相同的表?答案是原始表经常被截断。也就是说,如何使用下面的脚本将提交按钮合并到上面的脚本中。

<?php    

$query = "INSERT INTO floradtable (Category, Brand, Price) 
          SELECT Category, Brand, Price FROM florinvtable 
          WHERE id='".$mysqli->real_escape_string($_REQUEST['id'])."' 
          LIMIT 0,1";

$mysqli->query($query);
?> 

作为旁注,我知道我正在将mysql与mysqli混合使用,我将在解决当前问题后解决这个问题。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

添加此内容。

$scope.searchResult.push({'title':d.data[i]});