Assign ID to username, in PHP
Upon a user entering x.com/y.php?username=z
, I would like to take that username as an argument to generate an ID and associate it with that username by writing it to a table. However, so far I've been getting nothing but 500
errors when I input a username.
(Third day into this)
<?php
error_reporting(E_ALL);
$con = mysql_connect("localhost","&&&&&","&&&&&");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("wp", $con);
Function RandomString()
{
$characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for ($i = 0; $i < 20; $i++)
{
$randstring.= $characters[rand(0, strlen($characters))];
}
return $randstring;
}
if (isset($_GET["username"]) && !empty($_GET["username"]))
{
$username = $_GET['username'];
$usercheck = mysql_query("SELECT COUNT(*) AS a FROM wp_users WHERE user_login=".$username."",$con);
$res1 = $usercheck->fetch();
$usercheck->closeCursor();
if (empty($res1["a"]))
{
$log开发者_开发百科 = "genlog.txt";
$fh = fopen($log, 'a') or die("can't open file");
$date = date("m/d/Y");
$stringData = "Database write failed at ".time()." -- .\n Data entered was: ".$username."\n";
fwrite($fh, $stringData);
fclose($fh);
die('ERROR: Username does not exist.');
}
else
{
$n = 1;
while($n != 0)
{
$randstring = "live_".RandomString();
echo $randstring;
$req0 = mysql_query("SELECT COUNT(*) AS n, streamer_id FROM streamer_ids WHERE streamer_id=".$randstring."",$con);
$res0 = $req0->fetch();
$req0->closeCursor();
$n = $res0["n"];
}
$temp = mysql_query("INSERT INTO streamer_ids (username,streamer_id,premium) VALUES('".$username.",".$randstring.",0')",$con);
$temp->closeCursor();
}
}
else
echo "Wrong:".$username.""
?>
Try this, there were some syntax errors and MySql queries were not done correctly, not sure if you are using a different module, but I change it to work in generic setup.
<?php
error_reporting(E_ALL);
$con = mysql_connect("localhost","root","123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("wp_test", $con);
Function RandomString()
{
$characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for ($i = 0; $i < 20; $i++)
{
$randstring.= $characters[rand(0, strlen($characters))];
}
return $randstring;
}
if (isset($_GET["username"]) && !empty($_GET["username"]))
{
$username = $_GET['username'];
$result = mysql_query("SELECT COUNT(*) AS a FROM wp_users WHERE user_login='".$username."'",$con);
$res1 = mysql_fetch_assoc($result);
// $usercheck->closeCursor();
if (empty($res1["a"]))
{
$log = "genlog.txt";
$fh = fopen($log, 'a') or die("can't open file");
$date = date("m/d/Y");
$stringData = "Database write failed at ".time()." -- .\n Data entered was: ".$username."\n";
fwrite($fh, $stringData);
fclose($fh);
die('ERROR: Username does not exist.');
}
else
{
$n = 1;
while($n != 0)
{
$randstring = "live_".RandomString();
echo $randstring;
$result = mysql_query("SELECT COUNT(*) AS n, streamer_id FROM streamer_ids WHERE streamer_id='".$randstring."'",$con);
$res0 = mysql_fetch_assoc($result);
// $req0->closeCursor();
$n = $res0["n"];
}
$temp = mysql_query("INSERT INTO streamer_ids (username,streamer_id,premium) VALUES('".$username."', '".$randstring."',0)",$con);
// $temp->closeCursor();
}
}
else
echo "Wrong:".$username.""
?>
精彩评论