Connecting Tech Pros Worldwide Forums | Help | Site Map

PHP insert

Bonzol
Guest
 
Posts: n/a
#1: Oct 20 '06
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


petersprc@gmail.com
Guest
 
Posts: n/a
#2: Oct 20 '06

re: PHP insert


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.
Quote:
occured while connecting to the database. The following query is
>
not valid: \''.$query.'\'.</strong>');
}?>

Bonzol wrote:
Quote:
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
.:[ ikciu ]:.
Guest
 
Posts: n/a
#3: Oct 20 '06

re: PHP insert


Hmm Bonzol <Bonzol@hotmail.comwrote:
Quote:
<?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();


Petr Vileta
Guest
 
Posts: n/a
#4: Oct 21 '06

re: PHP insert


"Bonzol" <Bonzol@hotmail.compíse v diskusním príspevku
news:1161366905.830555.89540@m73g2000cwd.googlegro ups.com...
Quote:
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.)


peter
Guest
 
Posts: n/a
#5: Oct 21 '06

re: PHP insert


Quote:
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.


Colin Fine
Guest
 
Posts: n/a
#6: Oct 21 '06

re: PHP insert


Bonzol wrote:
Quote:
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
Closed Thread