0

I have a small problem with a form that imports the data inside a .csv file inside my db.

Inside my code I have an if loop that checks if the email parameter inside the .csv file already exists in the db, it returns an error message, otherwise it executes the insert query.

I always get the success message but the records are never registered inside the table.

Running the insert query var_dump, I get:

object(mysqli_stmt)#4 (10) { ["affected_rows"]=> NULL ["insert_id"]=> NULL ["num_rows"]=> NULL ["param_count"]=> NULL ["field_count"]=> NULL ["errno"]=> NULL ["error"]=> NULL ["error_list"]=> NULL ["sqlstate"]=> NULL ["id"]=> NULL }

code:

<?php
include('../connection/cnt.php');
$info = array();

if(isset($_POST["submit"]))
{
    $file = $_FILES['file']['tmp_name'];
    $handle = fopen($file, "r");
    $c = 0;
    $i = 0;

    while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
    {
        $i++;
        if($i === 1) continue;

        $username = $filesop[0];
        $name = $filesop[1];
        $surname = $filesop[2];
        $email = $filesop[3];

        /*CHECK IF EMAIL EXIST IN DB*/
        $q_check_email = $connessione->prepare("
        SELECT email
        FROM users
        WHERE email = ?");
        $q_check_email->bind_param('s', $email);
        $q_check_email->execute();
        $r_check_email = $q_check_email->get_result();

        $if_check_email=mysqli_num_rows($r_check_email);

        /*IF NOT EXIST RUN QUERY*/
        if($if_check_email==0)
        {

            $q_insertcsv = $connessione->prepare("INSERT INTO users
                                        (
                                            username, 
                                            fname, 
                                            lname, 
                                            email
                                        ) 
                                        VALUES 
                                        (?, ?, ?, ?)
                                        ");
            $q_insertcsv->bind_param("ssss", $username, $name,$surname, $email);
            $q_insertcsv->execute();
            $q_insertcsv->close();


            $c = $c + 1;
            $info[] = 'CSV uploaded!';

        }

        /*ECHO AN ERROR*/
        else
        {
            $info[] = 'CSV not uploaded!';

        }
    }
}
?>

<form enctype="multipart/form-data" method="post" role="form">

    <?php echo implode( $info); ?>
    <br>
    <?php var_dump($q_insertcsv);?>

    <input type="file" name="file" id="file" size="150" required>
    <br>
    <button type="submit" class="btn btn-default" name="submit" value="submit">CARICA</button>
</form>

I don't understand what I'm doing wrong

csv file

  • Can you give us some sample data of the csv? – NanoBit Mar 28 '21 at 14:01
  • Have you got mysqli error reporting enabled? [How to get the error message in MySQLi?](https://stackoverflow.com/a/22662582/1839439) – Dharman Mar 28 '21 at 14:03
  • I quickly tested your code with sample data and it works. Did you get any errors? Could you post your table details? – NanoBit Mar 28 '21 at 14:11
  • @NanoBit I uploaded an image of my .csv file in the question –  Mar 28 '21 at 14:20
  • have you checked the result from ->execute? if it's false you need to check what's $q_insertcsv->error https://www.php.net/manual/en/mysqli-stmt.error.php – Einacio Mar 28 '21 at 14:25
  • @OKJ , Could you try only to have one row inside the csv and try to import that? Does it work? (Check the table itself to verify) – NanoBit Mar 28 '21 at 14:31
  • @NanoBit if i delete the first row, i have null result, i don t understand why –  Mar 28 '21 at 14:46
  • @NanoBit no error with printf(q_check_email) –  Mar 28 '21 at 14:48
  • @NanoBit i don t know why for you run and for me no –  Mar 28 '21 at 14:52
  • @OKJ, could you create a new csv file with https://pastebin.com/GbWRuwSs details? Open a notepad, copy the contents, and save as csv. Then try to upload it? (Fyi, I also got the same output from vardump but the values do appear in the db) – NanoBit Mar 28 '21 at 15:01
  • @NanoBit Now I understand the problem is generated by the creation of the .csv file of the mac programs, as looking on google generator csv online and thus creating the csv file, everything is fine, but I do not understand why it presents this problem by creating csv with mac –  Mar 28 '21 at 15:07
  • @OKJ, you could try to open the csv with notepad (TextEdit) and see if there are any extra characters or values or formatting problems that may cause your issue. – NanoBit Mar 28 '21 at 15:09
  • @NanoBit Correct, analyzing the content of a .csv created with excel for mac and opened with textedit, I get that after each result it inserts after the (,) a; –  Mar 28 '21 at 15:13

0 Answers0