mysql_insert_id()总是返回0 [英] mysql_insert_id() always returns 0
问题描述
我搜索了4个小时,但仍然找不到正确的答案,为什么这个函数返回0.它昨天起作用了,但是现在我对脚本进行了一些更改(很多事情已经更改)...我知道有很多这样的话题,例如我的但我找不到正确的答案.
I searched 4 hours and still cannot find right answer why this function returns 0. It worked yesterday but now I made some changes to script (lot of things have been changed)... I know there are so much topics like mine but I cannot find right answer.
在我的index.php上有4个文本输入.我用update.php文件(POST方法)捕获了它们.
There are 4 text inputs on my index.php. I capture them with my update.php file (POST methods).
这是update.php的一部分
Here is piece of update.php
<?php
include "db.php";
connect_to_db();
require_once('recaptchalib.php');
$privatekey = "N/A";
$resp = recaptcha_check_answer ($privatekey,$_SERVER["REMOTE_ADDR"],$_POST["recaptcha_challenge_field"],$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
echo 'Tekst sa slike je pogresno prepisan. Vracamo Vas natrag.';
echo '<meta http-equiv="refresh" content="5; URL=../">';
} else {
$pic1 = $_POST["pic1"];
$p1 = htmlspecialchars($pic1);
$pic2 = $_POST["pic2"];
$p2 = htmlspecialchars($pic2);
$html = $_POST["html"];
$h = htmlspecialchars($html);
$link = $_POST["link"];
$l = htmlspecialchars($link);
$id = mysql_insert_id();
$query = "INSERT INTO `koraci` (`id`, `p1`, `p2`, `h`, `l`) VALUES ({$id}, '{$p1}', '{$p2}','{$h}','{$l}')";
$result = mysql_query($query) or die(mysql_error());
}
include_once("config.php");
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=Windows-1250" />
<link rel="stylesheet" type="text/css" href="style.css" />
<title><?php echo $title; ?></title>
<script type="text/javascript" src="cufon-yui.js"></script>
<script type="text/javascript" src="font.js"></script>
<script type="text/javascript">
Cufon.set('fontSize', '20px').replace('body');
</script>
</head>
<body>
<?php include_once("analytics.php"); ?>
<h1><?php echo $title; ?></h1>
<br />
<textarea name="one"><center><img src="<?php echo $p1; ?>" id="Like"></center></textarea>
<br />
<textarea name="two"><iframe width=800 height=500 frameborder=0 src="<?php echo $id; ?>"></iframe></textarea>
<br />
</body>
</html>
在我的db.php中是:
And in my db.php is:
<?php
define("DB_SERVER", "N/A");
define("DB_USER", "N/A");
define("DB_PASS", "N/A");
define("DB_NAME", "N/A");
function connect_to_db() {
$connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die("There was an error connecting to the database: " . mysql_error());
mysql_select_db(DB_NAME);
mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'");
return $connection;
}
?>
N/A表示我不会分享这些细节.
N/A means I wont share that details.
我的db.sql:
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+01:00";
CREATE TABLE IF NOT EXISTS `koraci` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`p1` varchar(255) NOT NULL,
`p2` varchar(255) NOT NULL,
`h` varchar(255) NOT NULL,
`l` varchar(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
INSERT INTO `koraci` (`id`, `p1`, `p2`, `h`, `l`) VALUES
(1, 'SOMETHING', 'SOMETHING', 'SOMETHING', 'SOMETHING');
因此,基本上,它会将所有内容发送到db(我可以通过phpMyAdmin看到).
So basiclly, it sends everything to db (I can see with my phpMyAdmin).
问题是,它总是以0的id传回我正在回显的ID!
The problem is, IT ALWAYS RETURNS 0 as id that im echoing!
它没有显示任何错误(PHP或MySQL).
It doesn't show any error (PHP or MySQL).
[15:41]我已将update.php更新为:
[15:41] I have updated update.php to:
if (!$resp->is_valid) {
echo 'Tekst sa slike je pogresno prepisan. Vracamo Vas natrag.';
echo '<meta http-equiv="refresh" content="5; URL=../">';
} else {
$pic1 = $_POST["pic1"];
$p1 = htmlspecialchars($pic1);
$pic2 = $_POST["pic2"];
$p2 = htmlspecialchars($pic2);
$html = $_POST["html"];
$h = htmlspecialchars($html);
$link = $_POST["link"];
$l = htmlspecialchars($link);
$query = "INSERT INTO `koraci` (`id`, `p1`, `p2`, `h`, `l`) VALUES ({$id}, '{$p1}', '{$p2}','{$h}','{$l}')";
$result = mysql_query($query) or die(mysql_error());
$id = mysql_insert_id();
}
?>
现在我有这个错误: 您的SQL语法有误;请查看与您的MySQL服务器版本相对应的手册,以获取在附近使用的正确语法
And now I have this error: "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 "
[16:04]如果在查询后设置ID,我可以看到ID不包含在查询中. 我在说这个:
[16:04] I can see that id isn't included in query if I set it after query. I'm talking about this:
INSERT INTO `koraci` (`id`, `p1`, `p2`, `h`, `l`) VALUES (, 'SOMETHING', 'SOMETHING','SOMETHING','SOMETHING')
推荐答案
在mysql_query
之后执行mysql_insert_id
.
在此处中查找文档,并尝试不使用mysql*
功能不再起作用,如红色框中所述.
这些功能存在证券问题.
Look the documentation here, and try to not use the mysql*
functions anymore, as it is explained in the red box.
There are securities issues with these function.
mysql_insert_id
:检索上一个查询(通常为INSERT)为AUTO_INCREMENT列生成的ID.
mysql_insert_id
: Retrieves the ID generated for an AUTO_INCREMENT column by the previous query (usually INSERT).
在您的代码中,当您执行mysql_insert_id
时,没有插入.
In your code, when you do mysql_insert_id
, no insert was made.
这篇关于mysql_insert_id()总是返回0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!