我想知道我是否可以让某人查看我的陈述,看看我可能会搞砸了。我已经测试了通过回显它发布了什么信息,一切似乎都正确地进行了,但我无法让它在物理上创建适当的记录。我也没有收到任何错误,它会回到标题位置,就像表单发布的那样。
//First we make sure there are enough licenses left to add the user
$limit = "select * from organization_seats WHERE orgid=$orgid";
$orglimit = mysql_query($limit);
$licenses = $orglimit['limit'];
$count = "select count(*) from organization_users WHERE organizationid=$orgid";
if ((!$licenses < $count)) {
echo 'You have reached the number of maximum licenses for your Organization.';
} else {
//If we have licenses left, proceed to add new user
//Populate the user table
$sql = "insert into user (firstname, lastname, title, address1, address2, country, city, state, zip, phone, mobile, birthday, username, email, password) values ('$fname','$lname','$title','$address1','$address2','$country', '$city', '$state', '$zip', '$phone', '$mobile', '$bday', '$username', '$email', '$password')";
$exec = mysql_query($sql);
//Add the user to the organization
$userid = mysql_insert_id(); //call the last ID entered into the user table first
$sql2 = "insert into organization_users(organizationid, userid, active) values ('$orgid', '$userid', $)";
$exec = mysql_query($sql2);
//recall the userid
$sql3 = "select * from user where username = $username";
$exec = mysql_query($sql3);
$newuserid = $newuserselect['id'];
//Add the user to the department
$sql4 = "insert into organization_dep_users(orgid, depid, userid) values ('$orgid', '$department', '$newuserid')";
$exec = mysql_query($sql4);
if ($exec === TRUE) {
header( 'Location: index.php' ) ;
} else {
echo mysql_error();
}
}
不过,我的mysql_real_escape_string附加了我的所有变量。
答案 0 :(得分:0)
1)$sql2
有错误 - 您正在传递$
而不是实际变量。
2)在$sql3
之后,您从不存在的资源中分配$newuserid
。我假设你在它之前缺少$newuserselect = mysql_fetch_assoc($exec);
。
3)您确实需要在查询中添加错误检查。如果第一个查询失败,则第二个查询将以错误的$userid
运行,如果之前的查询没有创建id,则可能FALSE
。其他问题可能会在您的代码中出现,而不会进行错误检查。
4)如上所述,建议过渡到pdo或mysqli。
5)刚刚注意到 - 您的第一个选择查询也试图滥用资源 - 您应该这样做
$orglimit = mysql_query($limit);
$orgrow = mysql_fetch_assoc($orglimit);
$licenses = $orgrow['limit'];
6)并且....你的$count
将不起作用,你将查询字符串分配给$count
但从未实际执行查询以获取数字。因此,当您执行if ((!$licenses < $count))
时,您实际上是在将数字与字符串进行比较,而不是将数字与数字进行比较。
答案 1 :(得分:0)
不确定你到底遇到了什么问题..但是如果你正确地复制了代码那么我发现了一个错误的陈述
insert into organization_users(organizationid, userid, active) values ('$orgid', '$userid', $)
什么是$
...?
<强>第二.. 强>
$limit = "select * from organization_seats WHERE orgid=$orgid";
$orglimit = mysql_query($limit);
$licenses = $orglimit['limit'];
应该是
$limit = "select * from organization_seats WHERE orgid=$orgid";
$resource = mysql_query($limit);
$orglimit = mysql_fetch_assoc($resource);
$licenses = $orglimit['limit'];
mysql_query总是返回一个resorce而不是数组..
与$ sql3相同
尝试更改这些,你应该没问题
建议:请开始使用mysqli_ *或PDO