<?php
include("global-settings.php");
session_start();
mysql_connect($dbhost, $dbuser, $dbpass)or die("Could Not Connect: " . mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$email = mysql_real_escape_string(strip_tags($_POST['email']));
$password = mysql_real_escape_string(strip_tags(sha1($_POST['password'])));
$result = mysql_query("SELECT * FROM users WHERE username='$email' AND password='$password'") or die(mysql_error());
while($row = mysql_fetch_array($result)) {
if(mysql_num_rows($result) > 0) {
$_SESSION['user_pid'] = $row['user_pid'];
$_SESSION['firstname'] = $row['first_name'];
$_SESSION['lastname'] = $row['last_name'];
header("Location: ../protected/home.php");
} else {
$userid_generator = uniqid(rand(), false);
$date = date("Y/m/d");
mysql_query("INSERT INTO users (user_pid, username, password, datetime_registered)
VALUES('$userid_generator', '$email', '$password', '$date')")or die(mysql_error());
$_SESSION['userid'] = $userid_generator;
$_SESSION['firstname'] = $row['first_name'];
$_SESSION['lastname'] = $row['last_name'];
$leaders_query = "SELECT * FROM leaders";
$leaders = mysql_query($leaders_query);
$id = $row['id'];
mysql_query("INSERT IGNORE INTO friends (node1id, node2id, is_leader, friends_since, friend_type)
VALUES('$id', '$userid_generator', 'Yes', '$date', 'full)")or die(mysql_error());
}
}
?>
由于某种原因,while循环似乎不起作用。当我在while循环中插入内容时,它会留下一个空白屏幕。有什么帮助吗?
答案 0 :(得分:2)
你的逻辑有点不稳定。在这种情况下不需要while循环(使用一个只能产生最多单行数据的东西是货物崇拜编程的证据)。逻辑流程应该是:
$sql = "...";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result);
$_SESSION stuff
} else {
... create user record ...
}
同样,SELECT * FROM Leaders
查询有什么意义?你实际上并没有从那个查询中获取任何东西,所以基本上它是一个无用的操作。
答案 1 :(得分:1)
代码的逻辑错误:你的if(mysql_num_rows(...))应该在while(mysql_fetch_array(...))循环之前。 尝试这样的事情:
<?php
include("global-settings.php");
session_start();
mysql_connect($dbhost, $dbuser, $dbpass)or die("Could Not Connect: " . mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$email = mysql_real_escape_string(strip_tags($_POST['email']));
$password = mysql_real_escape_string(strip_tags(sha1($_POST['password'])));
$result = mysql_query("SELECT * FROM users WHERE username='$email' AND password='$password'") or die(mysql_error());
if(mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result);
$_SESSION['user_pid'] = $row['user_pid'];
$_SESSION['firstname'] = $row['first_name'];
$_SESSION['lastname'] = $row['last_name'];
header("Location: ../protected/home.php");
} else {
$userid_generator = uniqid(rand(), false);
$date = date("Y/m/d");
$res_insert = mysql_query("INSERT INTO users (user_pid, username, password, datetime_registered)
VALUES('$userid_generator', '$email', '$password', '$date'); SELECT MAX(user_pid) FROM users;") or die(mysql_error());
$_SESSION['userid'] = $userid_generator;
$_SESSION['firstname'] = $row['first_name'];
$_SESSION['lastname'] = $row['last_name'];
$leaders_query = "SELECT * FROM leaders";
$leaders = mysql_query($leaders_query);
$id = mysql_insert_id($res_insert);
mysql_query("INSERT IGNORE INTO friends (node1id, node2id, is_leader, friends_since, friend_type)
VALUES('$id', '$userid_generator', 'Yes', '$date', 'full)") or die(mysql_error());
}
?>
答案 2 :(得分:0)
可能结果集为空,你可以在执行do while时更改de,因此它将至少执行一次。
它是这样的,你试图计算一下,如果没有行,如果没有,它将永远不会通过那里。
答案 3 :(得分:0)
<?php
session_start();
include("global-settings.php");
mysql_connect($dbhost, $dbuser, $dbpass)or die("Could Not Connect: " . mysql_error());
mysql_select_db($dbname) or die(mysql_error());
$email = mysql_real_escape_string(strip_tags($_POST["email"]));
$password = sha1($_POST["password"]);
$sql = "SELECT * FROM users WHERE email = '{$email}' AND password = '{$password}'";
$result = mysql_query($sql) or exit("ERROR: " . mysql_error() . "<br>IN QUERY: " . $sql);
if (mysql_num_rows($result) > 0) {
$row = mysql_fetch_array($result);
$_SESSION["userid"] = $row['user_pid'];
echo "logged in";
} else {
$userid_generator = uniqid(rand(), false);
mysql_query("INSERT INTO users (user_pid, email, password, datetime_registered, is_leader) VALUES ('$userid_generator', '{$email}', '{$password}', NOW(), 'no')");
$id = mysql_insert_id();
$leaders = mysql_query("SELECT * FROM users WHERE is_leader LIKE '%yes%'");
while($rows = mysql_fetch_array($leaders)) {
if ($rows['is_leader'] == 'yes') {
$leader_id = $rows['user_pid'];
mysql_query("INSERT IGNORE INTO friends (node1id, node2id, friends_since, friend_type)
VALUES('$leader_id', '$userid_generator', NOW(), 'full')");
$_SESSION["userid"] = $userid_generator;
echo "new user created and logged in";
}
if(is_dir($userid_generator)) {
echo "Something wen't wrong. A bug report has been sent and we are doing what we can to fix it.";
$message = 'Registration problem on account number $userid_generator. The user succesfully registered, but there is already
a directory with the account id of $userid_generator.';
mail($bug_report_email, "Registration Bug!", $message);
} else {
mkdir('../media/User-PID{' . $userid_generator . '}', 0777);
mkdir('../media/User-PID{' . $userid_generator . '}/photos', 0777);
mkdir('../media/User-PID{' . $userid_generator . '}/backups', 0777);
mkdir('../media/User-PID{' . $userid_generator . '}/videos', 0777);
mkdir('../media/User-PID{' . $userid_generator . '}/documents', 0777);
mkdir('../media/User-PID{' . $userid_generator . '}/developer', 0777);
mkdir('../media/User-PID{' . $userid_generator . '}/developer/apps', 0777);
mkdir('../media/User-PID{' . $userid_generator . '}/developer/themes', 0777);
mkdir('../media/User-PID{' . $userid_generator . '}/xml', 0777);
}
}
}
?>
我实际修好了它。谢谢大家的帮助!