通过链接关联PHP和数据库 [英] Relate PHP and database through links

查看:88
本文介绍了通过链接关联PHP和数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



我正在做一个代码,其中主页面有一些练习的标题(存储在Mysql数据库中)并取决于用户的标题点击(带链接),我希望标题和问题本身在另一个页面中:'2.php'。问题也将从数据库中提取。我试图在练习链接中使用带有练习ID的GET参数。然后在'2.php'中,从数据库中获取带有该id的练习



这是我到目前为止所做的一些代码,但我是卡住。你能给我一些建议吗?

谢谢。



我尝试了什么:



Exercises.php - 在这里我显示了所有练习的标题。



<?php 
$ servername =localhost;
$ username =root;
$ password =;
$ dbname =project;

< pre lang =PHP> $ conn = new mysqli($ servername,$ username,$ password,$ dbname);


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

?>

<?php
while($ row = $ result-> fetch_assoc())
{
?>
< tr>
< td><?php echo $ row [exercise_id]; ?>< / TD>
< td>< a name =searchhref =http://localhost/PHP%20Pages/2.php\"target =_ blank><?php echo $ row [title ]; ?>< / A>< / TD>
< td><?php echo $ row [difficulty]; ?>< / TD>

< / tr>
<?php
}
?>





2.php

<?php 
$ servername =localhost;
$ username =root;
$ password =;
$ dbname =project;

$ conn = new mysqli($ servername,$ username,$ password,$ dbname);
$ sql =SELECT * FROM exercise; / *从表名中选择:exercise * /
$ result = $ conn-> query($ sql); / *检查连接* /


$ result = $ conn-> query($ sql);
while($ row = $ result-> fetch_assoc()){
echo $ row [exercise_id]。 。 $ row [title]。 $ row [text]。 <峰; br> 中;
}
}
?>

解决方案

servername =localhost;

username =root;


password =;


Hello,

I'm doing a code where the main page has titles of some exercises (stored in a Mysql database) and depending on what title the user clicks (with links), I want the title and the question itself in another page: ‘2.php’. The questions will also be taken from the database. Im trying to use a GET parameter in the exercise link with the id of the exercise. Then in ‘2.php’, get the exercise with that id from the database

This is some of the code that I've done so far but I'm stuck. Could you give me some advice?
Thank you.

What I have tried:

Exercises.php – In here I have all of the titles of the exercises displayed.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "project";

<pre lang="PHP">$conn = new mysqli($servername, $username, $password, $dbname);


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

?>

<?php
while($row = $result->fetch_assoc())
{
    ?>
    <tr>
        <td><?php echo $row["exercise_id"]; ?></td>
        <td><a name="search" href="http://localhost/PHP%20Pages/2.php" target="_blank"><?php echo $row["title"]; ?></a></td>
        <td><?php echo $row["difficulty"]; ?></td>

    </tr>
    <?php
}
?>



2.php

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "project";

$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SELECT * FROM exercises"; /*Select from table name: exercises*/
$result = $conn->query($sql); /*Check connection*/


    $result = $conn->query($sql);
    while($row = $result->fetch_assoc()) {
        echo $row["exercise_id"] . ". " .  $row["title"] . $row["text"] . "<br>";
    }
}
?>

解决方案

servername = "localhost";


username = "root";


password = "";


这篇关于通过链接关联PHP和数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