使用if / else语句在同一页面上列出,编辑和删除的页面

时间:2013-05-07 09:26:56

标签: php

我是php的新手,我必须设计一个名为fixtures.php的页面。在此页面中,列表,编辑和删除使用if / else语句在同一页面上进行。

这是我到目前为止的fixtures.php页面。任何帮助表示赞赏。          

<?php

    include ('connect.php');

       $user_name="root";
       $password="";
       $database="team_management_db";
       $server="127.0.0.1";

       $db_handle = mysql_connect($server, $user_name, $password);
       $db_found = mysql_select_db($database, $db_handle);

       if($db_found){
       echo "Database found";
       }

    $fixture_id=$_GET['fixture_id'];
    $submit=$_POST['update'];
    $update=$_GET['fixture_id'];
    $delete=$_GET['fixture_id'];

    $SQL = "SELECT * FROM fixtures";
    $result = mysql_query($SQL);

    echo "<table border='1'><tr bgcolor='#cccccc'><td>Opponents</td><td>Date</td><td>Venue</td><td>Action</td>";

        while($row = mysql_fetch_assoc($result)) {
        $fixture_id=$row['fixture_id'];
        $opponents=$row['opponents'];
        $date=$row['date'];
        $venue=$row['venue'];

      echo "<tr><td>$opponents</td><td>$date</td><td>$venue</td><td><a href=fixtures.php?action=update&fixture_id=. $update .>Edit</a>
      </td><td><a href=fixtures.php?action=delete&fixture_id= . $delete . >Delete</a></td></tr>";

        }
    echo "</table>";



    if($update)
    {
?>
      <form action=$_PHP_SELF method=POST>
      <table border='1'><tr bgcolor='#cccccc'><td>Opponents</td><td>Date</td><td>Venue</td></tr>       
      <tr><td><input type='hidden' name='fixture_id' />
              <input type='text' name='opponents' />
              <input type='text' name='date' />
              <input type='text' name='venue' />
              <input type='submit' name='update' value='update' />
          </td>
      </tr>   
      </form>
<?php
  }
?>

<?php    
    if($delete)
    {
      echo "Are you sure you want to delete $fixture?<a href=fixtures.php?delete=absolutely&fixture_id=$fixture_id>Yes</a>&nbsp;
      &nbsp;<a href=fictures.php>No</a>";

      if($delete=='absolutely')
      {
        $sql = "DELETE FROM fixtures WHERE fixture_id = $fixture_id";
      }
    }       
    elseif($submit=='update')
    {
        $fixture_id=$_POST['fixture_id'];
        $opponents=$_POST['opponents'];
        $date=$_POST['date'];
        $venue=$_POST['venue'];

        $sql = "UPDATE FROM fixtures SET opponents =$opponents, date =$date, venue =$venue 
        WHERE fixture_id = $fixture_id";
        mysql_select_db('team_management_db');
        $retval = mysql_query($sql, $db_handle);
        if(!$retval)
        {
          die('Could not update data: ' .mysql_erroe());
        }
        echo "Updated data successfully\n";
        mysql_close($db_handle);
    }

    else
    {
      echo "<table border='1'><tr bgcolor='#cccccc'><td>Opponents</td><td>Date</td><td>Venue</td></tr>";

        while($row = mysql_fetch_assoc($result)) {
        $fixture_id=$row['fixture_id'];
        $opponents=$row['opponents'];
        $date=$row['date'];
        $venue=$row['venue'];

    echo "<tr><td>$opponents</td><td>$date</td><td>$venue</td></tr>";
        }
    echo "</table>";
    }

?>

</body>
</html>

2 个答案:

答案 0 :(得分:2)

改变 -

<form action=$_PHP_SELF method=POST>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">

修改

甚至这也有效(但为了正确的可读性,请使用1st)

<form action=<?php echo $_SERVER['PHP_SELF']; ?> method=POST>

还有一件事要补充 -

