ccrevcypsys Posted December 17, 2007 Share Posted December 17, 2007 I am haveing a problem updating things with text that are in my database. When ever i try to update or insert something it says Unknown column 'test' in 'field list' and my code is this (for testing purposes) $query = "UPDATE courses SET `FirstName`=test WHERE id = 1201"; $result = @mysql_query($query); if(mysql_affected_rows()==1){ echo '<h1> THANK YOU </h1>'; }else{ echo mysql_error(); exit(); } but when i try a number it will work. But only when i try a number all text even 1 letter it wont work. Quote Link to comment Share on other sites More sharing options...
themistral Posted December 17, 2007 Share Posted December 17, 2007 Put test in quote marks. $query = "UPDATE courses SET `FirstName`= "test" WHERE id = 1201"; HTH! Quote Link to comment Share on other sites More sharing options...
rlindauer Posted December 17, 2007 Share Posted December 17, 2007 $query = "UPDATE courses SET `FirstName`= 'test' WHERE id = 1201"; Strings should be in quotes. Quote Link to comment Share on other sites More sharing options...
trq Posted December 17, 2007 Share Posted December 17, 2007 That would be... $query = "UPDATE courses SET `FirstName`= 'test' WHERE id = 1201"; You should also use.... $result = mysql_query($query) || die(mysql_error()); Quote Link to comment Share on other sites More sharing options...
PHPNewbie55 Posted December 17, 2007 Share Posted December 17, 2007 Try this....... <?php $query = "UPDATE courses SET FirstName = 'test' WHERE id = 1201"; ?> I am a noob so that may not be what is wrong... Quote Link to comment Share on other sites More sharing options...
ccrevcypsys Posted December 17, 2007 Author Share Posted December 17, 2007 wow im dumb lol i have been trying to do that all day and now it works thanks as always guys! Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted December 17, 2007 Share Posted December 17, 2007 test may be a reserved keyword for MySQL. To avoid ever running into such a conflict, always use back ticks around ALL field and table names, and single quotes around values. Try this: $query = "UPDATE `courses` SET `FirstName`= 'test' WHERE id = '1201'"; ps - question already answered, but posting anyways to explain why and how to avoid in the future. Please also marked 'Topic Solved' when this thread is resolved. PhREEEk Quote Link to comment Share on other sites More sharing options...
ccrevcypsys Posted December 17, 2007 Author Share Posted December 17, 2007 Now i am haveing problems with inserting into the db. Does this look right ? <?php if($_GET['n']=="t"){ $UpNew = "INSERT INTO"; }elseif(!$_GET['n']){ $UpNew = "UPDATE"; } $query = "".$UpNew." courses SET `FirstName`='".$_POST['FirstName']."',`LastName`='".$_POST['LastName']."',`EventName`='".$_POST['EventName']."',`EventDescription`='".$_POST['EventDescription']."',`EventDate`='".$_POST['year']."-".$_POST['month']."-".$_POST['day']."',`EventHours`='".$_POST['EventHours']."',`RegNo`='".$_POST['RegNo']."',`SubHead`='".$_POST['SubHead']."' WHERE id = ".$_GET['e']; $result = mysql_query($query) || die(mysql_error()); } ?> Quote Link to comment Share on other sites More sharing options...
rlindauer Posted December 17, 2007 Share Posted December 17, 2007 The proper syntax is "INSERT INTO tablename..." Quote Link to comment Share on other sites More sharing options...
ccrevcypsys Posted December 17, 2007 Author Share Posted December 17, 2007 ok i did that but it still isnt working Quote Link to comment Share on other sites More sharing options...
rlindauer Posted December 17, 2007 Share Posted December 17, 2007 What error are you getting? I see some syntax errors. You can't use a WHERE clause when you inserting data. INSERT creates a new record, so the WHERE clause is totally unnecessary. Try something like this instead. <?php if ($_GET['n']=="t") { $sql = "INSERT INTO courses SET `FirstName`='".$_POST['FirstName']."',`LastName`='".$_POST['LastName']."',`EventName`='".$_POST['EventName']."',`EventDescription`='".$_POST['EventDescription']."',`EventDate`='".$_POST['year']."-".$_POST['month']."-".$_POST['day']."',`EventHours`='".$_POST['EventHours']."',`RegNo`='".$_POST['RegNo']."',`SubHead`='".$_POST['SubHead']."'"; } else { $sql = "UPDATE courses SET `FirstName`='".$_POST['FirstName']."',`LastName`='".$_POST['LastName']."',`EventName`='".$_POST['EventName']."',`EventDescription`='".$_POST['EventDescription']."',`EventDate`='".$_POST['year']."-".$_POST['month']."-".$_POST['day']."',`EventHours`='".$_POST['EventHours']."',`RegNo`='".$_POST['RegNo']."',`SubHead`='".$_POST['SubHead']."' WHERE id = ".$_GET['e']; } $result = mysql_query($query) || die(mysql_error()); ?> Quote Link to comment 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.