SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Enthusiast
    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

  2. #2
    SitePoint Wizard
    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.

  3. #3
    SitePoint Wizard gold trophysilver trophy
    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" );
    }
    Some people say for security, you want to remove the "mysql_error()" part when you go live with this, to prevent hacking.

    And now.......

  4. #4
    Prolific Blogger silver trophy Technosailor's Avatar
    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'
    Sketch
    Aaron Brazell
    Technosailor



  5. #5
    SitePoint Enthusiast
    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

  6. #6
    SitePoint Wizard
    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
    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.

  7. #7
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    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.
    mysql_pconnect()

    Because you removed your username and password, this command could not have worked. When testing on localhost I always leave out usernames and passwords.
    that's simply because, by default, MySQL isn't very secure and lets you connect anonymously. try not entering a username/password on my system = no workie.
    - 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

  8. #8
    SitePoint Enthusiast
    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

  9. #9
    SitePoint Wizard
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •