这是我的代码
<?php
$host="localhost";
$user="root";
$pass="admin";
$dbname="news";
$connection=mysqli_connect($host,$user,$pass,$dbname);
if(mysqli_connect_errno())
{
die("Database Connection Failed >> Error Name : " . mysqli_error() . " Error number : " . mysqli_connect_errno());
}
$html = file_get_contents('http://www.thenews.com.pk/NewsSubIndex.aspx?ID=5'); //get the html returned from the following url
$pokemon_doc = new DOMDocument();
libxml_use_internal_errors(TRUE);
$pokemon_doc->loadHTML($html);
libxml_clear_errors();
$pokemon_xpath = new DOMXPath($pokemon_doc);
$pokemon_row = $pokemon_xpath->query('//table[@id="ctl00_ContentPlaceHolder1_DataListSubIndex"]/tr');
foreach($pokemon_row as $row){
$headline= $row->nodeValue;
echo $headline;
$mysqli_query="INSERT INTO `thenews_entertainment` (`news`) VALUES ('".$headline."')";
echo $mysqli_query;
$result=mysqli_query($connection,$mysqli_query);
echo $result;
mysqli_close($connection);
break;
}
?>
我无法在数据库中插入记录。但是我已经回显了查询并从浏览器复制了该查询并在mysql控制台上执行,并且它成功地插入了记录。为什么它没有从我的代码插入。
答案 0 :(得分:1)
您的代码中可以看到两个基本缺陷。
这两件事你必须无条件,尽管你对此事有何看法。
然而,mysqli第一期太麻烦了。所以,我建议改用PDO。在PDO tag wiki中,您将找到两者的教程。
重写您的代码以遵循这些强制性规则,您将插入数据或错误消息,告诉您出现了什么问题。
答案 1 :(得分:-1)
也许逃避你的变量$标题不是一个坏主意: 使用此:http://fr2.php.net/mysqli_real_escape_string 我希望它会对你有所帮助。