2017-10-13 60 views
0

我有一个简单的HTML DOM脚本,简单的HTML DOM刮价格,并保存到MySQL表

有了这个脚本,我从4个URLS提取价格,并投入到MySQL表。

一切ok,但问题是当时的价格是例如:14,70€,在我的表,我只得到14.00,在通常我必须接受我的MySQL表14.70

我的MySQL类型行价格是DECIMAL(10,2)。

什么可能是错的?

如果我手动更新价格,以14.70作为为例,它的工作原理,它输出了我14.70

我用这个SQL查询:

$sql = "INSERT INTO test (name, price, price1, price2, price3) 
     VALUES('$title->plaintext', '$price->plaintext', 
       '$price1->plaintext' , '$price2->plaintext' , 
       '$price3->plaintext')"; 

编辑:纳乔帮助好。 我不明白为什么这个查询:

INSERT INTO productos (nombre, nombreFabricante, precio, precioComp1, precioComp2, precioComp3) VALUES('Staa', 'AOs', '38,25 € ', '34,27 € ', '14,70 € ', '21,00 € ') 

它保存在MySQL表有:

科技成果鉴定的AO 38.00 34.00 14.00 21.00

正如你所看到的,它不救我的小数我的价格。在MySQL数据库38,25是38 ... 34,27在MySQL数据库是34.

感谢您的任何帮助!问候

编辑2:纳乔已经帮助回答这个问题吧!好工作,真的很感激!此致敬礼

+0

MySQL是过时了,使用的mysqli或PDO来代替 –

+0

是的,我知道,但目前我使用的mysqli。稍后我将改为PDO!感谢您的建议! –

回答

0

删除`: 您需要删除€符号。你能做到这样,你需要点改变逗号(,)(。),因此MySQL分辩得到编号

$price_go=str_replace(",",".",str_replace(" €","",$price->plaintext)); 
$price_go1=str_replace(",",".",str_replace(" €","",$price1->plaintext)); 
$price_go2=str_replace(",",".",str_replace(" €","",$price2->plaintext)); 
$price_go3=str_replace(",",".",str_replace(" €","",$price3->plaintext)); 

$sql = "INSERT INTO test (name, price, price1, price2, price3) 
     VALUES('$title->plaintext', $price_go, 
       $price_go1, $price_go2, 
       $price_go3)"; 
+0

这是它显示我的错误,如果我按照你的建议去做:(谢谢你的尝试 错误:INSERT INTO测试(名称,价格,price1,price2,price3)VALUES(名称,38,25€,14, 70欧元,38.25欧元,21,00欧元) 您的SQL语法错误;查看与您的MariaDB服务器版本相对应的手册,以找到正确的语法,以便在'名称,38,25€,14, 70€,38,25€,21,00'在线1 –

+0

我修改了答案 – nacho

+0

Wow Nacho !!它已成功删除了“€”,但它仍然在代码上有错误 错误:INSERT INTO test名称,价格,价格1,价格2,价格3)VALUES(名称,14,70,38,25,38,25,38,25) 您的SQL语法错误;请检查与您的MariaDB服务器版本对应的手册在附近使用正确的语法'名称,14,70,38,25,38,25,38,25')在第2行 –

0

你为什么不尝试的mysqli或PDO,但我更喜欢使用的mysqli,这很简单,也许这可以帮助你:

$con = new mysqli("host","user","password","db"); 

$sql = "INSERT INTO test (name, price, price1, price2, price3) 
     VALUES('$title->plaintext', '$price->plaintext', 
       '$price1->plaintext' , '$price2->plaintext' , 
       '$price3->plaintext')"; 

$con->query($sql); 
+0

没有人?? @nacho的答案是最成功的 –