Jump to content

PHP array not saving to database?


gex80

Recommended Posts

Hi, I'm still new to php and having some problems. Sorry about the code not being formatted properly. The snippet I posted has both the code intend to work but it is commented out and a test version that has an array already set thus getting rid of the need of setting up every thing like I have.

 

Ok this is basically my problem. I'm building a store like website. Its suppose to be very basic for now Part of the website is an RMA system where the customer enters an order number and then pulls up all items in the order on a different page in a table format with check boxes next to each item found in the database.  Each item that is checked off is then saved to an array rma[] which posts to another page when submitted. What this page(copied down below) does is it pulls values from the cookie already made and creates new variables which is not a problem. After that it does a foreach loop for the array of the items into a mysql query which then is suppose to insert it into a database.

 

When the code runs it only inserts the first item in the array into the database no matter how many items are in the array. When I echo the $insert statement I get this(using the test code) like I expect I'm suppose to. I would assume that because I'm doing a foreach loop that it is doing an INSERT for the number of items in the array.

 

INSERT INTO `project`.`rma` (rmanumber, AccountID, item, ordernumber) VALUES ('857442', '1','Saab','123')

INSERT INTO `project`.`rma` (rmanumber, AccountID, item, ordernumber) VALUES ('857442', '1','Volvo','123')

INSERT INTO `project`.`rma` (rmanumber, AccountID, item, ordernumber) VALUES ('857442', '1','BMW','123')

INSERT INTO `project`.`rma` (rmanumber, AccountID, item, ordernumber) VALUES ('857442', '1','Toyota','123')

 

<?php

//connects to the data base
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("project") or die(mysql_error()); 

/* //retrieving rma array from previous page
$rmarequest = $_POST['rma'];

//retieves using name from cookie
$username = $_COOKIE['ID_my_site'];

//retrieves order number from cookie
$ordernumber = $_COOKIE['order'];


//creates rma number
$rmanumber = rand(1000, 999999);

//retrieves accountid from database using the username
$getid = mysql_query("SELECT AccountID FROM users WHERE username = '$username'");
$id = mysql_fetch_array( $getid );

//takes the value of $id
$final = $id['AccountID'];


//suppose to loop through all values for the array created by $request on the previous page and insert into database

foreach($rmarequest as $key){		


// $insert = "INSERT INTO `project`.`rma` (rmanumber, AccountID, item, ordernumber) VALUES ('".$rmanumber."', '".$AccountID."','".$key."','".$ordernumber."')";
//mysql_query($insert) OR die(mysql_error());


//this is test to see what is being passed to mysql server
$insert = "INSERT INTO `project`.`rma` (rmanumber, AccountID, item, ordernumber) VALUES ('".$rmanumber."', '1','".$key."','123')";
//mysql_query($insert) OR die(mysql_error());

//to view what $insert is passing to the data base.
echo $insert;}
*/


//THIS SECTION IS TEST ONLY

//test array
$cars[0]="Saab";
$rmarequest[1]="Volvo";
$rmarequest[2]="BMW";
$rmarequest[3]="Toyota";


//creates rma number
$rmanumber = rand(1000, 999999);


foreach($rmarequest as $key){		

//this is test to see what is being passed to mysql server
$insert = "INSERT INTO `project`.`rma` (rmanumber, AccountID, item, ordernumber) VALUES ('".$rmanumber."', '1','".$key."','123')";
mysql_query($insert) OR die(mysql_error());

//to view what $insert is passing to the data base.
echo $insert."<br />";
}

?>

 

Included is a copy of the table and the php code above.

 

[attachment deleted by admin]

Link to comment
Share on other sites

you can have a random id if you really want you just have to parity check it

$check = 0;
while($check<=0)
{
  $check = rand(10000,99999);
  $sql = "SELECT COUNT(*) FROM `project`.`rma` WHERE `rmanumber` = '".$check."'";
  if(mysql_query($sql)>0){$check=0;}else{$rmanumber=$check;}
}

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.