Fail la conectare PHP cu baza de date

Aici puteţi posta întrebari legate de instalare şi configurare PHP, Apache, IIS, MySQL, PostgreSQL, Oracle, SQL Server sau altele.

Moderator: Moderatori

bogdanho
PHPRomania Supporter
Mesaje: 7
Membru din: Mie Noi 16, 2011 6:40 pm

Fail la conectare PHP cu baza de date

Mesajde bogdanho » Joi Noi 21, 2019 5:30 pm

Buna ziua, am si eu de facut un website - la partea de design e ok, dar am probleme la partea cu PHP si MySQL.
Vreau sa fac celebrul sistem login si nu reusesc. Am cumparat un host si un domain si cand accesez situ-ul la sectiunea de login nu se intampla nimic cand apas pe "SUBMIT".
las aici codul poate ma poate sfatui cineva .
Va multumesc!!!

************************************************************************************************

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Bogdan University</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css">
</head>
<body>


<div class="containter">


<header><!-- here start the header -->
<div class="header-logo">
<img src="img/logo-university.png" alt="logo of university of Bogdan">
</div>

<div class="header-name">
<h1>UNIVERSITY OF BOGDAN</h1>
</div>

<div class="header-social">

<a class="btn" href="https://www.facebook.com">
<i class="fab fa-facebook"></i>
</a>

<a class="btn" href="https://twitter.com">
<i class="fab fa-twitter"></i>
</a>

<a class="btn" href="https://www.linkedin.com">
<i class="fab fa-linkedin"></i>
</a>

<a class="btn" href="https://www.youtube.com">
<i class="fab fa-youtube"></i>
</a>

</div>
</header><!-- here ends the header -->


<nav><!-- here starts the navigation menu -->
<div class="nav-menu">
<ul>
<li><a href="index.html">HOME</a></li>
<li><a href="courses.html">COURSES</a>
<div class="sub-menu">
<ul>
<li><a href="courses-undergraduated.html">UNDERGRADUATE</a></li>
<li><a href="courses-postgraduated.html">POSTGRADUATE</a></li>
</ul>
</div>
</li>
<li><a href="registration.html">REGISTRATION</a></li>
<li><a href="about.html">ABOUT</a>
<div class="sub-menu">
<ul>
<li><a href="about-why-BU.html">WHY BOGDAN UNI?</a></li>
<li><a href="about-accomodation.html">ACCOMMODATION</a></li>
<li><a href="about-student-fees.html">STUDENT FEES</a></li>
</ul>
</div>
</li>
<li><a href="contact.html">CONTACT</a></li>
<li class="active"><a href="access.html">LOGIN</a></li>
</ul>
</div>
</nav><!-- here ends the nav menu -->

<!-- here stats the main -->
<main>
<div class="login-box">
<div class="login-field">
<select name="username" id="Type-of-user">
<option value="-1">User type</option>
<option value="Admin">Admin</option>
<option value="Student">Student</option>
<option value="Lecturer">Lecturer</option>
</select>


<!-- <input type="text" placeholder="Type of User"> -->
</div>

<div class="login-field">
<input type="text" name="username" placeholder="Username">
</div>

<div class="login-field">
<input type="password" name="pwd" placeholder="Password">
</div>

<div class="login-field-submit">
<input type="submit" name="submit" value="Submit">
</div>
</div>
</main>
<!-- here end the main -->



<!-- here starts footer -->
<footer>
<div class="address">
<p>University of Bogdan || Radcliffe Sq, Oxford OX1 3BG || 01865 277162 || contact@universityofbogdan.ac.uk </p>
</div>

<div class="footer-botttom">
<p><a href="#">Privacy Policy</a> || <a href="#">Site Map</a> ||<a href="#">&#9400 Copyright by Bogdan Halinga</a></p>
</div>



</footer>
<!-- here ends footer -->
</div>



</body>
</html>

<?php


