#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2001
    Location
    cheshire
    Posts
    52
    Rep Power
    17

    Angry Inserting data into a table


    Hello
    I would like to know if there is anything strange about the script below(I have only just started to learn php so there may well be!)
    Although it seems to work, occasionally it doesn't just insert a single row into the data base, instead 2 or 3 duplicate rows end up in the table. Could the explanation be that I tried this morning to create a table and forgot I already had one with this table name in the database. Would that have damaged the table in some way making it misbehave!
    I have commented out the bit about creating tables as I only want to insert data with this script at the moment. One other thought occurs to me, is the problem due to my host freedom to surf as its only a free host and I expect you get what you pay for(or don't pay for in this case). I have subscribed to a pay host http://www.phpwebhosting.com but I am unable to use the database that they have provided because they have informed me that I must use a telnet shell to log into my database and I have absolutly no idea what this is(is it in my Win98?) or do I need to go and get one. I am still waiting for this host to email me back with some help. If there is anyone out there who could tackle all or some of my problems I would be very grateful indeed!
    One last thought - I think there is something very strange going on as sometimes when I visit my database, there is less entries in this table than last time, some have been chucked away and my id entries are all out of order now, something like 1,2,3,11,5,7,12 - the last insert had id = 12 next to it and somehow another duplicate has sprung up id = 11????????
    Whats going on please!

    <html>
    <head>
    <title> Connecting to Mysql server at ISP host</title>
    </head>
    <body>
    <?php
    $Host = "db.davegraham.f2s.com:3306";// Set the variables for the database access:
    $User = "p0c79";
    $Password = "********";
    $DBName = "p0c79";
    $TableName = "Jokes2";

    $link = @mysql_connect("$Host", "$User", "$Password");
    if (!$link)
    {
    print("<P>Unable to connect to the " .
    "database server at this time.</P>\n"); //note how 2 strings have been concatonated
    exit();
    }
    else
    {
    print("Your now connected to the Mysql server at the host ISP.<P>\n");
    }

    if (! @mysql_select_db("$DBName") )
    {
    print("<P>Unable to locate the $DBName database at this time.</P>\n");
    exit();
    }
    else
    {
    print("The database $DBName has been located.</P>\n");
    }
    /*
    $sql = "CREATE TABLE $TableName (ID INT NOT NULL AUTO_INCREMENT PRIMARY KEY, JokeText TEXT, JokeDate DATE NOT NULL )";
    if ( mysql_query($sql) )
    {
    print("<P>$TableName table successfully created!</P>\n");
    }
    else
    {
    echo("<P>Error creating $TableName table: " .
    mysql_error() . "</P>");
    }
    */

    $Query="INSERT into $TableName VALUES('0', 'hello hello hello!', '2000-08-08')";

    if (mysql_query ($Query))
    {
    print ("The query was successfully executed!<br>\n");
    }
    else
    {
    print ("The query could not be executed!<br>\n");
    }
    mysql_close($link);
    ?>
    </body>
    </html>
    David Graham
    Dave Graham
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2001
    Location
    Milton Keynes
    Posts
    4
    Rep Power
    0
    A couple of points 1.You should be able to type Telnet <your ISP's ip_address> at the DOS prompt to Telnett to your ISP. 2. In your database order of records is not guaranteed. You need to use an order clause in a select query if you need ordered records.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2000
    Location
    Sandown, Isle of Wight, UK
    Posts
    49
    Rep Power
    18
    Hi David,

    It might be worth trying

    $query = " INSERT into $tablename ";
    $query .= " ( id, JokeText, JokeDate ) ";
    $query .= " values ";
    $query .= " ( 0000, 'hello hello hello', '2000-08-08' ) ";

    It might not, of course but it's safer to name the fields to which the values apply.

    As to using Telnet - it's really only a command line version of what you are doing in your program. For instance, once you are logged in to your ISP and have a prompt, you would type mysql -h hostname -u username -p and hit return. It will then ask for your password, type it in, press return and then select your database with the 'use' command - use databasename

    You can then select, delete or modify your data using the commands in your program eg

    insert into Jokes2 (id, JokeText, JokeDate) values (0, 'hello hello hello', '2000-08-08'); would be the equivalent command line entry for the stuff at the top of this page.

    select * from Jokes2; will show you everything in the table Jokes2.

    Is there any other way? Yes, I know there is really, but once you learn a few sql commands it's pretty straightforward, honest

    If you were not happy using the MSDos window, Windows Hyperterminal will do the job just as well. You should find it under Programs, Accessories, Communications. If it's not there, you will need to install it from the Windows CD. Failing that, you can get a very good Telnet program from http://www.netterm.com

    Best regards
    Chalky

IMN logo majestic logo threadwatch logo seochat tools logo