SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
Thread: inserting into database
-
Feb 12, 2002, 09:59 #1
- Join Date
- Nov 2001
- Posts
- 56
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
inserting into database
hi
im working on a personal project, i have apache,mysql and php all working fine on my machine, i can perform a select query through a web form but i cant seem to put anything into the database through a web form.
im thinking this has something to do with a password although i have set one up using the grant command in mysql
anyways here some of the code
@ $db = mysql_pconnect("localhost","bookorama","bookorama123");
if (!$db)
{
echo "Error: Could not connect to database. Please try again later.";
exit;
}
mysql_select_db("books");
$query = "insert into books values ('".$isbn."', '".$author."', '".$title."', '".$price."')";
$result = mysql_query($query);
if ($result == true){
echo mysql_affected_rows()." book inserted into database.";
}
else {echo "**** didnt work again";}
by the way this is all being done using windows98 and im starting mysql by navigating through explorer and clicking on the file but i never get asked for any passwords ! am i starting it the right way ?
cheers chris
-
Feb 12, 2002, 10:03 #2
- Join Date
- Jul 2001
- Location
- The Netherlands
- Posts
- 2,617
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
You are starting it the right way, but you need to include the names of the colums in which you want to insert the values as well.
-
Feb 12, 2002, 10:06 #3
- Join Date
- Nov 2000
- Location
- Switzerland
- Posts
- 2,479
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Now I get to use my signature to full effect: you don't wanna start from here;
Code:$query = mysql_query ( "INSERT books SET isbn='$isbn', author='$author', title = '$title', price = '$price'" ); if ( !$query ) { echo ( "Error inserting in books: " . mysql_error()" ) } else { echo ( "Insert successful" ); }
And now.......
-
Feb 12, 2002, 10:07 #4
- Join Date
- Jun 2001
- Location
- Before These Crowded Streets
- Posts
- 9,446
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
try a query structure like this:
PHP Code:
INSERT INTO dbname SET
fieldname1 = '$corrsepondingFormField1',
fieldname2 = '$corrsepondingFormField2',
fieldname3 = '$corrsepondingFormField3';
-
Feb 12, 2002, 10:30 #5
- Join Date
- Nov 2001
- Posts
- 56
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
im trying right now!
Im trying all your sugestions but i cant get past not recieving the first error msg unless i remove the user name and password then it accepts my details but doesnt enter them so im thinking it must be to do with these.
cheers chris
-
Feb 12, 2002, 11:53 #6
- Join Date
- Jul 2001
- Location
- The Netherlands
- Posts
- 2,617
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
I totally missed that part, because my first advise would have been to remove your database username and password. I know you are running the script on localhost, but it is always best to leave those out when posting code on the forums, they have absolutely nothing to do with the working of the script.
Now, for the connection command, why not try this one:
PHP Code:$dbx = mysql_connect();
mysql_select_db('books', $dbx;
Because you removed your username and password, this command could not have worked. When testing on localhost I always leave out usernames and passwords.
-
Feb 14, 2002, 23:28 #7
- Join Date
- Jul 2001
- Location
- Missouri
- Posts
- 3,428
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Originally posted by Mark T.
Your problem was you used mysql_pconnect. I never used that string of command before, but I assume it is used to tell MySQL you are connecting with username and password -- as in mysql-passwordconnect.
Because you removed your username and password, this command could not have worked. When testing on localhost I always leave out usernames and passwords.- Matt** Ignore old signature for now... **
Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
"Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR
-
Feb 14, 2002, 23:36 #8
- Join Date
- Jul 2001
- Posts
- 86
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Guys,
You might want to check the syntax of an insert command first, it seems you're all mixing inserts with updates. Here's the syntax of a propert insert command:
INSERT INTO [TableName](FieldName1, FieldName2, FieldName3) VALUES(Value1, Value2, Value3)
If you will be inserting values into every field, you can use something like this:
INSERT INTO [TableName] VALUES(Value1, Value2, Value3)
For auto incrementing fields, specify 0 as the value and MySQL will automatically update it. So a real query might look like this:
INSERT INTO Products(prodId, title, price)
VALUES(0, 'My Widget', 45.95)
Get ConMan and run your own web site!
Want free programming eBooks? http://www.devarticles.com/ebooks.php
-
Feb 15, 2002, 07:57 #9
- Join Date
- Jul 2001
- Location
- The Netherlands
- Posts
- 2,617
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Aah well, I assume you know your stuff Matt
.
* MarkieMark thinks it's time to take a look at the online documentation for a few hours*
Bookmarks