undefined variable

Hi

I’m getting this error:

Undefined variable: c_pass in D:\Web Data\divdev\profile.php on line 57

this is the line 57:

 if ($c_pass != $cryptpass) {

And this is the full code>

[php]

<?php include "lib/configuration.php"; include "language/frontend/portuguese.php"; include "templates/frontend/header.tpl"; if ($_POST) { $link = mysqli_connect($host, $user, $pass, $db); $f_pass1 = $_POST['pass1']; $f_pass2 = $_POST['pass2']; $f_fname = $_POST['fname']; $f_lname = $_POST['lname']; $f_email = $_POST['email']; $f_type = 0; if (strlen($f_pass1) == 0 AND strlen($f_pass2) == 0) { $query = "UPDATE users SET first_name = ?, last_name = ?, email = ?, c_type = ? WHERE c_user = ?"; if ($stmt = mysqli_prepare($link, $query)) { mysqli_stmt_bind_param($stmt, "sssss", $f_fname, $f_lname, $f_email, $f_type, $_SESSION['user']); mysqli_stmt_execute($stmt); if (mysqli_stmt_errno($stmt)){ echo ""; } else { echo ""; } mysqli_stmt_close($stmt); } } else { $query = "SELECT c_pass FROM users WHERE c_user = " . $_SESSION['user']; if ($result = mysqli_query($link, $query)) { if (mysqli_stmt_errno($stmt)){ echo ""; } else { while ($row = mysqli_fetch_assoc($result)) { $c_pass = $row['c_pass']; } mysqli_free_result($result); } } if ($f_pass1 != $f_pass2) { echo ""; } else { $cryptpass = md5($f_pass1); $query = "UPDATE users SET first_name = ?, last_name = ?, email = ?, c_pass = ?, c_type = ? WHERE c_user = ?"; if ($stmt = mysqli_prepare($link, $query)) { mysqli_stmt_bind_param($stmt, "ssssss", $f_fname, $f_lname, $f_email, $cryptpass, $f_type, $_SESSION['user']); mysqli_stmt_execute($stmt); if (mysqli_stmt_errno($stmt)){ echo ""; } else { if ($c_pass != $cryptpass) { // line 57 $_SESSION = array(); session_unset(); session_destroy(); echo ""; } } mysqli_stmt_close($stmt); mysqli_close($link); } } } } else { include "templates/frontend/loginspace.tpl"; include "templates/frontend/registerlogin.tpl"; include "templates/frontend/searchspace.tpl"; include "templates/frontend/search.tpl"; include "templates/frontend/hmenuspace.tpl"; include "templates/frontend/menubar.tpl"; include "templates/frontend/leftspace.tpl"; include "templates/frontend/leftside.tpl"; include "templates/frontend/contentspace.tpl"; $link = mysqli_connect($host, $user, $pass, $db); if (mysqli_connect_errno()) { echo ""; exit(); } $query = "SELECT * FROM users WHERE c_user = '" . $_SESSION['user'] . "'"; if ($result = mysqli_query($link, $query)) { if (mysqli_errno($link)) { echo ""; } while ($row = mysqli_fetch_assoc($result)) { $fname = $row['first_name']; $lname = $row['last_name']; $email = $row['email']; $user = $row['c_user']; } } mysqli_free_result($result); mysqli_close($link); include "templates/frontend/profile.tpl"; include "templates/frontend/rightspace.tpl"; if (isset($_SESSION['logedin'])) { include "templates/frontend/myaccount.tpl"; } include "templates/frontend/rightside.tpl"; include "templates/frontend/footerspace.tpl"; include "templates/frontend/footerdiv.tpl"; include "templates/frontend/footer.tpl"; } ?>

[/php]

Can someone please tell me what is wrong?

Thank You

The error is coming from the fact that this query is not returning any rows
[php]$query = "SELECT c_pass FROM users WHERE c_user = " . $_SESSION[‘user’];[/php]
You coulc declare the $c_pass just below that line and set it to empty, this way if the query returns 0 rows the var is still set and won’t throw the error.
[php]$c_pass = ‘’;[/php]
Also there is no need to use a while() to get the row info since you are only getting a single row, just a waste of cpu usage and code.

It is working. Thank You.

How would I get the $c_pass without the while() loop?

It would take some reworking of the if() to get rid of the while loop.
[php]
$query = "SELECT c_pass FROM users WHERE c_user = " . $_SESSION[‘user’];
$c_pass = ‘’;
if ($result = mysqli_query($link, $query)) {
if (mysqli_stmt_errno($stmt)){
echo “”;
} elseif (mysqli_num_rows($result) > 0) {
$row = mysqli_fetch_assoc($result);
$c_pass = $row[‘c_pass’];
}
}
mysqli_free_result($result);
[/php]

No need for all of that:

          if ($row = mysqli_fetch_array($result)) {
            $c_pass = $row['c_pass'];
          }
Sponsor our Newsletter | Privacy Policy | Terms of Service