2015-09-25 25 views
2

我有以下php搜索腳本。如何使用php,msqli提交從一個表到另一個表

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

作爲一個方面說明,我知道我正在將mysqli與mysqli混合使用,在解決當前問題後我將解決這個問題。任何幫助表示讚賞。

+0

@您的常識 - 感謝您的面部拍擊。我需要那個。所以,現在我想,我想我應該在echo語句中包含一個表單&提交按鈕,用於在單獨的.php文件中訪問後面的腳本。 –

回答

1

加入此項。

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

感謝Abhihek - 看起來您提供的答案會將結果插入表格中,而不需要執行搜索以外的任何操作。但是,我想從搜索結果中選擇特定的行,我已經推理過......我必須通過在搜索腳本的回顯中包含表單並將按鈕提交到插入腳本中,從而將搜索腳本與插入腳本分開。 –

+0

在這種情況下,您可以使用要選擇的行的ID。將它們存儲爲以逗號分隔的值的字符串。使用它們來獲取行和我的解決方案,將這些行插入到另一個表中。 –

相關問題