douyuan3842 2015-10-25 18:39
浏览 44
已采纳

PHP - Foreach SQL查询

I'm trying to be able to paste a proxy list into a Text file and be able to update the rows status to 1 if the pasted proxy is in the Database. I got it kind of working im using explode to seperate the list and from there foreach it using a query function. The issue is it doesnt update. It works on the very last proxy only.

Query Function:

function Connect($host = false,$username = false,$password = false,$dbname = false) {

    //Try execute the PHP with no errors;
    try {

        //Create a PDO Session;
        $con = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);

        //Session Attributes;
        $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

    }

    //Catch all PDOException errors;
    catch (PDOException $e) {

        //Make the PDO session false;
        $con = false;

    }

    //If no errors happened Make the PDO session true;
    return $con;

}

//Create a new function named query;
function query($sql = false,$dbname = false,$bind = false,$obj = false) {

    //Prepare The SQL Query;
    $query = Connect('localhost','xx','xx',$dbname)->prepare($sql);

    $res = true;

    //Execute Binded Query;
    try { $query->execute($bind); $res = true; }

    catch (PDOException $e) {

        $res = false;

    }

    //If no errors happened Make $row true;
    return $res;

}

And PHP:

<?php

//Define if Error Logging will be turned on (true) or off (false);
$errorlogging = true;
$obstart = false;
$adminonly = false;

//Set the default Timezone to UK/Ireland;
date_default_timezone_set('Europe/London');

//Globalized functionality needed on all pages;
require('common/requisites.php');

//Include The Database Connection;
require('common/db.php');

if(!empty($_POST['proxies'])) {

    $proxyinput = $_POST['proxies'];

    $proxies = explode("
",$proxyinput);

    foreach($proxies as $proxy) {
        query('UPDATE proxies SET status = 1 WHERE :proxy = proxy','unfed_tools',array(':proxy'=>$proxy));
        echo "Updated: ".$proxy."
";
    }

}

?>

<form action="" method="post">
    <textarea name="proxies" style="height: 300px; width: 300px;"></textarea>
    <button type="submit">Click</button>
</form>
  • 写回答

1条回答 默认 最新

  • duancenxie2233 2015-10-25 19:04
    关注

    Are you sure the line endings aren't ? You might need to do explode(" ",$proxyinput); That would explain why it only works for the last proxy.

    For example: Example proxy file

    proxy1
    
    proxy2
    
    proxy3
    
    proxy4
    

    Your explode gives you:

    array("proxy1","proxy2","proxy3","proxy4")
    

    So your search will only find the proxy4

    Edit:

    To account for users being annoying, you can:

    explode("
    ",str_replace("
    ","
    ",$proxyinput));
    

    You shouldn't have to worry about the line endings ever being ... AFAIK.

    I noticed you commented that you are using Notepad++. You can use it to "Show All Characters" enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c