Page 1 of 1
php code
Posted: Wed Jan 07, 2004 7:59 pm
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

Posted: Wed Jan 07, 2004 8:11 pm
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')";
Posted: Wed Jan 07, 2004 8:22 pm
by Weirdan
LiL, first syntax is acceptable too.
Posted: Wed Jan 07, 2004 9:09 pm
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?
Posted: Wed Jan 07, 2004 10:01 pm
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
hi
Posted: Thu Jan 08, 2004 12:38 am
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.
Posted: Thu Jan 08, 2004 1:54 am
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.
Re: hi
Posted: Thu Jan 08, 2004 1:56 am
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.
Posted: Thu Jan 08, 2004 6:29 pm
by zyg0te
hi.. sory for the subject title

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,