数据库项目未显示在网页上

时间:2013-09-12 12:42:13

标签: php mysql sql pdo

我正在使用 PHP 脚本,并通过 PDO 连接到数据库。该数据库包含两个表:第一个是required_items,另一个是donations

required_items包含列:idnamerequired_amount

donations包含列:idnameemaildonation_amountitem_id

以下是我正在使用的简单代码:

<?php

    $pageName = "/donations/index.php";
    $databaseHost = "localhost";
    $databaseName = "donations";
    $databaseUser = "root";
    $databasePassword = "pwd";

    //TODO Check, validate, sanitize your input...
    $name = $_POST['name'];
    $email = $_POST['email'];
    $donation_amount = $_POST['amount'];
    $item_id = $_POST['radioButtons'];

    try {
        $db = new PDO('mysql:host=' . $databaseHost . ';dbname=' . $databaseName . ';charset=utf8', $databaseUser, $databasePassword);
        $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $db->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    } catch (PDOException $e) {
        echo "Exception: " . $e->getMessage(); //TODO better error handling
    }

    // Check to see if someone wants to donate something
    if(!empty($_REQUEST['donate']))
    {
        try {
            //Construct your query with placeholders
            $sql = "INSERT INTO donations (name, email, donation_amount, item_id) VALUES (?, ?, ?, ?)";
            //Prepare your query
            $query = $db->prepare($sql);

            //Execute it passing parameters
            $query->execute(array($name, $email, $donation_amount, $item_id));

            echo("Thank you for donating!\n<br>\n<br>");
    } catch (PDOException $e) {
        echo "Exception: " . $e->getMessage(); //TODO better error handling
    }
    }

    $request = "SELECT 
                required_items.id,
                required_items.name, 
                required_items.required_amount - donations.donation_amount AS Amount_Left,
                donations.item_id
                FROM required_items JOIN donations ON donations.item_id=required_items.id";

    $stmt = $db->query($request);
    $item_info = $stmt->fetch();

    // Round negative amounts to zero
    if($item_info['Amount_Left'] < 0){
        $item_info['Amount_Left'] = 0;
        }

?>
    <!--Print out the table tag and header-->
    <form name="donationForm" action="<?php $pageName ?>" method="POST">
    <fieldset>
    <table>
    <tr>
        <th>Item Name</th><th>Amount</th>
    </tr>
    <tr>
        <td><?php $item_info['name'] ?></td>
        <td><?php $item_info['Amount_Left'] ?></td>
        <td><input type="radio" name="radioButtons" value="<?php $item_info['item_id'] ?>"></input></td>
    </tr>
    </table>
    <div><label>Amount</label><input type="number" name="amount"></div>
    <div><label>Email</label><input type="email" name="email"></div>
    <div><label>Name</label><input type="text" name="name"></div>
    <div><input type="submit" name="donate" value="Donate"></div>
    </fieldset>
    </form>

任何人都可以帮我解决这个问题吗?我已经包含了整个脚本,因为我不确定到底出了什么问题。我已经尝试回应表中的信息,但它也没有用。基本上,除了单选按钮之外,表字段中没有任何内容。

3 个答案:

答案 0 :(得分:3)

您正在对表进行别名,请尝试:

SELECT r.name AS Name ... FROM required_items AS r ...

PDO :: query()返回PDOStatement对象,因此您需要执行:

$amount_left = $db->query("SELECT required_items.name AS Name, required_items.required_amount - donations.donation_amount AS Amount_Left 
                        FROM required_items AS r JOIN donations AS d ON d.item_id=r.id")
                        ->fetch(PDO::FETCH_ASSOC);

答案 1 :(得分:0)

试试这个

$amount_left = $db->query("SELECT r.name AS Name, r.required_amount - d.donation_amount AS Amount_Left FROM required_items AS r JOIN donations AS d ON d.item_id=r.id");

由于您使用的是AS,因此需要对相应的表格使用rd

答案 2 :(得分:-1)

避免过度工程

$sql = "SELECT name, required_amount - donation_amount AS Amount_Left 
        FROM required_items JOIN donations ON item_id=id";

也会为你服务

同样适用于所有其他代码,这些代码是膨胀的,可以像这样简化

$stmt = $db->query($sql);
$data = $stmt->fetch();
if($data['Amount_Left'] < 0) {
    $data['Amount_Left'] = 0;
}
?>
<form name="donationForm"  method="POST">
<fieldset>
<table>
  <tr> 
    <th>Item Name</th>
    <th>Amount</th>
  </tr>
  <tr>
    <td><?=$data['name']?></td>
    <td><?=$data['Amount_Left']?></td>
    <td>
      <input type="radio" name="radioButtons" value="<?=$row['id']?>">
    </td>
  </tr>