我创建了一个应该在数据库中提交一些内容的表单。 A创建一个随机数字字符串,然后在提交时我希望将内容插入到数据库中。但在提交之前,重复随机数,我得到了一个新的。怎么消除那个?
<?php
include_once('../includes/connection.php');
$random = rand(1000, 2000)
$dedu = $pdo->prepare("UPDATE users SET user_password ='$random'");
$password = $_POST['password'];
$query = $pdo->prepare("SELECT * FROM users WHERE user_password = ?");
$query->bindValue(1, $password);
$dedu->execute();
$query->execute();
$num = $query->rowCount();
if ($num == 1) {
if (isset($_POST['title'], $_POST['content'])) {
$title = $_POST['title'];
$content = nl2br($_POST['content']);
if (empty($title) or empty($content)) {
$error = 'All fields are requried';
} else {
$query = $pdo->prepare('INSERT INTO articles (article_title, article_content) Values (?, ?)');
$query->bindValue(1, $title);
$query->bindValue(2, $content);
$query->execute();
$msg = 'You added the following article!';
}
}
} else {
$error = 'Incorrect details!';
}
?>
<html>
<head>
<title>dwa</title>
<link rel="stylesheet" type="text/css" href="../assets/stylesheet.css">
</head>
<body>
<div class="container">
<a href="index.php" id="logo">CMS</a>
<br />
<h4>Add Article</h4>
<?php if (isset($error)) { ?>
<div class="error">
<small style="color:#aa0000;"><?php echo $error; ?></small>
</div>
<?php } ?>
<form action="test.php" method="post" autocomplete="off">
<input type="text" name="title" placeholder="title" /><br />
<textarea rows="15" cols="50" placeholder="content" name="content"></textarea><br />
<input type="submit" value="Add Article" />
<input type="password" name="password" />
</form>
<?php if (isset($msg)) { ?>
<div class="msg">
<small style="color:orange"><?php echo $msg;?> <br /><H3>
<?php echo $title; ?></h3><h5><?php echo $content; ?></h5></small>
</div><?php } ?>
</div>
</body>
</html>
答案 0 :(得分:0)
不确定我是否正确理解您想要实现的目标,但请尝试使用
$ random = rand(1000,2000)
后
$查询 - &GT;执行();
答案 1 :(得分:0)
在您的文件中使用此Cookie概念
<?php
$cookie_name = "user";
$cookie_value = "John Doe";
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/"); // 86400 = 1 day
?>
<html>
<body>
<?php
if(!isset($_COOKIE[$cookie_name])) {
//dont do anything
} else {
$random = rand(1000, 2000);
}........proceed with your code
?>
答案 2 :(得分:0)
if(isset($_POST['password']))$password = $_POST['password'];
else $random = rand(1000, 2000);
答案 3 :(得分:0)
我用会话解决了!
如果会话为false,则运行rand函数并将会话设置为true。当页面重新加载(按下提交)时,会话为真,所以其他所有鳕鱼都运行。在代码的最后(如果会话为真),会话是destroyd。因为如果用户想要一个新的rand,则重新加载页面并再次运行rand函数。