0

I have this code to select and output 2 rows (fixed) from database into html form with hidden input

<?php
$sql = ("SELECT * FROM `prices`");
$query = mysql_query($sql);
    while ($rows = mysql_fetch_array($query)) { 
 ?>
    <form method="post" action="process/save.php">
    <input type="hidden" value="<?php echo $rows['Id']; ?>" id="offerid" name="offerid[]" />
    <input type="hidden" value="<?php echo $rows['price']; ?>" id="offerprice" name="offerprice" />
    <input type="hidden" value="<?php echo $rows['service']; ?>" id="offerservice" name="offerservice" />

    <?php }; ?>     
    <input type="email" value="" id="customeremail" name="customeremail" placeholder="Enter Your Email Address" />
    <input type="submit" class="button" value="Save Prices" >
    </form>

and the save.php file is

<?php include("database.php");
$customeremail = mysql_real_escape_string($_POST['offerid']); 
$offerprice = mysql_real_escape_string($_POST['offerprice']);
$offerservice = mysql_real_escape_string($_POST['offerservice']);

if(isset($_POST['submit'])){
$sql="INSERT INTO Save_Quote(Email, Price1, Service1, Price2, Service2,) VALUES ('// what comes here')";
$result="mysql_query($sql) or die (mysql_error())";
}
?>

how can i insert the values?

Any help will be much appreciated, Thank You.

Shehary
  • 9,926
  • 10
  • 42
  • 71
  • I believe this is what you need: http://stackoverflow.com/questions/10054633/insert-array-into-mysql-database-with-php – EternalHour Jan 12 '15 at 00:14

1 Answers1

1

First, you must fix the code that generate the form:

<?php
$sql = ("SELECT * FROM `prices`");
$query = mysql_query($sql);
$counter = 1;

    echo '<form method="post" action="process/save.php">';

    while ($rows = mysql_fetch_array($query)) {

        echo '<input type="hidden" value="'.$rows['Id'].'" id="offerid'.$counter.'" name="offerid'.$counter.'" />
        <input type="hidden" value="'.$rows['price'].'" id="offerprice'.$counter.'" name="offerprice'.$counter.'" />
        <input type="hidden" value="'.$rows['service'].'" id="offerservice'.$counter.'" name="offerservice'.$counter.'" />';

        $counter ++;
    };  

    echo '<input type="email" value="" id="customeremail" name="customeremail" placeholder="Enter Your Email Address" />
    <input type="submit" class="button" value="Save Prices" >
    </form>';
?>

And then, you can get the variables in the save.php file:

$offerprice1 = mysql_real_escape_string($_POST['offerprice1']);
$offerservice1 = mysql_real_escape_string($_POST['offerservice1']);

$offerprice2 = mysql_real_escape_string($_POST['offerprice2']);
$offerservice2 = mysql_real_escape_string($_POST['offerservice2']);

$customeremail = mysql_real_escape_string($_POST['customeremail']);

Finally, the query:

$sql="INSERT INTO Save_Quote(Email, Price1, Service1, Price2, Service2) VALUES ('$customeremail','$offerprice1','$offerservice1','$offerprice2','$offerservice2')";