how can we avoid duplicate values in mysql database [duplicate] - php

This question already has answers here:
How to avoid duplicates while updating a MySQL database?
(5 answers)
Closed 8 years ago.
I am storing number of check boxes with delete ,add,view options in database,
if you check on that options,it storing some values.But if i click again that will store again and again.
how to avoid this duplicate values

You can set the column containing unique value as UNIQUE or do a condition :
if SELECT Query return this value , don't insert , else insert.

Related

input multiple check box data to database [duplicate]

This question already has answers here:
What's the best way to store Checkbox Values in MySQL Database?
(3 answers)
Closed 2 years ago.
I created a form with multiple checkboxes to add to database, what do I need to change in the VALUE $_POST[penerima] to enter multiple checkboxes data into the database?
if(isset($_POST['simpan'])){
$simpan = mysqli_query($koneksi, "INSERT INTO umum (tanggal_terima, pengirim, no_surat, perihal, disposisi, penerima )
VALUES ('$_POST[tgl_terima]',
'$_POST[pengirim]',
'$_POST[no_surat]',
'$_POST[perihal]',
'$_POST[disposisi]',
'$_POST[penerima]')
");
I'm not going to address the SQL injection issues in the code, but please read:
How can I prevent SQL injection in PHP?
You can store array data in a database using json_encode($array) - this will convert your array of checkboxes to a string which can be stored. I suggest using the TEXT column type, otherwise, the sting may be cut off if your values are long.
https://www.php.net/manual/en/function.json-encode.php
You can then json_decode the data back to an array once it's selected.
https://www.php.net/manual/en/function.json-decode.php

How to update with concat and if statement? [duplicate]

This question already has answers here:
Is storing a delimited list in a database column really that bad?
(10 answers)
Closed 3 years ago.
I have a table with contact codes. When contact code is not specified there are ^^ in the empty cell. If specified should be: ^Code123^,^Code321^,^Code987^
I want to update this cell, but I dont know how to rewrite empty cell with ^^?
With my query I get this result ^^,^Code123^,^Code321^,^Code987^. How to delete ^^ when updating cell?
$sql6 = "UPDATE contacts
SET clicks_c=IF(clicks_c='',
'^".$url_name."^',
CONCAT_WS(',',clicks_c,'^".$url_name."^'))
WHERE id_c='".$row_id."'";
If an empty cell contains ^^, you need to test for that in your IF(). Change:
IF(clicks_c='',
to:
IF(clicks_c IN ('', '^^),
Then it will replace ^^ with a nonempty code, instead of concatenating to it.

Is there a way to add a row only if it doesn't already exist? [duplicate]

This question already has answers here:
"Insert if not exists" statement in SQLite
(4 answers)
Closed 9 years ago.
Using PHP PDO with SQLite, I can write a PDO call to check if a certain row exists (by checking for the primary key) in the table, and then if it doesn't, write another PDO call to create the row. But I feel like it should be somehow possible to do in one command… similar to the "CREATE TABLE IF NOT EXISTS". Is there anything like "INSERT ROW IF NOT EXISTS"?
Use
INSERT OR IGNORE INTO ...
where your table has some constraint (such as a PRIMARY KEY column) that will cause a conflict when a row is attempted to be inserted again.
Reference: http://www.sqlite.org/lang_conflict.html

Get inserted row ids in PHP and MySQL with multiple inserts [duplicate]

This question already has answers here:
Retrieving the last inserted ids for multiple rows
(2 answers)
Closed 9 years ago.
MySQL code is something like:
INSERT INTO table(name, value) VALUES ('name1', 'value1'), ('name2', 'value2'), ('name3', 'value3')
Basically, multiple inserts in the same SQL statement.
How can I get the IDs of the inserted values. I assume mysql_insert_id() combined with the number of inserts isn't safe since someone else might insert something at the same time.
Is there another way?
You either need to insert them one at a time or figure out the id's with a followup query.
INSERT INTO table(primkey, value) VALUES ('pk1', 'val1'), ('pk2', 'val2');
SELECT FROM table id, primkey where primkey in ('pk1', 'pk2');

Can I use get the just insert table row id, from mysql_query? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP: how to get last inserted ID of a table?
I use mysql_query() in PHP to insert a new record in my Database.
It will auto generate a row, with a user Id, can I use the result to get that Id?
mysql_insert_id()
Have you considered upgrading to PDO?

Categories