Jump to content

if else is not working!


realjho

Recommended Posts

hello any body can help me with my codes? im new in php. my if else code is not working properly. it only read the else statement whether all the required input fields are empty or not. i always get the output of "Fill all the required fields!!" even if there are no empty fields. if a change the order of the condition it says that your registered but the data did not insert in my dayabase.

 

 

<?php

if($connect=@mysql_connect("localhost","root",""))
echo "";
else
die("Unable to Connect to the Database".mysql_error());
$connect=mysql_select_db("olreport");


if(isset($_POST['save']))
{        
$id = $_POST["userid"];
$user = $_POST['username'];
$pass = $_POST['password'];
$uname = $_POST['name'];
$pos= $_POST['position'];
$divi = $_POST['div'];
$divihd = $_POST['divhead'];
            
            if( !empty($id) && !empty($user) && !empty( $pass ) && !empty($uname) && !empty($pos) && !empty($divi) && !empty($divhd))    
            
            mysql_query("insert into signup(userid, username, password, name, position, division, divhead)values('".$id."','".$user."','".$pass."','".$uname."','".$pos."','".$divi. "','".$divihd."')");
            echo "<script>alert('You are Now Registered!!')</script>";
            
            }
            
            else if( empty($id) || empty($user) || empty( $pass ) || empty($uname) || empty($pos) || empty($divi) || empty($divhd))
            {
            echo "<script>alert('Fill all the required fields!!')</script>";
            }    
 include('signup.php')
?>

Link to comment
Share on other sites

Your code is messed up.

 

Problem lies with your connection part.

if($connect=@mysql_connect("localhost","root",""))
echo "";
else
die("Unable to Connect to the Database".mysql_error());
$connect=mysql_select_db("olreport")

 

--

You should not put a condition in a connection.

use or die(); instead.

 

--

 

I believe you knew this format

if () {

}

else if () {

}

else {

}

Edited by KaiSheng
Link to comment
Share on other sites

you actually need to debug what your code is doing. find out why it is not taking the execution path you expect. what values do all of those variables have in them?

 

also, here's some hints -

 

1) don't repeat yourself (DRY) and always try to use POSITIVE logic - 

if( empty($id) || empty($user) || empty( $pass ) || empty($uname) || empty($pos) || empty($divi) || empty($divhd))
{
    // your code for when this condition is true
    echo "<script>alert('Fill all the required fields!!')</script>";
} else {
    // your code for when this condition is false


}

2) you should also form your sql query statement in a php variable so that you can echo/log it for debugging purposes. when you do this and echo the sql query statement, does it contain the expected values?

 

3) you ALWAYS need to have error checking logic in your programs to test if your query executed or not before trying to use the result from a query. in the case of your "You are Now Registered" message, you should only display that if the query ran without any errors and the row was actually inserted.

 

4) a person's userid is generally the autoincrement field in your database and you wouldn't have that as a from field or a value you put into the sql query statement.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.