Jump to content

Problems with an array updating a db table


Sabmin

Recommended Posts

I have a text field in my database that I'm pulling the contents of and parsing into an array, at which point depending on the users action I'm either adding to the end or searching for a specific value and removing it.  I'm then converting the array back to a string and placing it back into the text field in the database.

 

Everything works fine except for either an empty array value or a new line forcing its way to the front. Causing, naturally an empty value loaded onto the page that displays this part of the db.

 

While I know theres a couple things in there that aren't necessary I was just too lazy to take them out before posting after attempting to find a way around this problem. But here is the bulk of the code.  I appreciate any help!

		$to_add = "SELECT * FROM teams WHERE name = '$teamname'";
		$que_to_add = mysql_query($to_add);
		while ($fetch_members = mysql_fetch_array($que_to_add, MYSQL_BOTH)) {
			$members = explode("\n", $fetch_members['members']);
			$members = array_filter($members);
			foreach ($members as $key => $value) {
				$value = trim($value);
				$new_insert[] = $value;
			}
		}
		$new_insert = array_filter($new_insert);
		foreach ($new_insert as $key => $value) {
			if ($value != "") {
				$to_insert = ($to_insert . "\n" . $value);
			}
		}
		$to_insert = ($to_insert . "\n" . $for_insert);
		$insert_ready = "UPDATE teams SET members = '$to_insert' WHERE name = '$teamname'";
		$insert = mysql_query($insert_ready);

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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