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

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

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?