rickyj Posted August 4, 2007 Share Posted August 4, 2007 this is the shothand sqlquerry(after connecting to the database an table): $result= mysql_query("INSERT INTO sigparsed (a, b, c, d, e ) VALUES ('2', $var1, '1', $var2, '0' ) ") or die(mysql_error()); and my 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 ' But I see nothing wrong with my syntext Quote Link to comment https://forums.phpfreaks.com/topic/63296-whats-wrong-with-this-php-sql-command/ Share on other sites More sharing options...
plutomed Posted August 4, 2007 Share Posted August 4, 2007 $var1 and $var2 need to be between the ' ' Quote Link to comment https://forums.phpfreaks.com/topic/63296-whats-wrong-with-this-php-sql-command/#findComment-315453 Share on other sites More sharing options...
mrjcfreak Posted August 4, 2007 Share Posted August 4, 2007 You also might need to use the addslashes function on var1 and 2. And, if possible, it's wise to use the mysqli functions for forward compatability. Quote Link to comment https://forums.phpfreaks.com/topic/63296-whats-wrong-with-this-php-sql-command/#findComment-315454 Share on other sites More sharing options...
Barand Posted August 4, 2007 Share Posted August 4, 2007 Depends on what $var1 and $var2 contain. try <?php $sql = "INSERT INTO sigparsed (a, b, c, d, e ) VALUES ('2', $var1, '1', $var2, '0' ) "; $result = mysql_query($sql) or die (mysql_error()."<pre>$sql</pre>"); and see what is actually being sent. @plutomed - only mandatory if the vars are non-numeric Quote Link to comment https://forums.phpfreaks.com/topic/63296-whats-wrong-with-this-php-sql-command/#findComment-315457 Share on other sites More sharing options...
Zane Posted August 4, 2007 Share Posted August 4, 2007 put your query in a separate variable to make sure you variables are going through correctly....and also echo the query out just to make sure $query = 'INSERT INTO sigparsed (a, b, c, d, e ) VALUES ("2", $var1, "1", $var2, "0")'; $result= mysql_query($query) or die("Error with your SQL query: " . $query . "\n Error: " . mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/63296-whats-wrong-with-this-php-sql-command/#findComment-315458 Share on other sites More sharing options...
Barand Posted August 4, 2007 Share Posted August 4, 2007 great idea, zanus Quote Link to comment https://forums.phpfreaks.com/topic/63296-whats-wrong-with-this-php-sql-command/#findComment-315459 Share on other sites More sharing options...
rickyj Posted August 4, 2007 Author Share Posted August 4, 2007 hmm now Im getting : abDuplicate entry '119' for key 1 what does this mean, my querry is now: print 'a'; $Querry = "INSERT INTO sigparsed (a, b, c, d, e ) VALUES ('2', '$var1', '1', '$var2', '0' ) "; $result= mysql_query($Querry) or die(mysql_error()); print 'b'; $Querry2 = "INSERT INTO usertextfield (f, g) VALUES ('$var3', '$var4') "; $result2= mysql_query($Querry2) or die(mysql_error()); print 'c'; Quote Link to comment https://forums.phpfreaks.com/topic/63296-whats-wrong-with-this-php-sql-command/#findComment-315468 Share on other sites More sharing options...
rickyj Posted August 4, 2007 Author Share Posted August 4, 2007 opps i should be replacing not inserting, hmm Quote Link to comment https://forums.phpfreaks.com/topic/63296-whats-wrong-with-this-php-sql-command/#findComment-315469 Share on other sites More sharing options...
Barand Posted August 4, 2007 Share Posted August 4, 2007 Sounds like you attempted to add an already existing value into a column defined as unique Quote Link to comment https://forums.phpfreaks.com/topic/63296-whats-wrong-with-this-php-sql-command/#findComment-315473 Share on other sites More sharing options...
rickyj Posted August 4, 2007 Author Share Posted August 4, 2007 yes, I shouldnt be using insert, But I have made some progress thanks to you guys, cheers Quote Link to comment https://forums.phpfreaks.com/topic/63296-whats-wrong-with-this-php-sql-command/#findComment-315474 Share on other sites More sharing options...
rickyj Posted August 4, 2007 Author Share Posted August 4, 2007 hmm, "INSERT INTO sigparsed (a, b, c, d) VALUES ('2', '1', '$var1', '0' )WHERE userid = '$UserID' " hmm getting there, but I cant use the where statment like this can I? how can I do the above? Quote Link to comment https://forums.phpfreaks.com/topic/63296-whats-wrong-with-this-php-sql-command/#findComment-315477 Share on other sites More sharing options...
AndyB Posted August 4, 2007 Share Posted August 4, 2007 I suspect you want an UPDATE query, not INSERT UPDATE tablename SET fields/values WHERE somefield = somevalue http://www.tizag.com/mysqlTutorial/mysqlupdate.php Quote Link to comment https://forums.phpfreaks.com/topic/63296-whats-wrong-with-this-php-sql-command/#findComment-315480 Share on other sites More sharing options...
rickyj Posted August 4, 2007 Author Share Posted August 4, 2007 yup, you where exactly right Thanks Quote Link to comment https://forums.phpfreaks.com/topic/63296-whats-wrong-with-this-php-sql-command/#findComment-315559 Share on other sites More sharing options...
dbo Posted August 4, 2007 Share Posted August 4, 2007 You also might need to use the addslashes function on var1 and 2. And, if possible, it's wise to use the mysqli functions for forward compatability. Some hosts don't use both mysql and mysqli functions and just the older mysql functions. I agree with what you're saying... but what I don't understand is why people don't "wrapper" their database functions inside of a class so that it doesn't really matter if you use mysql or mysqli... you start out using mysql and then when the standard changes you change the class to mysqli and all your code works again... rather than having to hunt through code and change 1000 instances of mysql.... doing it my way you can even potentially change the underlying database... mysql->postgres...sql server... oracle, whatever. Quote Link to comment https://forums.phpfreaks.com/topic/63296-whats-wrong-with-this-php-sql-command/#findComment-315563 Share on other sites More sharing options...
mrjcfreak Posted August 4, 2007 Share Posted August 4, 2007 but what I don't understand is why people don't "wrapper" their database functions inside of a class so that it doesn't really matter if you use mysql or mysqli... Because people are lazy. Me included. Quote Link to comment https://forums.phpfreaks.com/topic/63296-whats-wrong-with-this-php-sql-command/#findComment-315571 Share on other sites More sharing options...
dbo Posted August 4, 2007 Share Posted August 4, 2007 By "being lazy" you're in essence creating more work for yourself! Quote Link to comment https://forums.phpfreaks.com/topic/63296-whats-wrong-with-this-php-sql-command/#findComment-315574 Share on other sites More sharing options...
Barand Posted August 4, 2007 Share Posted August 4, 2007 And if I now write a wrapper class I have to go back and change 1000 instances of mysql... Quote Link to comment https://forums.phpfreaks.com/topic/63296-whats-wrong-with-this-php-sql-command/#findComment-315607 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.