<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地址,但它总是向我发送相同的IP用于AL。
根据代码它应该这样做,因为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。有谁知道我需要添加此ID的PHP代码,以使这一切都工作。任何帮助将不胜感激
谢谢大家
阿里
答案 0 :(得分:3)
RAND()
每次都会给出不同的值
如果您不想这样,您可以提供固定种子值
如果你是偏执狂,你每次都可以提供不同的种子价值。
SELECT ip FROM vincer WHERE state = ? ORDER BY RAND(UNIX_TIMESTAMP(NOW())) LIMIT 1
如果在一个州中很少有不同的ip,那么当ip的数量接近1时,看到相同ip的机会就会增加。
如果只有1,那么 当然会返回。
您可以通过将查询更改为:
来查看使用的RAND()
SELECT @rand:= RAND() as rand, ip
FROM vincer
WHERE state = ?
ORDER BY rand
LIMIT 1
答案 1 :(得分:0)
我以前也遇到过同样的问题
您可以使用 NEWID()
而不是 RAND()
函数
SELECT *
FROM Northwind.Orders
ORDER BY NEWID()