Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

php sql variable (revised)


ghost's Avatar
0 0

Alright, I asked this in my previous thread but it was garbled etc, so I thought I would ask here. Ok, here's what I need. I have a SQL table with 3 fields, username, password and id. I have manually set the id for me (the only person registered) to 1. What I need to do is to be able to retrieve the value of that column, and add 1 for the next person that registers so it tracks who registered in what order. Can anyone help me out?


ghost's Avatar
0 0

try something like this: $SQL = SELECT MAX(id) FROM table

then execute the query, grab the result of the query and increment it


ghost's Avatar
0 0

Yea…I know that but it doesn't work that way. First I realized you have to use LAST(id) and not max, that could cause errors. But it's much more than that, the code should look something like this:

$result = mysql_query("SELECT LAST(id) FROM users");
while($row = mysql_fetch_array($result))
{
mysql_select_db("slpctrl_users", $con);
mysql_query("INSERT INTO users (id)VALUES ('$result')");
mysql_close($con);
}

Or somethin, but between adding the other information (username and password) I can't get it to work. :@


ghost's Avatar
0 0

Ok, here's my code.

<html>
<body>
<?php
$username = addslashes($_POST['username']);
$password = addslashes($_POST['password']);
$con = mysql_connect("localhost:phpmyadmin","censor","censor");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$userid = ("SELECT LAST(id) FROM users");
if ($username == "" || $password == "")
{
die('<b><center>Please specify username or password</b></center><br><br>');
}
while($row = mysql_fetch_array($userid))
{
mysql_select_db("slpctrl_users", $con);
mysql_query("INSERT INTO users (username, password, id)VALUES ('$username', '$password', '$userid')");
mysql_close($con);
}
echo('<center><br><br><br><b><a href="memberlist.php">View Registered Members</a></center>');
?>
</body>
</html>

Now, without the: while($row = mysql_fetch_array($userid)) { } and without id and $userid in the query, it works fine but not with it. And I for the life of me can't figure out what is right >_<


ghost's Avatar
0 0

Nm got it :D


ghost's Avatar
0 0

slpctrl wrote: Nm got it :D

cool so wat was making it go haywire


ghost's Avatar
0 0

kmamoun wrote: [quote]slpctrl wrote: Nm got it :D

cool so wat was making it go haywire[/quote]

Lol I thought I had it because it wrote the user, but it still refuses to write the ID >_<