简体   繁体   中英

How to submit row from one table to another using php, msqli

I have the following php search script.

<?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;
    }
?>

I would like to incorporate a submit button for each row displayed that I can click on to copy a row to an identical separate table. You might ask, why maintain two identical tables? The answer is the original table is frequently truncated. That said, how would I incorporate a submit button into the above script using the script below.

<?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);
?> 

As a side note, I know I am mixing mysql with mysqli which I will address after I work out a solution for the current problem. Any help is appreciated.

Add this.

   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);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM