<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来容纳它。
其次,如何在输入错误的值时添加“你输入错误的值,他们必须是这样”,以便弹出?
谢谢你们!
答案 0 :(得分:1)
如果数据库表的id列设置为自动增量,则不希望尝试设置该值。这就是自动增量的作用:它自动创建一个唯一的id。此外,如果您的数据库未设置为自动递增ID,请更改它。要求用户创建ID只是一个糟糕的主意。忘记这样一个事实,即它需要大量不必要的代码才能确保它是独一无二的,并且可能会让用户来回挫败。 ICK!
在变量中设置你的POST值(如果你不需要在$ _POST [“value”]数组键上添加引号,你需要在语句中转义引号或者你有将每个用大括号括起来,然后将你的sql语句更改为:
$ sql =“插入斗地主(Landlord,地址,联系人)值 ($ landlord,$ add,$ con)“;
数据库将通过创建新记录来处理带有自动增量的id。
要进行验证,您需要搜索教程。 PHP有很多种。
修改强> 这是一个示例页面,其中包含一些检查以确保值不为空。就是这样。正如我所说,有许多用于验证的教程,您应该使用教程来学习可以实现的许多方法。
另外,你没有说你要连接的是什么类型的数据库,所以我只是猜测你的连接字符串和sql语句是正确的。此外,这假设您的数据库ID列确实设置为autogenerate和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>
答案 1 :(得分:0)
只需使用:
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
然后在输入字段中回显它:
echo generateRandomString();