0

This is the first time I'm using this website, I have recently started working on a project where the webpage will display two table, one of the table will gather all the information from a SQL Database and place them in the table along with a button next to each rows allowing you to insert that record to another to the other table, so far, this is the code I have written:

//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;              
}     

Could anyone suggest any guide or possibly an example how I could assign a function to each rows? Thanks a lot!

7
  • 1
    It may not help answer your question, but you should stop using mysql_* functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this article. Commented Aug 20, 2012 at 14:22
  • 2
    What, exactly, isn't working? Commented Aug 20, 2012 at 14:23
  • 1
    How are you connecting to the db? Commented Aug 20, 2012 at 14:23
  • Well, this is the script I have so far, but I'm confused how I can get it to assign a function based on each rows, I did try a counter but that didn't worked out. Thanks Commented Aug 20, 2012 at 14:25
  • 2
    What do you mean by "assign a function" ? Commented Aug 20, 2012 at 14:28

3 Answers 3

1

your code prints one table for each record in table "pricing", use this code instead :

//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>";

and your function :

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

}

Sign up to request clarification or add additional context in comments.

Comments

0

Not an answer, but if you want your code to be cleaner & more readable/maintainable, you should use HTML with PHP inserted, rather than echoing HTML via PHP:

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

Also, as I mentioned in the comments above, you should stop using mysql_* functions. They're being deprecated. Instead use PDO (supported as of PHP 5.1) or mysqli (supported as of PHP 4.1). If you're not sure which one to use, read this article.

Comments

0

Change your table output to the following:

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

Double check you database connection as well to ensure data can be retrieved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.