0

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

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

这是我的代码:

<?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);
4

2 回答 2

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 ");
}
于 2013-04-29T00:50:23.297 回答
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);
于 2013-04-29T00:50:11.543 回答