Payport
Payport

Reputation: 1

Issue in running loop php

I have written a code to do curl from text area from multiple url to store response data from URL. The problem is the programming is only repeatedly storing the first data only again and again in database. Kindly help me resolve this issue.

This is the code that is having the problem:

<?php
$db = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
if(isset($_POST['submit']))
{

    $count=0;
    $url_text=$_POST['content'];
    $urls=explode(",",$url_text);
    if(count($urls)>20)
    {
        echo "Url Should not exceed 20";           
    }
    else
    {

        for($j=0;$j<count($urls);$j++)
        {

            if($urls[$j]!='')
            {

                $url=$urls[$j];
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_TIMEOUT, '180');
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_ENCODING, "");
                $cUrlResponse = curl_exec($ch);
                $httpResponseArr = curl_getinfo($ch);
                curl_close($ch);

                $new=explode("<td>",$cUrlResponse);
                for($i=1;$i<count($new);$i++)
                {

                    $new_input=explode("</td>",$new[$i]);
                    $content_input=explode(":",$new_input[0]);
                    $content[]=trim($content_input[1]);

                }
                $stmt4 = $db->prepare("insert into example (data1,data2,data3) values ('$content[0]','$content[1]','$content[2]')");
                $stmt4->execute();
                $count++;

            }
            echo $count." Rows Inserted Successfully....";

        }

    }

}
?>
<form method="post" method="">

    <textarea name="content"></textarea>
    <br>
    <input type="submit" name="submit" value="Submit" />

</form>

Upvotes: 0

Views: 42

Answers (2)

Tempestas Ludi
Tempestas Ludi

Reputation: 1165

You append items to your $content array, but you don't ever reset that array, so the first three values remain the same, which you then insert into you db.

Upvotes: 1

Samuel Cook
Samuel Cook

Reputation: 16828

You need to reset your content array. The way you have it set up, data is just stacking into it and not overwriting.

Add $content = array(); just before your $new variable.

As a side note, indentation will go a LONG way:

<?php
$db = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);

if(isset($_POST['submit'])){
    $count=0;
    $url_text=$_POST['content'];
    $urls=explode(",",$url_text);
    if(count($urls)>20){
        echo "Url Should not exceed 20";           
    }else{
        for($j=0;$j<count($urls);$j++){
            if($urls[$j]!=''){
                $url=$urls[$j];
                $ch = curl_init();
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_TIMEOUT, '180');
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_ENCODING, "");
                $cUrlResponse = curl_exec($ch);
                $httpResponseArr = curl_getinfo($ch);
                curl_close($ch);

                $content = array();
                $new=explode("<td>",$cUrlResponse);
                for($i=1;$i<count($new);$i++){
                    $new_input=explode("</td>",$new[$i]);
                    $content_input=explode(":",$new_input[0]);
                    $content[]=trim($content_input[1]);
                }
                $stmt4 = $db->prepare("insert into example (data1,data2,data3) values ('$content[0]','$content[1]','$content[2]')");
                $stmt4->execute();
                $count++;
            }
            echo $count." Rows Inserted Successfully....";
        }
    }
}?>
<form method="post" method="">
    <textarea name="content"></textarea>
    <br>
    <input type="submit" name="submit" value="Submit" />
</form>

Upvotes: 0

Related Questions