将 mysql_query 与 foreach 验证检查 php 分开

发布于 2025-01-02 03:03:55 字数 699 浏览 0 评论 0原文

这是我的代码片段。本质上,我有一个新数组想要插入到表名中,但在执行此操作之前,我会根据旧的统计数据验证新的统计数据,确认它们是数字并插入到新行中。

我意识到我做了什么,mysql_query 在 foreach 循环内,它仍然插入 5 行。我希望它只插入 1 个新行。

如何将 mysql_query 置于循环之外,同时在尝试插入新行之前保留 foreach 循环提供的验证?

foreach ($statnumber as $element) {
if ($statnumber != $LastWeeksStats && is_numeric($element)) {
    mysql_query("INSERT INTO tablename (idproducts, stat1, stat2, stat3, stat4, stat5, superstat1) VALUES('8', '$statnumber[0]', '$statnumber[1]', '$statnumber[2]', '$statnumber[3]', '$statnumber[4]', '$statnumber[5]')") or die(mysql_error());  
    } else {
    echo "'{$element}' is NOT numeric or results same as last week", PHP_EOL;
}} 
mysql_close();

This is a snippet of my code. I essentially I have a new array I want to insert into tablename, but before I do that I validate the new stat figures against the old ones, confirm they are numeric and insert into a new row.

I realise what I have done, the mysql_query is inside the foreach loop, it inserts 5 rows all the same. I want it to only insert 1 new row.

How Do I take the mysql_query outside the loop yet keep the validation that the foreach loop provides before attempting to insert a new row?

foreach ($statnumber as $element) {
if ($statnumber != $LastWeeksStats && is_numeric($element)) {
    mysql_query("INSERT INTO tablename (idproducts, stat1, stat2, stat3, stat4, stat5, superstat1) VALUES('8', '$statnumber[0]', '$statnumber[1]', '$statnumber[2]', '$statnumber[3]', '$statnumber[4]', '$statnumber[5]')") or die(mysql_error());  
    } else {
    echo "'{$element}' is NOT numeric or results same as last week", PHP_EOL;
}} 
mysql_close();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋风の叶未落 2025-01-09 03:03:55

我将从数组中删除未通过检查的元素,然后将 mysql 语句放在最后的循环之外。使用 unset 但你必须跟踪要取消设置的键,如下所示:

foreach ($statnumber as $key => $element) {
    if($statnumber != $LastWeeksStats && is_numeric($element)) {
       //do nothing because it is valid      
    } else {
       //remove that element from the array
       unset($statnumber[$key]);
       echo "'{$element}' is NOT numeric or results same as last week", PHP_EOL;
    }
}  
//now only valid data is in the array and you can use a mysql query

你必须根据数组末尾实际有多少元素来更新 mysql 查询,如下所示:

foreach($statnumber as $key => $element) {
   $keys[] = "stat$key";
   $elements[] = "'$element'";
}
$keystring = implode(",",$keys);
$elementstring = implode(",",$elements);
mysql_query("INSERT INTO tablename (idproducts, $keystring, superstat1) VALUES('8', $elementstring)") or die(mysql_error());  

I would remove the elements that fail the check from the array then have the mysql statement outside the loop at the end. Use unset but you have to keep track of what keys to unset like this:

foreach ($statnumber as $key => $element) {
    if($statnumber != $LastWeeksStats && is_numeric($element)) {
       //do nothing because it is valid      
    } else {
       //remove that element from the array
       unset($statnumber[$key]);
       echo "'{$element}' is NOT numeric or results same as last week", PHP_EOL;
    }
}  
//now only valid data is in the array and you can use a mysql query

You will have to update the mysql query based on how many elements are actually in the array at the end like so:

foreach($statnumber as $key => $element) {
   $keys[] = "stat$key";
   $elements[] = "'$element'";
}
$keystring = implode(",",$keys);
$elementstring = implode(",",$elements);
mysql_query("INSERT INTO tablename (idproducts, $keystring, superstat1) VALUES('8', $elementstring)") or die(mysql_error());  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文