Anindit Sinha Anindit Sinha - 4 months ago 15
PHP Question

Else part is not working. Checked thoroughly, no such solution found which helps

<?php
if(isset($_POST['tt1']) && isset($_POST['tt2']) && isset($_POST['tt3']) && isset($_POST['tt4'])){
$con = mysqli_connect("localhost","root","","weblib");
if(mysqli_connect_errno()){
echo "Could not connect.";
}
$query = "SELECT * FROM login WHERE MEMBER_NAME = '{$_POST['tt1']}'";
$queryArray = mysqli_fetch_array(mysqli_query($con,$query));
if($_POST['tt2']==$_POST['tt3']){
if(empty($queryArray['MEMBER_NAME'])){
mysqli_query($con,"UPDATE login SET MEMBER_NAME = '{$_POST['tt1']}', PASSWORD = '{$_POST['tt2']}' WHERE MEMBER_NAME = '{$_POST['tt4']}'");
echo "
<script language='javascript'>
window.alert('Profile information successfully updated. Click ok to continue');
</script>
";
}
else{
echo "
<script type='javascript'>
window.alert('Username already exists. Please choose a different username');
</script>
";
}
}
else
echo "
<script type=javascript>
window.alert('Passwords do not match');
</script>
";
}


else part for the 'passwords do not match' part is not being executed. This same thing is happening for another page as well. There is no MySQL error, because everything else is working pretty perfectly. Not that the else part which says "username already exists" works perfectly as well. Only the outer else is not being executed.

Answer

Change the else part to this:

else {
    echo "
        <script>
           alert('Passwords do not match');
        </script>
    ";
}

According to PSR, I always recommend to use braces for block codes.