【问题标题】:How to view a table from database in the xampp php at the endofthis index page?如何在索引页末尾的 xampp php 中查看数据库中的表?
【发布时间】:2018-10-02 00:39:43
【问题描述】:

我想在本页末尾查看我的 MySQL 数据库中的一个表。该表具有以下属性:“id”、“longurl”和“shorturl”。

<!DOCTYPE html>
<html>
<head>
    <title>URL Shortening Service </title>
<head>
<body>
    <center>
        <h1>URL Shortening Service</h1>
        <form method="POST" action="index.php">
        Enter your Url: <input type="text" name="urlshort" placeholder="Enter here"> <br><br>
        <input type="submit" name="submit">
        </form>
    </center>
</body>
</html>



<?php

if (isset($_POST["submit"])){
    $conn = mysqli_connect('localhost','zarjis','123456','url');




    $longurl = $_POST ["urlshort"];
    $shorturl = substr(md5( microtime()), rand(0,26),5);
    $query = "INSERT INTO shorturl (shorturl,longurl) VALUES ('$shorturl','$longurl')";
    $result = mysqli_query($conn,$query);
    if ($result) {
        echo "Your Short Url Link is ::  http://localhost/url/$shorturl ";
    }
    else{
        echo "We are facing some problems";
    }
}

if (isset($_GET["link"])) {
    $conn = mysqli_connect('localhost','zarjis','123456','url');
    $link = $_GET["link"];
    $fetchquery = "SELECT * FROM shorturl WHERE shorturl= '$link'";
    $fetchresult = mysqli_query($conn,$fetchquery);
    while ($row = mysqli_fetch_assoc($fetchresult)) {
        $visitlongurl = $row ["longurl"];
        header ("Location: $visitlongurl");
        exit();
    }
}
?>

我想在本页末尾查看我的数据库中的一个表。 该表在我的数据库中。

【问题讨论】:

  • 你最好使用准备好的语句来防止sql注入攻击。

标签: php mysql mysqli xampp


【解决方案1】:

添加此代码,

$con = mysqli_connect('localhost','zarjis','123456','url');
if(mysqli_connect_errno()){
    die("Failed to connect to MySQL: " . mysqli_connect_error());
}
$sql = "SELECT * FROM shorturl";
$result = mysqli_query($con, $sql);
echo '<table>';
echo '<tr><th>ID</th><th>Long URL</th><th>Short URL</th></tr>';
while($row = mysqli_fetch_assoc($result)){
    echo '<tr><td>'.$row['id'].'</td><td>'.$row['longurl'].'</td><td>'.$row['shorturl'].'</td></tr>';
}
echo '</table>';

【讨论】:

    猜你喜欢
    • 2014-11-26
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 1970-01-01
    • 1970-01-01
    • 2021-06-14
    相关资源
    最近更新 更多