Kenneth .J
Kenneth .J

Reputation: 1433

Selecting and using primary key in a query

I have a table

----------------------------------------------------------
| id | Bizid |    Email     | Username | Password | Salt |
 ---------------------------------------------------------
| 6  |   5   |[email protected] | TestUser | 21412bjkb| 4151 |
----------------------------------------------------------

where Bizid references the primary key of another table.

I am currently trying to use the primary key of this table (id) in a query

<?PHP
if(@$_POST['addcat']=="Submit")
{
include("cxn.inc");
$userid=$_SESSION['UserId'];
$cat=$_POST['category'];
$branch="SELECT id WHERE Bizid=$userid";<--This query
$getbranch=mysqli_query($cxn,$branch) or die(mysqli_error($cxn));<--This query
$addcat="INSERT INTO categories (Business,Branch,Category) VALUES($userid,$getbranch,$cat)";
$runcat=mysqli_query($cxn,$addcat) or die (mysqli_error($cxn));
$success="Category added successfully";
}
?>

but i am getting the error

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 'WHERE Bizid=5' at line 1

I cant seem to see what exactly is wrong with my code above, unless i am unable to use primary keys in queries like what i am doing above.

If so, could anyone kindly point me in the right direction to go, of suggest a workaround?

Thanks!

PS:i'm new to coding, and am trying to pick things up as i go.

Upvotes: 1

Views: 87

Answers (4)

Arun Kumar
Arun Kumar

Reputation: 1667

$branch = "SELECT id FROM table_name WHERE Bizid=$userid";

Upvotes: 2

RbG
RbG

Reputation: 3193

syntax:..you didnt specify from which table you are fetching your data

SELECT [cloumn1,column2,....] FROM table_name WHERE [condition];

Upvotes: 0

Goutam Pal
Goutam Pal

Reputation: 1763

$branch="SELECT id WHERE Bizid=$userid";<--This query

should be

$branch="SELECT id FROM TABLE_NAME WHERE Bizid=$userid";

Upvotes: 0

Fahim Parkar
Fahim Parkar

Reputation: 31637

You didn't specified FROM tableName.

$branch="SELECT id FROM tableName WHERE Bizid=$userid";
                   ^^^^^^^^^^^^^^

Upvotes: 2

Related Questions