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