不要使用mysql_尝试抓住mysqli_PDO进行数据库处理。

我相信你应该阅读PHP手册以获取PHP HTML mix的概念

开始学习的好地方http://www.php.net/manual/en/ -

答案 1 :(得分:0)

在一个脚本中列出,更新和删除:使用$do$id控制脚本流 我会用......

  • PDO &amp; 准备好的陈述而不是旧的mysql_ * - &gt;将$db定义为包含文件中的PDO
  • 仅限'opponents'以保持代码简短
  • $_POST 以确保上述安全
  • 更新时用户输入的基本验证。

我们走了:

<?php
// define $do...
if (isset($_POST['submit'])) $do = $_POST['submit'];
elseif (isset($_POST['do'])) $do = $_POST['do'];
else $do = "list";    

// possible states of $do...
// $do == "update" --> display update-form 
// $do == "delete" --> display delete-form
// $do == "perform_upd" --> validate and perform update
// $do == "perform_del" --> perform delete
// $do == "list" --> display list  

// define $id
if (isset($_POST['id'])) $id = $_POST['id']; else $id = -1;

switch ($do) {

case "perform_upd":    
    // validate user-input...
    if (strlen(trim($_POST['opponents'])) == 0)
        $error[] = "Please fill in opponents!";
    // validate other fields, too...

    if (isset($error)) $do = "update"; // validation found errors --> back to update-form!
    else { 
        $sql = "UPDATE fixtures SET opponents=:opp WHERE id=:id LIMIT 1";
        $bind = array(
            ':opp' => $_POST['opponents'];
            ':id'  => $id);
    }
    break;

case "perform_del":
    $sql = "DELETE FROM fixtures WHERE id=:id LIMIT 1";
    $bind = array(':id' => $id);
    break;

default: // $do = list, update or delete
    $sql  = "SELECT * FROM fixtures";
    $bind = array();
    if ($do == 'update' || $do == 'delete') {
        $sql .= " WHERE id=:id";
        $bind = array(':id' => $id);
    } // if
} // switch

// prepare and execute database-operation
$stmt = $db->prepare($sql);
$stmt->execute($bind);
if (in_array($do, array('list','update','delete'))) 
    $fixtures = $stmt->fetchAll(PDO::FETCH_ASSOC); // read data into array $fixtures
else {
    header("Location: yoururl"); // set yoururl to your script --> redirect and show list
    exit();
}
<body> ...

<?php
switch ($do): // same game again...

case "list":
    // display the list...
    $h ='';
    foreach ($fixtures as $f) {
        $h .= "<form method=\"POST\”>$f[opponents], $f[date], $f[venue]";
        $h .= "<input type=\"submit\" name=\"submit\" value=\"update\" />";
        $h .= "<input type=\"submit\" name=\"submit\" value=\"delete\" />";
        $h .= "<input type=\"hidden\" name=\"id\" value=\"$f[id]\" />";
        $h .= "</form><br />";
    }
?>        
<h1>List</h1>
<?php=$h?>
<? php
    break; // list

case "update":
    // display update form
?>
    <form method="POST">
    <!-- if validation found errors, they are displayed here -->
    <?php=(isset($error)?'There were errors: ' . implode(", ", $error):'')?>
    <input type="text" name="opponents" value="<?php=$fixtures[0]['opponents']?>" />
    <input type="submit" name="update" value="update" />
    <input type="hidden" name="do" value="perform_upd" />
    <input type="hidden" name="id" value="<?php=$id?>" />
    </form>
<?php
    break; // update

case "delete":
    // display delete form
?>
    <form method="POST">
    Do you really want to delete <?php=$fixtures[0]['opponents']?>?
    <input type="submit" name="delete" value="delete" />
    <input type="hidden" name="do" value="perform_del" />
    <input type="hidden" name="id" value="<?php=$id?>" />
    </form>
<?php
    break; // delete
} // switch
?>