Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Can't create or insert into table?!

Options
  • 14-07-2007 10:39pm
    #1
    Registered Users Posts: 7,041 ✭✭✭


    I'm testing out a form that inserts into a Database but I'm having trouble submitting the table and info. I've gone over it a million times and compared it to other scripts and can't find anything wrong, can you?

    [PHP]<?PHP

    //Make Connection and Variable

    $con = mysql_connect("local host","user","pass");

    //Make sure its connected

    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }else{
    echo 'Connected <br />';
    }

    //Choose the Database and connect

    mysql_select_db("DB_NAME", $con);

    //Create the table

    $sql = "CREATE TABLE info(
    Fname VARCHAR(10),
    Sname VARCHAR(20),
    A1 VARCHAR(15),
    A2 VARCHAR (15),
    Town VARCHAR(15),
    County VARCHAR(10),
    Hphone VARCHAR(10),
    Mphone VARCHAR(10),
    Appliance VARCHAR(15),
    Brand VARCHAR(10),
    Snumber VARCHAR(15),
    PRIMARY KEY(Fname)
    )";


    $into = "INSERT INTO info (Fname, Sname, A1, A2, Town, County, Hphone, Mphone, Appliance, Brand, Snumber)
    VALUES( '$_POST[Fname]',
    '$_POST[Sname]',
    '$_POST[Address1]',
    '$_POST[Address2]',
    '$_POST[Town]',
    '$_POST[County]',
    '$_POST[Hphone]',
    '$_POST[Mphone]',
    '$_POST[Appliance]',
    '$_POST[Brand]',
    '$_POST[Snumber]'
    )";

    if(!sql_query($into,$con)){
    die('Error: ' . mysql_error());
    }else{
    echo 'Table and Values added, Success';
    }

    mysql_close($con);
    ?> [/PHP]

    When I view the source the ending </body></html> tags don't display so theres something messing up everything below it but I don't no where it begins.

    Please Help,
    S.


Comments

  • Moderators, Politics Moderators Posts: 39,822 Mod ✭✭✭✭Seth Brundle


    Well after going to http://xxx.xxx.xxx.xxx and using your DB login details (which you really shouldn't put online!), I was able to create (& drop) the table using your SQL so that end of it is fine.

    Your PHP defines the SQL used to create the table is not executed - you go straight into defining the insert SQL statement and then running that but by then the info table doesn't exist!

    edit: DB IP addy removed


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    Right, eh good you remove that from your post? I actually changed those bits but then made a change to my script and pasted it in. Its only a test DB from a free blog page which I'm dumping so Its not too big a deal. But for the time being can you forget what you saw?

    Hold on, so I'm going over my script with fresh eyes and relise that non of the commands are being executed. They should be executed like this [php]mysql_query($sql,$con);[/php] right?

    Well thats not working for me either so there must be another problem? Any help?


  • Closed Accounts Posts: 198 ✭✭sh_o


    I don't know php but I would change your 'die' statements to 'echo' so that you can see what mysql_error() is returning which may shed a bit of light on the situation.


  • Closed Accounts Posts: 8,866 ✭✭✭Adam


    Or just use:

    [php]die("insert error comment here");[/php]


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    And change your DB logon details asap.


  • Advertisement
  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    Thanks Phil, that was the first thing done.

    Also for anyone interested I fixed it but still don't know what the problem was. All I did was copy the bottom section out of it, tested it (it created the table), paste the bottom bit back in, test it and it worked. Dunno what was going on. Maybe I pasted over an extra brace that wasn't needed. Lucky.

    P.S. kbannon,
    Thanks for the heads up and removing the link. By the way did you really log in and create and drop the table or were you just messin'?


  • Moderators, Politics Moderators Posts: 39,822 Mod ✭✭✭✭Seth Brundle


    Seachmall wrote:
    P.S. kbannon,
    Thanks for the heads up and removing the link. By the way did you really log in and create and drop the table or were you just messin'?
    Adding http:// to your IP brought me to PHPMyAdmin and I could login using the credentials you supplied. Once in I copied your create table statement which worked. I then dropped the table and closed the site.


  • Registered Users Posts: 14,339 ✭✭✭✭jimmycrackcorm


    Seachmall wrote:
    Thanks Phil, that was the first thing done.

    Also for anyone interested I fixed it but still don't know what the problem was. All I did was copy the bottom section out of it, tested it (it created the table), paste the bottom bit back in, test it and it worked. Dunno what was going on. Maybe I pasted over an extra brace that wasn't needed. Lucky.

    P.S. kbannon,
    Thanks for the heads up and removing the link. By the way did you really log in and create and drop the table or were you just messin'?

    The problem was that you said SQL$="Create table...."
    But you didn't actually execute it programmatically. When you then manually created the table, your insert statement worked.

    Just goes to show you should do as you say....


  • Moderators, Politics Moderators Posts: 39,822 Mod ✭✭✭✭Seth Brundle


    The problem was that you said SQL$="Create table...."
    But you didn't actually execute it programmatically.
    If only someone had told the OP that!
    ;)


  • Registered Users Posts: 7,041 ✭✭✭Seachmall


    The problem was that you said SQL$="Create table...."
    But you didn't actually execute it programmatically. When you then manually created the table, your insert statement worked.

    Just goes to show you should do as you say....

    If you read my second post you'll see that I noticed that and inserted the appropriate statment. It still didn't work though. I have it now though, thanks.


  • Advertisement
Advertisement