Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在表单中自动生成ID? 测试_Php - Fatal编程技术网

Php 如何在表单中自动生成ID? 测试

Php 如何在表单中自动生成ID? 测试,php,Php,如果数据库表的id列设置为自动递增,则不希望尝试设置该值。这就是auto increment所做的:它自动创建一个唯一的id。此外,如果数据库未设置为自动增加id,请更改它。要求用户创建一个ID是一个糟糕的想法。忘记这一事实,它需要大量不必要的代码来尝试并确保它是唯一的,并且可能会让用户感到沮丧。哎呀 删除html中id的输入 删除array_key_exists if语句,您可以检查是否使用isset btw设置了值 在变量中设置POST值(如果不设置,则需要在$\u POST[“value”

如果数据库表的id列设置为自动递增,则不希望尝试设置该值。这就是auto increment所做的:它自动创建一个唯一的id。此外,如果数据库未设置为自动增加id,请更改它。要求用户创建一个ID是一个糟糕的想法。忘记这一事实,它需要大量不必要的代码来尝试并确保它是唯一的,并且可能会让用户感到沮丧。哎呀

  • 删除html中id的输入
  • 删除array_key_exists if语句,您可以检查是否使用isset btw设置了值
  • 在变量中设置POST值(如果不设置,则需要在$\u POST[“value”]数组键上添加引号,然后需要在语句中转义引号,或者必须将每个引号括在大括号中),然后将sql语句更改为类似以下内容:

    $sql=“插入房东(房东、地址、联系人)值 ($LONDER,$add,$con)”

  • 数据库将通过创建新记录来处理自动递增的id

    要进行验证,您需要搜索教程。PHP中有很多

    编辑: 下面是一个示例页面,其中包含一些检查,以确保值不是空的。这就是它的全部功能。正如我所说的,有很多关于验证的教程,您应该使用教程来学习可以实现的许多方法

    另外,您没有说明要连接到哪种数据库,所以我只是猜测您的连接字符串和sql语句是正确的。此外,这假设您的数据库ID列确实设置为autogenerate和ID with(auto increment或identity或数据库支持的任何内容)

    代码中有一些注释可以提供一些关于发生了什么的提示,但应该是不言自明的:

    <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>
    
    
    测试
    .错误{颜色:红色;};
    简单地说:

    <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>
    

    如果数据库生成ID,为什么需要用户输入ID?(提示:如果用户输入一个,则数据库不会生成它。这两种情况是互斥的。)要添加验证,您只需检查PHP代码中的值是否“有效”(不管您如何定义)。如果不是,则显示一个错误。如果是,请继续将它们添加到数据库中。我不太明白,抱歉,我在这方面很新。。如果我删除了数组键,那么页面似乎会中断。请编辑上面的代码以显示您的意思好吗?我的意思是,由于您的post数组中不再有id,您不能将其用作支票。另一种方法是使用:if($\u SERVER['REQUEST\u METHOD']=='POST'),它只会检查表单是否已发布。稍后我将用一个更完整的代码示例更新我的答案。非常感谢,我现在也有一个类似页面的问题。。我复制了代码并相对于另一个页面进行了编辑,但我不知道为什么它不能像这样工作?
    function generateRandomString($length = 10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, strlen($characters) - 1)];
        }
        return $randomString;
    }
    
    echo generateRandomString();