www.webdeveloper.com
Results 1 to 3 of 3

Thread: mysql_query help: check table for entires

  1. #1
    Join Date
    Jan 2009
    Location
    127.0.0.1
    Posts
    126

    mysql_query help: check table for entires

    ok back again for some help...

    I have created a table with entires. I would like to be able to check this table when a new entry is being inserted so I don't have duplicates.

    I have highlighted the line of code that is giving me trouble below. I get an error that reads "Parse error: syntax error, unexpected T_VARIABLE in /home2/whackand/public_html/index_files/basic_db_wiki.php on line 68" and I can't seem to figure out how to fix it.

    I know it is probably a simple fix. Any help is appreciated.


    Code:
    <html>
    <head>
    <?php
    //Checking database for existance of table
    function table_exists($table, $db)
    	{
    	//retrives the whole list of tables from database
    	$tables_check = mysql_list_tables($db);
    	echo mysql_list_tables($db) . "<br />";
    	//cycles through the list checking each entry
    	while (list ($temp) = mysql_fetch_array($tables_check))
    		{
    		if ($temp == $table)
    			{
    			return true;
    			}
    		}
    		
    	return false;
    	}
    	
    		
    ?>
    </head>
    
    <body>
    <?php
    
    //declaration of variables
    	//creating connection to server
    $con = mysql_connect("localhost","username","password") or die('Could not connect to server' . mysql_error());
    $table = "entries";
    $db = "whackand_wikieng";
    
    	//create table "entries" process
    mysql_select_db($db, $con);
    $crt_table = "CREATE TABLE $table
    	(
    	Number int NOT NULL AUTO_INCREMENT,
    	PRIMARY KEY(Number),
    	Name varchar(30),
    	ClassLt varchar(4),
    	ClassNo int(4),
    	Semester varchar(10),
    	Professor varchar(30),
    	Type varchar(5)
    	)";
    
    	//insert values into "entries" process
    mysql_select_db($db, $con);
    $insert_value = "INSERT INTO $table
    	(Name, 
    	ClassLt, 
    	ClassNo, 
    	Semester, 
    	Professor, 
    	Type)
    VALUES
    	('$_POST[Name]', 
    	'$_POST[ClassLt]', 
    	'$_POST[ClassNo]',
    	'$_POST[Semester]', 
    	'$_POST[Professor]', 
    	'$_POST[Type]')";
    
    	//getting entries from table for evaluation
    mysql_select_db($db, $con);
    $result = mysql_query(SELECT * FROM $table WHERE $name = '$_POST[Name]', $con);
    
    
    //execution of page
    	//checking for table existance
    if(!table_exists($table, $db))
    	{
    	mysql_query($crt_table, $con) or die('Could not create table' . mysql_error());
    	}
    	
    	//checking for value in table, if it doesnt exist, insert value
    if(mysql_num_rows($result))
    {
    	echo "The entry already exists."
    }
    else
    {
    mysql_query($insert_value);
    }
    
    	//closing server connection
    mysql_close($con);
    
    echo "Sup.";
    ?>
    </body>
    </html>

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    18,921
    You need quotes around the entire SQL query string, so that PHP treats it as a string literal and not as a sequence of constants, PHP keywords, and/or operators.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Jan 2009
    Location
    127.0.0.1
    Posts
    126
    thanks a bunch i will have to try to remember that one.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles