Page 1 of 1

Problem with my mysql adding form

Posted: Thu Nov 25, 2004 3:11 pm
by kzar
Im getting the odd error "Unknown column 'test' in 'field list'" when i try to use it. (thats when i put test in) . I cant really see the problem with it all but im really rusty with php so ive probably done somthing stupid.

Heres my code

Function for database queries

Code: Select all

<?php
	function db_query ($result_wanted, $sql)
	{
		$dbhost = "localhost";
		$dbuser = "censored";
		$dbpass = "censored";
		$dbname = "censored";

		$conn = mysql_connect($dbhost,$dbuser,$dbpass)
		or die('counter CONNECT error: '.mysql_errno().', '.mysql_error());
		mysql_select_db($dbname,$conn) or die( "Unable to select database");

		$result = mysql_query($sql, $conn);

		if (!$result)
		{
			echo 'Could not run query: ' . mysql_error();
			exit;
		}

		mysql_close($conn);

		if ($result_wanted == true)
		{
			return $result;
			mysql_free_result($result);
		}
	}
?>
Heres where its called

Code: Select all

<?php
	include('includes/header.txt');
	include('db.php');

	$subjectname = $_POST['subjectname'];

	if ($subjectname == '')
	{
		echo("<b>You forgot to fill in all of the form.</b><br><br>");
		include('includes/subjectform.txt');
	}
	else
	{
		echo("<b>You forgot to fill in all of the form.</b><br><br>");
		db_query(false,"INSERT INTO tt_subjects (SubjectName)
			VALUES ($subjectname)");
	}

	include('includes/footer.txt');
?>
And that is called by a simple html form. If you want to try it out for yourself its here http://www.kzar.co.uk/project

Posted: Thu Nov 25, 2004 3:21 pm
by evilmonkey
Try this for the query:

Code: Select all

INSERT INTO `tt_subjects` (`SubjectName`) VALUES ('$subjectname')
That is assuming that subject name is a string. Also, it is probably a bad idea to have your user see an error message no matter what. ;)

Code: Select all

<?
//...
    if ($subjectname == '')
    {
        echo("<b>You forgot to fill in all of the form.</b><br><br>");
        include('includes/subjectform.txt');
    }
    else
    {
        echo("<b>You forgot to fill in all of the form.</b><br><br>"); //take this out, bad idea
        db_query(false,"INSERT INTO tt_subjects (SubjectName)
            VALUES ($subjectname)"); //try the query i gave you
//put in a success message
//...
?>
Good luck.

Posted: Thu Nov 25, 2004 3:31 pm
by kzar
evilmonkey wrote:Try this for the query:

Code: Select all

INSERT INTO `tt_subjects` (`SubjectName`) VALUES ('$subjectname')
That is assuming that subject name is a string. Also, it is probably a bad idea to have your user see an error message no matter what. ;)

Code: Select all

<?
//...
    if ($subjectname == '')
    {
        echo("<b>You forgot to fill in all of the form.</b><br><br>");
        include('includes/subjectform.txt');
    }
    else
    {
        echo("<b>You forgot to fill in all of the form.</b><br><br>"); //take this out, bad idea
        db_query(false,"INSERT INTO tt_subjects (SubjectName)
            VALUES ($subjectname)"); //try the query i gave you
//put in a success message
//...
?>
Good luck.

Cheers mate. Yea the error on both outcomes was because i copied the error but forgot to change it to a success message! Anyway that sql worked a treat.