(Lesson 9) DB Connection (INSERT)
(Lesson 9) DB Connection (INSERT)
In the previous lesson, you created a simple database, and learned how to
add and retrieve the records, using phpMyAdmin.
In this lesson, you’ll learn how to manipulate the database you've created
using PHP code, though.
Using PHP code, you must open a connection to the database. Once the
connection is open, you can then manipulate its contents (i.e. retrieve, add,
delete, update).
First, a database connection has to be opened, before you can do anything
with it.
Open a DB connection with PHP code
Syntax:
mysqli_connect (server, username, password, database);
In our database case, the following values are passed to the
mysqli_connect function:
<html>
Server → localhost or 127.0.0.1 <body>
<?php
Username → root mysqli_connect("localhost", "root", "", "ali db");
Password → (no password) ?>
</body>
Database → ali db </html>
Open a DB connection with PHP code
That’s it. Your PHP webpage is now connected to your MySQL database!
However, it may be a good idea to get a feedback whether you are really
connected or not. So, we will modify the previous code to get a feedback.
<html><body>
<?php
$conn = mysqli_connect("localhost", "root", "", "ali db");
if($conn==TRUE)
echo "Successfully connected to the database.";
else
echo "Failed to connect to the database!";
?>
</body></html>
Adding a record to the Department table
Add a record to the Department table
First, you should create a form to input the values of the record.
<html>
<body>
<form action="add_dep.php">
Department Number <input type="text" name="depno"><br>
Department Name <input type="text" name="depname"><br>
Location <input type="text" name="location"><br>
Phone Extension <input type="text" name="phone"><br>
<input type="submit" value="Add Department">
</form>
</body>
</html>
Next, you should connect to the DB, and insert
these values into the Department table.
<html><body>
<?php
$num = $_GET["depno"];
$name = $_GET["depname"];
$loc = $_GET["location"];
$phone = $_GET["phone"];
$conn = mysqli_connect("localhost", "root", "", "ali db");
if($conn==TRUE)
echo "Successfully connected to the database.<br>";
else {
echo "Error. Connection failed!<br>";
die(); } //stop the program. there is no need to continue
$stmt = "INSERT INTO `department` (`Dept Num`, `Name`, `Location`, `Phone Ext`) VALUES ('$num', '$name',
'$loc', '$phone')";
$result = mysqli_query($conn, $stmt);
if($result==FALSE)
echo "Error. $name department was not added";
else
echo "$name department was successfully added";
?>
</body></html>
Let’s do that again in a bit different way
Adding records to the Department table
Suppose that we want to add all the departments at one time, rather than
adding one department at a time.
<html>
<body>
<form action="input.php">
<h3> Enter the number of departments </h3>
<input type="text" name="num">
<input type="submit">
</form>
</body>
</html>
Adding records to the Department table
<html><body>
<form action="add_dep2.php">
<table><tr><th>Number</th> <th>Name</th>
<th>Location</th> <th>Phone</th> </tr>
<?php
$num = $_GET["num"];
for($i=1; $i<=$num; $i++)
echo"<tr><td><input type=text name=depno[]></td>
<td><input type=text name=depname[]></td>
<td><input type=text name=location[]></td>
<td><input type=text name=phone[]></td></tr>";
?>
<tr><td><input type="submit"></td></tr>
</table></form>
</body></html>
Adding records to the Department table
<?php
$num = $_GET["depno"];
$name = $_GET["depname"];
$loc = $_GET["location"];
$phone = $_GET["phone"];
$conn = mysqli_connect("localhost", "root", "", "ali db");
for($i=0; $i<count($num); $i++)
{
$stmt = "INSERT INTO `department` (`Dept Num`, `Name`, `Location`,
`Phone Ext`) VALUES ('$num[$i]', '$name[$i]', '$loc[$i]', '$phone[$i]')";
$result = mysqli_query($conn, $stmt);
if($result==TRUE)
echo "$name[$i] department was added<br>";
}
?>
Adding a record to the Employee table
<html>
<body>
<form action="add_emp.php" method="post">
Employee ID <input type="text" name="empid"><br>
Full Name <input type="text" name="fullname"><br>
Username <input type="text" name="user"><br>
Password <input type="password" name="pwd1"><br>
Confirm Password <input type="password" name="pwd2"><br>
Gender <input type="radio" name="gen" value="Male" checked>Male
<input type="radio" name="gen" value="Female">Female<br>
Salary <input type="text" name="salary"><br>
Department <select name="department">
<option value="101">Finance</option>
<option value="201">Marketing</option>
<option value="301">Human Resources</option>
<option value="401">Technical Support</option></select><br>
<input type="submit" value="Add Employee"><input type="reset">
</form>
</body>
</html>
<?php
$id = $_POST["empid"];
$name = $_POST["fullname"];
$user = $_POST["user"];
$pwd1 = $_POST["pwd1"];
$pwd2 = $_POST["pwd2"];
$gen = $_POST["gen"];
$salary = $_POST["salary"];
$dept = $_POST["department"];
if ($pwd1 != $pwd2)
{ echo "Error. Passwords mismatch";
die(); }
$conn = mysqli_connect("localhost", "root", "", "ali db");
$stmt = "INSERT INTO `employee` (`empid`, `name`, `username`, `password`, `salary`, `gender`, `deptno`)
VALUES ('$id', '$name', '$user', '$pwd1', '$salary', '$gen', '$dept')";
$result = mysqli_query($conn, $stmt);
if($result==FALSE)
echo "Error. $name was not added";
else
echo "$name was successfully added";
?>
Add a record to the Employee table