By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,231 Members | 1,279 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,231 IT Pros & Developers. It's quick & easy.

PHP insert

P: n/a
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
<!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

Oct 20 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Hi,

Maybe mysql_error has some more info?

$result = mysql_query($query) or trigger_error(mysql_error());

Dunno if it's a problem having dbusername and dbpassword blank.
occured while connecting to the database. The following query is

not valid: \''.$query.'\'.</strong>');
}?>

Bonzol wrote:
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
<!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
Oct 20 '06 #2

P: n/a
Hmm Bonzol <Bo****@hotmail.comwrote:
<?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
show us your table in db
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ikciu | gg: 718845 | yahoo: ikciu_irsa | www: www.e-irsa.pl
( ads: www.e6y.eu )

2be || !2be $this =mysql_query();
Oct 20 '06 #3

P: n/a
"Bonzol" <Bo****@hotmail.compíse v diskusním príspevku
news:11*********************@m73g2000cwd.googlegro ups.com...
Hello, PHP n00b here.

<?php $query = "Insert into tbl_category (cat_description) Values
('%{$_REQUEST['name']}%')";
Problem is that you use single quoutes twice ;-) Try this

$name = $_REQUEST['name'];
$query = "Insert into tbl_category (cat_description) Values ('%{$name}%')";

--

Petr Vileta, Czech republic
(My server rejects all messages from Yahoo and Hotmail. Send me your mail
from another non-spammer site please.)
Oct 21 '06 #4

P: n/a
Problem is that you use single quoutes twice ;-) Try this

$name = $_REQUEST['name'];
$query = "Insert into tbl_category (cat_description) Values
('%{$name}%')";
no he isn't he is using single quotes within double quotes which is
perfectly valid.

Just a note to the op however. You are currently using $_REQUEST I would
advise that you use either $_GET or $_POST although this is not cuasing the
error it is something that is frowned upon unless you really HAVE too. The
problem with using $_REQUEST is that you do not know where it has come from
properly.

As mentioned by another poster try using mysql_error to see what error
message mysql is outputting. it might also help as also mentioned to infact
show us the structure of your table.
Oct 21 '06 #5

P: n/a
Bonzol wrote:
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
<!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
You haven't told us what the problem actually is: do you get an error
message? Does it apparently succeed but insert no rows? Does it insert
empty rows?

Colin
Oct 21 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.