gex80 Posted April 7, 2011 Share Posted April 7, 2011 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] Quote Link to comment https://forums.phpfreaks.com/topic/233029-php-array-not-saving-to-database/ Share on other sites More sharing options...
dawsba Posted April 7, 2011 Share Posted April 7, 2011 set your rmanumber to autoincrement in the db, your probably getting conflict in your index Quote Link to comment https://forums.phpfreaks.com/topic/233029-php-array-not-saving-to-database/#findComment-1198479 Share on other sites More sharing options...
gex80 Posted April 7, 2011 Author Share Posted April 7, 2011 Thanks it was $rmanumber that was causing the problem. I guess it's better off that it isn't random then. Quote Link to comment https://forums.phpfreaks.com/topic/233029-php-array-not-saving-to-database/#findComment-1198487 Share on other sites More sharing options...
dawsba Posted April 8, 2011 Share Posted April 8, 2011 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;} } Quote Link to comment https://forums.phpfreaks.com/topic/233029-php-array-not-saving-to-database/#findComment-1198493 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.