How to count invalid rows in csv file using php?

hii Very Good Morning :slight_smile:
Can anyone tell, how to count INVALID (Numbers) rows in csv file using php…
Below code is counting only VALID (Numbers) rows, i want to count. Both Valid & Invalid Rows(Numbers).
plzzzz tell me how to do it ?

<body>

    <div style="border:1px dotted #333333; width:300px; margin:0 auto; padding:10px;">

        <form name="import" method="post" enctype="multipart/form-data">
            <input type="file" name="file" /><br />
            <input type="submit" name="submit" value="Submit" />
        </form>
        <?php
        include ("connection.php");

        if (isset($_POST["submit"])) {
            $file = $_FILES['file']['tmp_name'];
           $handle = fopen($_FILES['file']['tmp_name'], "r");
            $c = 0;
            while (($filesop = fgetcsv($handle, 1000, ",")) !== false) {
                $contact = $filesop[0];
                $message = $filesop[1];
                if (preg_match('/(7|8|9)\d{9}/', $contact)) {
                    $sql = mysql_query("INSERT INTO csv (contact, message) VALUES ('$contact','$message')");

                 $c = $c + 1;
                }
            }
            if ($sql) {
                echo "You database has imported successfully. You have inserted " . $c . " records";
            } else {
                echo "Sorry! There is some problem.";
            }
        }
        ?>

    </div>

</body>

Try
$total_rows = count($filesop);
and
$invalid_rows = $total_rows - $c;

And for goodness sake, get rid of the “mysql_” function while you’re working on the code now so you won’t need to do it later. At least swap it out to “mysqli_”

Isn’t this the same question you asked here ( How to validate mobile number in php? - #7 by droopsnoot ) ? Add an else() clause to your if() statement, to count the ones you reject for the condition:

                if (preg_match('/(7|8|9)\d{9}/', $contact)) {
                    $sql = mysql_query("INSERT INTO csv (contact, message) VALUES ('$contact','$message')");

                 $c = $c + 1;
                }
// ** ADD THIS CODE
                else {
                   $invalid++;
                   }

On the line after you set $c=0, you need another line that sets $invalid =0 in the same format.

Thank u so much
The code is working :+1:

This code is right or wrong tell me plzzz…
To display the contact & message, which i uploaded in csv file…

echo "<h2>" . "File " . $_FILES['file']['name'] . " Uploaded Successfully." . "</h2>";
echo "<h3>Displaying Contents:</h3>";
readfile($_FILES['file']['tmp_name']);

If you want to display the information that you added to the database, you’ll need to add code within the loop, as you insert it into the database.

echo $contact . " - " . $message;

If there’s a lot of these in the file, you’ll need to do something with the page html to improve the display layout.

I’ve some problem…
When i upload & click on submit button output must display in same page.
But output displaying in another page…

Can u tell how to dispaly in same page…

The example code in neither of your previous posts looks anything like that image.

Nobody can help from an image alone, we need to see the code responsible for creating that form

This HTML page…

        .style1 {font-family: "Times New Roman", Times, serif}
    </style>
</head>
<body>
    <div style="margin-left:50px; margin-top: -1em;font-family: initial;">
        <h3>Bulk Sms</h3></div>
    <form name="import" method="post" enctype="multipart/form-data" action=''>
        <table border="3" style="margin-left:500px;margin-top:5px;font-family: initial;">
            <tr><td>
                    YOUR MOBILE NO:<input name="fromno" type="text" size="37" maxlength="10"></td></tr>
            <tr><td>TYPE MESSAGE:<textarea onKeyPress=check_length(this.form); onKeyDown=check_length(this.form); name=my_text rows=4 cols=30></textarea></td></tr>
            <br>                               
            <tr><td><input size=1 value=160 name=text_num> Characters Left</td></tr>
            <tr><td><input type="submit" class="btn btn-info" value="SendSMS">
                    <div style="margin-left:110px;margin-top: -26.5px ;">
                        <input type="file" name="csv" id="csv" />
                    </div>
                    <div style="margin-left:338px;margin-top: -35px;">
                        <input type="submit"class="btn btn-info" value="Submit" name="submitbtn" id="submitbtn">
                    </div>
        </table>
    </form>.
    <div id="loadmsg">
        
    </div>
    <div style="margin-left:553px; margin-top:-2em;">

    </div>
</body>

This is Upload page

<?php
include ("dbconnect2.php");

if ($_POST['submitbtn'] == "Submit") {
    $file = $_FILES['csv']['tmp_name'];
    //Count The Total Csv file
    $abc = count(file($_FILES['file']['tmp_name']));
    echo"<table width='500' border='1' margin-right:250px><tr><th>Contact</th><th>Message</th></tr>";
    $handle = fopen($_FILES['file']['tmp_name'], "r");
    $c = 0;

    while (($filesop = fgetcsv($handle, 1000, ",")) !== false) {
        echo "<tr>";
        $contact = $filesop[0];
        $message = $filesop[1];
        if (preg_match('/(7|8|9)\d{9}/', $contact)) {
            $sql = mysql_query("INSERT INTO csv (contact, message) VALUES ('$contact','$message')");
            $c = $c + 1;
            echo "<td>" . $contact . "</td><td>" . $message . "</td>";
        } else {
            $er = $abc - $c;
        }
    }
    if ($sql) {
        echo "<br>Total Mobile Numbers is: " . $abc . "<br>";
        echo "Vaild Mobile Numbers: " . $c . "<br>";
        echo "Invaild Mobile Numbers: " . $er . " <br>";
    } else {
        echo "There is some Error.";
    }
    echo "</tr>";
}
?>
</div>

I’m not sure what you mean. Do you mean that you want to leave the form in place on the screen, have the file uploaded in the background and then display the results on the original page? If so, you’ll need to look at Ajax to handle the upload, but I can’t see where you will display the data on your form page, or even how it will be logical to the user to still have the form displayed after they have uploaded the file.

When i click on submit button output must display in same page only. It displaying in other page…
Our sir told ur using POST & ACTION, so it displaying in other page…
So he told to try in Javascript…

This is HTML Page

<body>

    <div style="border:1px dotted #333333; width:300px; margin:0 auto; padding:10px;">

        <form name="import" method="post" enctype="multipart/form-data" action="Final.php">
            <input type="file" name="file" /><br />
            <div style="margin-left:  200px; margin-top: -20px;">
                <input type="submit" name="submit" value="Submit" />
            </div>
        </form>
       
    </div>
</body>

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.