0

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

required_items包含列:id, name,required_amount

donations包含列:id, name, email, donation_amount,item_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>

谁能帮我解决这个问题?我已经包含了整个脚本,因为我不确定到底出了什么问题。我已经尝试在表格中回显信息,但它也不起作用。本质上,除了单选按钮之外,表格字段中没有显示任何内容。

4

3 回答 3

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);
于 2013-09-12T12:43:59.080 回答
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您需要使用randd用于相应的表

于 2013-09-12T12:46:55.643 回答
-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>
于 2013-09-12T12:46:11.013 回答