0

The data isn't being deleted from a table when user clicks on a button.

I have 2 buttons one of them to delete the data in mysql tabel which called data when I pressed the button. The data still in the table although other queries do work fine.

Here is my code:

<?php
$arr=array();
if(isset($_POST['add']))
{
 $x=$_POST['x']."<br/>";
 $y=$_POST['y']."<br/>";
 $con = mysql_connect("localhost","root","");
if(!$con)
{
    die('could not connect').mysql_errno();
}
else
{
 mysql_select_db("kmean",$con);
 $sql="INSERT INTO data (x, y)
VALUES
 ('$x','$y')";
if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }


$result = mysql_query("SELECT x,y FROM data");

while($row = mysql_fetch_array($result))
  {
  //echo $row['x'] . " " . $row['y']."<br/>";

 array_push($arr,array($row['x'],$row['y']));
  //print_r($r);
  echo "<br />";
  }

  if(isset($_POST['delete']))
  {
    mysql_select_db("kmean", $con);

    mysql_query("DELETE FROM data ");

  }

}
//print_r($arr)."<br>";
mysql_close($con);
2
  • 1
    #1, people need to stop use the mysql_* functions, people end up with too many security problems with. #2, hopefully this is testing code, selecting the same database over and over again is really not necessary. #3, you are checking if the delete variable is set, not that its true or 1, is this correct? Commented Apr 29, 2013 at 0:49
  • it's testing code not actual code that i would upload to webhost delete is name of button Commented Apr 29, 2013 at 1:05

2 Answers 2

1

Put } before if(isset($_POST['delete'])) and remove } before //print_r($arr)."<br>";

Actually, your code is missing one more }, so its possible that you'll need to put }} before that "if" line.

And try to format code better next time, please :)

UPDATE:

<?php
$arr=array();
if(isset($_POST['add']))
{
    $x=$_POST['x']."<br/>";
    $y=$_POST['y']."<br/>";
    $con = mysql_connect("localhost","root","");
    if(!$con)
    {
        die('could not connect').mysql_errno();
    }
    else
    {
        mysql_select_db("kmean",$con);
        $sql="INSERT INTO data (x, y) VALUES ('$x','$y')";
        if (!mysql_query($sql,$con))
        {
          die('Error: ' . mysql_error());
        }

        $result = mysql_query("SELECT x,y FROM data");

        while($row = mysql_fetch_array($result))
        {
            //echo $row['x'] . " " . $row['y']."<br/>";

            array_push($arr,array($row['x'],$row['y']));
            //print_r($r);
            echo "<br />";
        }

    }
    //print_r($arr)."<br>";
    mysql_close($con);
}

if(isset($_POST['delete']))
{
    mysql_select_db("kmean", $con);

    mysql_query("DELETE FROM data ");
}
Sign up to request clarification or add additional context in comments.

5 Comments

not working my php designer indicate that the syntax if correct and no missing or increased {}
Even simple search will show you that there are 6 "{" and 5 "}". I have updated my answer with the correct code for you to use.
there is no mistake in {} as i have 6{ and 6 } the last } is before end tag of php ?> so the problem is in query i think
Then you haven't pasted the whole code. Try the one I posted though. Problem is not in the query its in the position of the "if(isset($_POST['delete']))".
i'm sorry for not posting the whole code my mistake wher should be the postion of if(isset($_POST['delete']))
0

Try this code:

  $arr=array();

  if (isset($_POST['add'])) {

    $x=$_POST['x']."<br/>";
    $y=$_POST['y']."<br/>";

    $con = mysql_connect("localhost","root","");

    if(!$con) die('could not connect').mysql_errno();
    else {
      mysql_select_db("kmean",$con);

      $sql="INSERT INTO data (x, y)
            VALUES
            ('$x','$y')";

      if (!mysql_query($sql,$con)) die('Error: ' . mysql_error());

      $result = mysql_query("SELECT x,y FROM data");

      while ($row = mysql_fetch_array($result)) {

        array_push($arr,array($row['x'],$row['y']));
        echo "<br />";

      }

      if (isset($_POST['delete'])) {
        mysql_query("DELETE FROM data ");
      }
  }

  mysql_close($con);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.