我正在尝试提交给自己的页面,但出于某种原因,以下代码不工作。另外,在成功插入数据后,如何获取table1主键ID?我有一个子表需要这个ID。谢谢你的建议。
<?php
include('db_login.php');
$connection = mysql_connect( $db_host, $db_username, $db_password );
if (!$connection){
die ("Could not connect to the database: <br />". mysql_error());
}
// Select the database
$db_select=mysql_select_db($db_database);
if (!$db_select){
die ("Could not select the database: <br />". mysql_error());
if ($_POST['Submit'])
{
$first = $_POST["first"];
$first = mysql_real_escape_string(get_magic_quotes_gpc() ? stripslashes($first): $first);
$last = $_POST["last"];
$last = mysql_real_escape_string(get_magic_quotes_gpc() ? stripslashes($last): $last);
$insertsql = "INSERT INTO table1(FirstName,LastName) VALUES ('".$first."', '" .$last. "')";
$result1 = mysql_query($insertsql) or die(mysql_error());
}
?>
<form name="hotlineForm" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>"
method="post">
<input id="first" type="text">
<input id="last" type="text">
<input type="submit" value="Submit"></form></body>发布于 2011-10-20 23:22:33
更改表单输入以包含名称属性。没有它们,你的$_POST将是空的。
<input name='first' id="first" type="text">
<input name='last' id="last" type="text">
<input name='Submit' type="submit" value="Submit">正如评论中提到的,不应该使用get_magic_quotes。您已经正确地对输入调用了mysql_real_escape_string()。
插入后,从mysql_insert_id()获取id
$result1 = mysql_query($insertsql) or die(mysql_error());
$new_id = myqsl_insert_id();发布于 2011-10-20 23:22:02
有什么部分没有在回帖上工作?您没有输入if语句吗?
要获取最后一次插入的ID,请在$result1 =mysql_query(...)后使用以下命令:
$primary_id = mysql_insert_id()发布于 2011-10-20 23:23:31
if ($_POST['Submit'])我看不到具有此名称的表单元素。
尝试:
if (isset($_POST['first']) && isset($_POST['last']))要获取插入的ID,您可以使用:
mysql_insert_id();https://stackoverflow.com/questions/7838165
复制相似问题