如何创建一个重定向到另一个php文件的按钮

时间:2015-05-27 04:20:09

标签: php html mysql mysqli

我需要给下表一个“编辑”按钮,该按钮重定向到我称之为edit.php的文件,并随之发送一些信息。我基本上是在展示一个充满电影的桌子,当他们选择他们选择的电影旁边的编辑按钮时,我需要有关该电影的信息发送到下一页,所以我的编辑表格知道要编辑哪个记录。谁能告诉我怎么做?我是php的新手。

提前致谢

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM movies";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table><tr>
    <th>Title</th>
    <th>Year</th>
    <th>Studio</th>
    <th>Price</th>
    <th>Edit</th>

    </tr>";

    // output data of each row
    while($row = $result->fetch_assoc()) {
        echo"<tr><td>".$row['title']."</td>
        <td>".$row['year']."</td>
        <td>".$row['studio']."</td>
        <td>".$row['price']."</td>
        <td>""</td>

        </tr>";
    }


    echo"</table>";
} else {
    echo "0 results";
}

$conn->close();
?>
</body>
</html>

4 个答案:

答案 0 :(得分:0)

使用a标记并将其设置为按钮样式 -

<a href="edit.php?id=3" class="button-class">Edit</a>

CSS -

.button-class {
    border: 1px solid;
    background-color: green;
    text-decoration: none;
}

根据需要设置按钮的样式。这只是一个例子。

<强>更新

对于上面的代码,它应该是 -

<td><a href='edit.php?id=" . $thevaluetopass . "' class='button-class'>Edit</a></td>

答案 1 :(得分:0)

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "SELECT * FROM movies";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    echo "<table><tr>
    <th>Title</th>
    <th>Year</th>
    <th>Studio</th>
    <th>Price</th>
    <th>Edit</th>

    </tr>";

    // output data of each row
    while($row = $result->fetch_assoc()) {

    echo"<tr><td>".$row['title']."</td>
    <td>".$row['year']."</td>
    <td>".$row['studio']."</td>
    <td>".$row['price']."</td>
    <td><a href='edit.php?id=".$row['id']."'>Edit</td>

    </tr>";
}


echo"</table>";
} else {
    echo "0 results";
}
        $conn->close();
        ?>
        </body>
</html>

