我正在从PHP文件运行一个insert语句,然后运行一些代码来检查插件是否有效。 PHP告诉我“成功”。但是,当我检查我的数据库以查看记录是否存在时,它不会。此外,当我直接在MySQL管理员中运行sql语句时,它工作正常。我很确定我没有插入错误的数据库或表。这是代码的清理版本:
$sql = "INSERT INTO companies (name, source_id, source_site, seeking_cf, currency, i_target, equity_offered, money_raised, num_investors, raised_pct, descr, city, state, country, categories_id, logo) VALUES (\"XYZ Widgets\", \"31\", 22, 1, \"£\", 250000, 0, 0, 0, 0, \"(Business Plan\", \"London\", \"\", \"UK\", 0, \"http://www.website.com/xyz.jpg\")";
$rs = mysql_query($sql, $conn);
if(mysql_affected_rows($conn) == 0) {
$utoh = "fail";
} else {
$utoh = "success";
}
echo $utoh;
因此结果显示“成功”,但记录不在数据库中。 PHP是5.3版。 MySQL是5.5。
答案 0 :(得分:2)
尽管我讨厌使用mysql_
宽恕,但您应该对mysql_query
声明进行错误检查......
<?php
$sql = "INSERT INTO companies (name, source_id, source_site, seeking_cf, currency, i_target, equity_offered, money_raised, num_investors, raised_pct, descr, city, state, country, categories_id, logo) VALUES (\"XYZ Widgets\", \"31\", 22, 1, \"£\", 250000, 0, 0, 0, 0, \"(Business Plan\", \"London\", \"\", \"UK\", 0, \"http://www.website.com/xyz.jpg\")";
$rs = mysql_query($sql, $conn);
if (!$rs)
{
echo mysql_error();
}
else
{
echo 'success';
}
我们现在可以看到错误是什么。
答案 1 :(得分:1)
使用single quotes
代替双引号。
您的代码容易出现 SQL Injection 。使用 PDO 或 MYSQLI
使用PDO扩展的示例:
<?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (?, ?)");
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $value);
// insert one row
$name = 'one';
$value = 1;
$stmt->execute();
?>
这将允许您插入带单引号的记录。
答案 2 :(得分:1)
试试这个:
$sql = "INSERT INTO companies (name, source_id, source_site, seeking_cf, currency, i_target, equity_offered, money_raised, num_investors, raised_pct, descr, city, state, country, categories_id, logo) VALUES (\"XYZ Widgets\", \"31\", 22, 1, \"£\", 250000, 0, 0, 0, 0, \"(Business Plan\", \"London\", \"\", \"UK\", 0, \"http://www.website.com/xyz.jpg\")";
$rs = mysql_query($sql, $conn);
if(!$rs) {
$utoh = "fail";
} else {
$utoh = "success";
}
echo $utoh;
答案 3 :(得分:0)
你试过吗?:
1:
mysql_query("set names utf8;");
before mysql_query($insert)?
2:
在查询内部更改“by”