Question
I need a php code that allows user to submit names. I already have milestone 1 and 2 completed. My Index.php so far: require_once 'db_connect.php';
I need a php code that allows user to submit names. I already have milestone 1 and 2 completed.
My Index.php so far:
require_once 'db_connect.php';
?>
Baby Names
Baby Names
// Create table with four columns: id, name, gender and no of occurences of the name
$createStmt = 'CREATE TABLE IF NOT EXISTS `babynames` (' . PHP_EOL
//. ' `id` int(11) NOT NULL AUTO_INCREMENT,' . PHP_EOL
. ' `name` varchar(15) DEFAULT NULL,' . PHP_EOL
. ' `gender` char(1) DEFAULT NULL,' . PHP_EOL
. ' `occurences` int(10)' . PHP_EOL
//. ' PRIMARY KEY (`id`)' . PHP_EOL
. ') ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;';
if($db->query($createStmt)) {
echo '
Table creation successful.
' . PHP_EOL;
} else {
echo '
Table creation failed: (' . $db->errno . ') ' . $db->error . '
' . PHP_EOL;
exit(); // Prevents the rest of the file from running
}
// Read data from File and insert into database
$file = fopen("yob2016.txt", "r");
if($file){
while (!feof($file)) {
$data = explode(",", fgets($file));
$insert_stmt = 'INSERT INTO `babynames` (`name`, `gender`, `occurences`)'.PHP_EOL
.'VALUES (\''.$data[0].'\', \''.$data[1].'\', '.(int)$data[2].');';
if(!$db->query($insert_stmt)){
die('Error while inserting data to database');
}
}
}
fclose($file);
// Read the data names from Table and display on webpage
$select_statement = 'SELECT * FROM `babynames;';
$result = $db->query($select_statement);
if($result->num_rows>0){
?>
while($row=$result->fetch_assoc()){
?>
}
}
?>
Name | Gender | Occurences |
---|---|---|
// Drop the TEST table now that we're done with it
$dropStmt = 'DROP TABLE `babynames`;';
?>
if($db->query($dropStmt)) {
echo '
Table drop successful.
' . PHP_EOL;
} else {
echo '
Table drop failed: (' . $db->errno . ') ' . $db->error . '
' . PHP_EOL;
exit();
}
?>
my db_connect.php(if you need):
// Do not change the following two lines. $teamURL = dirname($_SERVER['PHP_SELF']) . DIRECTORY_SEPARATOR; $server_root = dirname($_SERVER['PHP_SELF']);
// You will need to require this file on EVERY php file that uses the database. // Be sure to use $db->close(); at the end of each php file that includes this!
$dbhost = 'localhost'; // Most likely will not need to be changed $dbname = 'username'; // Needs to be changed to your designated table database name $dbuser = 'user'; // Needs to be changed to reflect your LAMP server credentials $dbpass = 'pass'; // Needs to be changed to reflect your LAMP server credentials
$db = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if($db->connect_errno > 0) { die('Unable to connect to database [' . $db->connect_error . ']'); }
Milestone 3: mplement basic functionality 11. Write a PHP script that allows users to vote on their favorite boy/girl baby name. Basically, it should: o Display a form containing: a way to select boy or girl, a text input for typing in the name, and a Submit button. o Once the Submit button is pressed, record the user's input into a table (separate from BABYNAMES'), which you use to keep track of the votes received so far. o Display the most popular names so far (in decreasing order by number of votes) See https://www.ssa.gov/oact/babynames/ for an example of such table (limited to the top 10 baby names)Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started