0

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>
1
  • 2
    On a side note, you should consider properly indenting your code. I don't know how you can even read it like that. Commented Oct 1, 2015 at 19:15

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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>

1 Comment

all data are stored blank. earlier it atleast ran and got one data and stored it multiple times.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.