gpt4 book ai didi

php - 按钮中的 MySQL 数据库行

转载 作者:行者123 更新时间:2023-11-29 20:07:24 25 4
gpt4 key购买 nike

我的 ToDo 应用程序有一个数据库,其中包含以下列:

| ID | ShortDescription | Description | Date | Status |

我已经可以将任务添加到数据表中,并且可以在 phphmyadmin 中看到它。

到目前为止我有以下代码:

$id = mysql_real_escape_string($_GET['id']);

$out = 'SELECT * FROM ToDo1 WHERE `id` = '.$id.' LIMIT 1';
$result = mysqli_query($link, $out);

$row= mysqli_fetch_array($result);
?>

<div id= "OutShortDescription">
<?php
echo $row['ShortDescription'];
?>
</div>
<div id= "OutDescription">
<?php
echo $row['Description'];
?>
</div>
<div id= "OutDate">
<?php
echo $row['Date'];
?>
</div>
<div id= "OutStatus">
<?php
echo $row['Status'];
?>
</div>

现在我想将每个 ID 行放在自己的站点上。为此,我想制作一个按钮表(Buttonnumber=ID)。此按钮上应仅显示 ShortDescription,当我单击它时,我想转到与该按钮匹配的站点。

有人可以帮助我吗?

编辑

好的,谢谢,现在我有了这段代码,但它不起作用:

    <?php
$dbname= 'Groups';
$dsn = 'mysql:host=localhost;dbname='.$dbname;
$user = 'root';
$pass = '';
$db = new PDO($dsn, $user,$pass);
$query = "SELECT * FROM groups2 WHERE id = :id LIMIT 1";
$ps = $db->prepare($query);
$ps->bindParam(':id', $id);
$ps->execute();
$row = $ps->fetch(PDO::FETCH_ASSOC);


?>
<div class="searchwindow">
<?php
$data = $link->query('SELECT * FROM Groups2');

foreach($data as $row) {

echo '<p><input type="button" onclick="window.location = All_Groups.php?id=' . $row['ID'] . ' value='.$row['ShortDescription'].' /></p>';
}

我现在有以下代码

<div data-role="page" id="SearchPage" data-title="SearchPage">
<div data-role="header">
<h1>Search</h1>
</div>

<div data-role="content">



<div data-role="header">

<form>
<fieldset data-role="controlgroup" data-type="horizontal" data-mini="true" align="center" id="selectMenu">


<select name="selectStatus" id="selectStatus">
<option value="0">Status</option>
<option value="1">Done</option>
<option value="2">In Progress</option>
</select>


</fieldset>
</form>

</div>
<?php
$dbname= 'Groups';
$dsn = 'mysql:host=localhost;dbname='.$dbname;
$user = 'root';
$pass = '';
$db = new PDO($dsn, $user,$pass);
$query = "SELECT * FROM groups2 WHERE id = :id LIMIT 1";
$ps = $db->prepare($query);
$ps->bindParam(':id', $id);
$ps->execute();
$row = $ps->fetch(PDO::FETCH_ASSOC);


?>
<div class="searchwindow">
<?php
$data = $link->query('SELECT * FROM Groups2');

foreach($data as $row) {

$path = $row['ID'];
$description = $row['ShortDescription'];
echo ("<form action='All_Groups.php?id=$path'><button type='submit' value='$description'/>$description</form>" );
}
?>



</div>
</div>

<div data-role="footer" data-position="fixed">
<div data-role="navbar">
<ul>
<li><a href="#SearchPage" data-transition="slideup" data-icon="search" data-iconpos="top">Search</a></li>
<li><a href="#NewPage" data-transition="slideup" data-icon="plus" data-iconpos="top">New</a></li>
<li><a href="#MorePage" data-transition="slideup" data-icon="bars" data-iconpos="top">More</a></li>
</ul>
</div><!-- Ende navbar -->
</div><!-- Ende footer -->
</div>

这是我的 All_groups.php

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Unbenanntes Dokument</title>
</head>

<body>
<?php
$servername ="localhost";
$username = "root";
$password = "";
$dbname = "Groups";

$link = mysqli_connect($servername, $username, $password, $dbname);
if (!$link) {
die('Verbindung nicht möglich : ' . mysqli_error($link) );
}
?>

<?php

$row="";
$Date="";
$Status="";
$ShortDescription="";
$Description="";

mysqli_select_db($link, "groups");

?>
</div>
<?php
$id = mysql_real_escape_string($_GET['id']);
$out = "SELECT * FROM groups2 WHERE ID = '$id' ";
$result = mysqli_query($link, $out);

$id = mysqli_fetch_array($result);
?>

<div id= "OutShortDescription">
<?php
echo $id['ShortDescription'];
?>
</div>
<div id= "OutDescription">
<?php
echo $id['Description'];
?>
</div>
<div id= "OutStatus">
<?php
echo $id['Status'];
?>
</div>
<div id= "OutDate">
<?php
echo $id['Date'];
?>


<div data-role="footer" data-position="fixed">
<div data-role="navbar">
<ul>
<li><a href="#SearchPage" data-transition="slideup" data-icon="search" data-iconpos="top">Search</a></li>
<li><a href="#NewPage" data-transition="slideup" data-icon="plus" data-iconpos="top">New</a></li>
<li><a href="#MorePage" data-transition="slideup" data-icon="bars" data-iconpos="top">More</a></li>
</ul>
</div><!-- Ende navbar -->

</div>
</body>
</body>
</html>

最佳答案

首先,不要使用 mysql_* 方法!使用PDOmysqli_*相反。

下面,我将使用 PDO 粘贴您的示例查询:

$dsn = 'mysql:host=localhost;dbname='.$dbname;//$dbName is the name of your database
$user = 'root';
$pass = '123';//use your login information here
$db = new PDO($dsn, $user,$pass);
$query = "SELECT * FROM ToDo1 WHERE id = :id LIMIT 1";
$ps = $db->prepare($query);
$ps->bindParam(':id', $id)
$ps->execute();
$row = $ps->fetch(PDO::FETCH_ASSOC);

现在,要获取按钮,您不需要使用 jquery:

<?php
$path = $row['ID'];
$description = $row['ShortDescription'];
echo "<form action='your/site/$path'><button type='submit' value='$description'/>$description</form>"
?>

另一个选项是使用onclick:

<?php
$path = $row['ID'];
$description = $row['ShortDescription'];
echo "<input type=\"button\" onclick=\"location.href='your/site/$path'\" value=\"$description\" />";
?>

" 之前的 \ 是转义符,因此 PHP 将打印字符 " 而不会将其解释为字符串的结尾。

建议:尽量避免混合 HTML 和 PHP,一般来说这是一个不好的做法。

关于php - 按钮中的 MySQL 数据库行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40301293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com