View Full Version : Query doesn't add on

07-14-2008, 06:05 PM
Hi, I am trying to make a ban list, where you type in a users id to ban them. I wanted to seperate each user id with a comma, the first one records but doesn't keep adding on. It overwrites the first one. Here is the coding I am using.

$id = $_POST["id"];
$id = intval($id);

$close = $id .= ",";
$final = $close;

doquery("UPDATE {{table}} SET banlist='$final' WHERE blah blah

07-14-2008, 06:11 PM
What would be the value of $close in
$close = $id .= ",";, at the time of execution?

BTW, that expression is equivalent to


07-14-2008, 06:35 PM
$id would be the id that a user types in, so if the user they want banned is ID 100, then $id would become 100.

07-14-2008, 06:52 PM
I've done something similar in the past and I used explode.
Here's the code. Maybe it will help.

//Check values for blanks and invalid data
//Explode usernames to see if there are multiple
$banUser = explode(",",$banUser);

//Check if username exists
foreach($banUser as $user){
//Check so they don't ban themselves
if($user == $searchName){
die("You cannot ban your own account.");
userExistsS = "SELECT `user_name` FROM `{$prefix}_users`
WHERE `user_name`='$user'";
$userExistsQ = mysql_query($userExistsS);
$num = mysql_num_rows($userExistsQ);

if($num == 0){
$errors[] = "The username {$user} was not found";

//Add to DB if no errors are present
//Separate usernames
foreach($banUser as $user){
$banUserSql = "INSERT INTO `{$prefix}_banned_users` (`ban_id`,`user_name`,`ban_caption`,`ban_length`)
$banUsersQ = mysql_query($banUserSql) or die(mysql_error());
die("An error was encountered and the query failed.");
header("Location: {$installPath}/admin/index.php");

I've left out a few odds and ends (mostly variable initialization) but the main parts are there which I think you help you.

07-14-2008, 07:34 PM
I don't quite understand that one. Half of it I do, but some parts I don't.
Doesn't a period mean to "add on" to a database field?

07-15-2008, 08:54 AM
$id would be the id that a user types in, so if the user they want banned is ID 100, then $id would become 100.
If you are trying to concatenate the current $id value to the present value in the table, you'd need to use a mysql concat() (http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_concat) function, say

update table set field_name=concat(field_name, ',$id') where ...

07-15-2008, 03:37 PM
Hmm I will try that, it's suppose to look something like this in the database.

166,52,305,12,18, etc.

07-15-2008, 06:09 PM
Works well. Thank you

07-15-2008, 06:14 PM
Works well. Thank you Have you ever thought about the initial case (when there is nothing in that field)? Or isn't it OK to have an extra comma at the beginning?