Prosím o odboru pomoc nebo radu, ( jsem začátečník a nějak nemůžu přijít co je špatně )!!! Potřeboval by hromadné importovat data z ( CSV nebo TXT ) cronem do MySQL. Mam skript PHP který spustím a se tváři že to provedl ale změna v DB se ne projeví.
( http://levne-parfemy-eshop.cz/top.php z http://levne-parfemy-eshop.cz/data.csv ) Tak za každý nápad budu vděčny.
<?php
// Connect to MySQL
$sql = mysql_connect("localhost", "xxxx", "xxxxx") or die(mysql_error());
$db = mysql_select_db("xxxx", $sql) or die(mysql_error());
#if first row of csv file is headings set $row to 1.
$row = 0;
#database primary table
$table_to_update = "zen_products";
#get the csv file
$handle = fopen("data.csv", "r");
$result = mysql_query("SELECT * FROM zen_products");
mysql_query('START TRANSACTION');
#go through the csv file and print each row with fields to the screen.
#and import them into the database updating only the price and quantity
while (($data = fgetcsv($handle, 3000, ",")) !== FALSE) {
$num = count($data);
echo "<p> $num fields in line $row: </p>\n";
$row++;
for ($c=0; $c < $num; $c++) {
if ($c = 1) {
$product_model = $data[($c - 1)];
echo $product_model . " SKU Assigned \n";
}
if ($c = 2) {
mysql_query('COMMIT');
mysql_query('START TRANSACTION');
$product_price = $data[($c - 1)];
mysql_query("UPDATE $table_to_update SET products_price='$product_price' WHERE products_model='$product_model'")
or die(mysql_error());
mysql_query('COMMIT');
echo $product_price . " Imported for row $row in product $product_model\n";
}
if ($c = 3) {
mysql_query('COMMIT');
mysql_query('START TRANSACTION');
$product_quantity = $data[($c - 1)];
mysql_query("UPDATE $table_to_update SET products_quantity='$product_quantity' WHERE products_model='$product_model'")
or die(mysql_error());
echo $product_quantity . " Imported for row $row in product $product_model \n";
}
}
}
mysql_query('COMMIT');
fclose($handle);
echo "<h1>Update Complete.</h1>";
?>
Fórum › PHP
Hromadný zápis PHP v MySql

To R76 :
mysql_query("UPDATE $table_to_update SET products_price='$product_price' WHERE products_model='$product_model'")
Túto časť kódu skús prepísať takto:
$query = "UPDATE $table_to_update SET products_price='$product_price' WHERE products_model='$product_model'";
echo $query;
mysql_query($query);
Takto sa ti dopyt ukaze na obrazovke a ty si ho budes moct skontrolovat, ci sa premenne v dopyte spravne dosadili. Potom ho skus skopirovat a zadat priamo v databaze. Mozno ti to nepomoze hned najst chyubu ale aspon zistis co je chyba v php alebo v databaze.
To R76 : Nerozumiem tomu ako ti moze echo $query vypisat len "11Imported ". Ak je v premennej $query slovo "UPDATE" a ine, tak sa musia aj oni zobrazit v prikaze echo. Viac pouzivaj prikaz "echo $nejaka_premenna;" aby si si mohol skontrolovat hodnoty premennych v priebehu kodu a zistis v ktorej casti ti to viazne.
Po vloženi
if(!$result = mysql_query($db))
{
echo mysql_error();
}
echo $result;
vyskočí hláška: 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 '1' at line 1 Imported for row 1 in product 17541;99990000;150
neví někdo čim to je ja jsem z toho jelen
To R76 : nie mysql_query($db) ale mysql_query($query). A v tej premennej $query (mozes si ju nazvat aj $dotaz ako chces budes mat samotny text dotazu. Napriklad $query = "select nieco from nieco";
Ked das prikaz echo $query vypise sa ti do prehiladaca ten text dopytu a zistis ake hodnoty sa pokusas zapisat do databazy.

Přidej příspěvek
Ano, opravdu chci reagovat → zobrazí formulář pro přidání příspěvku
×Vložení zdrojáku
×Vložení obrázku
×Vložení videa
































Uživatelé prohlížející si toto vlákno
Podobná vlákna
Mysql list pridanie policka input zapis do mysql — založil Rastislav
MySQL Zapis — založil zelenac1
Zápis do mysql — založil FanatiK
Zapis dat do mysql — založil matouch
Zapis do MySQL + PDO — založil zelenac1
Moderátoři diskuze