• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
having issues with Error Number: 1064

#1
any time i try to access a page i created it says:Error Number: 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 '' at line 3

SELECT * FROM (`_db_tb_article`) WHERE `article_id` =

Filename: C:\xampp\htdocs\gtech\__ghtec__$y$tem__core\database\DB_driver.php

Line Number: 330
this is my model
<?php

class article_model extends CI_Model {


function add()
{
$this->db->insert('article',$_POST);
if($this->db->_error_number())
{
return $this->db->_error_number();
}
}

function update($article_id, $data_fields = NULL){
if($data_fields == NULL)
{
$this->db->where("article_id =".$article_id);
$this->db->update('article',$_POST);
}
else
{
$this->db->where("article_id =".$article_id);
$this->db->update('article',$data_fields);
}

$is_error = $this->db->_error_number();
if($is_error){
echo $is_error;
}

return TRUE;
}

function delete($id){
$this->db->where("article_id =".$id);
return $this->db->delete('article');
}

//return the article with this id
function get_article($id){
$this->db->where("article_id =".$id);
$query = $this->db->get('article');
if ($query->num_rows() > 0){
return $query->row_array();
}
else
echo $this->db->_error_message();
return FALSE;
}

//return the available article in the table
function get_article_all(){

$query = $this->db->get('article');
if ($query->num_rows() > 0)
{
foreach($query->result() as $row)
{
$result[] = $row;
}
return $result;
}
}





}

i have checked the line 330 and can't seem to find a way to solve that problem....i need help
Reply

#2
Hey xiaolee, welcome to the forum.

Are you sure you are calling get_article with an id like get_article(22)? I tried to sabotage a query of mine by leaving the id empty and I got the very same SQL error.

Maybe you can add a temporarily var_dump() to your function like so:

PHP Code:
function get_article($id){
var_dump($id);
$this->db->where("article_id =".$id);
... 


This will display the contents of $id. If there is no contents, your query will miss the id of the article you're trying to load which tells you your call to get_article is wrong.

Hope this helps!

-Roger
Reply

#3
(06-10-2015, 08:28 AM)RogerMore Wrote: Hey xiaolee, welcome to the forum.

Are you sure you are calling get_article with an id like get_article(22)? I tried to sabotage a query of mine by leaving the id empty and I got the very same SQL error.

Maybe you can add a temporarily var_dump() to your function like so:


PHP Code:
function get_article($id){
var_dump($id);
$this->db->where("article_id =".$id);
... 


This will display the contents of $id. If there is no contents, your query will miss the id of the article you're trying to load which tells you your call to get_article is wrong.

Hope this helps!

-Roger
thanks man, i did as you said, it did not throw that error again, instead "null" showed on the page. 
Again, Thanks
-Xiao Lee
Reply

#4
As @RogerMore mentioned, you need to check your inputs. There may be a few other issues in there, too.


PHP Code:
<?php

class article_model extends CI_Model 
{
 
   public function add()
 
   {
 
       // Note: $_POST should be filtered, 
 
       // and not accessed directly by the model
 
       $result $this->db->insert('article'$_POST);
 
       if (! $result) {
 
           log_message('error'$this->getDbErrorMessage());
 
       }

 
       return $result;
 
   }

 
   public function update($article_id$data_fields NULL)
 
   {
 
       // You may want to perform additional validation on $article_id
 
       if (empty($article_id)) {
 
           log_message('error''Invalid article ID supplied to update');
 
           return false;
 
       }

 
       if ($data_fields == NULL) {
 
           // Note: $_POST should be filtered, 
 
           // and not accessed directly by the model
 
           $data_fields $_POST;
 
       }

 
       $this->db->where("article_id"$article_id);
 
       $result $this->db->update('article'$data_fields);

 
       if (! $result) {
 
           log_message('error'$this->getDbErrorMessage());
 
       }

 
       return $result;
 
   }

 
   public function delete($id)
 
   {
 
       // You may want to perform additional validation on $id
 
       if (empty($id)) {
 
           log_message('error''Invalid article ID supplied to delete');
 
           return false;
 
       }

 
       $this->db->where("article_id"$id);
 
       return $this->db->delete('article');
 
   }

 
   //return the article with this id
 
   public function get_article($id)
 
   {
 
       // You may want to perform additional validation on $id
 
       if (empty($id)) {
 
           log_message('error''Invalid article ID supplied to get_article');
 
           return false;
 
       }

 
       $this->db->where("article_id"$id);
 
       $query $this->db->get('article');
 
       if ($query->num_rows() > 0){
 
          return $query->row_array();
 
       }

 
       log_message('error'$this->getDbErrorMessage());
 
       return FALSE;
 
   }

 
   //return the available article in the table
 
   public function get_article_all()
 
   {
 
       $query $this->db->get('article');
 
       if ($query->num_rows() > 0) {
 
           return $query->result();
 
       }

 
       return null;
 
   }

 
   // This should probably be in MY_Model (extends CI_Model), 
 
   // which article_model would extend in place of CI_Model.
 
   protected function getDbErrorMessage()
 
   {
 
       if (substr(CI_VERSION01) != '2') {
 
           $error $this->db->error();
 
           return isset($error['message']) ? $error['message'] : '';
 
       }

 
       return $this->db->_error_message();
 
   }

Reply

#5
thanks, but now i have another problem, anytime i go to the view and click the link I pathed to the add function it just refreshes the page... i made the link something like... <div style="float:right"><a href="javascript:load_content('index.php/article/add')"> Add</a></div> what should I do to solve that....
Reply

#6
At best, that link is just going to do whatever your load_content() function tells it to do in your JavaScript.

You would be better off creating a form with
Code:
<?php form_open(site_url('article/add')); ?>
(or the equivalent form element with method='post') and a submit button in the form. If you want to send the post via AJAX, you can override the submit button to do so in the script after you get the form working.

It's always best to take these things one step at a time. If the form works, you'll know that your HTML/view, controller, and model are working. Once that's done, you add JavaScript to change the button/form behavior to do the same thing via AJAX, and you shouldn't have to troubleshoot anything other than the script (as long as you leave everything else alone). Further, you gain the benefit of the form continuing to work if someone disables the script.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.