Welcome to Dream.In.Code
Become a PHP Expert!

Join 136,941 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,867 people online right now. Registration is fast and FREE... Join Now!




using array to insert into new table

 
Reply to this topicStart new topic

using array to insert into new table, can anyone help me...

imaro
14 Mar, 2008 - 07:12 PM
Post #1

New D.I.C Head
*

Joined: 16 Feb, 2008
Posts: 10


My Contributions
in TmphBaru.php, i view all the data from the table produk...so it use an array to make sure all data are viewed...then each of the data 1 assign a button and three textfield. the textfields are for the quantity of the selected products..when the button is clicked...the data of the product and the quantity that user specify must inserted into the new table, tempahan...but only the name of the product had been inserted..not the quantity...so what should i do to make the quantity also inserted? please help me...


TmphBaru.php

CODE

<script type ="text/javascript">

function Tempahan(Id)
{
    if (confirm('Adakah anda pasti untuk menempah Produk ini?'))
    {
        document.booking.action="test.php";
        document.booking.book.value=Id;
        document.booking.submit();
    }
    else
        window.location="TmphBaru.php";
}
</script>


<?
include("../inc/dbconn.inc");

$db = mysql_connect($hostname,$username,$password);
mysql_select_db($dbname, $db);
    
# Call function returned array which containing id of users
$Prod= ListProd();
?>
        <br />
        <strong>Sila isi kuantiti produk yang diinginkan </strong><br />
      </div>
      <form id="booking" name="booking" method="post" action="">
        <table width="500" border="1" align="center" cellpadding="1" cellspacing="0" bordercolor="#999999">
        <tr bgcolor="#dfdfdf">
        
        <td><div align="center"><strong>
          <?
session_start();
$user=$_SESSION['user'];

$sql2 = "Select * from user WHERE id='$user'";    
$result2 = mysql_query($sql2,$db) or die ("Could not execute sql");
$num_row2 = mysql_num_rows($result2);

if ($num_row2 > 0)
{
    # Fetches a result row as an associative array
    $row = mysql_fetch_assoc($result2);

?>
          Nama Produk</strong></div></td>
          <td><div align="center"><strong>Berat </strong></div></td>
          <td><div align="center"><strong>Harga</strong></div></td>
          <td><div align="center"><strong>Kuantiti</strong></div></td>
          <td><div align="center"><strong>J. Harga</strong></div></td>
        </tr>
        <?
    //<input name=\"choice\"type=\"checkbox\" value=\"checked\" />
    for($i=0; $i<count($Prod); $i++)
    {    
        $ProdID=$Prod[$i][0];
        //$Prod1=$Prod[$i][1];
        print("<td rowspan=\"3\" align=\"center\"><input type=\"hidden\" name=\"produk\" value=".$ProdID." /><input name=\"user\" type=\"hidden\" value=".$row['perniagaan']. " /><font class=\"small\"><img src=\"  ../gambar/produk/".$Prod[$i][8]."\" width=\"55\" height=\"60\"><br>".$Prod[$i][0]."</td>");        
        print("<td align=\"left\"><input name=\"berat1\"type=\"checkbox\" value=\"5kg\"".$Prod[$i][1]." />5 kg</td>");
        print("<td align=\"left\"><font class=\"small\">RM ".$Prod[$i][4]."</td>");
        print("<td align=\"left\"><input type=\"text\" name=\"q1\"  maxlength=\"4\" size=\"4\"  value=".$Prod[$i][7]."></td>");
        print("<td align=\"center\" rowspan=\"3\"><input class=\"formbutton\" type=\"submit\" name=\"Tempah\" value=\"Tempah\" onClick=\"return Tempahan('$ProdID');\"></td>");
        print("</tr>");
        
        print("<tr>");
        print("<td align=\"left\"><input type=\"checkbox\" name=\"berat2\"value=\"10kg\"".$Prod[$i][2]."/>10 kg</td>");
        print("<td align=\"left\"><font class=\"small\">RM ".$Prod[$i][5]."</td>");
        print("<td align=\"left\"><input type=\"text\" name=\"q2\" maxlength=\"4\" size=\"4\" id=".$ProdID." /></td>");
                print("</tr>");
        print("<tr>");
        print("<td align=\"left\"><input type=\"checkbox\" name=\"berat3\"value=\"15kg\"".$Prod[$i][3]."/>15 kg</td>");
        print("<td align=\"left\"><font class=\"small\">RM ".$Prod[$i][6]."</td>");
        print("<td align=\"left\"><input type=\"text\" name=\"q3\"  maxlength=\"4\" size=\"4\" id=".$ProdID." /></td>");
        //print("<td align=\"left\">RM <input type=\"text\" name=\"kod1\" size=\"4\" id=\"kod1\" /></td>");
        print("</tr>");
    
    }
    print("<input type=\"hidden\" name=\"book\" value=\"\" >");         
    print("</font></form>");
    print("</table><p>");
    
    
# Close the 'if' loop
}


    
function ListProd()
{
    $m=0;
    $query = "SELECT * FROM produk";
        
    $result = mysql_query($query);
    $numrows=mysql_num_rows($result);

    if ($numrows > 0)
    {
        while ($row = mysql_fetch_array($result))
        {
            $dbarray[$m][0] = $row["nama"];
            $dbarray[$m][1] = $row["5kg"];
            $dbarray[$m][2] = $row["10kg"];
            $dbarray[$m][3] = $row["15kg"];
            $dbarray[$m][4] = $row["harga5"];
            $dbarray[$m][5] = $row["harga10"];
            $dbarray[$m][6] = $row["harga15"];
            $dbarray[$m][7] = $row["bil1"];
            $dbarray[$m][8] = $row["gambar"];
            
            $m++;
        }
    }
    else
    {
        print("<br>");
        print("<center><font class=\"big\"><b>");
        print("Tiada rekod dalam pangkalan data!<br><br>");
    }
    return $dbarray;
}
?>
      </table>
      <br />
      <br />
      </form>




