Jump to content

How to create an add button to add all details at once to mysql table


hance2105

Recommended Posts

hello guys, i have the below code which echoes records from tblproduct. a field is displayed with an add button against each retrieved record. how can i have only one add button to add all records at once to the table?

 

<?php session_start(); include('db_connect.php'); $sql = mysql_query("select * from tblproduct"); $num = mysql_num_rows($sql); if($num>0){ echo "<center><table bgcolor='grey' width='80%' border=0>"; echo "<tr bgcolor='#CCCCCC'>";echo "<td><b><center>Product</td>";echo "<td><b><center>Name</td>";echo "<td><b><center>Brand</td>";echo "<td><b><center>Price</td>";echo "</tr>"; while($row = mysql_fetch_assoc($sql)){     extract($row);       echo "<tr>";     echo '<td style="text-align:center;"><img height="100" width="100" src="Images/Products/'.$row['prod_photo'].'"/></td>';      echo "<td style='text-align: center;'>".$row['prod_name']."</td>";      echo "<td style='text-align: center;'>".$row['prod_brand']."</td>";      echo "<td>"."<form name=\"price\" method=\"post\" action=\"ret_addprod.php\">".        "<input type=\"hidden\" name=\"prod_id\" value=".$row['prod_id']." />".        "<input type=\"text\" name=\"prod_price\" />".        "<input type=\"submit\" value=\"Add\" /></form>"."</td>";    }echo "</table>";echo "<center><a href ='retailer_home.php' >Home</a>";} else{    echo "No products found.";} ?>

Link to comment
Share on other sites

You are making this much more difficult than needs to be...  But you cant if you are going to have a form on each page.  A way I would do it is after you enter in a price have javascript update that one .  But everyone has their own ways of doing things.

 

From the html5 working draft:

4.10.3 The form element

Content model:

Flow content, but with no form element descendants.

Link to comment
Share on other sites

First you would build your form different, using arrays:

<?php
session_start();
include('db_connect.php');
$sql = mysql_query("select * from tblproduct");
$num = mysql_num_rows($sql);
if($num>0) {
    echo "<form name=\"price\" method=\"post\" action=\"ret_addprod.php\">"
                ."<center><table bgcolor='grey' width='80%' border=0>"
                . "<tr bgcolor='#CCCCCC'>"
                . "<td><b><center>Product</td>"
                . "<td><b><center>Name</td>"
                . "<td><b><center>Brand</td>"
                . "<td><b><center>Price</td>"
                . "</tr>";
    while($row = mysql_fetch_assoc($sql)){
        
            echo "<tr>"
                    .'<td style="text-align:center;"><img height="100" width="100" src="Images/Products/'.$row['prod_photo'].'"/></td>'
                    . "<td style='text-align: center;'>".$row['prod_name']."</td>"
                    . "<td style='text-align: center;'>".$row['prod_brand']."</td>"
                    . "<td>"
                    ."<input type=\"hidden\" name=\"prod_id[]\" value=".$row['prod_id']." />"
                    ."<input type=\"text\" name=\"prod_price[]\" />"
                    ."</td>";
}
echo "</table>"."<input type=\"submit\" value=\"Add\" /></form>"
        ."<center><a href ='retailer_home.php' >Home</a>";
}
else{
    echo "No products found.";
}
?>

Then on:

ret_addprod.php

You would have a processing portion like:

<?php
include('db_connect.php');
if($_SERVER['REQUEST_METHOD'] == 'POST') {
 foreach($_POST['prod_id'] as $index => $id) {
  $sql = sprintf("UPDATE tblproduct SET prod_price = %f WHERE prod_id = %d",
                         $_POST['prod_price'][$index],
                         $id);
 if(mysql_query($sql)) continue;
 exit('There was an error in your syntax<br />' . mysql_error());
 }
}
?>

I don't really like running queries in loops.  The only way to make it work without looping the queries, would require a huge case statement.  If there is a problem with overhead (shouldn't be, unless your server is remote from the db server) perhaps we could write that out.

 

Edit:  I'm an idiot, mixed the syntax for sprintf and mysqli->prepare

Edited by jcbones
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.