Jump to content

SUBIECTE NOI
« 1 / 5 »
RSS
Info Coronavirus/Vaccinare vs Fake News

La multi ani @marinel1976!

La multi ani de Sfanta Maria!

La multi ani @xcit!
 Chiuveta de baie (alba) ceramica ...

Sapa autonivelanta - uscare

Parere Audi/VW/Skoda

ROA (Rise Of Artificial) - FLOW D...
 Sfaturi dimensiuni elemente lemn

Se mai dau tichete de masa pentru...

Nu imi apare chat-ul pe YouTube

Modem Huawei E3372 vs E3372h / al...
 Indepartare pete infiltratii cu c...

Sfaturi pentru ingrasare

Relatie disfunctionala cu sotul d...

Decizia FINALA a boxelor de podea...
 

Undefined Variable PHP ?!

- - - - -
  • Please log in to reply
10 replies to this topic

#1
g0dafk

g0dafk

    Member

  • Grup: Members
  • Posts: 459
  • Înscris: 10.11.2016
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div id="id02" class="modal">
<form class="modal-content animate" action="/action_page.php">

<div class="container">
<form method="GET" action="register.php">
	 <label for="uname"><b>Username</b></label>
	 <input type="text" placeholder="Enter Username" name="uname" value="<?php echo $uname; ?>" required >
	 <label for="psw"><b>Password</b></label>
	 <input type="password" placeholder="Enter Password" name="psw" value="<?php echo $psw; ?>" required >
	 <label for="psw"><b>Confirm Password</b></label>
	 <input type="password" placeholder="Confirm Password" name="repsw" value="<?php echo $repsw; ?>" required >
	 <button type="submit">Sing up</button>
	 <label>
		 <input type="checkbox" checked="checked" name="remember"> Remember me
	 </label>
</form>
</div>
<div class="container" style="background-color:#f1f1f1">
	 <button type="button" onclick="document.getElementById('id02').style.display='none'"
		 class="cancelbtn">Cancel</button>
</div>
</form>
</div>
</body>
</html>


Si zice undefined variable ( acele $uname, $psw si $repsw ).
Fisierul are extensia .php, folosesc wampp ca sa execute php-ul... E un fisier separat de index, si index-ul eu cred ca e ok, deci care e problema...?

#2
dani.user

dani.user

    Guru Member

  • Grup: Senior Members
  • Posts: 29,041
  • Înscris: 24.02.2007
Nu vad sa fi scris niciunde $uname = ...

#3
g0dafk

g0dafk

    Member

  • Grup: Members
  • Posts: 459
  • Înscris: 10.11.2016
Pai parca in PHP nu trebuia initializate variabilele, nu?
Pur si simplu le folosesti cand ai nevoie de ele.
Trebuie initializata anterior ?
$uname=""

Si apoi sa o utilizez?

Inca o intrebare, pot scrie asta:
<form class="modal-content animate" action="/action_page.php"  method="GET" action="register.php">

Asa, intr-o singura linie de cod? Sau trebuie sa fac 2 <form> ? Fiind 2 action acolo ma gandesc ca... poate apare conflict ceva.

Edited by g0dafk, 19 April 2019 - 20:02.


#4
Buleandra94

Buleandra94

    Member

  • Grup: Members
  • Posts: 644
  • Înscris: 19.08.2007

View Postg0dafk, on 19 aprilie 2019 - 19:59, said:

Pai parca in PHP nu trebuia initializate variabilele, nu?
Pur si simplu le folosesti cand ai nevoie de ele.
Trebuie initializata anterior ?
$uname=""

Si apoi sa o utilizez?

Ce anume incerci sa faci? Acele variabile sunt nedefinite, ce te astepti sa contina?

Scrii "afiseza variabila $PAROLA", de unde sa stie saracul PHP ce ar vrei sa contina acea variabila?

Edited by Buleandra94, 19 April 2019 - 20:02.


#5
g0dafk

g0dafk

    Member

  • Grup: Members
  • Posts: 459
  • Înscris: 10.11.2016
Aaaaaaaa, ok, acum am inteles.

#6
iulian_1976

iulian_1976

    Member

  • Grup: Members
  • Posts: 991
  • Înscris: 10.05.2008
Probabil porneste de la confuzia tipul unei variabile nu este declarata explicit ci in momentul afectarii valorii.

Edited by iulian_1976, 19 April 2019 - 23:00.


#7
g0dafk

