gpt4 book ai didi

php - 按兰德订购不起作用

转载 作者:可可西里 更新时间:2023-11-01 06:49:10 26 4
gpt4 key购买 nike

希望一切顺利。我有一些不是随机选择 ip 的 php 问题。我会尽力解释。

<select name="State">
<option value="0" selected="selected">Select a State</option>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
<option value="AZ">Arizona</option>
<option value="AR">Arkansas</option>
etc.....
</select>

只要客户选择一个州并提交表格,它就会进入我的数据库并提取与该州相关的 IP 地址。这是我的数据库的样子

+-------+---------------+
| state | ip |
+-------+---------------+
| AL | 67.100.244.74 |
| AK | 68.20.131.135 |
| AZ | 64.134.225.33 |
+-------+---------------+

感谢这个论坛上的人,我有一些 php 代码在提交表单时收集 ip 地址并将其发送到我的电子邮件。完美的。这是代码

<?php
// visit http://php.net/pdo for more details
// start error handling

try
{
// connect
$pdo = new PDO('mysql:host=localhost;dbname=name', 'dbuser', 'pass');
// enable error handling through exceptions
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// create safe query
$query = $pdo->prepare("SELECT ip FROM vincer WHERE state = ? ORDER BY RAND() LIMIT 1");
// pass data & execute query (since the data are of string type
// and therefore can be passed in this lazy way)
$query->execute(array($_POST['State']));
// get value
$ip = $query->fetchColumn();
// print out the IP address using $ip
}
catch (Exception $e)
{
echo "sorry, there was an error.";
mail("email@gmail.com", "database error", $e->getMessage(), "From: email@gmail.com");
}
?><?php

if(isset($_POST['email'])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "1stoptutorials@gmail.com";
$email_subject = "This is a test";


function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}

// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['State']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}

$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$state = $_POST['State']; // not required
$comments = $_POST['comments']; // required

$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z .'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";

function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}

$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "State: ".clean_string($ip)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";


// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
if (!mail($email_to, $email_subject, $email_message, $headers))
{
echo "failed to send message";
}

?>

它唯一没有做的就是从状态中获取一个随机 ip。对于每个州,即 AL、AK、A​​Z 等。我有大约 150 个不同的 IP 地址。因此,假设有人选择了阿拉巴马州 (AL),我希望它从与 AL 位于同一行的数据库中随机选择和 ip。它正在获取 ip 地址,但它总是向我发送相同的 AL ip。

根据代码,它应该这样做,因为 ORDER BY RAND 在那里

$query = $pdo->prepare("SELECT ip FROM vincer WHERE state = ? ORDER BY RAND() LIMIT 1");

我四处询问,有些人建议我需要在我的表中添加一个名称为 id 的列,所以它应该是这样的

-------+-------+---------------+
| id | state | ip |
+------+-------+---------------+
| 1 | AL | 67.100.244.74 |
| 2 | AK | 68.20.131.135 |
| 3 | AZ | 64.134.225.33 |
+------+-------+---------------+

他们说我需要 id 以便我可以随机提取 ip。有谁知道我需要什么 php 代码来添加这个 id 才能使这一切正常。任何帮助将不胜感激

谢谢大家

阿里

最佳答案

RAND() 每次都会给出不同的值。
如果您不想这样做,您可以提供一个固定种子值。
如果您偏执,您可以每次提供不同的种子值。

SELECT ip FROM vincer WHERE state = ? ORDER BY RAND(UNIX_TIMESTAMP(NOW())) LIMIT 1  

如果在一个状态下几乎没有不同的 ip,那么看到相同 ip 的机会会随着 ip 的数量接近一个而增加。
如果只有 1 个,那么 that 当然会被返回。

您可以通过将查询更改为以下内容来查看使用的 RAND():

SELECT @rand:= RAND() as rand, ip 
FROM vincer
WHERE state = ?
ORDER BY rand
LIMIT 1

关于php - 按兰德订购不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8077828/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com