Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
in SQL by (5k points)

1. There is an index.php page that include the login form (2 textbox and Login Button)

2. There is MySQL database that contains two tables: system_users & user_errors. 

3. The moment a user login successfully in the index.php page, this will redirect him to the page1.php and then retrieve his errors from the user_errors table and list them.

I need this in php.

I have listed my code down below:

index.php

    <html>

<link rel="stylesheet" type="text/css" href="cssDesign.css">

<head>

<title>A BASIC HTML FORM</title>

<?PHP

//MySQL Database Connect 

include 'mysql_connect.php';

$conn = MySQLConnection::getConnection();

// Create connection

//global $con;

//$con = new mysqli("192.168.0.2", "Invest", "NeeD$123","investment projects","3306");

if (isset($_POST['Submit1'])) {

    $username = $_POST['username'];

    $password = $_POST['password'];

$result = mysqli_query($conn,"SELECT id,username,password,type FROM system_users WHERE username = '".$username."' AND password='".$password."' ");

$rows = mysqli_num_rows($result); 

if ($rows == 1) {

  header('Location: page1.php');

}       

else {

    print ("You're not a member of this site");

    }

}

//mysqli_close($conn);

?>

</head>

<body>

<FORM NAME ="form1" METHOD ="POST" ACTION = "index.php">

<table border="0" style="width:370px;border:0px;">

    <tr> 

        <td style ="width:30%;border:0px;">User</td>

        <td style ="width:70%;;border:0px;"><INPUT style ="width:100%;" TYPE = "TEXT" VALUE ="" NAME = "username"> </td>

    </tr>

    <tr> 

        <td style ="width:30%;border:0px;">Password</td>

        <td style ="width:70%;border:0px;"><INPUT style ="width:100%;" TYPE = "TEXT" VALUE ="" NAME = "password"> </td>

    </tr>

    <tr> 

    <td style ="width:30%;border:0px;"></td>

        <td align='right' style ="width:70%;border:0px;"><INPUT style ="width:30%;" TYPE = "Submit" Name = "Submit" VALUE = "Login"> </td>

    </tr>

</table>

</FORM>

</body>

</html>

page1.php

    <html>

<link rel="stylesheet" type="text/css" href="cssDesign.css">

<head>

<title>A BASIC HTML FORM</title>

</head>

<body>

<Form name ="form1" Method ="POST" Action ="page1.php">

</FORM>

</body>

</html>

<?php

include 'mysql_connect.php';

$conn = MySQLConnection::getConnection();

$username = $_POST['username'];

$sql = "SELECT user_name,error_form,error_date,error_description " . 

        "FROM error_log WHERE username ='".$username."'";

$result = mysqli_query($conn,$sql);

if ($result->num_rows > 0) {

    echo "<table>";

        echo "<tr>";

        echo "<th>User</th><th>Error Form</th><th>Error Date</th><th>Error Description</th>";

        echo "</tr>";

    // output data of each row

    while($row = $result->fetch_assoc()) {

        echo "<tr>";

        echo "<td>".$row["user_name"]."</td>";

        echo "<td>".$row["error_form"]."</td>";

        echo "<td>".$row["error_date"]."</td>";

        echo "<td>".$row["error_description"]."</td>";

        echo "</tr>";

    }

    echo "</table>";

} else {

    echo "0 results";

}

//mysqli_close($conn);

?>

1 Answer

0 votes
by (10.2k points)

You need to make file connection.php with all of the tangences with MySQL connection and after ad in each file where you use MySQL query the line include_once('connection.php'); or you can have include_once('some_folder\connection.php');

Use the following mode to connect to DB. 

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

$link = mysqli_connect('localhost', 'mysql_user', 'mysql_password');

echo 'Connected successfully';

$res = mysqli_query($link, "CREATE TEMPORARY TABLE myCity LIKE City");

If you want to get more insights into SQL, check out this SQL Course from Intellipaat.

Browse Categories

...