g0dafk

    Member

  • Grup: Members
  • Posts: 459
  • Înscris: 10.11.2016
Buna ziua, revin cu inca o problema la care sper ca ma puteti ajuta:

Am creat o baza de date folosind WampServer ( phpmyadmin ) in care am inserat 2 campuri respecetiv nume si parola.
Am creat o forma de "inregistrare" care contine nume si parola si am incercat sa inserez din acea forma in baza mea de date, dar... nu functioneaza.

Am separat codul in mai multe fisiere:

register.php
<?php include('server.php') ?>
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div id="id02" class="modal">
<form class="modal-content animate" method="GET" action="php/server.php">

	<div class="container">

		<label for="uname"><b>Username</b></label>
		<input type="text" placeholder="Enter Username" name="uname" value="<?php echo $uname=""; ?>" required >
		<label for="psw"><b>Password</b></label>
		<input type="password" placeholder="Enter Password" name="psw" value="<?php echo $psw=""; ?>" required >
		<label for="psw"><b>Confirm Password</b></label>
		<input type="password" placeholder="Confirm Password" name="repsw" value="<?php echo $repsw=""; ?>" required >
		<button type="submit">Sing up</button>
		<label>
			<input type="checkbox" checked="checked" name="remember"> Remember me
		</label>
 
	</div>
	<div class="container" style="background-color:#f1f1f1">
		<button type="button" onclick="document.getElementById('id02').style.display='none'"
			class="cancelbtn">Cancel</button>
	</div>
</form>
</div>
</body>
</html>



server.php
<?php
if ( session_id() != "" ) {
	session_start();
}
// initializing variables
$uname = "";
$errors = array();
// connect to the database
$db = mysqli_connect('localhost', 'root', '', 'avizier');
// REGISTER USER
if (isset($_POST['reg_user'])) {
  // receive all input values from the form
  $uname = mysqli_real_escape_string($db, $_POST['uname']);
  $psw = mysqli_real_escape_string($db, $_POST['psw']);
  $repsw = mysqli_real_escape_string($db, $_POST['repsw']);
  // form validation: ensure that the form is correctly filled ...
  // by adding (array_push()) corresponding error unto $errors array
  if (empty($uname)) { array_push($errors, "uname is required"); }
 
  if (empty($psw)) { array_push($errors, "psw is required"); }
  if ($psw != $repsw) {
array_push($errors, "The two psws do not match");
  }
  // first check the database to make sure
  // a user does not already exist with the same uname and/or email
  $user_check_query = "SELECT * FROM users WHERE uname='$uname' LIMIT 1";
  $result = mysqli_query($db, $user_check_query);
  $user = mysqli_fetch_assoc($result);
 
  if ($user) { // if user exists
	if ($user['uname'] === $uname) {
	  array_push($errors, "uname already exists");
	}

  // Finally, register user if there are no errors in the form
  if (count($errors) == 0) {
   //$psw = md5($psw);//encrypt the psw before saving in the database
   $query = "INSERT INTO users (uname, psw)
	   VALUES('$uname','$psw')";
   mysqli_query($db, $query);
   $_SESSION['uname'] = $uname;
   $_SESSION['success'] = "You are now logged in";
   header('location: index.php');
  }
}
}

// LOGIN USER
if (isset($_POST['login_user'])) {
  $uname = mysqli_real_escape_string($db, $_POST['uname']);
  $psw = mysqli_real_escape_string($db, $_POST['psw']);
  if (empty($uname)) {
   array_push($errors, "uname is required");
  }
  if (empty($psw)) {
   array_push($errors, "psw is required");
  }
  if (count($errors) == 0) {
   $psw = md5($psw);
   $query = "SELECT * FROM users WHERE uname='$uname' AND psw='$psw'";
   $results = mysqli_query($db, $query);
   if (mysqli_num_rows($results) == 1) {
	 $_SESSION['uname'] = $uname;
	 $_SESSION['success'] = "You are now logged in";
	 header('location: index.php');
   }else {
	array_push($errors, "Wrong uname/psw combination");
   }
  }
}
?>



Si desigur si index.php pe care nu cred ca are rost sa-l postez, dar daca este nevoie o sa o fac.

Unde am gresit? Tin sa mentionez ca daca scriu comenzile SQL acolo in phpmyadmin acestea functioneaza, dar nu si prin intermediul "site-ului", a formei de register, ma rog.

#8
dani.user

dani.user

    Guru Member

  • Grup: Senior Members
  • Posts: 29,041
  • Înscris: 24.02.2007
