Hello Experts,

I hate PHP when these things comes in front of me every time when i create and insert query I stuck up by viewing everything is ok but not working don't know why. please help me out

previewuser.php

<?php
    include_once('includes/header.php');
    include_once('includes/session.php');
    include_once('includes/functions.php');
    include_once('includes/connection.php');

    if(isset($_POST["submit"])) {
        echo add_user();
    }
?>

<form method="POST" action="previewuser.php">
                <label>
                    <p>First Name</p>
                    <p class="info"><?php echo  $fname = $_SESSION["fname"]; ?></p>
                    <input type="hidden" name="fname" value="<?php echo $fname; ?>" />
                </label>

                <div class="clear"></div>

                <label>
                    <p>Last Name</p>
                    <p class="info"><?php echo  $lname = $_SESSION["lname"]; ?></p>
                    <input type="hidden" name="lname" value="<?php echo $lname; ?>" />
                </label>

                <div class="clear"></div>

                <label>
                    <p>Father's Name</p>
                    <p class="info"><?php echo  $father = $_SESSION["father"]; ?></p>
                    <input type="hidden" name="father" value="<?php echo $father; ?>" />
                </label>

                <div class="clear"></div>

                <label>
                    <p>User Name</p>
                    <p class="info"><?php echo  $uname = $_SESSION["username"]; ?></p>
                    <input type="hidden" name="uname" value="<?php echo $uname; ?>" />
                </label>

                <div class="clear"></div>

                <label>
                    <p>User Type</p>
                    <p class="info"><?php echo  $utype = $_SESSION["utype"]; ?></p>
                    <input type="hidden" name="utype" value="<?php echo $utype; ?>" />
                </label>

                <div class="clear"></div>

                <label>
                    <p>Date Of Birth</p>
                    <p class="info"><?php echo  $dob = $_SESSION["dob"]; ?></p>
                    <input type="hidden" name="dob" value="<?php echo $dob; ?>" />
                </label>

                <div class="clear"></div>

                <label>
                    <p>Address</p>
                    <p class="info"><?php echo  $add = $_SESSION["add"]; ?></p>
                    <input type="hidden" name="add" value="<?php echo $add; ?>" />
                </label>

                <div class="clear"></div>

                <label>
                    <p>Phone Number</p>
                    <p class="info"><?php echo  $phno = $_SESSION["phnumber"]; ?></p>
                    <input type="hidden" name="phno" value="<?php echo $phno; ?>" />
                </label>

                <div class="clear"></div>

                <label>
                    <p>E-Mail</p>
                    <p class="info"><?php echo  $email = $_SESSION["email"]; ?></p>
                    <input type="hidden" name="email" value="<?php echo $email; ?>" />
                </label>

                <div class="clear"></div>

                <label>
                    <p>Designation</p>
                    <p class="info"><?php echo  $desig = $_SESSION["designation"]; ?></p>
                    <input type="hidden" name="desig" value="<?php echo $desig; ?>" />
                </label>

                <div class="clear"></div>

                <label>
                    <p>Employment Status</p>
                    <p class="info"><?php echo  $emp = $_SESSION["empstatus"]; ?></p>
                    <input type="hidden" name="desig" value="<?php echo $emp; ?>" />
                </label>

                <div class="clear"></div>

                <label>
                    <p>Allowed Clock In</p>
                    <p class="info"><?php echo  $aci = $_SESSION["aci"]; ?></p>
                    <input type="hidden" name="empstatus" value="<?php echo $aci; ?>" />
                </label>

                <div class="clear"></div>

                <label>
                    <p>Shift</p>
                    <p class="info"><?php echo  $shft = $_SESSION["shft"]; ?></p>
                    <input type="hidden" name="shift" value="<?php echo $shft; ?>" />
                </label>

                <div class="clear"></div>

                <div class="input" style="text-align:center;">
                    <p>&nbsp;</p>             
                    <input type="submit" value="Preview" class="btn1" name="submit" />
                    <a href="adduser.php" style="text-decoration:none;" class="btn1">Edit<a/>
                </div>
                </form>

Insert function

