Question

PHP : I need to make a table in database that holds three fields.  A unique key, a name, and an email. then I have to...

PHP :

I need to make a table in database that holds three fields.  A unique key, a name, and an email.

then I have to make two PHP files.  One of those files has a form with two fields that allow the user to enter their name and email.  This file then enters that data into the database table and acknowledges the entry.

The second PHP file does a query on that database and prints out a table with two columns: One with the names and one with the emails.

Use PDO, not mysql functions.

1 0
Add a comment Improve this question Transcribed image text
Answer #1

//Create database
CREATE DATABASE 'Info';


//Create table
CREATE TABLE 'ContactInfo' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'name' varchar(255) NOT NULL,
'email' varchar(255) NOT NULL
PRIMARY KEY ('id'))

//Create Index.php page

<html>
<head>
<title>Add data in DB via PDO(php data object)</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<div id="main">
<h1>Add data in DB via PDO</h1>
<div id="login">
<h2>Info Form</h2>
<hr/>
<form action="" method="post">

<label>Name :</label>
<input type="text" name="stu_name" id="name" required="required" placeholder="Please Enter Name"/><br /><br />
<label>Email :</label>
<input type="email" name="stu_email" id="email" required="required" /><br/><br />

<input type="submit" value=" Submit " name="submit"/><br />
</form>
</div>


</div>
<?php

//Attempt server connection with default settings
if(isset($_POST["submit"]))

{
$hostname='localhost';
$username='root';
$password='';

try {

//conects to the database
$dbh = new PDO("mysql:host=$hostname;dbname=Info",$username,$password);

$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

// adds info to the database
$sql = "INSERT INTO ContactInfo (name, email)
VALUES ('".$_POST["name"]."','".$_POST["email"]."')";
if ($dbh->query($sql)) {
echo "<script type= 'text/javascript'>alert('New Record has been Inserted Successfully');</script>";
}
else{
echo "<script type= 'text/javascript'>alert('Data not Inserted.');</script>";
}

$dbh = null;
}
catch(PDOException $e)
{
echo $e->getMessage();
}

}
?>
</body>
</html>

This is how we can access DB using php data object PDO.

Add a comment
Know the answer?
Add Answer to:
PHP : I need to make a table in database that holds three fields.  A unique key, a name, and an email. then I have to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PHP : I need to make a table in database that holds three fields.  A unique...

    PHP : I need to make a table in database that holds three fields.  A unique key, a name, and an email. then I have to make two PHP files.  One of those files has a form with two fields that allow the user to enter their name and email.  This file then enters that data into the database table and acknowledges the entry. The second PHP file does a query on that database and prints out a table with two...

  • How can I print the Database table in PHP please ? here I have form for name and email, also I have php code that allow...

    How can I print the Database table in PHP please ? here I have form for name and email, also I have php code that allow user to add his name and email to my database, all I need to print my final database when the user click on submit. <form action="" method="post"> <label>Name :</label> <input type="text" name="name" required="required" placeholder="Please Enter Name"/><br /><br /> <label>Email :</label> <input type="email" name="email" required="required" /><br/><br /> <input type="submit" value=" Submit " name="submit"/><br /> </form>...

  • You need to implement a web application that is split in three parts, namely, Webpage, PHP and My...

    You need to implement a web application that is split in three parts, namely, Webpage, PHP and MySQL. Each of them will be used accordingly to solve a simple problem described below. Remember to implement the logic in the most secure way of your knowledge. PHP Implement a PHP function that reads in input a string from the user and store it in a table (e.g., in a field called "Content Name"). The function should be able to read the...

  • 1. Write the php statements to connect to MySQL server and select the database named personnel....

    1. Write the php statements to connect to MySQL server and select the database named personnel. The database personnel is located at localhost server. The user id and password to log on to the server are finalexam and thePassword respectively. 2. Using the above connection, write the php statements to retrieve the name and the email fields from all records of the users table ( 'SELECT name, email FROM users') and display them one record per line.

  • PHP you need to create a form to allow the user to enter their name, email,...

    PHP you need to create a form to allow the user to enter their name, email, and address information. That information will be sent to a PHP script that will process and display that information. Your assignment should have two pages. The first page is straight html (user_input.html) that has a form with the appropriate form elements to collect the user input. The form should then be submitted using the POST method to a php script (display_user_info.php) that will process...

  • Microsoft Access 0 database name should be the broad name of what will be inside it....

    Microsoft Access 0 database name should be the broad name of what will be inside it. If I were creating a database on the history of the Pittsburgh Steeler Coaches I would call mine SteelerCoaches.accdb, for example. A. Design one table with at least a. 6 Fields (can have more, but not more than 10) b. Correct datatypes for your fields c. Property changes appropriate to the fields. Have all Field names with more than one word have no spaces...

  • PHP, HTML, CSS I have to make a website for a health club. Or a spa. Or some other kind of business where you have a mem...

    PHP, HTML, CSS I have to make a website for a health club. Or a spa. Or some other kind of business where you have a membership and keep track of transactions for those customers. First I need to create a page where the customer is signed up for a membership. The fields should include: First name Last name Street Address City State Zip Code Email Phone Number I should create a table that has all of that data as...

  • Need a PHP code that has a box to type in that allows the user to...

    Need a PHP code that has a box to type in that allows the user to put the information through the PHP site to the database for the following categories: INT, First name, Last name, Sex. Once injected it needs to echo to the page to show the table of all entries. The code below is how far I got with it. MySQL: add a row and query $link = mysql_connect('stewcraw.dotstermysql.com', 'prof', '3632password'); if (!$link) { die('Could not connect: '...

  • CHAPTER 1: DATABASES AND DATABASE USERS Given the simple Doctor-Treats_Patient database schema which contains three files...

    CHAPTER 1: DATABASES AND DATABASE USERS Given the simple Doctor-Treats_Patient database schema which contains three files describing information about doctors and patients they treat as follows, answer the following questions with regards to this database.                                                                                                                                                                                 (Total for que 1 is 10 marks) Doctor (DocID: string, DName: string, DAddress: string, Numpatients: integer) Treats (DocId: string, SSN: string, illness: string, day: string, cost: real) Patient (SSN: string, PatientName: string, PAddress: string, Age:integer) Note : DocID, DName, DAddress and Numpatients...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT