如何在PHP / mysql中从我的数据库中删除?

时间:2013-11-20 07:33:50

标签: php html mysql mysqli

大家好,我有很多php文件的实现。所有这些都有一些错误。我将首先道歉,因为这是我在这里的第一个问题,我确信我会做错,因为我看到许多初次参加的人。我将尽可能多地提供信息,并使其与尽可能多的人相关。

我有一个数据库,无法从中删除。数据库很简单。它包括resource_id名称房间描述time_available和uer_id。

虽然我希望它输出名称描述和resources_id,但它只输出名称和描述,它不会让我通过resources_id删除名称。

如何从PHP / mysql中的数据库中删除?

    This is my delete_resources.php
    {
    <html>
    <head>
    <title>Delete a Record from MySQL Database</title>
    </head>
    <body>

    <?php

    $db_host = "@@@@@@@";

    // Place the username for the MySQL database here

    $db_username = "@@@@@@@"; 

    // Place the password for the MySQL database here

    $db_pass = "@@@@@@@";

    // Place the name for the MySQL database here

    $db_name = "@@@@@@@";


    //
    $con = mysqli_connect("$db_host","$db_username","$db_pass","$db_name");

    if (mysqli_connect_errno())

      {

      echo "Failed to connect to MySQL: " . mysqli_connect_error();

      mysqli_close($con);

      }

     $result = mysqli_query($con, "SELECT * FROM resources");


     echo 'name' . "\t" . 'description' . "\t" . 'resources_id';
     echo "<br>";

     while($row = mysqli_fetch_array($result))
    {
       echo $row['name'] . "\t" . $row['description'] . "\t" . $row['resources_id'];
       echo "<br>";
    }


    // Echoes: string

    echo gettype($array);

    //

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

      // Query to select an int column


        $resources_id = $_POST['resources_id'];

        $sql = "DELETE name From resources ".
               "WHERE resources_id = $resources_id" ;



    //mysql_select_db('b32_13993766_csc411');
    //$retval = mysql_query( $sql, $conn );
    if(! $result )
    {
      die('Could not delete data: ' . mysql_error());
    }
    else if( $result )
    {
        echo "Deleted data successfully\n";
    }

    //mysql_close($conn);
    }
    else
    {
    ?>
    <form method="post" action="<?php $_PHP_SELF ?>">
    <table width="400" border="0" cellspacing="1" cellpadding="2">
    <tr>
    <td width="100">Resource ID</td>
    <td><input name="resources_id" type="text" id="resources_id"></td>
    </tr>
    <tr>
    <td width="100"> </td>
    <td> </td>
    </tr>
    <tr>
    <td width="100"> </td>
    <td>
    <input name="delete" type="submit" id="delete" value="Delete">
    </td>
    </tr>
    </table>
    </form>
    <?php
    }
    ?>
    </body>
    </html>
//
}

4 个答案:

答案 0 :(得分:3)

执行删除查询。应该看起来像

 $recources_id=intval($resources_id);
 $sql = "DELETE FROM resources WHERE resources_id = $resources_id" ;
 $result = mysqli_query($con, $sql);  // This is missing

答案 1 :(得分:1)

$sql_query="Delete from your_table_name where id ='".$your_id."'"; 

答案 2 :(得分:1)

$sql = "DELETE FROM resources WHERE resources_id = $resources_id" ;

答案 3 :(得分:-1)

您的$result与您的删除查询完全无关(它指的是上面的$ result,而不是删除的结果)。尝试更改为此,看看它是否有效。

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

  // Query to select an int column


    $resources_id = $_POST['resources_id'];

    $sql = "DELETE name From resources ".
           "WHERE resources_id = $resources_id" ;

    $result = mysqli_query($con, $sql); //add this line        

//mysql_select_db('b32_13993766_csc411');
//$retval = mysql_query( $sql, $conn );
if(! $result )
{
  die('Could not delete data: ' . mysql_error());
}
else if( $result )
{
    echo "Deleted data successfully\n";
}

//mysql_close($conn);
}