Jump to content

Usernames being added in mysql from txt in seperate rows


CBaZ

Recommended Posts

the issue is this stores it in my database but all in a single row.  also it keeps adding the names eventhough they already exist.

<?php error_reporting(E_ALL) ; ini_set('display_errors','1'); ?>
<?php 
$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_get_contents("/trivia/users_online2.txt");  
$date = date("YmdHis");
echo  "$triv"; 
if (mysql_num_rows($query) >= 0) { 
for($i=0;$i<count($triv);$i++)  {
   $result = "INSERT INTO users (username, created) values ('".$triv."', '".$date."')" ; 
}} else { 
   echo "number of rows: '".mysql_num_rows($query)."'<br>"; 
   exit; 
} 
$row2 = mysql_query($result) or die("QUERY FAILED: " . mysql_error()); 
$sql = mysql_query("SELECT * FROM users WHERE user_id != '$user_id' ORDER BY created");


?> </code>

still need some help figuring out why it can not find existing username or how to manage this insert to be in seperated rows

 

<code><?php error_reporting(E_ALL) ; ini_set('display_errors','1'); ?>

<?php

$connect = mysql_connect("localhost", "user", "pw"); mysql_select_db("db", $connect);

$query = mysql_query("SELECT * FROM users WHERE username != '" . $username . "'"); 

$triv = file_get_contents("/trivia/users_online2.txt"); 

$date = date("YmdHis");

echo  "$triv";

$sql = sprintf(

    'SELECT `username` FROM `users` WHERE `username` = \'%s\' LIMIT 1',

    mysql_real_escape_string( '" . $username . "' )

);

$qry = mysql_query( $sql );

$rows = mysql_num_rows( $qry );

if ( FALSE === $rows )

{

    exit;

}

elseif ( 0 === $rows )

{

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

  $result = "INSERT INTO users (username, created) values ('".$triv."', '".$date."')";

}}

else

{

$username = ($triv);

$count = mysql_num_rows(mysql_query("SELECT * FROM `users` WHERE `username`='".$username."'")); 

if (mysql_num_rows($count) > 0) {

            return "username exists";

   

}}

$row2 = mysql_query($result) or die("QUERY FAILED: " . mysql_error());

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

?></code>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.