php - 如何使用 php、mysqli 将行从一个表提交到另一个表

标签 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 混合使用,我将在为当前问题制定解决方案后解决这个问题。感谢您的帮助。

最佳答案

添加这个。

   if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following
        $query = "INSERT INTO floradtable (Category, Brand, Price) VALUES";
        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
            $query .=  "('".$results['Category']."','".$results['Brand']."','".$results['Price']."'),";
            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'])
        }
        $query = substr($query, 0, -1);//to remove the trailing comma
        $mysqli->query($query);
    }

关于php - 如何使用 php、mysqli 将行从一个表提交到另一个表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32782795/

相关文章:

php - MPDF 无输出(空白页)

php - yii 多记录多表单多模型

php - Python des-ede-cbc 等价物

python - 变量不通过python传入mysql

mysql - MySQL 中的日期时间帮助

php - MYSQL innodb 全文搜索不显示所有结果

mysql - 根据另一个表的数据删除逗号分隔字段中的条目

php - 通过 PHP PDO 创建 MySQL 表后无法 INSERT INTO

php - 如何在wordpress插件中创建新页面?

mysql - 如何使用 Rails 5 迁移在 MySQL 中添加 JSON 列