Okay.... Some help is needed here!

He has: 296 posts

Joined: May 2002

I have a problem, and I don't know how to fix it. I just now started with MySQL and PHP togethor, and I can't get MySQL to work right. What I mean is when I have a form and send it to a PHP file that connects to the MySQL DB it says it works. But when I print the stuff in the table it's empty... help please???

I think I was confusing there

[James Logsdon]

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

Post the code please so we can see whats wrong...
What versions of PHP and MySQL are you using?

He has: 296 posts

Joined: May 2002

The code I use to Read from the DB...

<?php
<html>
<
body>


$db = mysql_connect(\"localhost\", \"USERNAME\", \"PASSWORD\");

mysql_select_db(\"testdb\",
$db);

$result = mysql_query(\"SELECT * FROM users\",$db);

echo \"<table border=1>\n\";

echo \"<tr><td>Member</td><td>E-Mail</tr>\n\";

while (
$myrow = mysql_fetch_row($result)) {

        printf(\"<tr><td>%s %s</td><td>%s</td></tr>\n\",
       
$myrow[1], $myrow[2], $myrow[3]);
}

echo \"</table>\n\";


</body>
</html>
?>

Do you need the one I use to input to the DB?

[James Logsdon]

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

Change it to:

<?php
<html>
<
body>


$db = mysql_connect(\"localhost\", \"USERNAME\", \"PASSWORD\");

mysql_select_db(\"testdb\",
$db);
$query = \"SELECT * FROM users\";
$result = mysql_query($query);

echo \"<table border=1>\n\";

echo \"<tr><td>Member</td><td>E-Mail</tr>\n\";

while (
$myrow = mysql_fetch_array($result)) {

        print \"<tr><td>%s %s</td><td>%s</td></tr>\n\";
        echo \"<tr><td>
$myrow[1] $myrow[2]</td><td> $myrow[3]</td></tr>\";
}

echo \"</table>\n\";
?>

Also, show us the code for inputting as well... please

Laughing out loud

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

The original printf() statement is ok. I don't see anything wrong with that script. Does it produce any errors?

Have you checked the mySQL DB to make sure that there is data inside the table?

Posting the insert script will help.

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 296 posts

Joined: May 2002

The insert script:

<?php
<html>
<
body>


if (
$submit) {
 
// process form

 
$db = mysql_connect(\"localhost\", \"USERNAME\", \"PASSWORD\");

  mysql_select_db(\"testdb\",
$db);

 
$sql = \"INSERT INTO users (user,password) VALUES ('$user','$password')\";

 
$result = mysql_query($sql);

  echo \"Thank you! Information entered.\n\";

} else{
  // display form

 

  <form method=\"post\" action=\" echo
$PHP_SELF\">

  Username:<input type=\"Text\" name=\"user\"><br>

  Password:<input type=\"Text\" name=\"password\"><br>

  <input type=\"Submit\" name=\"submit\" value=\"Register\">

  </form>

 
} // end if


</body>
</html>
?>

I checked the table before and it was empty, so I think it's the insert script.

[James Logsdon]

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

one thing.. you forgot a semicolon..

<?php
echo $PHP_SELF<strong>;</strong>
?>

I don't see anything else wrong with it.

try testing $result and printing errors...

<?php
$result
= mysql_query($sql);

if (!
$result) {
    echo \
"QUERY ERROR: \" . mysql_error() . \"\n\";
    echo \"QUERY:
$sql\n\";
}
?>

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 296 posts

Joined: May 2002

Now I get the error:

<?php
QUERY ERROR
: No Database Selected QUERY: INSERT INTO users (user,password) VALUES ('necrotic','darkone')
?>

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

alright, that's progress..

Make sure that your connection info is correct (username, password, hostname, database name). I believe mySQL is case sensitive.

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 296 posts

Joined: May 2002

Now it inserts it into the database but doesn't display with the get script. It shows only the password. You can find both scripts here(insert) and here(get)

[James Logsdon]

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Everything is getting inserted ok?
Just the password is being successfully retrieved?
Have you altered the get script?

Can you post the database structure?

Mark Hensler
If there is no answer on Google, then there is no question.

He has: 296 posts

Joined: May 2002

It inserts both the Username and Password but only displays the Password... I didn't edit the Get script. What do you mean db structure? Like I said, I'm new to this.

[James Logsdon]

Peter J. Boettcher's picture

They have: 812 posts

Joined: Feb 2000

Database structure means the design and layout of your database. This includes the names of your tables, the field names in those tables as well as the data types of those fields.

PJ | Are we there yet?
pjboettcher.com

He has: 296 posts

Joined: May 2002

So.... something like:

<strong>
Table | Field(s) | Type        | Attributes | Null | Default | Extra</strong>
--------------------------------------------------------------------
users | user     | varchar(20) |            |  No  |         |      |
users | password | tinytext    |            |  No  |         |      |
--------------------------------------------------------------------
'

[James Logsdon]

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.