PHP insert

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Bonzol
Forum Newbie
Posts: 10
Joined: Mon Mar 20, 2006 8:44 pm

PHP insert

Post 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]
Last edited by Bonzol on Fri Oct 20, 2006 12:58 pm, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Take off the @ in front of $result, and use mysql_error() in your exit part
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Bonzol
Forum Newbie
Posts: 10
Joined: Mon Mar 20, 2006 8:44 pm

Post by Bonzol »

thanx buddy, what exactly is the significane of the @?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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>
User avatar
Jade
Forum Regular
Posts: 908
Joined: Sun Dec 29, 2002 5:40 pm
Location: VA

Post by Jade »

@ supresses the warning or error messages you might get
Post Reply