创建一个页面edit.php,其中通过$ _GET [&#34; id&#34;]和preform查询获取id SELECT * FROM电影,其中id = $ _ GET [&#34; id&#34;];

答案 2 :(得分:0)

只需要发送行的主键即可。这样您就可以获取有关该主键的数据。就像那样

<a href='edit.php?id=".$row['id']."'>Edit</a>

并在编辑时以这种方式获取数据:

$id = $_REQUEST['id'];
$sql = "SELECT * FROM movies WHERE id='$id'";
$result = $conn->query($sql);

此$ result变量包含该特定id的所有数据。现在根据您的需要进行操作。

答案 3 :(得分:0)

几年前我开始使用php。这篇文章太熟悉了。我花了一些时间来找到这些东西,但stackoverflow是学习php和MySQL的一个很棒的工具。还可以查看w3schools获取有关基本语法的大量信息。

如果您希望能够向任何人发送指向特定电影的链接,我建议使用$ _GET而不是$ _POST。如果没有,根据我的阅读,如果您担心人们操纵您的URL以获取数据库中的其他记录,则$ _POST至少会更安全一些。不是说它不能完成,它只是有点困难。以下是如何设置两种工作的示例。

第二,我的一个朋友教我这个。将您的第一个文件拆分为两个部分。有关与数据库连接的所有行都应该在它们自己的文件(dbInfo.php)中。这样,您可以在其他页面上反复使用它们,而无需在更改简单密码时重写每个页面。

dbInfo.php

<?php
//Great spot to put global variables for all files.


//Database information
$db_username    = "yourDBusername";
$db_password    = "yourDBPassword";
$db_db        = "databaseName";
$db_host      = "databaseURL";


function setVariables($u, $p, $d, $h){
    global $db_username, $db_password, $db_db, $db_host;
    $db_username = $u;
    $db_password = $p;
    $db_db = $d;
    $db_host = $h;
}

//Function that opens the database
function dbOpen(){
    global $db_username, $db_password, $db_db, $db_host;
    $con = mysql_connect($db_host, $db_username, $db_password);
    if (!$con){
         die('Error: Could not connect: ' . mysql_error());
    }   
    mysql_select_db($db_db, $con);
    return $con;
}

// Use this function when you need a result of anything more than one answer or element
function dbGetArray($query){
    $con = dbOpen();

    $result = mysql_query($query);
    if(!empty($result)){
        $return_val = array();
        while($row = mysql_fetch_array($result)){
            $i = 0;
            $temp = array();
            foreach($row as $key => $value){        
                if($i/2 != $key){
                    $temp[$key] = trim($value);
                }
                $i++;
            }
        array_push($return_val, $temp);
        }
        dbClose($con);
        return $return_val;
    }else{
        dbClose($con);
        return NULL;
    }
}

//This function gets just one value
function dbGetOne($query){
    $con = dbOpen();
    $result = mysql_query($query);
    if(!$result){
        echo "SERIOUS PROBLEM HERE!<br />";
        echo $query . "<br /><br />";
    }else{
        $row = mysql_fetch_row($result);
    }
    dbClose($con);
    return $row[0];
}

//Use this function to insert or update your database
function dbSimpleQuery($query){
    $con = dbOpen();
    $return = true;
    //echo $query;
    if(!mysql_query($query, $con)){
        echo "Error: An error occurred while running Simple Query:  " . mysql_error() . "<br>";
        echo "Original Query: " . $query . "<br><br>";
        $return = false;
    }
    dbClose($con);
    return $return;
}

//Bonus for you.  You can use this function to block sql injections
function SanitizeString($string){
    $con = dbOpen();

    //echo htmlentities(mysql_fix_string($string));
    return htmlentities(mysql_fix_string($string));
    dbClose($con);
}

然后在第二个文件中将顶部更改为如下所示。

listMovies.php

<?php
    require_once('dbInfo.php');

    $query = "SELECT * FROM movies";
    $result = dbGetArray($query);

   //Helpful for debugging
   //echo $query;
?>
<html>
    <body>
       <table><tr>
         <th>Title</th>
         <th>Year</th>
         <th>Studio</th>
         <th>Price</th>
         <th>Edit</th>
      </tr>

<?  // output data of each row
    if(!empty($result)) {
        foreach($result as $row) {
            echo"<tr><td>".$row['title']."</td>
               <td>".$row['year']."</td>
               <td>".$row['studio']."</td>
               <td>".$row['price']."</td>
               <td><a href='edit.php?id=".$row['id']."'>Edit</td>
            </tr>";
        }
    } else {
        echo"<tr><td colspan= '5'>No Results found</td></tr>";
    }  
    ?>
            </table>
        </body>
    </html>

然后你的第三个文件会变成这样的东西。

editMovies.php

<?
require_once('dbInfo.php');
//Check to see if a $_GET is set
if(isset($_GET['id']) {
    $id = SanitizeString($_GET['id']);  //This is where you stop sql injection
    $movies = dbGetArray("SELECT * FROM movies WHERE id = '$id'");
    foreach ($movies as $movie) {
        $title = $movie['title'];
        $year = $movie['year'];
        $studio = $movie['studio'];
        $price  = $movie['price'];
    }
} else {
   $id= 0; //Can be used to show an 'empty results' page, because URL was messed with
}

//Check for $_POST from edit form
if(isset($_POST['submit'])) {
    //Handy debugging line
    //echo "<pre>"; print_r($_POST); die("</pre>");
    $id = SanitizeString($_POST['id']);
    $title = SanitizeString($_POST['title']);   
    $year  = SanitizeString($_POST['year']);
    $studio = SanitizeString($_POST['studio']);
    $price = SanitizeString($_POST['price']);

    $query = "UPDATE movies SET 
        title = '$title',
        year = '$year',
        studio = '$studio',
        price = '$price'
        WHERE id = '$id'";

    //Handy Debugging tool
    //die($query);
    dbSimpleQuery($query);
    $submitted = TRUE;
    //You could also put a redirect in here to take user back to listMovies.php
} else {
   $submitted  = FALSE;
}

?>
<html>
   <body>
<?if(!$submitted) { ?>
     <form action='editMovies.php' method='post'>
        <input type='hidden' name='id' value='<?=$id?>'>
        <input type='text' name='title' value='<?=$title?>'>
        <input type='text' name='year' value='<?=$year?>'>
        <input type='text' name='studio' value='<?=$studio?>'>
        <input type='text' name='price' value='<?=$price?>'>
        <button type='submit' name='done' value='submit'>Update Info</button>
     </form>
<? } else if($submitted) { ?>
     <p>Congrats, your movie is updated</p>
<? } ?>
   </body>
</html>

我实际上没有运行这些页面来检查语法错误,但这就是我设置它们的方法。希望它有所帮助。