Jump to content

How to store array values into mysql database

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Astha

Astha

    Newbie

  • Members
  • PipPip
  • 25 posts
Hello,

I've created one Design Form. Please take a look at the attached image.

I've created an array for property names (i.e., paper type, paper color, product size, etc.) and spec names (check box options are spec names).

On echo it prints the proper values, but cannot store this array values into mysql database.

design_from.php

<?php

include "Connections/database_connect.php"; 


$quer2=mysql_query("SELECT * FROM products"); 


@$products=$_GET['products'];

if(isset($products)){

$quer=mysql_query("SELECT * FROM sample_design WHERE product_ID=$products"); 

}else{$quer=mysql_query("SELECT * FROM sample_design"); } 




$query="SELECT * FROM property_spec ps 

JOIN property p ON p.property_ID = ps.property_ID 

JOIN specification s ON s.spec_ID = ps.spec_ID";


$query1="SELECT p.property_name FROM property_spec ps 

JOIN property p ON p.property_ID = ps.property_ID 

JOIN specification s ON s.spec_ID = ps.spec_ID";


$result=mysql_query($query);

$result1=mysql_query($query1);


?>


<form name="f1" method="post" action="design_update.php">

<?php

echo "<p class=place><label>Product Name: 	 ";

echo "<select name='products' onchange=\"reload(this.form)\"><option value=''>Select one</option>";

while($noticia2 = mysql_fetch_array($quer2)) { 

if($noticia2['product_ID']==@$products){echo "<option selected value='$noticia2[product_ID]'>$noticia2[product_name]</option>"."<BR>";}

else{echo  "<option value='$noticia2[product_ID]'>$noticia2[product_name]</option>";}

}echo "</select>"."<BR>"."<BR>"."<BR></p>";


echo "<p class=place><label>Design ID: 	 ";

echo "<select name='design' onchange=\"reload3(this.form)\"><option value=''>Select one</option>";

while($noticia = mysql_fetch_array($quer)) { 

if($noticia['design_ID']==@$design)

{echo "<option selected value='$noticia[design_ID]'>$noticia[design_ID]</option>"."<BR>";}

else{echo  "<option value='$noticia[design_ID]'>$noticia[design_ID]</option>";}

}echo "</select><br><br></p>";

?>

<?php



$flag=0;

$k=0;

while($noticia = mysql_fetch_array($result)) {

$noticia1 = mysql_fetch_array($result1);

if($flag==0) 

{    //if flag is 0, make $t=property_id and print it and make flag=1


    $t=$noticia[property_ID];

  

    echo  "<p class=place><br><br>$noticia1[property_name]".": 	 </p>";

             echo "<input type='hidden' name='property_name[$k]' value='$noticia1[property_name]'>";

    $flag=1;

}

?>

<?php


if($t!=$noticia[property_ID])

{    //this condition will be false for same property id records

    //will be true if product id chnges in next record

    $flag=0;

    $t=$noticia[property_ID];

    echo  "<p class=place><br><br>$noticia1[property_name]".": 	 </p>";

             echo "<input type='hidden' name='property_name[$k]' value='$noticia1[property_name]'>";

    $flag=1;

}

?>

<?php

$j=0;

echo "<span class='unbold'><INPUT TYPE=CHECKBOX name='spec_name[$k][$j]' value='$noticia[spec_name]'>$noticia[spec_name]	 </span>";

$j++;

$k++;

}

 echo "<input type='hidden' name='k' value='$k'>";

echo "<input type='hidden' name='j' value='$j'>";

echo "<br>"."<br>"."<BR>"."<input type=submit class=orange name=submit value=Submit>";

echo "		   <input type=reset class=orange value=Reset Form>";

?>

</form>



design_update.php

<?php

include "Connections/database_connect.php"; 


if(isset($_POST['submit'])) 

{

for($i=0; $i<$k; $i++)

{

echo $property_name[$i];

for($m=0; $m<$j; $m++){


    //QEURY COMES HERE



    echo $spec_name[$i][$m];

    $product_ID = $_POST['product_ID']; 


    $design_ID = $_POST['design_ID']; 

    

 $property_name = $property_name[$i];

    $spec_name = $spec_name[$i][$m];


    $sql="INSERT INTO product_design (product_ID, design_ID, property_name, spec_name)

    VALUES

    ( '$product_ID', '$design_ID', '$property_name', 'spec_name')";


    mysql_query($sql)  or die ('Error updating databse');


}//END M FOR LOOP


}//END I FOR LOOP

 

echo "Thank you, your product has been designed. ."; 

echo '<br>Click <a href="order_form_working.php">here</a> to place an order of this designed product.';

}


else{

echo "Error updating database.";

}

?>


Please assist.

Thanks

Attached Files



#2
dbug

dbug

    Programmer

  • Members
  • PipPipPipPip
  • 155 posts
I think a '$' is missing before 'spec_name' in the values to insert to the product_design table. Also there is a variable named $design that it doesn't seem to be initialized. There are some other things that I have doubts.

However it's hard to analize all the code only visually when we do not have all the information (parts of code, tables structure, ...) and we have no easy way to try it. It would help if you tell us what doesn't work. Does it throw an error ? what error ? or does it insert bad data into the database ? or it does not return any error but nothing is done ?

Some more information could help a lot.

#3
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Why not just store a serialized array?

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
Queries in a loop, 100% of the time means the flow of your application is wrong somewhere.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
Astha

Astha

    Newbie

  • Members
  • PipPip
  • 25 posts
Hello,

I've modified the design_update.php code slightly.

design_update.php
<?php

include "Connections/database_connect.php"; 


if(isset($_POST['submit'])) 

{

for($i=0; $i<$k; $i++)

{

echo $property_name[$i];

for($m=0; $m<$j; $m++){


    //QEURY COMES HERE



    echo $spec_name[$i][$m];

    $product_ID = $_POST['product_ID']; 

    $design_ID = $_POST['design_ID']; 

    $property_name = $_POST['property_name'][$i];

    $spec_name = $_POST['spec_name'][$i][$m];


    $sql="INSERT INTO product_design (product_ID, design_ID, property_name, spec_name)

    VALUES ( '$product_ID', '$design_ID', '$property_name', 'spec_name')";


    mysql_query($sql)  or die ('Error updating databse');


}//END M FOR LOOP


}//END I FOR LOOP

 

echo "Thank you, your product has been designed. ."; 

echo '<br>Click <a href="order_form_working.php">here</a> to place an order of this designed product.';

}


else{

echo "Error updating database.";

}

?>

Take a look at the attached screen shot to see how data is stored in database.

Attached Files