Ok.. at first I wanna try to put "include.php" on the php.. but it goes around and around.... this is the case...

if ($v_name=="")

$query="DELETE FROM key_show WHERE code='$v_name'";

else{


 $query="insert into key_show(code) values('$v_name')";

mysql_query($query)  or die(mysql_error());




}

The problem here..the system keep inserting data...it shouldn't be like that yea..it supposed to delete the data if the array is equal..

Anyone please help me .. thanks...

Dani AI

Generated

The behaviour seen here came from testing the wrong condition and from using the old mysql_* flow without guarding the input or the DB side. was right to ask you to inspect the value first, fixed the braces, and pointed you toward an existence check — that solved the immediate problem. A few practical, safer steps to make this robust and future‑proof follow.

Use a prepared, transactional approach so a single request either deletes the matching row or inserts it if none existed. Trim and validate the incoming value first, then run a parameterised delete and check how many rows were affected; if none were removed, insert. This avoids building SQL with raw input and reduces race windows when wrapped in a transaction.

$code = isset($_POST['code']) ? trim($_POST['code']) : '';
if ($code === '') exit; // nothing to do

$pdo->beginTransaction();
$stmt = $pdo->prepare('DELETE FROM key_show WHERE code = :code');
$stmt->execute([':code' => $code]);

if ($stmt->rowCount() === 0) {
    $stmt = $pdo->prepare('INSERT INTO key_show (code) VALUES (:code)');
    $stmt->execute([':code' => $code]);
}
$pdo->commit();

Also add a UNIQUE index on code at the database level to prevent duplicates and make logic simpler: ALTER TABLE key_show ADD UNIQUE (code);. With that in place you can also attempt an INSERT and, if it fails with a duplicate key, perform the delete; or use INSERT IGNORE and check affected rows — both patterns reduce extra SELECTs.

Final notes: always validate/sanitize $_POST input (trim, allowed chars), log failures, and avoid the deprecated mysql* extension — migrate to PDO or mysqli (see the PDO manual and PHP migration notes for removal of mysql*).

Recommended Answers

All 13 Replies

What is

$v_name

supposed to be?

my input data... I mean from a form...

Tried debugging the variable contents with

var_dump($v_name);

It is most likely that the variable is not empty, etc.

i think I got the problem ... the array should not be empty ...
I need to use IF EXIST...

Ok this is new for me... what is the way to tell if the array is exist ?

is_array

should do

Your Array is empty also your if statement is opened correctly you had missed the first set {} from the IF

if ($v_name==""){
    $query="DELETE FROM key_show WHERE code='$v_name'";
    }else{
    $query="insert into key_show(code) values('$v_name')";
    mysql_query($query) or die(mysql_error());
    }
Member Avatar for Member #671080

As Squidge pointed out, but also your query is only run during the ELSE part of the loop. You may want to place it after the last }

Still :(... It wont delete the first information that I key in .. keep looping ... where it should delete the first one ...

Member Avatar for Member #671080

Before the IF statement, try echo "The variable is: " . $v_name, as matejkramny suggested, to make sure it is what you expect it to be.

if ($v_name==""){
 $query="DELETE FROM key_show WHERE code='$v_name'";
 }else{
 $query="insert into key_show(code) values('$v_name')";
 }
 mysql_query($query) or die(mysql_error());

This code will only delete a record if $v_name is blank. Is that what you want?

v_name not supposed to be empth .. let say I key in "1234" .... and in the database have "1234" ... it will delee "1234"... else... if the v_name is empty ... it will insert "1234"....something like that I supposed

Oh yea by the way, before that I've set the arrays
$v_name=$_POST['code'];

Member Avatar for Member #671080

Ok, I think I understand what you want. If there is a record that matches "1234" delete it, if no record matches "1234" insert it?

Try this

$sql="SELECT * FROM key_show WHERE code='$v_name'";
$result = mysql_query($sql);  // Try to get matching row.
if (mysql_num_rows($result) == 1){ // If there is a matching row delete it.
    $query="DELETE FROM key_show WHERE code='$v_name'";
}else{ // ELSE if no matching row insert it.
    $query="insert into key_show(code) values('$v_name')";
}
mysql_query($query) or die(mysql_error());

My Gosh!!.. hehe.. it works... Thanks Lotsssss.. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.