0

这是我第一次使用这个网站,我最近开始做一个项目,该网页将显示两个表,其中一个表将从 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;              
}     

任何人都可以建议任何指南或可能的示例,我如何为每一行分配一个函数?非常感谢!

4

3 回答 3

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);

}

于 2012-08-20T14:35:51.843 回答
0

不是答案,但如果您希望您的代码更干净、更易读/可维护,您应该使用插入了 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 开始支持)。如果您不确定要使用哪一个,请阅读这篇文章

于 2012-08-20T14:27:21.357 回答
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>

仔细检查您的数据库连接,以确保可以检索数据。

于 2012-08-20T14:41:28.517 回答