这是我第一次使用这个网站,我最近开始研究一个项目,其中网页将显示两个表,其中一个表将收集SQL数据库中的所有信息并将它们放在表中以及每行旁边的按钮允许您将该记录插入另一个表中的另一个表,到目前为止,这是我编写的代码:
//Include the Following Files
include 'connect.php';
//Select All Price Plans
$query = "SELECT * FROM pricing";
$result = mysql_query($query);
//Print all Plans in Table
while ($pricing = mysql_fetch_array($result))
{
echo "<table border=1>";
echo "<tr>";
echo "<td>" .$pricing['planID']. "</td>";
echo "<td>" .$pricing['planTitle']. "</td>";
echo "<td>" .$pricing['planDescription']. "</td>";
echo "<td>" .$pricing['planPricing']. "</td>";
echo "<td>" . **<a href="'. $link .'">BUTTON HERE</a>** . "</td>";
echo "</tr>";
echo "<table>";
}
//Assign $link to Case
$link = '?run=planA';
//Pre-defined Functions
function planA()
{
$query = "INSERT into restaurant_plan (`planID`, `planTitle`, `planDescription`, `planPricing`) SELECT * FROM pricing WHERE planID='2' ";
$result = mysql_query($query);
}
//Setting Case to Run Each Functions
if (isset($_GET['run'])) $linkchoice=$_GET['run'];
else $linkchoice='';
switch($linkchoice)
{
case 'planA':
planA();
break;
case 'planB':
planB();
break;
}
有人可以建议任何指南或可能的示例我如何为每行分配功能吗?非常感谢!
答案 0 :(得分:1)
您的代码为表“定价”中的每条记录打印一张表,请改用此代码:
//Select All Price Plans
$mysqli = new mysqli("hostname", "username", "pass", "dbname");
$query = "SELECT * FROM pricing";
$result = $mysqli->query($query);
//Print all Plans in Table
echo "Available Price Plans";
echo "<table border=1>";
while ( $pricing = $result->fetch_assoc() ) {
echo "<tr>";
echo "<td>" .$pricing['planID']. "</td>";
echo "<td>" .$pricing['planTitle']. "</td>";
echo "<td>" .$pricing['planDescription']. "</td>";
echo "<td>" .$pricing['planPricing']. "</td>";
echo "<td>" .'<a href="'. $link .'"><img style="border:none;" src="'. $icon .'" /></a>'. "</td>";
//print a button as you mentioned
echo "<td>"."<form action='#' method='get'><input type='hidden' name='id' value='".$pricing['planID']."'/><input type='submit' value='copy'/></form></td>";
echo "</tr>";
}
echo "</table>";
和你的职能:
function planA()
{
// get selected plan info
$query = "SELECT * FROM pricing";
$result = $mysqli->query($query);
$row = $res->fetch_assoc();
//copy info to table 'restaurant_plan'
$query = "INSERT into restaurant_plan (".$_GET["id"].", ".$row["planTitle"].", ".$row["planDescription"].", ".$row["planPricing"].")";
$result = $mysqli->query($query);
}
答案 1 :(得分:0)
不是答案,但如果您希望您的代码更清洁&amp;更易读/可维护,你应该使用插入PHP的HTML,而不是通过PHP回显HTML:
<?php
//Select All Price Plans
$query = "SELECT * FROM pricing";
$result = mysql_query($query);
//Print all Plans in Table
?>
Available Price Plans
<?php while ($pricing = mysql_fetch_array($result)) : ?>
<table border=1>
<tr>
<td><?= $pricing['planID'] ?></td>
<td><?= $pricing['planTitle'] ?></td>
<td><?= $pricing['planDescription'] ?></td>
<td><?= $pricing['planPricing'] ?></td>
<td><a href="<?= $link ?>"><img style="border:none;" src="<?= $icon ?>" /></a></td>
</tr>
<table>
<?php endforeach; ?>
另外,正如我在上面的评论中提到的,您应该停止使用mysql_*
函数。他们被弃用了。而是使用PDO(从PHP 5.1开始支持)或mysqli(从PHP 4.1开始支持)。如果您不确定使用哪一个,read this article。
答案 2 :(得分:0)
将表格输出更改为以下内容:
<table border="1"> <?php while ($pricing = mysql_fetch_array($result)) { ?> <tr> <td><?php echo $pricing['planID']; ?></td> <td><?php echo $pricing['planTitle']; ?></td> <td><?php echo $pricing['planDescription']; ?></td> <td><?php echo $pricing['planPricing']; ?></td> <td><a href='<?php echo $link; ?>'><img style="border:none;" src='<?php echo $icon; ?>' /></a></td> </tr> <?php } ?> <table>
仔细检查数据库连接,以确保可以检索数据。