Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6795835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:23:40+00:00 2026-05-26T18:23:40+00:00

I’m trying to create a script for a user to enter in their username,

  • 0

I’m trying to create a script for a user to enter in their username, and then have other logged in usernames randomly show, in a chatroulette fashion.

So, you will enter in your name and hit submit, then your name will be stored in a database and someone else’s name will be pulled out at random and shown. Then the user can hit a next button to see another random user name. When the user closes the page, their name will be unloaded from the system.

What I have tried is creating a simple post submission form which will return you to the same page logged in with your name, and it inserts your name into a mysql database. That worked.

Then I added some PHP code to detect that the name variable has been set and to find a random username in the database by finding the amount of users in the database and using a random integer to pick one out. I’m pretty sure it worked, however I was unable to get the user name to show with echo "$name";.

Then I tried adding an automatic logout by using:

<body onUnload=<?php session_destroy();?>>

That didn’t work. I didn’t get around to creating a next button because I was having a few problems, because I figured out that the logout wouldn’t work because I would be dropping rows from the database that wouldn’t be filled in again as new rows were added to the SQL database with an auto increment function causing blank pages to be shown.

Here is my code:

<html>
<head>
<title>random name</title> 
</head>
<body>
<center>
<h1>random name</h1>
<h5>By DingleNutZ</h5>
</center>
<?php
if (!isset($_POST['name'])){
echo "<form action=\"index.php\" method=\"POST\" name=\"form\"><center><h4>name:</h4><input name=\"name\" id=\"name\" type=\"text\"/><br/>
<input type=\"submit\" name=\"submit\" value=\"Play!\"/></center></form>";
}else{
$name = $_POST['name'];
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="ftr"; // Database name 
$tbl_name="players"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// To protect MySQL injection (more detail about MySQL injection)
$name = stripslashes($name);
$name = mysql_real_escape_string($name);

$sql="SELECT * FROM $tbl_name WHERE name='$name'";
$result=mysql_query($sql);

// Mysql_num_row is counting table row
$count=mysql_num_rows($result);


if($count==1){

session_register("name");
session_start();
if(session_is_registered(name)){
$players=mysql_query("SELECT MAX (id) FROM $tbl_name");
$chooserand=rand(1,$players);
$callee=mysql_query("SELECT name FROM $tbl_name WHERE id=$chooserand");
echo "$callee";
echo "<a href=\"index.php?playing=1\">Logout</a>";
if (isset($playing)){
if ($playing == 1){
$drop_name=mysql_query("DELETE FROM $tbl_name WHERE name=$name");
}}
}
}
echo "show random name here";
}
?>
</body>
</html>

There is a variable in there called $playing which was an attempt at a logout system.

I would be very grateful for any answers. Many thanks in advance.

as i didnt make it obvious (sorry guys) i need to fix my main problem which is being able to show a random user without ever showing a blank page due to the rows being dropped from the database. it is essential that usernames are removed from the system for privacy

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T18:23:40+00:00Added an answer on May 26, 2026 at 6:23 pm

    You have a few issues in your code, not all are errors as such, some code is unneeded, other code is potentially dangerous.

    $name = stripslashes($name);              <<-- delete this line.
    $name = mysql_real_escape_string($name);  <<-- this is all you need.
    

    mysql_real_escape_string() is all you need. No other escaping is need to protect against SQL-injection.
    A few caveats apply, which I will discuss below.

    $sql="SELECT * FROM $tbl_name WHERE name='$name'";  
    $result=mysql_query($sql);  
    

    Select * is an anti-pattern, never use it in production code. Explicitly select the fields you need.
    You are using dynamic tablenames, I fail to see the need for this and it’s also a dangerous SQL-injection hole.
    Never use it but if you must, see this question how to secure your code: How to prevent SQL injection with dynamic tablenames?
    You do the query, but you don’t test if it succeeds, put a test in there:

    $sql = "SELECT id FROM users WHERE name='$name' ";  
    $result = mysql_query($sql);  
    if ($result) 
    {
      $row = mysql_fetch_array($result);
      $user_id = $row['id'];  
    }
    else { do stuff to handle failure }
    

    You are trying to get data out of the database, but this is not the way to do it:

    $players = mysql_query("SELECT MAX (id) FROM $tbl_name");                
    $chooserand = rand(1,$players);                
    $callee = mysql_query("SELECT name FROM $tbl_name WHERE id=$chooserand");                
    echo "$callee";         
    

    But I see a few issues:
    Please stop using dyname tablenames, it is a really bad idea.
    The return value of mysql_query is a query_handle, not the actual data you’re quering.
    I would suggest escaping all values, whether from outside or inside your code; I know this is paranoid, but that way, if you code design changes, you cannot forget to put the escaping in.
    Never ever ever echo unsanitized data in an echo statement.
    If you echo a $var, always sanitize it using htmlentities. If you don’t XSS security holes will be your fate.
    See: What are the best practices for avoiding xss attacks in a PHP site

    rewrite the code to:

    $result = mysql_query("SELECT MAX (id) as player_id FROM users"); 
    $row = mysql_fetch_array($result);
    $max_player = $row['player_id'];               
    $chooserand = mysql_real_escape_string(rand(1,$max_player)); 
    //not needed here, but if you change the code, the escaping will already be there.
    //this also makes code review trivial for people who are not hep to SQL-injection.               
    $result = mysql_query("SELECT name FROM users WHERE id = '$chooserand' ");
    $row = mysql_fetch_array($result);  
    $callee = $row['name'];            
    echo "callee is ".htmlentities($callee);  
    

    Finally you are deleting rows from a table, this looks like a very strange thing to do, but it is possible, however your code does not work:

    $drop_name = mysql_query("DELETE FROM $tbl_name WHERE name=$name");  
    

    As discussed mysql_query does not return values.
    On top of that only a SELECT query returns a resultset, a DELETE just returns success or failure.
    All $vars must be quoted, this is a syntax error at best and an SQL-injection hole at worst.
    Technically integers don’t have to be, but I insist on quoting and escaping them anyway, because it makes your code consistent and thus much easier to check for correctness and it elimiates the chance of making errors when changing code
    Rewrite the code to:

    $drop_name = $name;
    $result = mysql_query("DELETE FROM users WHERE id  = '$user_id' ");  
    //user_id (see above) is unique, username might not be.
    //better to use unique id's when deleting.
    $deleted_row_count = mysql_affected_rows($result);  
    if ($deleted_row_count == 0)
    { 
      echo "no user deleted"; 
    } else {
      echo "user: ".htmlentities($drop_name)." has been deleted";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a script that will create a user in MediaWiki, so
I'm trying to create a MSBuild script for running in TeamCity. I have a
I'm trying to create a script that will delete all user properties for a
I am trying to create a script for quiz. I have stored current timestamp
I'm new at Greasemonkey. I'm trying to create a user script to improve my
I am trying to create a user via a T-SQL script in SQL Server
im trying to create a script that opens a file and replace every 'hola'
I am trying to create a script to help me move large files accross
I am trying to create a script that finds the closest store/location to a
I am trying to create some script variables in T-SQL as follows: /* Deployment

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.