test.php

CODE

<?
include("../inc/dbconn.inc");

$conn = mysql_connect($hostname, $username, $password);
mysql_select_db($dbname, $conn);
$Id = $_REQUEST['book'];
$sendUser = $_POST['user'];
$no1=$_POST['q1'];
$no2=$_POST['q2'];
$no3 = $_POST['q3'];

$query1 = "INSERT INTO tempahan (Pengguna,produk,5kg,10kg,15kg) VALUES ('$sendUser','$Id','$no1','$no2','$no3')";

if((mysql_query($query1)))
    {
    
        Header("Location:TmphBaru.php");

    }
        else die (mysql_error());

?>

User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Using Array To Insert Into New Table
15 Mar, 2008 - 01:01 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,232



Thanked: 220 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
Is the 5kg, 10kg, and 15kg columns of the datatype "int" in the database? If so, you should remove the single quotes in your insert query to make it look like this...

CODE

$query1 = "INSERT INTO tempahan (Pengguna,produk,5kg,10kg,15kg) VALUES ('$sendUser','$Id',$no1,$no2,$no3)";


See if that works first.

If that doesn't, put in a print_r($_POST) statement in test.php to see if the quantity is even being seen by the page. Let us know the results.

smile.gif
User is online!Profile CardPM
+Quote Post

imaro
RE: Using Array To Insert Into New Table
16 Mar, 2008 - 11:07 AM
Post #3

New D.I.C Head
*

Joined: 16 Feb, 2008
Posts: 10


My Contributions
QUOTE(Martyr2 @ 15 Mar, 2008 - 02:01 PM) *

Is the 5kg, 10kg, and 15kg columns of the datatype "int" in the database? If so, you should remove the single quotes in your insert query to make it look like this...

CODE

$query1 = "INSERT INTO tempahan (Pengguna,produk,5kg,10kg,15kg) VALUES ('$sendUser','$Id',$no1,$no2,$no3)";


See if that works first.

If that doesn't, put in a print_r($_POST) statement in test.php to see if the quantity is even being seen by the page. Let us know the results.

smile.gif





thanks for your concern...
i've already find a solution for my problem...
althought the solution did not look like an advanced solution,
but i already satisfied with it.....
and i will try your suggestion...
anyway..thanks....
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 10:57PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month