单击按钮不起作用时,删除MySQL中的表数据

时间:2013-04-29 00:40:03

标签: php mysql

当用户点击按钮时,不会从表中删除数据。

我有两个按钮,其中一个用于删除mysql tabel中的数据,当我按下按钮时,它会调用数据。尽管其他查询工作正常,但数据仍在表中。

这是我的代码:

<?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 个答案:

答案 0 :(得分:1)

}之前加if(isset($_POST['delete']))并在}之前删除//print_r($arr)."<br>";

实际上,您的代码缺少一个},因此您可能需要在{if“行之前放置}}

下次尝试更好地格式化代码,请:)

<强>更新

<?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 ");
}

答案 1 :(得分:0)

试试这段代码:

  $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);