function add_user() {
        global $connection;

        $fname      = $_POST['fname'];
        $lname      = $_POST['lname'];
        $fathername = $_POST['father'];
        $uname      = $_POST['uname'];
        $pswrd      = $_POST['pswrd'];
        $utype      = $_POST['utype'];
        $dob        = $_POST['dob'];
        $add        = $_POST['add'];
        $phno       = $_POST['phno'];
        $email      = $_POST['email'];
        $desig      = $_POST['desig'];
        $emp        = $_POST['emp'];
        $aci        = $_POST['aci'];
        $shift      = $_POST['shift'];

            $insert  = "INSERT INTO users (fname, lname, father_name, utype, dob, address, ph_number, email, designation, emp_stat, clock_in, shift, user_name, password)";
            $insert .= "VALUES ('$fname', '$lname', '$fathername', '$utype', '$dob', '$add', '$phno', '$email', '$desig', '$emp', '$aci', '$shift', '$uname', '$pswrd')";

            $insert_query = mysqli_query($connection, $insert);

            if(confirm_query($insert_query)) {
                $_SESSION["message"] = "Your user was added successfully";
                redirect_to("mng_user.php");
            } else {
                $_SESSION["message"] = "There were some errors please re-add the user";
                redirect_to("adduser.php");
            }
    }

and please do let me know also how do i cal $connection in global scope as a parameter

Thank You

what error you have when run the form ?
did you try the sql in phpmyadmin ?

yes the error is database query failed query is not working

In which line you are facing error?

Hi
I have explained it. the error i dont know in which line it is when i click on submit it says data base query failed the insert query is not working

            $insert  = "INSERT INTO users (fname, lname, father_name, utype, dob, address, ph_number, email, designation, emp_stat, clock_in, shift, user_name, password)";
            $insert .= "VALUES ('$fname', '$lname', '$fathername', '$utype', '$dob', '$add', '$phno', '$email', '$desig', '$emp', '$aci', '$shift', '$uname', '$pswrd')";
            $insert_query = mysqli_query($connection, $insert);
            if(confirm_query($insert_query)) {
                $_SESSION["message"] = "Your user was added successfully";
                redirect_to("mng_user.php");
            } else {
                $_SESSION["message"] = "There were some errors please re-add the user";
                redirect_to("adduser.php");
            }
    }

password is a reserved word, use backticks.

changed but still not working

try including your connection.php after you include the header.php. Include it above all, when header.php is also needing the connection.

In your case the $connection is a variable, and one of the Dont's in developers code of ethics is to avoid using variable in global scope. The reason is that if connection is a function or a method of an object, that object or the class will not know what is being modified by other methods and functions calling the connection.

In other words, this is nothing different than using an static method of a class. The class itself have no knowledge on how the static method is implemented.

example in procedural

$connection = ''; define your mySQLI connection here.

if the mysqli connection is successful, then connection is now a variable that holds a persistent database connection. Making it a global variable creates a problem when debugging which is your case at this very moment.

The least advice I can probably give you is for you to modify your connection and wrap it inside a function. Making it accessable ONLY when the function is called.

Basic example

function dbConnect($db_host, $db_user, $db_pass, $db_name)
{
   $db = mysqli_connect($db_host,$db_user, $db_pass,$db_name);

    /* check if there is a connection error */
    if (mysqli_connect_errno())
      {
      /* you may want to remove the error reporting in production server */
      echo "Failed to connect to Database: " . mysqli_connect_error();
      /* in production server, you need to replace the above with */

      //return false;

      }

      return($db);

  }

and then on your add_user function, you can call the dbConnect function

function add_user()
{

    $connection = dbConnect('localhost','your_user','your_password','your_db_name');

    /* if the connection is successful, then we can use it */

    if($connection}{

        /* put all of your codes here including queries */

    }else{
            echo 'something is wrong';

   }

}

ADDED LATER: If you want to use mysQLI object, then just modfiy the above

$db = new mysqli($db_host,$db_user,$db_pass,$db_name);

another alternative method to use is the so called try, throw and catch .

changed but still not working

Unhelpful. At least post the failing query, and the new error message.

still not working
query is not working

query is not working

Last time. Post the failing query and the error message returned by mysql.

Yes, I agree with pritaes, when executing sql in phpmyadmin and when there is an error it shows the place where the error is occuring.

Example

1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '13' at line 1
if($insert_query) {
                $_SESSION["message"] = "Your user was added successfully";
                redirect_to("mng_user.php");
            } else {
                $_SESSION["message"] = "There were some errors please re-add the user";
                redirect_to("adduser.php");
            }

Okay I am also not pulling up the error the error message which I had set this is the one coming up hough

There were some errors please re-add the user

if statement is not running its jumping up to the else statement

Now it tottaly means that mysqli_query is not properly working dont know what is the error why its not woking .

Thank You

try to echo your sql statment after this

            $insert  = "INSERT INTO users (fname, lname, father_name, utype, dob, address, ph_number, email, designation, emp_stat, clock_in, shift, user_name, password)";
            $insert .= "VALUES ('$fname', '$lname', '$fathername', '$utype', '$dob', '$add', '$phno', '$email', '$desig', '$emp', '$aci', '$shift', '$uname', '$pswrd')";
            echo $insert;

and paste your output here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.