php脚本生成1000个联系人列表

时间:2014-11-25 01:41:36

标签: mysql sql arrays database random

此代码应该从插入的值中随机生成1000个联系人,但它不起作用。有人可以查看它并帮助我找到问题。谢谢..... 这里是URL http://web-students.armstrong.edu/~jl4303/contactForm.html,您可以单击链接,查看生成器。它只是不起作用....

   <?php 
 //Mysql informatino login information
    $host="localhost"; 
   $username="xxxx"; 
   $password="xxxx"; 
   $db_name="xxxx"; 
  //Mysql connection
   mysql_connect("$host", "$username", "$password")or die("cannot connect");
   mysql_select_db("$db_name")or die("cannot select DB");
  //generate array
  $totalContacts=1000;

   $fName =
   array('Michelle','Jing','Mike','Ricky','David','Matt',
  'Melony','Alex','Andrew','Sally','Sue');
   $lName =array('Smith','Lee','Dobson','Johnson','Zammit','Brown','Jones',
  'Miller','Garcia','Wilson','Martinez');
   $Streets=array('Central Ave','Broadway','1st St','2nd St','3rd St','Washington St',
  'Jefferson Ave','Woodcreek Blvd');
   $stateList = array(
  'AL','AK','AZ','AR','CA','CO','CT','DE','DC','FL','GA','HI','ID','IL','IN','IA','KS','KY',
  'LA','ME','MD','MA','MI','MN');
  $domains = array('yahoo.com','mail.com','gmail.com','example.net','host.org');

  for($i=0; $i < totalContacts; $i++){
  $data = array(
 'firstName' => gFirst(), 
 'lastName' => gLast(),
 'address' => gAddress(),
 'state' => gState(),
 'zip' => gZip(),
 'email' =>gEmail (),
 'phone' => gPhone(),
    );
   };

//我不确定此处的插入值...

$sql="INSERT INTO $tbl_name(fName, lName, Address, City, State, Zip, 
Phone,Email)VALUES('$fName', '$lName', '$Address', '$City', '$State', '$Zip', 
'$Phone', '$Email')";
$result=mysql_query($sql);

function gFirst() {
global $firstNames;
return $firstNames[array_rand($firstNames)];
}
`enter code here`function gLast() {
global $lastNames;
return $lastNames[array_rand($lastNames)];
};

function gStreet() {
global $streets;
$houseNumber = mt_rand(1,3000);
$street = $streets[array_rand($streets)];
return $houseNumber.' '.$street;
};

function gState() {
global $stateList;
return $stateList[array_rand($stateList)];
};

function gZip() {
return str_pad(mt_rand(0,99999),5,'0',STR_PAD_LEFT);
};

function gEmail() {
global $domains;
$randomCharacters = md5(mt_rand());
$firstIndex = mt_rand(3,7); // length
$user = substr($randomCharacters,0,$firstIndex);
$domain = $domains[array_rand($domains)];
return $user.'@'.$domain;
};


function gPhone() {
$areacode = mt_rand(100,999);
$first3 = mt_rand(100,999);
$last4 = mt_rand(1000,9999);
return $areacode.'-'.$first3.'-'.$last4;
};

?>

这个php脚本将是我主页面的文件链接,假设当我点击链接时,数据库将根据我插入的值自动生成1000个随机联系人。但它不能以某种方式工作,我认为它是插入部分,我没有做对,但调试时没有任何错误信息,不知道我应该从这里做什么,我应该打印出数据?我是新人,如果这个问题看起来很愚蠢,请不要介意我,我还在学习,所以有人可以帮助我....我非常感激。

1 个答案:

答案 0 :(得分:1)

问题是 - 1)您使用相同的变量进行插入,用于存储数据。 2)插入中缺少城市(例如,没有gCity()方法可用)。或者,有一个gStreet()在任何地方都没有使用。请先清理一下代码,然后试试 -

for($i=0; $i < totalContacts; $i++){

    $data = array(
   'firstName' => gFirst(), 
   'lastName' => gLast(),
   'address' => gAddress(),
   'state' => gState(),
   'zip' => gZip(),
   'email' =>gEmail (),
   'phone' => gPhone(),
    );

   $sql="INSERT INTO $tbl_name (
          fName, 
          lName, 
          Address, 
          State, 
          Zip, 
          Phone,
          Email)
        VALUES('" . $data['firstName'] . "', '" 
          . $data['lastName'] . "', '" 
          . $data['address'] . "', '" 
          . $data['state'] . "', '" 
          . $data['zip'] . "', '" 
          . $data['email'] . "', '" 
          . $data['phone'] . "', '" 
          . $data['email'] . "')";

     $result=mysql_query($sql);

  };

我已将插入放在循环中。