Objectives Create a database using Mysql Create a tables and insert data on them Create a relationship between tables Create a user Export your database to a text file Print & echo variables Arrays Using different types of loops PHP Forms & field validation Insert a record to mysql database using php form Update a record in mysql database using php file Update a record in mysql database using php form Select everything from mysql database table
22
Embed
Objectives Create a database using Mysql Create a tables and insert data on them Create a relationship between tables Create a user Export your.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Objectives
Create a database using Mysql
Create a tables and insert data on them
Create a relationship between tables
Create a user
Export your database to a text file
Print & echo variables
Arrays
Using different types of loops
PHP Forms & field validation
Insert a record to mysql database using php form
Update a record in mysql database using php file
Update a record in mysql database using php form
Select everything from mysql database table
Activity Outcomes■ The student should know how to Create a database using Mysql in phpmyadmin
■ The student should know how to Create tables and insert data on them
■ The student should know how to Create relations between tables
■ The student should know how to Export your database to a text file
■ The student should know how to print in php
■ The student should know how to use & manipulate arrays in php
■ The student should know how to use loops in php
■ The student should know how to use form and field validation in php
■ The student should know how to Insert a record to mysql database using php form
■ The student should know how to Update a record in mysql database using php file
■ The student should know how to Update a record in mysql database using php form
■ The student should know how to Select everything from mysql database table
Create a database using Mysql Run the wamp server Open the web browser and write on the address bar http://localhost . The wamp web page will open go to phpmyadmin Create a new database called AZcompany
<?php echo "Using echo to write this sentence, and print to write the name in red <br/><br/>"; ?> Welcome to PHP, <span style= 'color: red'><?php print( "$name" ); ?>!<span/> </strong> </p> </body></html>
Print & Echo variable In PHP there are two basic ways to get output: echo and print.1. Open the notepad 2. Write the following code 3. save the file as welcome.php in the wamp\www directory 4. open the browser write the following in the address bar (http://localhost/welcome.php)
PHP FormsFirst we create an html file that contains a form, the action = a php file:
-Now we create the welcome.php file as follows:
PHP WITH DATABASE
<html>
<body>
<form action="insert.php" method="post">
Emp-Id: <input type="text" name="id">
First-name: <input type="text" name="firstname">
Last-name: <input type="text" name="lastname">
Age: <input type="text" name="age">
<input type="submit">
</form>
</body>
</html>
Insert into mysql databaseWe want to make a connection between a form and mysql database, so we can insert the values from the form directly into database.
1. First , create an html form and save the file as insertform.html
2. Second, create a php file and save it as insert.php as the following:
<?php $con= mysqli_connect("localhost","root","","AZcompany"); if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $sql="INSERT INTO Employees VALUES ('$_POST[id]','$_POST[firstname]','$_POST[lastname]','$_POST[age]')"; if (!mysqli_query($con,$sql)) { die('Error: ' . mysqli_error($con)); } echo "1 record added"; mysqli_close($con);?>
<?php$con=mysqli_connect("localhost","root","","AZcompany");if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } mysqli_query($con,"UPDATE Employees SET Age=36 WHERE Emp_Id='10' "); mysqli_close($con);?>
Update a record in mysql database:
Create a php file to update the record directly with known values: