Page 1 of 1

PHP insert

Posted: Fri Oct 20, 2006 12:56 pm
by Bonzol
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello, PHP n00b here.

Using SQL just working off some examples, I have no problem selecting data, but I cant seem to be able to insert. If someone could see where im going wrong.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Database Connection</title>
</head>
<body>
<?php
	#Server Name - e.g. localhost:3309
	$dbserver = 'localhost';
	#Username used to log into MySQL database server.
	$dbusername = '';
	#Password used to log into MySQL database server.
	$dbpassword = '';
	#Error message shown if an error whilst attempt to establish a connection with the server.
	$dberrormessage = '<strong>Error connecting to database...</strong>';	
	#Name of database on server.
	$dbdatabase = 'bonzollibrary';
	#Establish Connection
	@$conn = mysql_connect($dbserver, $dbusername, $dbpassword) or exit ($dberrormessage); 
	#Selects which database is to be used on server.
	mysql_select_db($dbdatabase); 
?>
	<h1>Sample Database Connection</h1>
	
	<form action="db-conn2.php" method="get">
		<input type="text" id="name" name="name" />&nbsp;
		<input type="submit" value="Search" />
	</form>

<?php		if(isset($_REQUEST['name']))
			{ ?>
				<table border="1">
					<tr>
						<th>Number</th>
						
					</tr>
"Insert into tbl_category (cat_description)
Values ('%{$_REQUEST['name']}%')


<?php			$query = "Insert into tbl_category (cat_description) Values ('%{$_REQUEST['name']}%')";
				@$result = mysql_query($query) or exit('<strong>An error has occured while connecting to the database. The following query is 

not valid: \''.$query.'\'.</strong>');
			}?>
		</table>
<?php
	#Check if connection is still open
	if($conn)
	{
		#Close the connection
		mysql_close($conn); 
	}
?>
</body>
</html>
Thanx in advance


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Fri Oct 20, 2006 12:58 pm
by s.dot
Take off the @ in front of $result, and use mysql_error() in your exit part

Posted: Fri Oct 20, 2006 1:44 pm
by Bonzol
thanx buddy, what exactly is the significane of the @?

Posted: Fri Oct 20, 2006 1:45 pm
by volka

Posted: Fri Oct 20, 2006 1:59 pm
by RobertGonzalez
Just a brief code cleanup. Try this and report back what is shown to you.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Database Connection</title>
</head>
<body>
<?php
        //Server Name - e.g. localhost:3309
        $dbserver = 'localhost';
        //Username used to log into MySQL database server.
        $dbusername = '';
        //Password used to log into MySQL database server.
        $dbpassword = '';
        //rror message shown if an error whilst attempt to establish a connection with the server.
        $dberrormessage = '<strong>Error connecting to database...</strong>';   
        //Name of database on server.
        $dbdatabase = 'bonzollibrary';
        #Establish Connection
        $conn = mysql_connect($dbserver, $dbusername, $dbpassword) or die($dberrormessage . ': ' . mysql_error());
        //Selects which database is to be used on server.
        if (!mysql_select_db($dbdatabase))
		{
			die(mysql_error());
		}
?>
        <h1>Sample Database Connection</h1>
       
        <form action="db-conn2.php" method="get">
                <input type="text" id="name" name="name" />&nbsp;
                <input type="submit" value="Search" />
        </form>

<?php      if(isset($_REQUEST['name']))
                        { ?>
                                <table border="1">
                                        <tr>
                                                <th>Number</th>
                                               
                                        </tr>
<?php         $query = "INSERT INTO `tbl_category` (`cat_description`) VALUES ('{$_REQUEST['name']}')";
              if (!$result = mysql_query($query))
			  {
				die('<strong>An error has occured while connecting to the database. The following query is not valid: \''.$query.'\' because: ' . mysql_error() . '.</strong>');
              }?>
                </table>
<?php
        //Check if connection is still open
        if($conn)
        {
                //Close the connection
                mysql_close($conn);
        }
?>
</body>
</html>

Posted: Fri Oct 20, 2006 6:56 pm
by Jade
@ supresses the warning or error messages you might get