为什么我不能使用此代码删除数据?

时间:2012-04-12 09:45:36

标签: php xampp

我用php和xampp写了一个笔记本,不知何故我的删除功能不起作用,有人能找出问题吗?

admin   %   global  ALL PRIVILEGES  Yes     Edit Privileges Edit Privileges
database-specific   ALL PRIVILEGES  No  Edit Privileges Edit Privileges
admin   localhost   global  ALL PRIVILEGES  Yes     Edit Privileges Edit Privileges
database-specific   ALL PRIVILEGES  No  Edit Privileges Edit Privileges
root    127.0.0.1   global  ALL PRIVILEGES  Yes     Edit Privileges Edit Privileges
root    localhost   global  ALL PRIVILEGES  Yes     Edit Privileges Edit Privileges

 ALTER TABLE `note` CHANGE `id` `id` INT( 100 ) NOT NULL AUTO_INCREMENT ,
CHANGE `topic` `topic` VARCHAR( 1000 ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL ,
CHANGE `topicoutline` `topicoutline` VARCHAR( 2000 ) CHARACTER SET latin1 COLLATE latin1_swedish_ci NOT NULL ,
CHANGE `timestamp` `timestamp` TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00'

  <?php
    $userName = "admin";
    $password = "admin";
    $hostname = "localhost";
    $databaseName = "notebook";
    $tableName = "note";
    $myDB = mysql_connect("localhost", $userName, $password);

if ($myDB) { print "Database Connect succeeded<p>"; }
else { print "Database Connect failed<p>"; die(1);}

//connect and astart using the database

$status = mysql_select_db($databaseName, $myDB);
if ($status) 
{ echo "Select Database $databaseName succeeded<p>"; }
else 
{ echo "Select Database $databaseName failed<p>";}

#$result = mysql_query('Select * from note', $myDB );

 #while ($row = mysql_fetch_row($result)) {

    #echo $row[0] . ", "; #display results
    #echo $row[1] . ", ";
    #echo $row[2] . ", ";
    #echo $row[3] . "<p>";
#}
#mysql_free_result($result); # release memory
#mysql_close($myDB);

  function AddTopic() {
      global $myDB, $this_script;

      if (isset ($_POST['topic']))
      $topic = $_POST['topic'];
      else 
      return;

      if (isset ($_POST['topicoutline']))
      $topicoutline = $_POST['topicoutline'];
      else 
      return;

      if($topic == "" || $topicoutline == "")
      return;



      $query = "insert into note (topic, topicoutline, timestamp) values('$topic', '$topicoutline', now());";
      $result = mysql_query($query, $myDB);

      if (!$result) mysql_error($myDB);
      else header("Location: $this_script"); #redirect to Show all
      }

  function ShowAll() {
       global $myDB;
      $query="Select * from note;";
      $result = mysql_query($query, $myDB );
    while ($row = mysql_fetch_object($result)) {
    echo $row->id . "<br>";
    echo $row->topic . "<br>";
    echo $row->topicoutline . "<br>";
    echo $row->timestamp . "<p>";
    }       

     mysql_free_result($result);
    }

    function td($item){
    return "<td>$item</td>\n";
    }

    function linktodelete($url, $prompt){

    return "<a href = '$url'>$prompt</a>";

    }

    function FrontPage() {

       global $myDB, $this_script;
       deleteTopic();
       AddTopic();

    if (isset($_REQUEST['findthis'])) 
    $where = $_REQUEST['findthis'];
    else $where = '';
    $query = 'Select * from note';
    if ($where) 
    $query = $query . " where topic like '%$where%' or topicoutline like '%$where%'";
    $query = $query .";" ;
    $result = mysql_query($query, $myDB );
    print "<table border='1' bgcolor='#AADDFF' width='80%'>\n";

    print "<tr><td>id</td> <td>topic</td> <td>topicoutline</td> <td>timestamp</td></tr> \n";

    while ($row =mysql_fetch_object($result)) {
    $id = $row->id;
    print "<tr>";

    print td($row->id). td($row->topic). td($row->topicoutline). td($row->timestamp). td(linktodelete($this_script . "?cmd=del&id=$id","Delete"));
    print "</tr>\n";

    }

    print "<table>\n";
    mysql_free_result($result);

    print DisplayFindForm();
    print DisplayNameAdditionForm();
    }
  //showAll();

  function deleteTopic() {
  global $myDB, $this_script;
  if (isset($_REQUEST['$id']))
    $id = $_REQUEST['id'];
    else
    $id = '';
    $query = "delete from note where id='$id';";
    $result = mysql_query($query, $myDB);
  if (!$result) mysql_error($myDB);
  else header("Location: $this_script");
  }

    function DisplayNameAdditionForm() {
    global $this_script;
    print "
    <hr>
    <form method='POST' action='$this_script'>
    <input type='hidden' name='cmd' value='add'>
    topic <input type='text' name='topic' size='100'>
    topicoutline <input type='text' name='topicoutline' size='100'>
    <input type='submit' value=' Add ' name='B1'>
    </form>
    ";
    }


    function DisplayFindForm() {
    global $this_script;
    print "
    <hr>
    <form method='POST' action='$this_script'>
    <input type='hidden' name = 'cmd' value='find'>
    <input type='text' name = 'findthis' size='20'>
    <input type='submit' value=' Find name ' name='B1'>
    </form>
    ";
    }

  FrontPage();

  ?>

1 个答案:

答案 0 :(得分:1)

if (isset($_REQUEST['$id']))
                     ^ 

必须删除美元符号。

相关问题