$con=mysql_connect('localhost:3306','ogdfios_admin','admin','bogdfios_University_of_Bogdan');
if(!$con)
{
echo"Unable to establish connection!" .mysql_error();
}

$db=mysql_select_db("bogdfios_University_of_Bogdan",$con);
if(!$db)
{
echo"Database not found!!!" .mysql_error();
}
if(isset($_POST['submit']))
{
$type=$_POST['type'];
$username=$_POST['username'];
$password=$_POST['pwd'];

$query="select * from login where username='$username' and password='$password'and type='$type' ";
$result=mysql_query($query);

while($row=mysql_fetch_array($result))
{
if($row['username']==$username && $row['password']==$password &&$row['type']=='Admin'){
header("Location: admin.html");
}
elseif($row['username']==$username && $row['password']==$password &&$row['type']=='Student'){
header("Location: student.html");
}
elseif($row['username']==$username && $row['password']==$password &&$row['type']=='Lecturer'){
header("Location: lecturer.html");
}
}
}
?>



************************************************************************************************



Avatar utilizator
Jetix
Average Member
Mesaje: 50
Membru din: Mie Aug 09, 2017 3:16 am
Contact:

Re: Fail la conectare PHP cu baza de date

Mesajde Jetix » Joi Noi 21, 2019 9:47 pm

Pai normal sa nu faca nimic daca nu ai definit tagul "<form>" :lol:

Cod: Selectaţi tot

<?php


$con=mysql_connect('localhost:3306','ogdfios_admin','admin','bogdfios_University_of_Bogdan');
if(!$con) {
   echo"Unable to establish connection! ".mysql_error();
}

$db=mysql_select_db("bogdfios_University_of_Bogdan",$con);

if(!$db) {
   echo"Database not found!!!" .mysql_error();
}

