show single entry on a new page with sending id of

2019-09-10 20:02发布

问题:

i am very novice to php and mysqli and found a great tutorial but am needing some help.

i am wanting a row to be linkable and send it to another page named single.php?id=ROWID so it will show the single entry

this is what i got so far.

    <html>
    <head>
        <title>MySQLi Tutorial</title>

    </head>
<body>

<?php
//include database connection
include 'db_connect.php';

$action = isset($_GET['action']) ? $_GET['action'] : "";

if($action=='delete'){ //if the user clicked ok, run our delete query

        $query = "DELETE FROM users WHERE id = ".$mysqli->real_escape_string($_GET['id'])."";
        if( $mysqli->query($query) ){
                echo "User was deleted.";
        }else{
                echo "Database Error: Unable to delete record.";
        }

}

$query = "select * from users";
$result = $mysqli->query( $query );

$num_results = $result->num_rows;

echo "<div><a href='add.php'>Create New Record</a></div>";

if( $num_results ){

    echo "<table border='1'>";//start table
        //creating our table heading
        echo "<tr>";
                echo "<th><a href=\"single.php?id={$id}\">Firstname</></th>";
                echo "<th>Lastname</th>";
                echo "<th>Username</th>";
                echo "<th>Action</th>";
        echo "</tr>";

        //loop to show each records
        while( $row = $result->fetch_assoc() ){
            //extract row
            //this will make $row['firstname'] to
            //just $firstname only
            extract($row);

            //creating new table row per record
            echo "<tr>";
                echo "<td>{$firstname}</td>";
                echo "<td>{$lastname}</td>";
                echo "<td>{$username}</td>";
                                echo "<td>";
                                        echo "<a href='edit.php?id={$id}'>Edit</a>";
                                        echo " / ";
                                        echo "<a href='#' onclick='delete_user( {$id} );'>Delete</a>";
                                echo "</td>";
            echo "</tr>";
        }

        echo "</table>";//end table

}else{
        //if table is empty
        echo "No records found.";
}

//disconnect from database
$result->free();
$mysqli->close();

?>

<script type='text/javascript'>

    function delete_user( id ){
        //this script helps us to

        var answer = confirm('Are you sure?');
        if ( answer ){ //if user clicked ok
            //redirect to url with action as delete and id to the record to be deleted
            window.location = 'index.php?action=delete&id=' + id;
        } 
    }
</script>

</body>
</html>

i am right in thinking i would be sending the rows id in the url ?

echo "<th><a href=\"single.php?id={$id}\">Firstname</></th>";

but i am having issues with single.php what code would i have to put to show the single entry?

i have been on this a while and got no were near so i deleted the code and swallowed my pride to seek some help :/

thanks in advance

回答1:

Thank you for the interesting question.
First, let me inform you that, although you are using a moder-looking database access library, the way you are using it is as ancient as a mammoth fossil.

Several things to consider

  • Never use mysqli as is, but only in the form of some higher level abstraction library.
  • Never use real_escape_string in the application code but use prepared statements only.
  • Never mix your database code with HTML output. Get your data first, then start for output.
  • Never use GET method to modify the data.

Here goes the example based on the above principles. It does ALL basic CRUD operations:

<?  
include 'safemysql.class.php'; // a library 
$db    = new SafeMysql();
$table = "test"; 

if($_SERVER['REQUEST_METHOD']=='POST') {
  if (isset($_POST['delete'])) {
    $db->query("DELETE FROM ?n WHERE id=?i",$table,$_POST['delete']);
  } elseif ($_POST['id']) { 
    $db->query("UPDATE ?n SET name=?s WHERE id=?i",$table,$_POST['name'],$_POST['id']);
  } else { 
    $db->query("INSERT INTO ?n SET name=?s",$table,$_POST['name']);
  } 
  header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);  
  exit;  
}  
if (!isset($_GET['id'])) {
  $LIST = $db->getAll("SELECT * FROM ?n",$table);
  include 'list.php'; 
} else {
  if ($_GET['id']) {
    $row = $db->getRow("SELECT * FROM ?n WHERE id=?i", $table, $_GET['id']);
    foreach ($row as $k => $v) $row[$k]=htmlspecialchars($v); 
  } else { 
    $row['name']=''; 
    $row['id']=0; 
  } 
  include 'form.php'; 
}

It is using templates to display the data:

list.php

<a href="?id=0">Add item</a>
<? foreach ($LIST as $row): ?>
<li><a href="?id=<?=$row['id']?>"><?=$row['name']?></a>
<? endforeach ?>

and form.php

<form method="POST">
<input type="text" name="name" value="<?=$row['name']?>"><br>
<input type="hidden" name="id" value="<?=$row['id']?>">
<input type="submit"><br>
<a href="?">Return to the list</a>
</form>
<? if ($row['id']):?>
<div align=right>
<form method="POST">
<input type="hidden" name="delete" value="<?=$row['id']?>">
<input type="submit" value="Удалить"><br>
</form>
</div>
<?endif?>

here goes the part for display.

  if ($_GET['id']) {
    $row = $db->getRow("SELECT * FROM ?n WHERE id=?i", $table, $_GET['id']);
    foreach ($row as $k => $v) $row[$k]=htmlspecialchars($v); 
  } else { 
    $row['name']=''; 
    $row['id']=0; 
  } 
  include 'form.php'; 

if you don't want to show the form - create another template called single.php with whatever markup you wish



回答2:

Single.php

I Use PDO if u want you can make it with MySQLi too.

<?php
    include("db_connect.php"); // database configuration file
    if(isset($_GET['id'])
    {
        $id = (int) $_GET['id'];
        $sql = "SELECT * FROM `users` WHERE id=?";
        $query = $conn->prepare($sql); // $conn is PDO object yours can be different
        $query->bindValue(1,$id);
        $query->execute();
        if($query){
            $row = $query->fetch(); // 
        }else{ 
            echo "Error with Database";
        }
    }        
    else // Error for the Id selection
    {
        echo("ID is not selected");
    }
?>

No while loop because you want just 1 record. $row variable is just for test because i don't know your fields in your DB

<table border="1">
    <tr>
        <td>ID</td>
        <td>Firstname</td>
        <td>Lastname</td>
    </tr>
    <tr>
        <td><?php echo $row['id]; ?></td>
        <td><?php echo $row['firstname']; ?></td>
        <td><?php echo $row['lastname']; ?></td>
    </tr>
</table>


回答3:

in your single.php

$id=$_GET['id'];
$query="select * from users where id='".$id."'";


标签: php mysqli