php code

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
zyg0te
Forum Newbie
Posts: 3
Joined: Wed Jan 07, 2004 7:59 pm
Contact:

php code

Post by zyg0te »

hi forum.

i'm thinking of where did it went wrong.. code doesn't add a new record to a local mysql database.

coming from an input page to this php code...

pls see:

<head><title>add</title></head>
<body>
<?php
echo $_POST["item"]; //i put this on for testing if i got the correct item
$item = addslashes($item);
@ $db = mysql_pconnect("localhost", "test");
mysql_select_db("mydb");
$sql="Insert into Item values('".$item."')";
$query= mysql_query($sql);
if(!$query){
echo mysql_affected_rows()."entry not inserted.";
} else {
echo mysql_affected_rows()."entry inserted.";
}
?>
</body>
</html>



No error displayed and connection to the database is established.. but entry are not inserted. pls help :cry:
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Code: Select all

$sql="Insert into Item values('".$item."')";
insert is like this:

Code: Select all

"INSERT INTO tablename (dbvar1, dbvar2) VALUES ('$var1','var2')";
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

LiL, first syntax is acceptable too.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

oh.. thought you had to specifiy a variable to insert into..

ie. if he has 3 vars in his db, which one does it insert to? the first one? all of them?
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

mMm, not entirely sure but I think you use his type of syntax if you're inserting into all of them but your type of syntax when inserting into specific rows.

-Nay
zyg0te
Forum Newbie
Posts: 3
Joined: Wed Jan 07, 2004 7:59 pm
Contact:

hi

Post by zyg0te »

elo .. thank you for your time.. im sory if i didn't gave much info.. the table has two fields namely: item and date.. item is a text type and date is datastamp type.

Is there something wrong in my code? err.. php.ini or httpd.conf ? pls help

thank again.
User avatar
lazy_yogi
Forum Contributor
Posts: 243
Joined: Fri Jan 24, 2003 3:27 am

Post by lazy_yogi »

Brilliant thread title : php code

who would have thought you would post something about php in an all php forum ?

Spend a few seconds thinking of an appropriate title that describes something about your problem. Like "can't add new record to db"
Atleast we will know what you need help with and whether we are the best person to offer help.
microthick
Forum Regular
Posts: 543
Joined: Wed Sep 24, 2003 2:15 pm
Location: Vancouver, BC

Re: hi

Post by microthick »

zyg0te wrote:elo .. thank you for your time.. im sory if i didn't gave much info.. the table has two fields namely: item and date.. item is a text type and date is datastamp type.

Is there something wrong in my code? err.. php.ini or httpd.conf ? pls help

thank again.
I believe if your table has two columns, you have to use the more traditional method of inserting data, rather than the shorter way you are. That is, unless you also include the date in your current statement.

Inserting just the item without date will probably result in a failed insertion.
zyg0te
Forum Newbie
Posts: 3
Joined: Wed Jan 07, 2004 7:59 pm
Contact:

Post by zyg0te »

hi.. sory for the subject title :roll: but anyway thank you for the tips, the code work good.. the only mistake i had was the privileges of the user "test"..

god bless,
Post Reply