user3009883 user3009883 - 5 months ago 15
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 :)

HTML CODE:

<form action="Registered.php" method="post">
<p>
<label>First Name:</label>
<input name="fName" type="text">
</p>
<p>
<label>Last Name:</label>
<input name="lName" type="text">
</p>
<p>
<label>Address:</label>
<input name="address" type="text">
</p>
<p>
<label>State:</label>
<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>
</select>
</p>
<p>
<label>ZIP Code:</label>
<input name="zip" required="required" placeholder="12345" type="text">
</p>
<p>
<label>Email:</label>
<input name="email" required="required" placeholder="fake@email.com" type="email">
</p>
<p>
<label>Phone Number:</label>
<input name="phone" required="required" placeholder="912-555-1234" type="text">
</p>
<p>
<input value="Submit" type="submit">
<input type="reset" value="Reset">
</p>
<p>
<td align="center"><a href="http://web-students.armstrong.edu/~tp2283/FormData.txt"> View contacts in database </a></td>
</p>
<p>
<td align="center"><a href="http://web-students.armstrong.edu/~tp2283/contactsFile.htm"> View contacts in file </a></td>
</p>
</body>
</html>


PHP CODE:

<html>
<head>
<title> Thank You </title>
</head>
<body>
<?php
$username="tp2283";
$password="tootandnut";
$database="tp2283";

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


mysql_connect(localhost,$username,$password);
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);
}
mysql_close();
fclose($fh);

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

<?php
$outputstring =
"First Name: $fName \n
Last Name: $lName \n
Address: $address \n
State: $statedropdown \n
Zip: $zip \n
Telephone: $phone \n
Email: $email \n
-----------------------\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>
</body>
</html>

Answer

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

A basic loop would do it

<?php
for($i = 0; $i < 100; $i ++)
{
    mysql_query("INSERT INTO `contacts` (`first_name`, `email`, `etc`) VALUES ('someone ".rand(0,999)."','someone".rand(0,999)."@test.com','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.

Comments