我知道这个问题已被多次回答,但似乎无法使其在我现有的代码中运行。请原谅我是个笨蛋。我的问题:我只需要在创建新记录时获取索引和自动递增的client_id值,因此我可以通过电子邮件将其与密码一起发送给新用户,以便他们可以登录到他们的帐户。这是我的代码,感谢您的帮助。我也明白我应该从MySQL转换为mysqli:
$conn = mysql_connect($servername, $username, $password, $dbname);
if (!$conn) { die('Could not connect: ' . mysql_error()); }
mysql_select_db($dbname,$conn);
$sql="INSERT INTO ricks_inserts (companyname, contact, email)
VALUES ('$_POST[companyname]','$_POST[contact],'$_POST[email]')";
$id = mysql_insert_id(); "My actual variable in my table is called client_id"
if (!mysql_query($sql,$conn))
{
die('Error: ' . mysql_error());
}echo "<br />";
$message= <<<EOD
Client Id:$_POST[client_id]
Email: $_POST[email]
Company: $_POST[companyname]
Contact: $_POST[contact]
EOD;
答案 0 :(得分:-1)
也许你可以尝试这个 MySQL版本
$conn = mysql_connect($servername, $username, $password, $dbname);
if (!$conn) { die('Could not connect: ' . mysql_error()); }
mysql_select_db($dbname,$conn);
$companyname = mysql_real_escape_string($_POST['companyname']);
$contact = mysql_real_escape_string($_POST['contact']);
$email = mysql_real_escape_string($_POST['email']);
$sql="INSERT INTO ricks_inserts (companyname, contact, email)
VALUES ('".$companyname."','".$contact."','".$email."' ) " or die(mysql_errno());
if (mysql_query($sql)) {
$last_id = mysql_insert_id();
echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
echo "Error: " . $sql . "<br>" . mysql_error();
}
$message= <<<EOD
Client Id:$_POST[client_id]
Email: $_POST[email]
Company: $_POST[companyname]
Contact: $_POST[contact]
EOD;
MySQLi版
<?php
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) { die('Could not connect: ' . mysqli_error($conn)); }
mysqli_select_db($conn,$dbname);
$companyname = mysqli_real_escape_string ($conn,$_POST['companyname']);
$contact = mysqli_real_escape_string ($conn,$_POST['contact']);
$email = mysqli_real_escape_string ($conn,$_POST['email']);
$sql="INSERT INTO ricks_inserts (companyname, contact, email)
VALUES ('".$companyname."','".$contact."','".$email."' ) " or die(mysqli_errno($conn));
if (mysqli_query($conn,$sql)) {
$last_id = mysqli_insert_id($conn);
echo "New record created successfully. Last inserted ID is: " . $last_id;
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
$message= <<<EOD
Client Id:$_POST[client_id]
Email: $_POST[email]
Company: $_POST[companyname]
Contact: $_POST[contact]
EOD;
?>