Jump to content

script doesn't check for existing username.


CBaZ

Recommended Posts

2 issues in this script. one being I don't want it to keep entering the same usernames over and over i need a check if exist type thing and its not working what I currently have in place...

 

also the profile2.php link at the bottom does not generate the correct id. the id needs to be for $triv usernames. not sure if this can be figured out with just this script by itself please let me know if you need more info.

 

<?php

echo mysql_error();

$connect = mysql_connect("localhost", "user", "pw") or die ("Could not connect to database: " . mysql_error());

mysql_select_db("db", $connect) or die ("Could not select database");

$query = mysql_query("SELECT * FROM users WHERE username != '$username'") or die("QUERY FAILED: " . mysql_error());

$triv = file("users_online2.txt");

$date = date("YmdHis");

echo $triv;

if (mysql_num_rows($query) > 1 ) {

// loop though array

for($i=0;$i<count($triv);$i++) {

// and add each entry to SQL

$result = "INSERT INTO users (username,created) VALUES ('$triv[$i]', '$date')";

$row2 = mysql_query($result);

}} else {

$connect = mysql_connect("localhost", "user", "pw") or die ("Could not connect to database: " . mysql_error());

mysql_select_db("db", $connect) or die ("Could not select database");

$sql = mysql_query("SELECT user_id FROM users WHERE username != '$username' ORDER by created");

$row = mysql_fetch_assoc($sql);

echo '</div><div class="title" id="top_bar_right"><a href="profile2.php?user_id='.$row['user_id'].'" target="RSIFrame2">'$triv'</div></div>';

}

?>

Link to comment
Share on other sites

In the future put your code between


tags.  It makes it easier to read.

 

For your username issue.

Search your database with the requested username.

If you get a positive match, kick the user back with a duplicate name error.

otherwise, create the new username.

 

As far as the link for your profile2.php the method looks correct at first glance... what type of output are you getting?

Link to comment
Share on other sites

i am not getting the id for the usernames being added in the above code where its insert.  it gives me id's that have nothing to do with the ones just added. do you have an example of how i would search the database like with another select from or something have been trying to google it just can't think of it.

Link to comment
Share on other sites

this is how you would search for an id based on the username.

SELECT user_id FROM users WHERE username = '$username'

it will select the user_id only, you then have to pull the resulting number and display/use it in your script.

How are you storing 'user_id' in your database?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.