Jump to content

problems...


ccrevcypsys

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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());	}		

?>

Link to comment
Share on other sites

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());
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.