Codeigniter setting new value to db - php

I am working on Basketball Referee project. I am trying to update table checking input value.
I am sending the value from my control.php like this:
$check_user = $this->input->post("headreferee");
$check = $this->sql_model->check1($check_user);
Here is my sql_model.php:
function check1($referee_name)
{
$sql = "SELECT * FROM duties WHERE username ='{$referee_name}' ";
$query = $this->db->query($sql);
if($query->num_rows()>0)
{
$this->db->set('count','count'+1);
$this->db->insert('duties');
}
else
{
return 0;
}
}
Actually it is increasing count and adding new row but without any referee_name. It has to find the correct referee_name and increase that row's count.

If you want to update so please modify your check1() function by below script.
function check1($referee_name)
{
$sql = "SELECT * FROM duties WHERE username ='{$referee_name}' ";
$query = $this->db->query($sql);
$result_referee = $query->row();
$countNew = $result_referee->count + 1;
if($query->num_rows()>0)
{
$countArray = array('count'=>$countNew);
$this->db->where('username',$referee_name);
$this->db->update('duties',$countArray);
}
else
{
return 0;
}
}
I hope that will be helpful for you as your solution.

Related

Checking the SUM of specific column with condition before inserting data

I need help! I just want to insert data from user but before that it will check first if the 'dami' column in database is <=100, if yes then it will insert the data entered by the user if no then it will prompt the user.
But with my code it keeps inserting regardless of the 'dami' sum. Badly need it! Here's my code!
public function do_create_businesscard($txttelno,$txttelno1,$txtfaxno,$txtmobileno,$txtmobileno1,$txtemail,$txtemail1,$txtPieces,$is_deleted)
{
$sql = "SELECT (SELECT SUM(dami)
FROM tbl_business_card_information
WHERE is_deleted = 1)
AS 'Total'";
if ('Total' >= 100){
echo "Sorry but you already reach you maximum request";
}else{
$sql = "INSERT INTO tbl_business_card_information (`tel_no`,`tel_no1`,`fax_no`,`mobile_no`,`mobile_no1`,`email`,`email1`,`dami`) VALUES (?,?,?,?,?,?,?,?)";
$data = array($txttelno,$txttelno1,$txtfaxno,$txtmobileno,$txtmobileno1,$txtemail,$txtemail1,$txtPieces);
$query = $this->db->query($sql,$data);
return $query;
}
}
Thank You !
Your usage of SELECT is wrong. Here is the way.
public function do_create_businesscard($txttelno,$txttelno1,$txtfaxno,$txtmobileno,$txtmobileno1,$txtemail,$txtemail1,$txtPieces,$is_deleted)
{
$sql = "SELECT SUM(dami) FROM tbl_business_card_information WHERE is_deleted = 1";
$result = $this->db->query($sql);
$row = $result->fetch_array(MYSQLI_NUM);
$total = $row[0];
if ($total > 100){
echo "Sorry but you already reach you maximum request";
}else{
$sql = "INSERT INTO tbl_business_card_information (`tel_no`,`tel_no1`,`fax_no`,`mobile_no`,`mobile_no1`,`email`,`email1`,`dami`) VALUES (?,?,?,?,?,?,?,?)";
$data = array($txttelno,$txttelno1,$txtfaxno,$txtmobileno,$txtmobileno1,$txtemail,$txtemail1,$txtPieces);
$query = $this->db->query($sql,$data);
return $query;
}
}

After fetching a value from DB then assign it to a variable, then i can't use it to the next query

