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

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 Zagga

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 Zagga

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 Zagga

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.