It is currently Sun May 19, 2013 1:02 am

All times are UTC + 2 hours [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Problem inserting record into my table
PostPosted: Sat Mar 10, 2012 8:46 am 
Offline

Joined: Sat Mar 10, 2012 8:32 am
Posts: 2
I get a very strange error when i try to insert record into my database. Strange because I dont see anything wrong with my code syntax.

Quote:
INSERT 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 '(usrip, user, status) VALUES ('', '', '')' at line 1


Code:
$ipaddr=$_SERVER['REMOTE_ADDR'];
$mycnct=mysql_connect("abc","xyz","pass");
    if($mycnct){
        if(mysql_select_db("mydb",$mycnct)){
        $grpid=$_GET['gid'];
        $thisuser=$_GET['usersid'];
        $mylockr=$_GET['accessid'];
        $grpmode=$_GET['gmode'];   
        $grplock=$_GET['islock'];
        $isonline="online";
        $groupFullName="GRP_".$grpid."_users";
      if(mysql_query("INSERT INTO $groupFullName (usrip, user, status) VALUES ('$ipaddr', '$thisuser', '$isonline')")){
                            echo "Success";
                        }else{                           
                            echo "INSERT ERROR:".mysql_error();
                        }
      }
   }
/*Link with variables:
mysite.com/testing.php?gid=bluebell&usersid=henry&gmode=join&islock=off&accessid=abc
*/


Before running the above code, this is how the table was created:
Code:
if(mysql_query("CREATE TABLE $groupFullName(SL int(11) NOT NULL AUTO_INCREMENT, usrip text NOT NULL, user varbinary(35) NOT NULL, status varchar(15) NOT NULL, getreq varchar(15) NOT NULL, PRIMARY KEY (SL))")){
echo "DONE";
}else{
echo "Fail";
}


Top
 Profile  
 
 Post subject: Re: Problem inserting record into my table
PostPosted: Thu Mar 15, 2012 8:13 pm 
Offline

Joined: Sun May 02, 2004 11:34 pm
Posts: 6579
Location: toronto, canada
echo out the whole query and post that

Code:
$query ="INSERT INTO $groupFullName (usrip, user, status) VALUES ('$ipaddr', '$thisuser', '$isonline')";
if(mysql_query($query)){
                            echo "Success";
                        }else{                           
                            echo "INSERT ERROR:".mysql_error()." $query ";
                        }
      }
   }

_________________
Lostboy

Cat, the other other white meat

Please read Posting Etiquette before posting

You can always try Google


Top
 Profile  
 
 Post subject: Re: Problem inserting record into my table
PostPosted: Fri Mar 16, 2012 4:27 pm 
Offline

Joined: Sat Mar 10, 2012 8:32 am
Posts: 2
I tried out something similar to what you had suggested to see whats happening with my query variables...

Code:
if(mysql_query("INSERT INTO $groupFullName (usrip, user, status) VALUES ('$ipaddr', '$thisuser', '$isonline')")){
                            echo "Success";
          }else{                           
                            echo "INSERT ERROR:".mysql_error();
          }
echo "<br/><br/>INSERT INTO $groupFullName (usrip, user, status) VALUES ('$ipaddr', '$thisuser', '$isonline')";


And I got this....
Quote:
INSERT 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 '(usrip, user, status) VALUES ('', '', '')' at line 1

INSERT INTO (usrip, user, status) VALUES ('', '', '')


So why am I getting empty variables in my output when I had passed this in my URL:
Quote:
mysite.com/testing.php?gid=bluebell&usersid=henry&gmode=join&islock=off&accessid=abc


---------------------------------------------------------------------

And later I tried this....
Code:
$copystr="GRP_".$groupFullName."_users";
if(mysql_query("INSERT INTO $copystr (usrip, user, status) VALUES ('$ipaddr', '$thisuser', '$isonline')")){
                             echo "Success";
          }else{                             
                            echo "INSERT ERROR:".mysql_error();
          }
echo "<br/><br/>INSERT INTO $copystr (usrip, user, status) VALUES ('$ipaddr', '$thisuser', '$isonline')"


..and this is the output I got...
Quote:
INSERT ERROR:Table 'b20_mydb_chatbox.GRP__users' doesn't exist

INSERT INTO GRP__users (usrip, user, status) VALUES ('', '', '')



Now I tried replacing $groupFullName by $_GET['gid']
Code:
$copystr="GRP_".$_GET['gid']."_users";
if(mysql_query("INSERT INTO $copystr (usrip, user, status) VALUES ('$ipaddr', '$thisuser', '$isonline')")){
                             echo "Success";
          }else{                             
                            echo "INSERT ERROR:".mysql_error();
          }
echo "<br/><br/><br/><br/>INSERT INTO $copystr (usrip, user, status) VALUES ('$ipaddr', '$thisuser', '$isonline')"


Here is the output:
Quote:
INSERT INTO GRP_bluebell_users (usrip, user, status) VALUES ('', '', '')


It works now. I checked the table(GRP_bluebell_users), and found that a record has been inserted into this table with empty datas under columns (usrip, user, status). So again had to create new variable copies of $ipaddr, $thisuser, $isonline just like I did with table name.

I want to know why this is happening? Why cant I simply use the same variables instead of a creating a new one?


Top
 Profile  
 
 Post subject: Re: Problem inserting record into my table
PostPosted: Fri Mar 16, 2012 5:14 pm 
Offline

Joined: Sun May 02, 2004 11:34 pm
Posts: 6579
Location: toronto, canada
You seem to have a problem selecting the db...that is where i noticed the problem

But I moved your code around to this:
Code:

   $ipaddr=$_SERVER['REMOTE_ADDR'];
   $grpid=$_GET['gid'];
   $thisuser=$_GET['usersid'];
   $mylockr=$_GET['accessid'];
   $grpmode=$_GET['gmode'];   
   $grplock=$_GET['islock'];
   $isonline="online";
   $groupFullName="GRP_".$grpid."_users";

   $mycnct=mysql_connect("localhost","root","*******");
    if($mycnct){

   $conn =  mysql_select_db("mydb",$mycnct);

       if($conn){
         $query = "INSERT INTO $groupFullName (usrip, user, status) VALUES ('$ipaddr', '$thisuser', '$isonline')";         
         if(mysql_query($query)){
            echo "Success";
         }else{                           
            echo "INSERT ERROR:".mysql_error(). $query;
         }
      }
   }


and it seems to work now

_________________
Lostboy

Cat, the other other white meat

Please read Posting Etiquette before posting

You can always try Google


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 2 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron