0

我是 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>
4

2 回答 2

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 组合的概念

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

于 2013-05-07T09:32:00.970 回答
0

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

  • PDO准备好的语句而不是旧的 mysql_* -->$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\”&gt;$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
?>
于 2013-05-07T12:01:45.973 回答