提问人:David 提问时间:8/21/2014 最后编辑:David 更新时间:3/29/2016 访问量:111
显示两个表中的数据 - PHP mySQL
Display data from two tables - PHP mySQL
问:
我在数据库 1 中有两个表。post 和 2.操作系统
post:
+----+------------+-------+-------+-----+--------+------+-----+-------+
| id | title | metaD | metaK | img | author | date | cat | text |
+----+------------+-------+-------+-----+--------+------+-----+-------+
| 1 | some title | data | data |data | data | data | app | data |
| 2 | title2 | data | data |data | data | data | os | data |
| 3 | title3 | data | data |data | data | data | oth | data |
+----+------------+-------+-------+-----+--------+------+-----+-------+
os:
+------+----------+----------+--------+--------+--------+--------+-------+-------+------+------+--------+
| osId | ostitle | osimg | oscpuM | oscpuR | osramM | osramR | oshdM | oshdR | osgM | osgR | osdown |
+----+------------+----------+-----------------+--------+--------+-------+-------+------+------+--------+
| 1 | title8 | name | here comes text | data | data | data | data | data | data | data |
| 2 | title2 | name | here comes text | data | data | data | data | data | data | data |
| 3 | title4 | name | here comes text | data | data | data | data | data | data | data |
+----+------------+----------+-----------------+--------+--------+-------+-------+------+------+--------+
和代码:
//get data from "post"
$post = "SELECT * FROM post WHERE id='{$_GET['id']}'";
$post1 = mysql_query($post);
$postV = mysql_fetch_array($post1);
$postID = $postV['id'];
$postTitle = $postV['title'];
$postD = $postV['metaD'];
$postK = $postV['metaK'];
$postImg = $postV['img'];
$postAuthor = $postV['author'];
$postDate = $postV['date'];
$postCat = $postV['cat'];
$postText = $postV['text'];
//get data from "os"
$os = "SELECT * FROM os WHERE ostitle='{$_GET['ostitle']}'";
$os1 = mysql_query($os);
$osV = mysql_fetch_array($os1);
$osID = $osV['osId'];
$osTitle = $osV['ostitle'];
$osImg = $osV['osimg'];
$osCpuM = $osV['oscpuM'];
$osCpuR = $osV['oscpuR'];
$osRamM = $osV['osramM'];
$osRamR = $osV['osramR'];
$osHdM = $osV['oshdM'];
$osHdR = $osV['oshdR'];
$osGM = $osV['osgM'];
$osGR = $osV['osgR'];
$osDown = $osV['osdown'];
//dispaly post
<?php
echo "<img class='view_newsimg' src='$postImg'>
<h3 class='lath'>$postTitle</h3>
<ul class='det'>
<li class='adc'>avtori: $postAuthor</li>
<li class='adc'>TariRi: $postDate</li>
<li class='adc'>kategoria: $postCat</li>
</ul>
<p class='news'>
$postText";
//display from "os"
$osif = $postV['cat'];
if ($osif == 'os')
{
echo "<div class='os1'>
<div>1</div>
<div class='os1_1'>procesori</div>
<div class='os1_1'>operatiuli mexsiereba</div>
<div class='os1_1'>adgili myar diskze</div>
<div class='os1_1'>grafikuli baraTi</div>
</div>
<div class='os1'>
<div>minimaluri</div>
<div>$osCpuM</div>
<div>$osRamM</div>
<div>$osHdM</div>
<div>$osGM</div>
</div>
<div class='os1'>
<div>rekomendebuli</div>
<div>$osCpuR</div>
<div>$osRamR</div>
<div>$osHdR</div>
<div>$osGR</div>
</div>";
}
?>
此页面的 URL 是:“.com/view.php?id=2ostitle=title2” 但我有这个错误:“注意:未定义的索引:第 54 行的 D:\XAMPP\htdocs\LinuxOid.com\blocks\db3.php 中的 ostitle”(这是:$os = “SELECT * FROM os WHERE ostitle='{$_GET['ostitle']}'”;
我该如何解决这个问题?
答:
1赞
Mani
8/21/2014
#1
你错过了 & 在 url 中更改此行
.com/view.php?id=2ostitle=title2
自
.com/view.php?id=2&ostitle=title2
编辑:使用此代码
<?php
//get data from "post"
$post = "SELECT * FROM post WHERE id='{$_GET['id']}'";
$post1 = mysql_query($post);
$postV = mysql_fetch_array($post1);
$postID = $postV['id'];
$postTitle = $postV['title'];
$postD = $postV['metaD'];
$postK = $postV['metaK'];
$postImg = $postV['img'];
$postAuthor = $postV['author'];
$postDate = $postV['date'];
$postCat = $postV['cat'];
$postText = $postV['text'];
//get data from "os"
$os = "SELECT * FROM os WHERE ostitle='{$_GET['ostitle']}'";
$os1 = mysql_query($os);
$osV = mysql_fetch_array($os1);
$osID = $osV['osId'];
$osTitle = $osV['ostitle'];
$osImg = $osV['osimg'];
$osCpuM = $osV['oscpuM'];
$osCpuR = $osV['oscpuR'];
$osRamM = $osV['osramM'];
$osRamR = $osV['osramR'];
$osHdM = $osV['oshdM'];
$osHdR = $osV['oshdR'];
$osGM = $osV['osgM'];
$osGR = $osV['osgR'];
$osDown = $osV['osdown'];
//dispaly post
echo "<img class='view_newsimg' src='{$postImg}'>
<h3 class='lath'>{$postTitle}</h3>
<ul class='det'>
<li class='adc'>avtori: {$postAuthor}</li>
<li class='adc'>TariRi: {$postDate}</li>
<li class='adc'>kategoria: {$postCat}</li>
</ul>
<p class='news'>{$postText}";
//display from "os"
$osif = $postV['cat'];
if ($osif == 'os')
{
echo "<div class='os1'>
<div>1</div>
<div class='os1_1'>procesori</div>
<div class='os1_1'>operatiuli mexsiereba</div>
<div class='os1_1'>adgili myar diskze</div>
<div class='os1_1'>grafikuli baraTi</div>
</div>
<div class='os1'>
<div>minimaluri</div>
<div>{$osCpuM}</div>
<div>{$osRamM}</div>
<div>{$osHdM}</div>
<div>{$osGM}</div>
</div>
<div class='os1'>
<div>rekomendebuli</div>
<div>{$osCpuR}</div>
<div>{$osRamR}</div>
<div>{$osHdR}</div>
<div>{$osGR}</div>
</div>";
}
?>
评论
0赞
David
8/21/2014
谢谢。它没有显示此通知。但不显示表“os”中的任何内容
0赞
jimmy
8/21/2014
#2
url 中有一些问题。它应该像
com/view.php?id=2&ostitle=title2
上一个:未定义索引有多严格?
评论
id=2ostitle=title2
id=2&ostitle=title2
<?php
//dispaly post <?php echo "<img class='view_newsimg' src='$postImg'>
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);