user3009883 user3009883 - 1 year ago 74
SQL Question

MySQL Database/PHP - Generate 100 contacts from a script

Well I have most of the code working now. The point of the application is to store information from a contact form to a text file. So basically the point of the application is to take in information inputted by the user, store the information in a text file and allow the user to go back to the homepage.

It doesn't necessarily have to be a script to generate the contacts, it can be PHP, Bash, Script, HTML, etc. I just don't know how to do it!

Here is the code I have so far, I just need help with randomly generating the 100 contacts without manually inputting them, if I could get some input that would be appreciated :)


<form action="Registered.php" method="post">
<label>First Name:</label>
<input name="fName" type="text">
<label>Last Name:</label>
<input name="lName" type="text">
<input name="address" type="text">
<select name="statedropdown">
<option value="Al"> Al </option>
<option value="AK"> AK </option>
<option value="AS">AS</option>
<option value="AR">AR</option>
<option value="CA">CA</option>
<option value="CO">CO</option>
<option value="CT">CT</option>
<option value="DE">DE</option>
<option value="DC">DC</option>
<option value="FL">FL</option>
<option value="GA">GA</option>
<option value="HI">HI</option>
<option value="ID">ID</option>
<option value="IL">IL</option>
<option value="IN">IN</option>
<option value="IA">IA</option>
<option value="KS">KS</option>
<option value="KY">KY</option>
<option value="LA">LA</option>
<option value="ME">ME</option>
<option value="MD">MD</option>
<option value="MA">MA</option>
<option value="MI">MI</option>
<option value="MN">MN</option>
<option value="MS">MS</option>
<option value="MO">MO</option>
<option value="MT">MT</option>
<option value="NE">NE</option>
<option value="NV">NV</option>
<option value="NH">NH</option>
<option value="NJ">NJ</option>
<option value="NM">NM</option>
<option value="NY">NY</option>
<option value="NC">NC</option>
<option value="ND">ND</option>
<option value="OH">OH</option>
<option value="OK">OK</option>
<option value="OR">OR</option>
<option value="PA">PA</option>
<option value="RI">RI</option>
<option value="SC">SC</option>
<option value="SD">SD</option>
<option value="TN">TN</option>
<option value="UT">UT</option>
<option value="VT">VT</option>
<option value="VA">VA</option>
<option value="WA">WA</option>
<option value="WV">WV</option>
<option value="WI">WI</option>
<option value="WY">WY</option>
<label>ZIP Code:</label>
<input name="zip" required="required" placeholder="12345" type="text">
<input name="email" required="required" placeholder="" type="email">
<label>Phone Number:</label>
<input name="phone" required="required" placeholder="912-555-1234" type="text">
<input value="Submit" type="submit">
<input type="reset" value="Reset">
<td align="center"><a href=""> View contacts in database </a></td>
<td align="center"><a href=""> View contacts in file </a></td>


<title> Thank You </title>

#declare variables
$fName = $_POST['fName'];
$lName = $_POST['lName'];
$address = $_POST['address'];
$statedropdown = $_POST['statedropdown'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email = $_POST['email'];

mysql_select_db($database) or die( "Unable to select database");
//$query = "SELECT * FROM contacts";
//$result = mysql_query($query);
//$num = mysql_num_rows($result);

$sql = mysql_query("SELECT * FROM contacts");
$file = "FormData.txt";
$fh = fopen($file, 'a') or die("can't open file");

while($row = mysql_fetch_array($sql)){
$username = $row['user'];
$password = $row['pass'];

$accounts = "$username:$password\n";

fwrite($fh, $accounts);

<h1 align = "center"> Thanks for Registering! </h1> <br /><br />
<p align = "center"> Your information is: </p>
<table align = "center">
<td> First Name: </td>
<td> &nbsp </td>
<td> <?php echo $fName ?> </td>
<td> Last Name: </td>
<td> &nbsp </td>
<td> <?php echo $lName ?> </td>
<td> Address: </td>
<td> &nbsp </td>
<td> <?php echo $address ?> </td>
<td> State: </td>
<td> &nbsp </td>
<td> <?php echo $statedropdown ?> </td>
<td> Zip: </td>
<td> &nbsp </td>
<td> <?php echo $zip ?> </td>
<td> Telephone: </td>
<td> &nbsp </td>
<td> <?php echo $phone ?> </td>
<td> E-mail: </td>
<td> &nbsp </td>
<td> <?php echo $email ?> </td>

$outputstring =
"First Name: $fName \n
Last Name: $lName \n
Address: $address \n
State: $statedropdown \n
Zip: $zip \n
Telephone: $phone \n
Email: $email \n

file_put_contents("FormData.txt", $outputstring, FILE_APPEND | LOCK_EX);

<p align="center"><a href="Assignment3.html"> Return to Main Page </a> </p>
<p align="center"><a href="FormData.txt"> View Contacts in Database </a> </p>

Answer Source

Assuming you are asking for 100 fake contacts to test with...

A basic loop would do it

for($i = 0; $i < 100; $i ++)
    mysql_query("INSERT INTO `contacts` (`first_name`, `email`, `etc`) VALUES ('someone ".rand(0,999)."','someone".rand(0,999)."','etc')");

Obviously this relies on an open connection and your actual fields plugged in. No need to bother with escaping anything since this is just script generated test data, right?

Obligatory announcement: mysql_ functions are deprecated. Switch to mysqli or PDO. There are plenty of resources available with just simple google searches like "mysqli_connect" etc.

If this is not what you needed, please update your question.