This is my model:
public function GetStudentData()
{
$sID = $this->input->get('id');
$class_id = $this->db->query("SELECT student.class_id FROM student WHERE student.sID = '$sID'");
$query = $this->db->query("SELECT * FROM student WHERE(sID='$id'AND class_id = '$class_id')");
if($query->num_rows() > 0)
{
return $query->row();
}else{
return false;
}
}
How can I use $class_id in the next query?
you could do it in one query... (from your example [SIC])
public function GetCSData() {
$id = $this->input->get('id');
$query = $this->db->query("SELECT student.*
FROM student
WHERE student.sID = '$id'
AND student.class_id = (SELECT class_id
FROM student
WHERE student.sID = '$id')");
if($query->num_rows() > 0){
return $query->row();
}else{
return false;
}
}//..GetCSData end
looking at it though, that will give the same result as
SELECT *
FROM student
WHERE sID = '$id'
maybe I am missing a point?

Why when I return a false value from a function the parent function gets it as true in php?

I am new with PHP and maybe it is a stupid question but when I use a function to check if a record exists in my database using the following:
function isGameExisted($con,$name)
{
$query = "SELECT * from Games WHERE NAME ='$name' LIMIT 1";
$check = mysqli_query($con,$query);
// var_dump($check);
if(mysqli_num_rows($check)){
//echo " EXISTS ";
return true;
}else
{
//echo " OFFER DOES NOT EXIST ";
return false;
}
}
So far so good it returns false when name does not exist
but in parent function
$isExisting = isGameExisted($con,$name);
if($isExisting)
{
$json['Result'] = "Fail";
$json['message'] = "Error in registering. Probably the name already exists";
}else
{
}
I am getting true! the message Error in Registering... what I am missing here
thank you
The problem is that isset($check) is always true. If the query finds a match row, $check will be set to an array. If the query doesn't find anything, $check will be set to NULL.
You should just do:
return $check != false;
The whole function should be:
function isGameExisted($con,$name)
{
$query = "SELECT 1 from Games WHERE NAME ='$name' LIMIT 1";
$check = mysqli_fetch_array(mysqli_query($con,$query));
return $check != null;
}
Please try with this
function isGameExisted($con,$name)
{
$query = "SELECT * from Games WHERE NAME ='$name' LIMIT 1";
$excQuery = mysqli_query($con,$query);
if(mysqli_num_rows($excQuery)){
//echo " EXISTS ";
return true;
}else
{
//echo " GAME DOES NOT EXIST ";
return false;
}
}

Check empty rows query

In fact I am working on a small PHP script but I am facing a problem right now.The problem is that i want to check if my query return records this is my mysqli query:
$sql = "select * from specs where btitleid=$id and phoneid=$aydi"
$check = $conn->query($sql)
while($row = $check->fetch_assoc()) {$tocheck = $row['content'];}
I don't want to check the number of rows of this query to see if it is null.I want to check if all $row['content'] are empty.
How about this:
$sql = "select * from specs where btitleid=$id and phoneid=$aydi";
$check = $conn->query($sql);
$contentAllEmpty = true;
while ($row = $check->fetch_assoc()) {
if (strlen($row['content']) > 0) {
$contentAllEmpty = false;
}
}
if ($contentAllEmpty) {
//do something
}
use ==
while ($row = $check->fetch_assoc()) {
if ($row['content'] == '') {
... code here
}
}
To get back only records where the content column is not empty -
$sql = "SELECT * FROM `specs` WHERE `btitleid` = $id AND `phoneid` = $aydi AND (`content` IS NOT NULL OR `content` != '') ";

mysql_query() failing very oddly

This is a really simple thing, but it's not working for some reason. Heres my code.
I am making function (its part of a class) which checks if a username or email exists:
public function exists ($what, $who)
{
$sql = "SELECT * FROM users WHERE $what = $who";
$query = mysql_query($sql);
if (mysql_num_rows($query) != 0)
{
return true;
}
else
{
return false;
}
}
The function returns nothing. In fact if I run that query through regular PHP it returns nothing also. I don't understand why.
This following piece of code returns news entries perfectly:
function fetch($id = '')
{
if (empty($id))
{
$query = 'SELECT * FROM news ORDER BY id desc';
}
elseif (is_numeric($id))
{
$query = "SELECT * FROM news WHERE id = $id";
}
else
{
$route->to(SITE_URL);
}
$result = mysql_query($query);
if (mysql_num_rows($result) > 0)
{
return $result;
}
}
I am confused.
The problem is that you are missing quotes in your query:
$sql = "SELECT * FROM users WHERE $what = $who";
//SELECT * FROM users WHERE username = Mario is not a valid query
should be:
$sql = "SELECT * FROM users WHERE $what = '$who'";
the other queries are working because you are checking against an id, in this case against a string (and in this case you need quotes)
maybe the query execution failed and you have error turned off on screen in your php.ini
Try to add an intermediate check on the correct execution of the query:
$query = mysql_query($sql);
if ($query === FALSE) {
// log error with mysql_errno($conn) and mysql_error($conn);
} else {
if (mysql_num_rows($query) != 0) {
return true;
etc. etc.

Categories