Import CSV naar Database
Ik had een scriptje gevonden dat een CSV file importeert naar de database en de rows toevoegd maar nu krijg ik de error: Column count doesn't match value count at row 1
Ik krijg niet gevonden wat er fout zou moeten zijn, hoe kan ik deze error oplossen?
De tabel 'NewslettersNoCustomers' ziet er zo uit:
Screenshot:
en de CSV File ziet er zo uit:
Screenshot:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
<?php
$link_id = mysql_connect("localhost", "USERNAME", "PASSWORD") or die("Could not connect.");
if(!mysql_select_db("DATABASENAAM",$link_id)) die("database was not selected.");
$file_handle = fopen("Test.csv", "r");
while (($line_of_data = fgetcsv($file_handle, 1000, ",")) !== FALSE) {
$line_import_query="INSERT into NewslettersNoCustomers values('$line_of_data
[0]','$line_of_data[1]','$line_of_data[2]')";
mysql_query($line_import_query) or die(mysql_error());
}
?>
$link_id = mysql_connect("localhost", "USERNAME", "PASSWORD") or die("Could not connect.");
if(!mysql_select_db("DATABASENAAM",$link_id)) die("database was not selected.");
$file_handle = fopen("Test.csv", "r");
while (($line_of_data = fgetcsv($file_handle, 1000, ",")) !== FALSE) {
$line_import_query="INSERT into NewslettersNoCustomers values('$line_of_data
[0]','$line_of_data[1]','$line_of_data[2]')";
mysql_query($line_import_query) or die(mysql_error());
}
?>
Gewijzigd op 23/01/2013 14:37:40 door - Marco -
In je PMA-screenshot zie ik twee velden, maar je INSERT telt er drie...
want hij hoeft alleen de emails in te voeren bij het veld email
-edit mijn code is nu zo maar ik krijg nogsteeds dezelfde error.
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
<?php
$link_id = mysql_connect("localhost", "USERNAME", "PASSWORD") or die("Could not connect.");
if(!mysql_select_db("DATABASENAAM",$link_id)) die("database was not selected.");
$file_handle = fopen("Test.csv", "r");
while (($line_of_data = fgetcsv($file_handle, 1000, ",")) !== FALSE) {
$line_import_query="INSERT into NewslettersNoCustomers values('$line_of_data
[0]')";
mysql_query($line_import_query) or die(mysql_error());
}
?>
$link_id = mysql_connect("localhost", "USERNAME", "PASSWORD") or die("Could not connect.");
if(!mysql_select_db("DATABASENAAM",$link_id)) die("database was not selected.");
$file_handle = fopen("Test.csv", "r");
while (($line_of_data = fgetcsv($file_handle, 1000, ",")) !== FALSE) {
$line_import_query="INSERT into NewslettersNoCustomers values('$line_of_data
[0]')";
mysql_query($line_import_query) or die(mysql_error());
}
?>
Gewijzigd op 23/01/2013 13:27:59 door - Marco -
Edit:
Ik had jouw edit nog niet gelezen, probeer het zo eens:
Code (php)
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
<?php
$link_id = mysql_connect("localhost", "USERNAME", "PASSWORD") or die("Could not connect.");
if(!mysql_select_db("DATABASENAAM",$link_id)) die("database was not selected.");
$file_handle = fopen("Test.csv", "r");
while (($line_of_data = fgetcsv($file_handle, 1000, ",")) !== FALSE) {
$line_import_query= "INSERT into NewslettersNoCustomers values('', ".$line_of_data[0]."')";
mysql_query($line_import_query) or die(mysql_error());
}
?>
$link_id = mysql_connect("localhost", "USERNAME", "PASSWORD") or die("Could not connect.");
if(!mysql_select_db("DATABASENAAM",$link_id)) die("database was not selected.");
$file_handle = fopen("Test.csv", "r");
while (($line_of_data = fgetcsv($file_handle, 1000, ",")) !== FALSE) {
$line_import_query= "INSERT into NewslettersNoCustomers values('', ".$line_of_data[0]."')";
mysql_query($line_import_query) or die(mysql_error());
}
?>
Gewijzigd op 23/01/2013 13:31:03 door Robin S
Ik heb je code geprobeerd en dan krijg ik deze error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@hotmail.com')' at line 1
Gewijzigd op 23/01/2013 13:31:35 door - Marco -
Dus als het goed is moet dit werken:
Code (php)
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
<?php
$link_id = mysql_connect("localhost", "USERNAME", "PASSWORD") or die("Could not connect.");
if(!mysql_select_db("DATABASENAAM",$link_id)) die("database was not selected.");
$file_handle = fopen("Test.csv", "r");
while (($line_of_data = fgetcsv($file_handle, 1000, ",")) !== FALSE) {
$line_import_query= "INSERT into NewslettersNoCustomers values('', '".$line_of_data[0]."')";
mysql_query($line_import_query) or die(mysql_error());
}
?>
$link_id = mysql_connect("localhost", "USERNAME", "PASSWORD") or die("Could not connect.");
if(!mysql_select_db("DATABASENAAM",$link_id)) die("database was not selected.");
$file_handle = fopen("Test.csv", "r");
while (($line_of_data = fgetcsv($file_handle, 1000, ",")) !== FALSE) {
$line_import_query= "INSERT into NewslettersNoCustomers values('', '".$line_of_data[0]."')";
mysql_query($line_import_query) or die(mysql_error());
}
?>
Ik heb nu btw dit toegevoegd zodat je kan zien of het "echt" gelukt is :p
Code (php)
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
if($line_import_query)
{
echo "Invoeren van de Emails is gelukt!";
}
else
{
echo "Invoeren van de Emails is niet gelukt";
}
{
echo "Invoeren van de Emails is gelukt!";
}
else
{
echo "Invoeren van de Emails is niet gelukt";
}
Gewijzigd op 23/01/2013 13:42:33 door - Marco -
Ok mooi dat het werkt!
Gewijzigd op 24/01/2013 13:45:09 door - Marco -