SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    May 2001
    Location
    x
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql table column/values. help!

    this is weird, or im missing something thats right in front of me. i have this script that connects to a db and enters data in a table.. it works fine with one table, but not at all with another. the error MUST be with the values i give the table. Because i made a table with only one value (username varchar(50)) and it works. so it must be in my integer line. i.e-
    userid int not null auto_increment primary key,
    this has to be the line in the mysql commands. i get no error in the php, it says it was successful but no data really gets entered in table. the prob isnt in the php(i dont think).
    heres my mysql code and my php code. thanks.
    (btw the table is created successfully, its just it dont work with this script..there are no mysql errors)

    MySQL> create table members (
    userid int not null auto_increment primary key,
    username varchar(30),
    email varchar(100)
    );

    ..........and now the php

    if ($username) {
    mysql_connect("localhost", "user", "pass") or die ("Problem connecting with database");
    $query = "insert into members values ('$username')";
    $result = mysql_db_query("box", $query);

    echo "data inserted. new table: <br><p></p>";
    $query = "SELECT * FROM members";
    $result = mysql_db_query("box", $query);

    if ($result) {


    while ($r = mysql_fetch_array($result)) {
    $username= $r["username"];



    echo "<table class='outline_table' width='100%' cellpadding='5'><tr>
    <td align='left'>$username </td></tr><tr>
    </tr></table><br />";
    } //end while loop



    } else {
    echo"No data.";
    } // end of if ($result)

    } else {
    echo "No UserName entered. Please go back and reenter UserName";
    } // end of if ($username)


    ....now remember that when i make a table with only one column (i.e username varchar(50)) it works fine.
    any suggestions on what that first column/values should be?
    Last edited by fockit; May 28, 2001 at 09:53.

  2. #2
    SitePoint Zealot Alarion's Avatar
    Join Date
    May 2001
    Location
    Virginia
    Posts
    126
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is your problem right here:

    table definition:
    create table members (
    userid int not null auto_increment primary key,
    username varchar(30),
    email varchar(100)
    );

    but here is your SQL:
    "insert into members values ('$username')"

    Now, the problem is that when you use an 'insert' SQL statement, you have two ways of doing it.

    You can either list the columns you want to put data in, and THEN list the values -or- you can just list the values (like you are trying to do).. BUT.. in this case you have to list a value for ALL columnns - MySQL has no clue what column you want to put "$username" in.

    so change your SQL to either:
    "insert into members values(null, '$username', '')"
    -or-
    "insert into members (username) values('$username')"

    HTH
    -=Alarion=-
    Protollix - Linux hosting from $3.95/m

  3. #3
    SitePoint Member
    Join Date
    May 2001
    Location
    x
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks so much Alarion it worked ...finally, now i can move on with the script. thanks again.


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
  •