- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
希望一切顺利。我有一些不是随机选择 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、AZ 等。我有大约 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/
今天有小伙伴给我留言问到,try{...}catch(){...}是什么意思?它用来干什么? 简单的说 他们是用来捕获异常的 下面我们通过一个例子来详细讲解下
我正在努力提高网站的可访问性,但我不知道如何在页脚中标记社交媒体链接列表。这些链接指向我在 facecook、twitter 等上的帐户。我不想用 role="navigation" 标记这些链接,因
说现在是 6 点,我有一个 Timer 并在 10 点安排了一个 TimerTask。之后,System DateTime 被其他服务(例如 ntp)调整为 9 点钟。我仍然希望我的 TimerTas
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我就废话不多说了,大家还是直接看代码吧~ ? 1
Maven系列1 1.什么是Maven? Maven是一个项目管理工具,它包含了一个对象模型。一组标准集合,一个依赖管理系统。和用来运行定义在生命周期阶段中插件目标和逻辑。 核心功能 Mav
我是一名优秀的程序员,十分优秀!