if(isset($_POST['submit'])) {
   list($type, $user, $pass) = $_POST['data'];

   $query = "SELECT * FROM `login` WHERE `username` = $user AND `password` = $pass AND `type` = $type LIMIT 1;";
   $result = mysql_query($query);
   $check = mysql_num_rows($result);

   //Mai lenjer este foreach decat while, parerea mea.
   foreach (mysql_fetch_array($result) as $row) {
      // Nu inteleg de ce mai faci verificari, daca baza de date iti returneaza true.
      if ($check > 0) {
         header('Location: '.$row['type'].'.html');
      } else {
         // Aici ii dai ce sa faca daca baza de date nu gaseste nimic cu datele introduse
         header("Location: user.html");
      }
      // Gata, cu un singur if ai rezolvat tot si l-ai automatizat.

      if($row['username'] == $user && $row['password'] == $pass && $row['type'] == 'Admin') {
         header("Location: admin.html");
      } elseif ($row['username'] == $user && $row['password'] == $pass && $row['type'] == 'Student') {
         header("Location: student.html");
      } elseif ($row['username'] == $user && $row['password'] == $pass && $row['type'] == 'Lecturer') {
         header("Location: lecturer.html");
      }
   }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Bogdan University</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css">
</head>
<body>


<div class="containter">


<header><!-- here start the header -->
<div class="header-logo">
<img src="img/logo-university.png" alt="logo of university of Bogdan">
</div>

<div class="header-name">
<h1>UNIVERSITY OF BOGDAN</h1>
</div>

<div class="header-social">

<a class="btn" href="https://www.facebook.com">
<i class="fab fa-facebook"></i>
</a>

<a class="btn" href="https://twitter.com">
<i class="fab fa-twitter"></i>
</a>

<a class="btn" href="https://www.linkedin.com">
<i class="fab fa-linkedin"></i>
</a>

<a class="btn" href="https://www.youtube.com">
<i class="fab fa-youtube"></i>
</a>

</div>
</header><!-- here ends the header -->


<nav><!-- here starts the navigation menu -->
<div class="nav-menu">
<ul>
<li><a href="index.html">HOME</a></li>
<li><a href="courses.html">COURSES</a>
<div class="sub-menu">
<ul>
<li><a href="courses-undergraduated.html">UNDERGRADUATE</a></li>
<li><a href="courses-postgraduated.html">POSTGRADUATE</a></li>
</ul>
</div>
</li>
<li><a href="registration.html">REGISTRATION</a></li>
<li><a href="about.html">ABOUT</a>
<div class="sub-menu">
<ul>
<li><a href="about-why-BU.html">WHY BOGDAN UNI?</a></li>
<li><a href="about-accomodation.html">ACCOMMODATION</a></li>
<li><a href="about-student-fees.html">STUDENT FEES</a></li>
</ul>
</div>
</li>
<li><a href="contact.html">CONTACT</a></li>
<li class="active"><a href="access.html">LOGIN</a></li>
</ul>
</div>
</nav><!-- here ends the nav menu -->

<!-- here stats the main -->
<main>
<div class="login-box">
<div class="login-field">
   <form method="post">
      <select name="data[]" id="Type-of-user">
      <option value="-1">User type</option>
      <option value="Admin">Admin</option>
      <option value="Student">Student</option>
      <option value="Lecturer">Lecturer</option>
      </select>


      <!-- <input type="text" placeholder="Type of User"> -->
      </div>

      <div class="login-field">
      <input type="text" name="data[]" placeholder="Username">
      </div>

      <div class="login-field">
      <input type="password" name="data[]" placeholder="Password">
      </div>

      <div class="login-field-submit">
      <input type="submit" name="submit" value="Submit">
   </form>
</div>
</div>
</main>
<!-- here end the main -->



<!-- here starts footer -->
<footer>
<div class="address">
<p>University of Bogdan || Radcliffe Sq, Oxford OX1 3BG || 01865 277162 || contact@universityofbogdan.ac.uk </p>
</div>

<div class="footer-botttom">
<p><a href="#">Privacy Policy</a> || <a href="#">Site Map</a> ||<a href="#">&#9400 Copyright by Bogdan Halinga</a></p>
</div>



</footer>
<!-- here ends footer -->
</div>



</body>
</html>


P.S: Ti-am aranjat putin codul, daca iti place, bine, daca nu folosestel pe al tau si mai adauga tagul "<form>".

bogdanho
PHPRomania Supporter
Mesaje: 7
Membru din: Mie Noi 16, 2011 6:40 pm

Re: Fail la conectare PHP cu baza de date

Mesajde bogdanho » Lun Noi 25, 2019 4:50 pm

[quote="Jetix"]Pai normal sa nu faca nimic daca nu ai definit tagul "<form>" :lol:

Am folosit codul tau si cand deschid pagina in browser imi da eroare "This page isn’t workingbogdanho.com is currently unable to handle this request.
HTTP ERROR 500"

de la ce crezi ca e?

Avatar utilizator
Jetix
Average Member
Mesaje: 50
Membru din: Mie Aug 09, 2017 3:16 am
Contact:

Re: Fail la conectare PHP cu baza de date

Mesajde Jetix » Mar Noi 26, 2019 7:07 pm

bogdanho scrie:
Jetix scrie:Pai normal sa nu faca nimic daca nu ai definit tagul "<form>" :lol:

Am folosit codul tau si cand deschid pagina in browser imi da eroare "This page isn’t workingbogdanho.com is currently unable to handle this request.
HTTP ERROR 500"

de la ce crezi ca e?


Cod: Selectaţi tot

<?php


$pdo = new PDO("mysql:host=localhost:3306; dbname=bogdfios_University_of_Bogdan;charset=utf8", 'admin', 'ogdfios_admin');

if(isset($_POST['submit'])) {
   list($type, $user, $pass) = $_POST['data'];

   $data = [
      $user,
      $pass,
      $type
   ];

   $result = $pdo->prepare("SELECT `type` FROM `login` WHERE `username` = ? AND `password` = ? AND `type` = ? LIMIT 1;");
   $result->execute($data);

   //Mai lenjer este foreach decat while, parerea mea.
   if ($result->rowCount() > 0) {
      foreach ($result->fetchAll(PDO::FETCH_ASSOC) as $row) {
         switch ($row['type']) {
            case 'admin':
               header("Location: admin.html");
               break;
            case 'Student':
               header("Location: student.html");
               break;
            case 'Lecturer':
               header("Location: lecturer.html");
               break;
           
            default:
               header("Location: user.html");
               break;
         }
      }
   } else {
      print 'No data output.';
   }
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Bogdan University</title>
<link rel="stylesheet" href="style.css">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.4.1/css/all.css">
</head>
<body>


<div class="containter">


<header><!-- here start the header -->
<div class="header-logo">
<img src="img/logo-university.png" alt="logo of university of Bogdan">
</div>

<div class="header-name">
<h1>UNIVERSITY OF BOGDAN</h1>
</div>

<div class="header-social">

<a class="btn" href="https://www.facebook.com">
<i class="fab fa-facebook"></i>
</a>

<a class="btn" href="https://twitter.com">
<i class="fab fa-twitter"></i>
</a>

<a class="btn" href="https://www.linkedin.com">
<i class="fab fa-linkedin"></i>
</a>

<a class="btn" href="https://www.youtube.com">
<i class="fab fa-youtube"></i>
</a>

</div>
</header><!-- here ends the header -->


<nav><!-- here starts the navigation menu -->
<div class="nav-menu">
<ul>
<li><a href="index.html">HOME</a></li>
<li><a href="courses.html">COURSES</a>
<div class="sub-menu">
<ul>
<li><a href="courses-undergraduated.html">UNDERGRADUATE</a></li>
<li><a href="courses-postgraduated.html">POSTGRADUATE</a></li>
</ul>
</div>
</li>
<li><a href="registration.html">REGISTRATION</a></li>
<li><a href="about.html">ABOUT</a>
<div class="sub-menu">
<ul>
<li><a href="about-why-BU.html">WHY BOGDAN UNI?</a></li>
<li><a href="about-accomodation.html">ACCOMMODATION</a></li>
<li><a href="about-student-fees.html">STUDENT FEES</a></li>
</ul>
</div>
</li>
<li><a href="contact.html">CONTACT</a></li>
<li class="active"><a href="access.html">LOGIN</a></li>
</ul>
</div>
</nav><!-- here ends the nav menu -->

<!-- here stats the main -->
<main>
<div class="login-box">
<div class="login-field">
   <form method="post">
      <select name="data[]" id="Type-of-user">
      <option value="-1">User type</option>
      <option value="Admin">Admin</option>
      <option value="Student">Student</option>
      <option value="Lecturer">Lecturer</option>
      </select>


      <!-- <input type="text" placeholder="Type of User"> -->
      </div>

      <div class="login-field">
      <input type="text" name="data[]" placeholder="Username">
      </div>

      <div class="login-field">
      <input type="password" name="data[]" placeholder="Password">
      </div>

      <div class="login-field-submit">
      <input type="submit" name="submit" value="Submit">
   </form>
</div>
</div>
</main>
<!-- here end the main -->



<!-- here starts footer -->
<footer>
<div class="address">
<p>University of Bogdan || Radcliffe Sq, Oxford OX1 3BG || 01865 277162 || contact@universityofbogdan.ac.uk </p>
</div>

<div class="footer-botttom">
<p><a href="#">Privacy Policy</a> || <a href="#">Site Map</a> ||<a href="#">&#9400 Copyright by Bogdan Halinga</a></p>
</div>



</footer>
<!-- here ends footer -->
</div>



</body>
</html>


L-am editat putin, iar eroarea probabil o da pentru ca nu merge sa se conecteze la baza de date.


Înapoi la “Instalare”

Cine este conectat

Utilizatori ce ce navighează pe acest forum: Niciun utilizator înregistrat și 6 vizitatori