MYSQL Update and add a value into cell - php

hello friends I want to update a cell in a table whit new content, but retaining the content that has. I need to add the new data separated whit comma or line break (is'nt relevant), preserving the data that have inside.
|id | title | referer|
|--------------------|
| 1 | post1 | google |
| 2 | post2 | yahoo |
| 3 | post3 | bing |
| 4 | post4 | google |
The table should look like this with new content added.
|id | title | referer |
|-------------------------------|
| 1 | post1 | google,yahoo,bing |
| 2 | post2 | yahoo,bing,etc |
| 3 | post3 | bing |
| 4 | post4 | google,google |
this is my code to insert new rows in my Database:
mysql_query("INSERT IGNORE INTO posts (id,title,referer) VALUES (null,'$q','$referer')");
What would be the code to add information in the cell "referer" whit Comma separated or line break ?
thanks in advance

I am not sure about what you might do with how separate them with commas unless you will want to create an array of variables and concat them into a single variable and use mysql's CONCAT function to update your table in order to keep your last field data:
$referes_list = '';
foreach ($referers as $referer) {
$referes_list .= $referer . ", ";
}
UPDATE referers SET referer=CONCAT(referer,'$referes_list') WHERE id='$id' ;

You'll have to forgive me if my MySQL skills aren't the greatest, but what I would do here is simply get the current value, append a comma and new value to it, then update the cell.
...Or did I misinterpret your question?

In mysql there isnt "comma separated type", that is just a simple string or VARCHAR, so, you will need get previous data and concatenate it like string in php and then update it.

I think you are looking for something like:
update t
set referrer = (case when referer is null or referer = ''
then #newvalue
else concat(referer, ',', #newvalue)
end)
where id = #id;

mysql_query('UPDATE tablename SET referer = concat(referer,", '.$value.'") where id="'.$id.'")';
this should do it, it's untested though.

Related

How can I copy the content of one column to another one?

I have following table structure:
// Posts
+----+---------+-----------------+------------------+------------------------+------------------------+
| id | title | content_html | content_markdown | content_edit_html | content_edit_markdown |
+----+---------+-----------------+------------------+------------------------+------------------------+
| 1 | title1 | <b>content1</b> | **content1** | <b>content1_edited</b> | **content1_edited** |
| 2 | title2 | <i>content2</i> | *content2* | | |
+----+---------+-----------------+------------------+------------------------+------------------------+
As you see in the above table, I just keep two last edit (content(html/markdown) is one before the last edit or the content without editing and content_edit(html/markdown) is last edit or empty if there isn't any edit)
Now please take a look at these two examples:
Example One: I want to edit first post which is edited already. Here is new value:
**content1_new_value**
Well I also convert it to html using a PHP library and make this:
<b>content1_new_value</b>
I want to fill (update) the content of content_(html/markdown) with the current content of content_edit_(html/markdown) and fill (update) this column content_edit_(html/markdown) with new value, something like this:
+----+---------+------------------------+----------------------+----------------------------+------------------------+
| id | title | content_html | content_markdown | content_edit_html | content_edit_markdown |
+----+---------+------------------------+----------------------+----------------------------+------------------------+
| 1 | title1 | <b>content1_edited</b> | **content1_edited** | <b>content1_new_value</b> | **content1_new_value** |
+----+---------+------------------------+----------------------+----------------------------+------------------------+
Example Two: I want to edit second post which is not edited already. Here is new value:
*content2_new_value*
Well I also convert it to html using a PHP library and make this:
<i>content2_new_value</i>
In this case I want to keep content_(html/markdown) intact (without any change) and just update content_edited_(html/markdown) with new value, something like this:
+----+---------+-----------------+------------------+------------------------+--------------------------+
| id | title | content_html | content_markdown | content_edit_html | content_edit_markdown |
+----+---------+-----------------+------------------+------------------------+--------------------------+
| 2 | title2 | <i>content2</i> | *content2* | *content2_new_value* | <i>content2_new_value</i>|
+----+---------+-----------------+------------------+------------------------+--------------------------+
So as you see, I just keep the two last edit. How can I do that?
Check CASE WHEN ELSE END
case
when content_edit_html is not null
update table set content_html = content_edit_html, content_markdown = content_edit_markdown,
content_edit_html = <b>content1_new_value</b>, content_edit_markdown = **content_new_value**
where id = 1
else
update table set content_edit_html = <b>content1_new_value</b>, content_edit_markdown = **content_new_value**
where id = 1
end

delete record from mysql using php by comparing data in coloumn

I am trying to delete a record from database and I am using this code.
$value = $_POST['name'];
$sql="DELETE FROM savedemail WHERE email='$value'";
This code is deleting records. But with the indexes like if I enter 3 it deletes the record of third row not by matching the content of the database.
I want to delete a record by matching the data inside the column with my given $values variable.
You want to use LIKE
$sql="DELETE FROM savedemail WHERE serial LIKE '%$value%'";
So if $value is equal to blah then it will delete all the rows that contain the word blah. The blah could be anywhere inside of the serial column field.
Read more here: http://dev.mysql.com/doc/refman/5.7/en/pattern-matching.html
Pattern Matching
[..] “%” to match an arbitrary number of characters [..]
To find names containing a “w”:
mysql> SELECT * FROM pet WHERE name LIKE '%w%';
+----------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+------------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
+----------+-------+---------+------+------------+------------+

Get column value with same row id

I got this MYSQL table:
+---------------+
|ID|OID| mpn |
+--+---+--------+
|1 | 1 | 12345 |
|2 | 1 | 54321 |
|3 | 2 | 78912 |
|4 | 2 | 12431 |
|5 | 2 | 78787 |
|6 | 3 | 14565 |
.................
Now I want to set the values [mpn] for each [OID] in an array.
Something like this:
$oid1 = array([mpn1], [mpn2])
$oid2 = array([mpn1], [mpn2], [mpn3])......
Or just manage to output the [mpn] referring to [OID]. So, I want to get every MPN referring to OID=1, for example. So, how do I select those values out of the table?
Here used COALESCE() and GROUP_CONCAT Functions.
The GROUP_CONCAT function concatenates strings from a group into one string with various options.
The COALESCE function evaluates the arguments in order and returns the current value of the first expression that initially does not evaluate to NULL.
select t.OID,COALESCE( GROUP_CONCAT( t.mpn) , '' ) as mpn
from tableName t
group by t.OID
If mpn have any null value then you can fill up this null value with any String or null. That u want to do.
I think you want group_concat():
select oid, group_concat(mpn) as mpns
from t
group by oid;
This creates the list of mon as a comma-delimited list which you can parse in php.

mysql query how to fill in blank rows using the value of its top row with content?

I need a mysql query to do this
But I can't think of something to make this happened
We have an excel file and that contains more than 5,000 rows
some are blank and others have value what I manually do is
to copy top value to its below blank fields and so on
so I think a way that I can import the excel into myslq database
and make some query to save effort and time in terms of doing this..
and this is my table looks like:
only column Date-paid has value for the entire rows but some has value and other don't have see this example but the original file is more than 5,000 rows
|Name | age | Salary| Date_paid |
-----------------------------------
bbjake| 16 | 200 | 03/25/2015 |
----------------------------------|
| | | 04/25/2015 |
----------------------------------
| | | 05/25/2015 |
-----------------------------------
kentot| 20 | 300 | 03/25/2015 |
-----------------------------------
| | | 04/25/2015 |
----------------------------------|
| | | 05/25/2015 |
-----------------------------------
and after running the query this is my desired result
|Name | age | Salary| Date_paid |
----------------------------------|
bbjake| 16 | 200 | 03/25/2015 |
-----------------------------------
bbjake| 16 | 200 | 04/25/2015 |
-----------------------------------
bbjake| 16 | 200 | 05/25/2015 |
-----------------------------------
kentot| 20 | 300 | 03/25/2015 |
-----------------------------------
kentot| 20 | 300 | 04/25/2015 |
-----------------------------------
kentot| 20 | 300 | 05/25/2015 |
-----------------------------------
So blank fields will be filled in with the value on the raw with content and
drag down until with another raw with value in short will fill in blank rows with
the content of top row
table is manually imported into the database and let just say columns are only three Name(varchar) Age(int) Salary (int) Date Paid (Date) Table name = "Emplyoee" Database name = "Company"
your question is strange...why would somebody wanna do this?
Anyway, to do this, it's interesting, how this table is created / generated. Your question is very weird...i think you want to know, how to fill this table AFTER the server built it and created the HTML table (because of your post title here). On the other side, this "php" tag does not make any sense in this scope....
JavaScript solution:
var trs = document.getElementsByTagName('tr');
var innerHTML = "";
for (var i = 0; i < trs.length; i++){
if (trs[i].className !== "heading") {
innerHTML = (trs[i].innerHTML.children[0] == "") ? innerHTML : trs[i].innerHTML;
trs[i].innerHTML = innerHTML;
}
}
note that you have to set the classname of the description rows to "heading" in this example, that this will not be interpreted from the script. If your server alone shall do this, just follow the same logic like in my JavaScript. Always check, if next element would be empty - if yes: fill in the same string like before; if not: take the new string and fill it into all next empty cells, etc....

Remove particular id from database field

Am storing a string separated using |, which lists the groups allowed, now my issue is if I delete a group than I am not able to remove the ID from that particular field, for example
allowed_group_id
+---------------+
1332|4545|5646|7986
So for example am deleting the group say no 5646, so how do I alter the scripts and remove that particular group from the allowed_group_id in script table?
I recommend taking the entry, exploding it by "|", removing the appropriate entry, imploding it back and updating.
$allowedGroupId = '1332|4545|5646|7986';
$parts = explode('|', $allowedGroupId);
if(($key = array_search($deleteGroup, $allowedGroupId)) !== false) {
unset($allowedGroupId[$key]);
}
$update = " ... "; //update with the new imploded values
Hope it helps
You can try this-
update table tableName set allowed_group_id = REPLACE(allowed_group_id, '5646', '');
Use explode:
$allowed_ids = explode('|', $current_allowed_group_ids);
if($remove_key = array_search($remove_id, $allowed_ids) !== false) {
unset($allowed_ids[$remove_key]);
}
$update_query = 'UPDATE table_name SET allowed_group_id = "'. implode('|', $allowed_ids) .'" WHERE id= ...';
But you might want to alter your database design slightly, creating a pivot table to check for allowed ids. Example:
+------------+
| GROUPS |
+----+-------+
| id | name |
+----+-------+
| 1 | grp_1 |
| 2 | grp_2 |
...
+--------------------+
| ALLOWED_GROUPS |
+--------------------+
| user_id | group_id |
+---------+----------+
| 2 | 1 |
| 2 | 2 |
| 5 | 2 |
...
Using Suresh response and improving it:
UPDATE TABLE tableName SET allowed_group_id = REPLACE(
REPLACE(allowed_group_id, '5646', ''),
'||',
'|');
First you search for '5646' string in allowed_group_id and replace it with empty string ''. Secondly you search and replace two bars from your result '||' with only one bar '|'. This will avoid having '1332|4545||7986' in your allowed_group_id.
Or you should have a table with 2 columns 1 with the id of the action you whant to allow and one with the id of a group.
Whilst the other answers will solve your problem as-is: you might want to consider normalising your database.
For example, if you currently have a table table_name containing id and allowed_group_id, then you would create a new table allowed_group containing multiple rows for each allowed group:
foreign_id | group_id
-----------+---------
1 | 1332
1 | 4545
1 | 5646
1 | 7986
...and so on. Here, foreign_id is the ID of the row in your existing table_name table. Now, instead of your above problem, you can simply DELETE FROM allowed_group WHERE foreign_id = 1 AND groupId = 5646.
This is called putting your data in first normal form.

Categories