gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:53:50 26 4
gpt4 key购买 nike

我有以下 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com