0

我一直在努力解决这个问题,但我做不到。

$sqluser = "--";
$sqlpass = "--";
$hostname = "localhost"; 

$clientuser = $_COOKIE['user'];
//connection to the database
$dbhandle = mysql_connect($hostname, $sqluser, $sqlpass) 
 or die("Unable to connect to MySQL");
//select a database to work with
$selected = mysql_select_db("clhtechn_si1",$dbhandle) 
  or die("Could not select database!");
//execute the SQL query
$idfinder = mysql_query("SELECT id FROM `si_customers` WHERE username='$clientuser'") or die(mysql_error());
$clientid = mysql_fetch_assoc($idfinder);
$query = "SELECT invoice_id, date, SUM(total) as invoiceTotal
          FROM si_invoices
          LEFT JOIN si_invoice_items ON si_invoices.id = si_invoice_items.invoice_id
          WHERE si_invoices.customer_id='$clientid'
          GROUP BY invoice_id
          ORDER BY invoice_id";
$result = mysql_query($query);

$row=0;
while ($record = mysql_fetch_assoc($result))
{
    $class = 'd' . ($row % 2);
    $inv_date=substr($record['date'], 0, -9);
    $inv_total=$record['invoiceTotal'];
    $invoice_total=number_format($inv_total, 2);

    echo "<tr class=\"{$class}\">";
    echo "<td valign='top'>{$inv_date}</td>";
    echo "<td valign='top'>{$record['invoice_id']}</td>";
    echo "<td valign='top'>$invoice_total</td>";
    echo "<td><a href='invoices/Invoice_{$record['invoice_id']}.pdf' target='_blank'>";
    echo "<img src='urlhere' width='17' height='17' alt='PDF File' border='0' /></a></td>";
    echo "</tr>";
    $row++;
}
//close the connection

mysql_close($dbhandle);

这是我的客户表的对齐方式: 链接到图片。

我的代码有什么问题,我可以使用什么代码来修复它?帮助表示赞赏。

4

1 回答 1

1

$clientid 是一个数组,因为 mysql_fetch_assoc() 返回此类型。

$row = mysql_fetch_assoc($idfinder);
$clientid = $row['id'];

此外,您需要在将 $clientuser 放入查询之前正确格式化它。
所以,完整的代码将是

$user = mysql_real_escape_string($_COOKIE['user']);
$sql = "SELECT id FROM `si_customers` WHERE username='$user'";
$res = mysql_query($sql) or trigger_error(mysql_error());
$row = mysql_fetch_assoc($res);
$clientid = $row['id'];

为了使它成为你想要的方式,你必须使用一些抽象库
,你可以将你的 id 放在一行中(实际上是 2 行只是为了便于阅读)

$sql = "SELECT id FROM si_customers WHERE username=?s";
$clientid = $db->getOne($sql,$_COOKIE['user']);

在这里您可以看到这样一个Mysql 抽象库的示例。

于 2013-02-14T08:56:45.867 回答