如何在表单中自动生成ID


How do i autogenerate an ID in a form?

<html>
<head>
<title>The test</title>
</head>
<BODY>
<?php
require 'DBConn.php';
$db = @odbc_connect ($dbconn, '', '');
if (array_key_exists('ID', $_POST)) {
$sql = "insert into Landlords (ID, Landlord, Address, Contact) values 
('$_POST[ID]','$_POST[land]', '$_POST[add]', '$_POST[con]')";
print '<h2>Landlord ' .$_POST['ID'] .' added</h2>';
print '<p>Return at <a href=admin.htm>Admin Page</a>';
odbc_exec ($db, $sql);
} else {
?>
<form action="addLandlord.php" method="post">
<p>Create a new Landlord ID:<br><input type="text" name="ID" size="30">
<p>Enter the Landlord's Name:<br><input type="text" name="land" size="30">
<p>Enter the Landlord's Address:<br><textarea name="con" cols="50" rows="10"
</textarea>
<p>Enter the Landlord's Contact Details:<br><textarea name="con" cols="50" rows="10">
</textarea>
<p>and click here
<input type=submit value="Submit">
</form>
<?php
}
?>
</body>
</html>

嗨,首先使用此代码时,您必须创建一个ID -我需要帮助自动生成这些值并将它们输入数据库。数据库会自动生成它们,但是我不知道如何修改php来适应这个。

其次,我如何添加一个"你输入了错误的值,他们必须是某某"弹出时输入错误的值?

谢谢你们了!

如果您的数据库表有一个id列被设置为自动递增,您不希望尝试设置该值。这就是auto increment所做的:它自动创建一个唯一的id。此外,如果您的数据库没有设置为自动增加id,请更改它。要求用户创建ID是个糟糕的主意。忘记这样一个事实,即它需要大量不必要的代码来尝试并确保它是唯一的,并且可能会让用户来回沮丧。恶心!

  1. 删除html中id的输入。
  2. 删除array_key_exists语句,你可以用isset btw检查值是否设置。
  3. 在变量中设置您的POST值(如果您不这样做,您需要在$_POST["value"]数组键上添加引号,然后您需要在语句中转义引号,或者您必须将每个引号括在花括号中),然后将您的sql语句更改为如下内容:

    $sql = "插入房东(房东,地址,联系方式)值($landlord, $add, $con)";

数据库将通过创建新记录来自动增加id。

对于验证,您需要搜索教程。PHP有很多这样的工具。

编辑:

下面是一个示例页面,其中进行了一些检查以确保值不是空的。这就是它的全部功能。就像我说的,有很多很多关于验证的教程,你应该使用一个教程来学习许多可以完成验证的方法。

也,你没有说你连接到什么样的数据库,所以我只是猜测你的连接字符串和sql语句是正确的。此外,这假设您的数据库ID列确实被设置为自动生成和ID with(自动增量或标识或任何您的数据库支持)。

代码中的注释提供了一些提示,但它应该是相当不言自明的:

<html>
<head>
<title>The test</title>
<style> 
.error { color: red; };
</style>
</head>
<body>
<?php
require 'DBConn.php';
$db = @odbc_connect ($dbconn, '', '');
$display = "form";
$landlord = "";
$landlorderror = "";
$address = "";
$addresserror = "";
$contact = "";
$contacterror = "";
if($_SERVER['REQUEST_METHOD'] == 'POST') {
   if (empty($_POST["landlord"])) {
     $landlorderror = "Landlord name is required";
   } else {
     $landlord = cleanup($_POST["landlord"]);
   }
   if (empty($_POST["address"])) {
     $addresserror = "Address is required";
   } else {
     $address = cleanup($_POST["address"]);
   }
   if (empty($_POST["contact"])) {
     $contacterror = "Contact info is required";
   } else {
     $contact = cleanup($_POST["contact"]);
   }
    if ($landlorderror === "" && $addresserror === ""  && $contacterror === "") {
        $sql = "insert into Landlords (Landlord, Address, Contact) values ('$landlord', '$address', '$contact')";
        odbc_exec ($db, $sql);
        //not sure what DB you're connecting to, if it supports odbc_num_rows() 
        //you can check here if the insert resulted in a record being created
        //by checking if it returns 1 and setting the $display value inside the if statement
        $display = "successmsg";
    }
}
//this does a little sanitizing but if you can use PDO, that would be better
function cleanup($data) {
   $data = trim($data);
   $data = stripslashes($data);
   $data = htmlspecialchars($data);  //b/c you're displaying the data back in the same form
   return $data;
}
//So, this is just going to display the success message if the display variable has been set to something other than form.
//That only happens if there were no empty fields and the db insert was run
if ($display != 'form') { 
    print '<h2>Landlord ' .$landlord .' added</h2>';
    print '<p>Return at <a href=admin.htm>Admin Page</a>';
} else {
//The form action is set to itself and each of the input values is set to the value that was entered
//if the form was already attempted to be submitted.  If an error exists, it will be displayed after the input label.
?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
<p>Enter the Landlord's Name:<span class="error">* <?php echo $landlorderror;?></span><br><input type="text" name="landlord" size="30"  value="<?php echo $landlord;?>">
<p>Enter the Landlord's Address:<span class="error">* <?php echo $addresserror;?></span><br><textarea name="address" cols="50" rows="10"><?php echo $address;?>
</textarea>
<p>Enter the Landlord's Contact Details:<span class="error">* <?php echo $contacterror;?></span><br><textarea name="contact" cols="50" rows="10"><?php echo $contact;?>
</textarea>
<p>and click here
<input type=submit value="Submit">
</form>
<?php
}
?>
</body>
</html>

直接使用:

function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
}

,然后在你的输入框中像这样回显:

echo generateRandomString();