Use PHP to implement login and registration functions and read MySQL database using PHP-display data in tabular form

Source: Internet
Author: User
Tags sql injection

Login interface

<body>
<form action= "login1.php" method= "POST" >
<div> Username: <input type= "text" name= "UID"/></div><br/>
<div> Password: <input type= "password" name= "pwd"/></div><br/>
<div><input type= "Submit" value= "Login"/></div>
</form>
</body>

Background login Processing

<?php
$uid = $_post["UID"];
$pwd = $_post["pwd"];

$db = new Mysqli ("localhost", "root", "" "," 0710_info ");
if (Mysqli_connect_error ()) {
Die ("Connection failed");
}
$sql = "Select pwd from users where uid= ' {$uid} '";
Echo $sql;
$result = $db->query ($sql);
$arr = $result->fetch_row ();

Preventing SQL injection attacks
if ($arr [0]== $pwd &&!empty ($pwd)) {
Jump to main interface (PHP mode)
Header ("location:register_system.php");
JS Jump Page Way
/*echo "<script>
window.location.href= ' register_system.php '
</script> "; * *
}else{
Header ("location:login_system.php");
}

?>

Registration interface

<body>
<form action= "register1.php" method= "POST" >
<div style= "font-size:20px; font-weight:1000; font-family: Microsoft ya black; margin-left:50px; margin-bottom:20px "> User registration </div>
<div> Username: <input type= "text" name= "UID"/></div><br/>
<div> login Password: <input type= "password" name= "pwd"/></div><br/>
<div> Name: <input type= "text" name= "name"/></div><br/>
<div> Gender:
<input type= "Radio" name= "Sex" value= "1"/> Male
<input type= "Radio" name= "Sex" value= "0"/> Female
</div><br/>
<div><input type= "Submit" value= "register" onclick= "return Check ()"/></div>
</form>
</body>
JS Form Verification
<script>
function Check () {
var uid = document.getelementsbytagname ("input") [0].value;
if (uid = = "") {
Alert ("Please enter user name!");
return false;
}
var pwd = document.getelementsbytagname ("input") [1].value;
if (pwd = = "") {
Alert ("Please enter your password!");
return false;
}
var name = document.getElementsByTagName ("input") [2].value;
if (name = = "") {
Alert ("Please enter your name!");
return false;
}
}
</script>

Background registration Processing

<?php
$uid = $_post["UID"];
$pwd = $_post["pwd"];
$name = $_post["name"];
$sex = $_post["Sex"];

$db = new Mysqli ("localhost", "root", "" "," 0710_info ");
if (Mysqli_connect_error ()) {
Die ("Connection failed");
}
$sql = "INSERT into users values (' {$uid} ', ' {$pwd} ', ' {$name} ', {$sex})";
$r = $db->query ($sql);
if ($r) {
echo "registered successfully!";
}else{
echo "Registration failed! ";
}

?>

PHP reads MySQL database--Displays data in tabular form

<BODY>
<table width= "50%" border= "1";
     <tr>
        <td> code </td>
         <td> name </td>
        <td> Gender </td>
        <td> birthday </td>
         <td> national </td>
    </tr>
<?php
$db = new Mysqli (" LocalHost "," root "," "", "0710_info");
$sql = "Select Info.code,info.name,sex,birthday,nation.name from info join nation on Info.nation=nation.code";// Union query
$result = $db->query ($sql);
$arr = $result->fetch_all ();
//var_dump ($arr);

Traversing all data with a foreach loop
foreach ($arr as $v) {
$sex = $v [2]? ' Male ': ' Female '; //Using ternary operator to judge gender
echo "<tr>
<td>{$v [0]}</td>
<td>{$v [1]}</td>
<td>{$sex}</td>
<td>{$v [3]}</td>
<td>{$v [4]}</td>
</tr> ";
}
?>
</table>
</body>

Use PHP to implement login and registration functions and read MySQL database using PHP-display data in tabular form

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.