出于某种原因,我有一个只能部分工作的INSERT语句。该代码获取POST数据并使用它来恢复XML文件,从XML文件中提取数据并将其呈现给用户。然后,用户可以确认数据是否正确并将其添加到数据库中。
<?php
require($DOCUMENT_ROOT . "header.html");
// Conect to Database
$con = mysql_connect("localhost","USER","PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("DATABASE", $con);
// Find XML File
$xml = simplexml_load_file("http://www.crossref.org/openurl/".
"?id=doi:".urlencode($_POST[send_doi]).
"&noredirect=true&pid=APIKEY&format=unixref");
// Set variables with XML data
$title = $xml->doi_record->crossref->journal->journal_article->titles->title;
$year = $xml->doi_record->crossref->journal->journal_article->publication_date->year;
$journal = $xml->doi_record->crossref->journal->journal_metadata->full_title;
$doi = $xml->doi_record->crossref->journal->journal_article->doi_data->doi;
// If user click yes add to the Database
if (!empty($_GET['act'])) {
$sql="INSERT INTO ref (uid, date, doi, title, year, journal) VALUES ('1',NOW(),'$doi','$title','$year','$journal')";
if (!mysql_query($sql,$con))
{
die('The Reference could not be added, beacuse of SQL Error:' . mysql_error());
}
echo "New Reference Added";
mysql_close($con);
} else {
?>
<!-- Present the XML data to user -->
<h1>Add Reference</h1>
<p><strong>Is this the reference you want to add?</strong></p>
Title: <?php echo $xml->doi_record->crossref->journal->journal_article->titles->title;?><br />
Year: <?php echo $xml->doi_record->crossref->journal->journal_article->publication_date->year;?><br />
Journal: <?php echo $xml->doi_record->crossref->journal->journal_metadata->full_title;?><br />
DOI: <?php echo $xml->doi_record->crossref->journal->journal_article->doi_data->doi;?>
<form action="newref-getxml.php" method="get">
<input type="hidden" name="act" value="run">
<input type="submit" value="Yes">
</form>
<?php
}
require($DOCUMENT_ROOT . "footer.html");
?>
代码成功恢复XML数据并将其呈现给用户,当他们单击提交按钮时,新记录将使用正确的uid和数据值添加到数据库,但没有doi,title,year或journal的值
答案 0 :(得分:1)
键入将对象转换为String
例如: -
$title = (string) $xml->doi_record->crossref->journal->journal_article->titles->title;
答案 1 :(得分:0)
你有没有输入所有的xml对象?
同时尝试以下查询
“INSERT INTO ref(uid,date,doi,title,year,journal)VALUES('1',NOW(),'”。$ doi。“','”。$ title。“','” 。$年 “ ''” $期刊 “')”;