0

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

Edited by trektrak

4
Contributors
13
Replies
14
Views
5 Years
Discussion Span
Last Post by trektrak
Featured Replies
  • 1

    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 … Read More

0

Tried debugging the variable contents with

var_dump($v_name);

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

0

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 ?

0

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());
    }

Edited by Squidge

0

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 }

0

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

0

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?

0

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

0

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

1

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());
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.