Attached File  Untitled.png   56.59K   13 downloads

#9
g0dafk

g0dafk

    Member

  • Grup: Members
  • Posts: 459
  • Înscris: 10.11.2016
Mersi, acum mi-am dat si eu seama. L-am schimbat complet si-mi mergea o perioada, acum l-am stricat iar si nu inteleg de ce.


server.php
<?php
if (session_id() != "") {
session_start();
}
$servername = "127.0.0.1";
$username = "root";
$password = "";
$usern = $_GET['username'];
$passw = $_GET['password'];
$confirm_passw = $_GET['confirm_password'];
// Create connection
$conn = new mysqli($servername, $username, $password);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "avizier";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($passw === $confirm_passw) {
$sql = "INSERT INTO users (username, password) VALUES ('$usern','$passw')";
include('avizier.php');
$query=mysqli_query($sql,$conn);
if ($query === TRUE) {
	 include('avizier.php');
}
}
?>



register.php
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style.css">
</head>
<body>
<div id="id02" class="modal">
<form class="modal-content animate" action="php/server.php" method="GET" >

<div class="container">

	 <label for="uname"><b>Username</b></label>
	 <input type="text" placeholder="Enter Username" name="username" id="username" required >
	 <label for="psw"><b>Password</b></label>
	 <input type="password" placeholder="Enter Password" name="password" id="password" required >
	 <label for="psw"><b>Confirm Password</b></label>
	 <input type="password" placeholder="Confirm Password" name="confirm_password" id="confirm_password" onkeyup='check();' required >
<span id='message'></span>
	 <button type="submit" value="submit" id="submit">Sing up</button>
	 <label>
		 <input type="checkbox" checked="checked" name="remember"> Remember me
	 </label>

</div>
<div class="container" style="background-color:#f1f1f1">
	 <button type="button" onclick="document.getElementById('id02').style.display='none'"
		 class="cancelbtn">Cancel</button>
</div>
</form>
<script>
var check = function() {
if (document.getElementById('password').value ==
document.getElementById('confirm_password').value) {
document.getElementById('message').style.color = 'green';
document.getElementById('message').innerHTML = 'Matching';
} else {
document.getElementById('message').style.color = 'red';
document.getElementById('message').innerHTML = 'Not matching';
}
}
</script>
</div>

</body>
</html>


Problema este ca acea comanda din variabila $sql ($sql = "INSERT INTO users (username, password) VALUES ('$usern','$passw')"; ) nu functioneaza cred, deoarece am folosit un debugger si acele variabile sunt goale, deci nu imi primeste numele si parola din forma... Si culmea cu cateva ore inainte mergea, dar nu stiu ce s-a intamplat.

Edited by g0dafk, 29 April 2019 - 21:30.


#10
dani.user

dani.user

    Guru Member

  • Grup: Senior Members
  • Posts: 29,041
  • Înscris: 24.02.2007
Cand faci schimbari folosesti POST.
Observ ca ai scos si fragila protectie anti sql injection, lasand codul acum vulnerabil.

#11
g0dafk

g0dafk

    Member

  • Grup: Members
  • Posts: 459
  • Înscris: 10.11.2016
Am incercat si cu POST si aceeasi eroare, mai exact mysqli_query() expects parameter 1 to be mysqli, object given bla bla on line ... adica pe linia $query=mysqli_query($sql,$conn); pentru ca .. sql dupa cum ziceam nu primeste nimic.

Nu o sa-i fac protectie fiind un proiect pentru scoala... doar daca imi cere profesorul ( sau aduce vorba de asa ceva ).

Anunturi

Chirurgia cranio-cerebrală minim invazivă Chirurgia cranio-cerebrală minim invazivă

Tehnicile minim invazive impun utilizarea unei tehnologii ultramoderne.

Endoscoapele operatorii de diverse tipuri, microscopul operator dedicat, neuronavigația, neuroelectrofiziologia, tehnicile avansate de anestezie, chirurgia cu pacientul treaz reprezintă armamentarium fără de care neurochirurgia prin "gaura cheii" nu ar fi posibilă. Folosind tehnicile de mai sus, tratăm un spectru larg de patologii cranio-cerebrale.

www.neurohope.ro

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

Forumul Softpedia foloseste "cookies" pentru a imbunatati experienta utilizatorilor Accept
Pentru detalii si optiuni legate de cookies si datele personale, consultati Politica de utilizare cookies si Politica de confidentialitate