0

我无法弄清楚如何在此代码中实现连接。还有第二个问题是我想'Charity No: '.$don['charity_id'].'<br/>'.在 mysql_query中使用("SELECT * FROM charity where id='?????????'")

代码现在所做的是它检索所有这些数据,这很好,但我想要charity_id [FK](在捐赠表中)作为charityname(来自慈善表),并且我确定这是使用连接完成的但是无法弄清楚如何在我的代码中执行此操作。第二个问题是我想创建一个评级系统,对慈善机构进行评级(基于慈善机构编号),数据在上面几行,但我也无法弄清楚。

我的代码是:

<?php
if (!isset($_POST['do']) || !isset($_POST['id']) || !$_POST['id'])
exit;

    require_once 'connection.php';

    $don=mysql_fetch_assoc(
        mysql_query('SELECT *
            FROM donation
            WHERE id="'.mysql_real_escape_string($_POST['id'],$con).'"',$con));
    if ($don===false || !$don['id'])
        print '<h3>Donation id #'.$_POST['id'].' does not exist!</h3>';
    else {
        print '<h3>Information about donation id #'.$_POST['id'].'</h3>'.
            'Donor ID: '.$don['donor_id'].'<br/>'.
            'Charity No: '.$don['charity_id'].'<br/>'.
            'Date & Time: '.$don['TransactionTime'].'<br/>'.
            'Donation Amount: £ '.number_format($don['D_Amount'],2).'<br/>'.
         }

$find_data = mysql_query("SELECT * FROM charity where id='?????????'");

while($row = mysql_fetch_assoc($find_data))
{
    $id = $row['id'];
    $C_Name = $row['CharityName']; 
    $C_Desc = $row['CharityDescription'];
    $Hits = $row['Hits'];
    $Ranking = $row['Ranking'];

    echo "

    <form action='rate.php' method='POST'>
    $C_Name: <select name='Ranking'>

    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>

    </select>
    <input type='hidden' value=$id name='ID'>
    <input type='submit' value='Rate'>; 
    </form>


    ";
}

我现在尝试的 JOIN 是:

$don=mysql_fetch_assoc(
        mysql_query('SELECT *
                   FROM donation As D JOIN charity as C ON d.charity_id=C.id 
            WHERE id="'.mysql_real_escape_string($_POST['id'],$con).'"',$con));

但现在运气。

给予任何帮助将不胜感激,谢谢。

4

2 回答 2

0

不能将 isset 与 $_POST 一起使用,因为它始终被设置并返回数组作为响应。改用空。

<?php
if (!empty($_POST['do']) || !empty($_POST['id']) || !$_empty['id'])
exit;

require_once 'connection.php';

//I'm guessing that ID is a number.
if(is_numeric($_POST['id'])){
        $temp_ID = $_POST['id'];    
    }
//Do a count before you trust the ID
$don=mysql_result(mysql_query('SELECT count(*) FROM donation WHERE id="'.$temp_ID.'"'),0);

if ($don!=='1') {
    //Don't print anything they give you here.
    print '<h3>That id does not exist!</h3>';
} else {
    $id = $temp_ID;

    $SQL = "select * from donation d,charity c where c.id=$id and d.id=c.id";
    $result = mysql_query($SQL);
    while ($row = mysql_fetch_array($result)){
        $donorID = $row['donor_id'];
        $charityID = $row['charity_id'];
        $transactionTime = $row['TransactionTime'];
        $amount = number_format($row['D_Amount'],2);
        $C_Name = $row['CharityName']; 
        $C_Desc = $row['CharityDescription'];
        $Hits = $row['Hits'];
        $Ranking = $row['Ranking'];
    }
    $form = "<h3>Information about donation id #$id</h3>\n
        Donor ID: $donorID<br/>\n
        Charity No: $charityID<br/>\n
        Date &amp; Time: $transactionTime<br/>\n
        Donation Amount: £ $amount<br/>\n";



$form .= "
<form action='rate.php' method='POST'>\n
$C_Name: <select name='Ranking'>\n
<option>1</option>\n
<option>2</option>\n
<option>3</option>\n
<option>4</option>\n
<option>5</option>\n
</select>\n
<input type='hidden' value=$id name='ID'>\n
<input type='submit' value='Rate'>\n 
</form>";
print $form;
}
于 2013-03-23T00:07:21.403 回答
0

这是JOIN

SELECT charity.*, donation.* 
FROM charity 
JOIN donation 
ON charity.id = donation.charity_id; 

此声明选择所有捐赠。未显示没有捐款的慈善机构。

这是给你的现场演示:http ://sqlfiddle.com/#!2/be925/2

请改写并指定第二个问题,我不明白你想要什么。

于 2013-03-22T23:34:37.180 回答