Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems inserting records in Database using PHP

Status
Not open for further replies.

kjspear

Programmer
Feb 13, 2002
173
US
I'm still having problems inserting records into the database table. I can do the command listed below on the command line in mysql monitor;

insert into person(name, age, sex) values("JAMES BROWN", 6, 'M')

However, I can't seem toget it to work under PHP. What is the proper syntax to get it to work in PHP? I hae created a form and would like to input to be inserted into the table.

Here's what I have;

<?php
$conn=mysql_connect(&quot;localhost&quot;,&quot;mkt2000&quot;,&quot;chelsea7&quot;);
mysql_select_db(&quot;mylogin&quot;,$conn);
$addrec=&quot;INSERT INTO logins (userID,passID,nickname) VALUES ('','$userID','passID','nickname')&quot;;
if (mysql_query($addrec, $conn)){
echo &quot;entry successful!&quot;;
} else{
echo &quot;entry unsuccessful. Contact the Webmaster&quot;;
}
?>

I'm really stuck on this one. Any assistance would be helpful.

Thanks
Kyle
 

item 1. you have more fields in the VALUES section than in the field columns section (4 vs 3) will cause an error...


$addrec=&quot;INSERT INTO logins (userID,passID,nickname) VALUES ('' what is the blank for? an autoincrement number? then no need to place the empty quotes
,'$userID' is $userID a number or alphanumeric?
,'passID'
,'nickname')&quot;;

Bastien

cat, the other other white meat
 
,'$user ID', '$passID,'$nickname',)&quot;; are all alphanumeric. The blank is nothing. Am I missing something? I can't insert any records using PHP. I can do it on the myslq monitor command line.
 
the best way is to ask mysql the error:
$addrec=&quot;INSERT INTO logins (userID,passID,nickname) VALUES ('','$userID','passID','nickname')&quot;;
mysql_query($addrec, $conn) or die(mysql_error().&quot;<BR>$addrec&quot;);

this will give the mysql error...

post it if u still cannot undertand it...

Known is handfull, Unknown is worldfull
 
Hello again,

I used the following as stated above and this is what the result was;


'No Database Selected
INSERT INTO logins (userID,passID,nickname) VALUES ('mkt111','qqqqqq','kkkkkk')'

So I gather that this has been the problem all along. But now how do I select the database
using PHP? I know in command line mode via mysql monitor the 'USE' command as in 'use mydatabase',
selects the database. Any recommendations?

Thanks,

Kyle
 
buddy as the error said u have not connected to the database...

connect first to the database...

Known is handfull, Unknown is worldfull
 
OK, Daniel. I used the syntax;

mysql_select_db(&quot;database name&quot;);

I also used te following,

mysql_pconnect();

I found the error. I wasn't connected to the database. I wrote a small code with will allow me to retrieve the data from the table with no problem. But once again, I still cannot add records to it. I receive no error messages at all. Even using the command in the previous;

$addrec=&quot;INSERT INTO logins (userID,passID,nickname) VALUES ('$userID','passID','nickname')&quot;;
mysql_query($addrec, $conn) or die(mysql_error().&quot;<BR>$addrec&quot;);

I even set the user privileges to Grant Select, Insert.

What else could be the problem? Does anyone have a sample of one that you know works?

Thank you
Kyle
 
o.k if it does not still insert then try this:
$addrec=&quot;INSERT INTO logins (userID,passID,nickname) VALUES ('$userID','passID','nickname')&quot;;
echo $addrec;

now copy the sql from the browser and try running it in mysql directly...

Known is handfull, Unknown is worldfull
 
OK I got it working. Thanks everyone. To be honest with you, I don't know why it's working now. All I did was juggle with the examples listed above. Now its works for some reason. No argument here. Thanksyou all your help!

Kyle
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top