I'm trying to use if statement by comparing $resultsub (pass by sql) and $_id (session). I have tried to use <> and == . It seem doesn't work. I think it have some error on my if statement. Please help me to correct my php code.
This is the code.
<?php
include 'db_connect.php';
session_start();
$_id = $_SESSION['staff_id'];
if(isset($_POST['submit']))
{
$sub_code = $_POST['sub_code'];
$doc_name = $_POST['doc_name'];
$checksub = "SELECT staff_id FROM subject WHERE sub_code='$sub_code' ";
$resultsub = mysqli_query( $link,$checksub) or die("Query failed");
if ($resultsub == $_id)
{
if((IsChecked('tick','delete')) && (IsChecked('tick','add')))
{
//some other code
}
}
else
echo "you cannot upload file. Only coordinator for every subject only can upload file." ;
}
?>
It does not show any error. But it does not check my if statement and jump to else.
try starting your session just after the php open tag like following
<?php
session_start();
include 'db_connect.php';
and the second thing is that you need to fetch the required field from your variable $result_sub by using the function mysqli_fetch_array() try this code.
<?php
session_start();
include 'db_connect.php';
$_id = $_SESSION['staff_id'];
if(isset($_POST['submit']))
{
$sub_code = $_POST['sub_code'];
$doc_name = $_POST['doc_name'];
$checksub = "SELECT staff_id FROM subject WHERE sub_code='$sub_code' ";
$resultsub = mysqli_query( $link,$checksub) or die("Query failed");
$row=mysqli_fetch_array($resultsub,MYSQLI_ASSOC);
if ($row['nameOfYourIdField'] == $_id)
{
if((IsChecked('tick','delete')) && (IsChecked('tick','add')))
{
//some other code
}
}
else
echo "you cannot upload file. Only coordinator for every subject only can upload file." ;
}
?>
Related
I am trying to run Insert query on my php page but its not executing and takes me to else part.
I have checked query on MySQL its fine. Also I have couple of select queries on the same page, and those works fine. So I am sure there is no issue with the connection or accessing DB. Heres my code:
<?php
session_start();
include './db_config.php';
if ((!isset($_SESSION['first_name']) == true)) {
unset($_SESSION['first_name']);
}
$logged = $_SESSION['first_name'];
if ((!isset($_SESSION['id']) == true)) {
unset($_SESSION['id']);
}
$id = $_SESSION['id'];
if ((!isset($_SESSION['email']) == true)) {
unset($_SESSION['email']);
}
$email= $_SESSION['email'];
$query1 = "select * from user_master where id='$id'";
$result1 = mysqli_query($con, $query1);
$num1 = mysqli_num_rows($result1);
if ($num1 > 0) {
while ($data = $result1->fetch_assoc()) {
$branch_name = $data['branch_name'];
}
}
if(isset($_POST['save'])){
$cust_id=$_POST['cust_id'];
$meter_no=$_POST['meter_no'];
$lock_no=$_POST['lock_no'];
$customer_name=$_POST['customer_name'];
$customer_type=$_POST['customer_type'];
$customer_zone=$_POST['customer_zone'];
$status=$_POST['status'];
$phoneno=$_POST['phoneno'];
$city=$_POST['city'];
$address=$_POST['address'];
$houseno=$_POST['houseno'];
$ownership=$_POST['ownership'];
$landmark=$_POST['landmark'];
$opening_reading=$_POST['opening_reading'];
$opening_reading_date=$_POST['opening_reading_date'];
$branch_name=$_POST['branch_name'];
$created_on=$_POST['created_on'];
$created_by=$_POST['created_by'];
$email=$_POST['email'];
$consumption=0;
$current_reading=$opening_reading;
$status1="True";
$total_bill=0;
$total_paid=0;
$total_dues=0;
$select="insert into meter(cust_id,meter_no,lock_no,customer_name,customer_type,customer_zone,status,phoneno,city,address,houseno,ownership,landmark,opening_reading,opening_reading_date,created_on,branch_name,created_by,email)
VALUES('$cust_id','$meter_no','$lock_no','$customer_name','$customer_type','$customer_zone','$status','$phoneno','$city','$address','$houseno','$ownership','$landmark','$opening_reading','$opening_reading_date','$created_on','$branch_name','$created_by','$email')";
if ($r = mysqli_query($con, $select)) {
else {
echo '<script language="javascript">';
echo 'alert("Information Not Inserted!!!");';
//echo 'window.location.href="bill_generation.php";';
echo '</script>';
}
}
I have Checked on Godaddy HERE that one need just Localhost as host name.
I know I have pasted a long piece of code, however dont know where the issue is.
You have a typo at the end of your code.
if ($r = mysqli_query($con, $select)) {
echo 'data inserted successfully !';
// do something ?
} // this one is missing
else {
echo '<script language="javascript">';
echo 'alert("Information Not Inserted!!!");';
//echo 'window.location.href="bill_generation.php";';
echo '</script>';
}
This sql function works on all my scripts except this one. Does anyone see what's wrong with it? The part that isn't working... is the part where it's supposed to insert the variable into a table. The include is for logging in the database, and that's all correct(I double checked).
<?php
session_start();
include_once 'dbh.php';
$confirm = $_POST['confirm'];
$check = $_SESSION['forum_name'];
if ($confirm == $check) {
include_once 'dbh.php';
$sql = "INSERT INTO forum_names (name) VALUES ('$forum_name');";
$result = mysqli_query($conn, $sql);
header("Location: ../redir.php?postsuccess=success");
} else {
echo "Your names do not match" . " ";
echo "<a href='../redir.php'>Click here to try again</a>";
}
?>
$forum_name doesn't exist. Therefore, either replace it with $check, or replace $check with $forum_name.
I have zero idea how this could possibly work on other pages using that code.
I've searched thoroughly and nothing seems to be working; I have this code here which posts into my database but the problem is I am trying to run a conditional which checks if a row exists using the mysqli_num_rows function, but it is not actually working. I have tried many different versions and other functions as well such as mysqli_fetch_row, but nothing seems to work. Here is my code:
if (!empty($_POST)) {
$db_conx="";
$name = $_POST['name'];
$module = $_POST['module'];
$secret = $_POST['secret'];
$uid1 = $dmt->user['uid'];
$queryA = "INSERT INTO table_a (uid1,name,module,secret) VALUES ('$uid1','$name','$module','$secret')";
$resultA = mysqli_query($db_conx,$queryA);
$queryB = "SELECT 1 FROM table_a WHERE name='$name' LIMIT 1";
$resultB = mysqli_query($db_conx,$queryB);
$resultC = mysqli_query($db_conx,$queryB);
$query = mysqli_query($db_conx,"SELECT * FROM table_a WHERE name='$name'");
if (empty($name)||empty($module)||empty($secret)) {
echo "Oops! Can't leave any field blank <br />";
exit();
} elseif(mysqli_num_rows($query) > 0){
echo "name already exists.";
exit();
} elseif ($db_conx->query($queryA) === TRUE) {
echo "New record created successfully.";
exit();
} else {
echo "Error: " . $queryA . "<br>" . $db_conx->error;
exit();
}
}
As you can see the query appears to run but indeed does not do what it's told.
The first line of code inside your IF is destroying the variable you are using to hold the database connection
if (!empty($_POST)) {
$db_conx=""; // get rid of this line
So basically nothing using the mysqli API will work.
ALSO:
Add these as the first 2 lines of a script you are trying to debug
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
as you are obviously not readng your php error log
Here is the code which I am trying to block a user from my website. I am using MySQL database and I insert a Banned column in my user table and set their value 1 by default so tell me what I have to do. Table name of this section in MySQL database is Lectures So please help me as soon as possible.
<?php
session_start();
include("header.php");
include("conection.php");
//echo "lec name".$_SESSION["lecname"];
?>
<?php
if($banned == "1")
{
header("Location:lectureaccount.php");
}
else
{
header("Location:banned.php");
}?>
<?php
//if(isset($_SESSION["userid"]))
{
$lec_id = $_SESSION["userid"];
?>
you are doing good.
I am assuming you need to know what your SQL should be to get user data from the database.
<?php
session_start();
include("header.php");
include("conection.php");
//echo "lec name".$_SESSION["lecname"];
if(isset($_SESSION["userid"]))
{
$user_id = $_SESSION["userid"];
$con=mysqli_connect("example.com","username","password","database_name");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT banned FROM Lectures where user_id = ".$user_id);
while($row = mysqli_fetch_array($result)){
$banned= $row['banned'];
}
mysqli_close($con);
if($banned == "1"){
header("Location:lectureaccount.php");
}
else{
header("Location:banned.php");
}
}
?>
<?php
//if(isset($_SESSION["userid"]))
{
$lec_id = $_SESSION["userid"];
All this section is supposed to do is collect an int from active and use it in the if statement to continue with the code. Can somebody please tell me why this is not working?
$act_qry = "Select active FROM user_m WHERE username = '$username' and password = '$Menrypted_password'";
$result_act = mysqli_query ( $connMS, $act_qry );
$value_act = mysqli_fetch_object($result_act);
if($value_act == 1)
{
//Do php stuff.
}
I am having this table in my database:-http://prntscr.com/9tnalx
Check this code:-
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
//connect to database
$link = mysqli_connect('localhost','root','','stack');
$act_qry = "Select product_id FROM bom WHERE bom_description = 'Table Tops'";
$result_act = mysqli_query ( $link, $act_qry ) or die(mysqli_error($link));
$value_act = mysqli_fetch_object($result_act);
if($value_act->product_id == 1)
{
echo $value_act->product_id;
}
// or you can do this
$value_act = mysqli_fetch_assoc($result_act);
if($value_act['product_id'] == 1)
{
echo $value_act->product_id;
}
mysqli_close($link);
?>
Output on my browser:- http://prntscr.com/9tnazj
Note:- I hope you can understand the code by checking my screenshot of table.Thanks