Hi, my problem is that I can't insert the value of $branch to my database, because it returns a null value.
I'm still new to PHP and only started using it a couple of days ago, I still don't get much of the functions.

Here's my whole code:

<?php
session_start();
$user = $_SESSION["login_User"];


$result = mysql_query("SELECT * FROM tblaccount WHERE Username='$user'");
        while($row = mysql_fetch_assoc($result))
        {

    //$encodr=$row['FirstName'] . " " . $row['LastName'];
        $branch=$row['Branch']; 
        }


    include "configdb.php";
    $prodCode=$_REQUEST['txtProdCode'];
    $brand=$_REQUEST['txtBrand'];
    $prodNa=$_REQUEST['txtProdNa'];
    $cat=$_REQUEST['optCat'];
    $qty=$_REQUEST['txtQty'];
    $unit=$_REQUEST['optUnit'];
    $price=$_REQUEST['txtPrice'];
    $optSupp=$_REQUEST['optSupp'];
    $merchant=$_REQUEST['txtMerchant'];
    $img=$_REQUEST['imglocation'];
    $dat=date("F d, Y");
    $b = time (); 
    $tim= date("g:i:s A", $b);




    $result = mysql_query("SELECT * FROM tblproducts WHERE ProdCode='$prodCode' AND Branch='$branch'");
        $count=mysql_num_rows($result);

        if($count > 0)
        { header('Location: prodAddErr.php'); 
          $msgs = "There is already an existing record!"; 
        }
        else
        {   
            $query="INSERT INTO tblproducts(ProdCode,Brand,ProductName,Category,Quantity,Unit,
            Price,Branch,Merchant,Supplier,DateStored,TimeStored,Encoder,picture) 
            VALUES('$prodCode','$brand','$prodNa','$cat','$qty','$unit','$price','$branch','$merchant',
            '$optSupp','$dat','$tim','$user','$img')";

            $result = mysql_query($query) or die ("Cannot Execute Query!");
            header('Location: prodAdded.php');
        }

    ?>

and why is it that when I do this code:

<?php
session_start();
$user = $_SESSION["login_User"];

$connect = mysql_connect("localhost","root","") or die("Couldn't connect to database!");
    mysql_select_db("inventory") or die("Couldn't find database!!");
        $result = mysql_query("SELECT * FROM tblaccount WHERE Username='$user'");
        while($row = mysql_fetch_assoc($result))
    {
        echo $row['FirstName'];
        print " ";
        echo $row['LastName'];
        print "<br/> ";
        echo $row['Branch'];
    }
            ?>

I get the desired output i want. It really prints the record under those fieldnames, so why is it that it doesn't work when I pass the value to another variable?

thanks in advance for helping me :)

Recommended Answers

All 3 Replies

In your first code snippet there's no DB connection before line 6, where you run your first query.

omg! -_- so simple yet I overlook it. wew, thank you so much!

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.