Jump to content
Killersites Community

makamo66

Member
  • Content Count

    15
  • Joined

  • Last visited

Community Reputation

0 Neutral

About makamo66

  • Rank
    Member
  1. makamo66

    Undefined index error

    How do I get my session to be unset? My form looks like this: foreach ($joined as $i => $qty){ echo "<form action='' method='get' name='yourForm'>"; echo "<button type='submit' name='remove_" . $i . "' value='remove_" . $i . "' class='deletebtn' >X</button>"; echo "</form>"; } My SESSION is defined like this: for($i=0; $i<=5; $i++){ if (isset($_REQUEST["element_id_$i"]) ) { $_SESSION["element_id_$i"] = $_REQUEST["element_id_$i"]; $id = $_SESSION["element_id_$i"]; array_push($_SESSION["element_id"],$id); } $id = $_SESSION["element_id"]; } and the form submits to: if (isset($_REQUEST["remove_$i"]) ){ unset($_SESSION["quantity[$i]"]); unset($_SESSION["element_id[$i]"]); var_dump($_SESSION["element_id"]); var_dump($_SESSION["quantity"]); echo "Received variable " . $_REQUEST["remove_$i"]; echo 'TARGET INDEX TO BE REMOVED: ' . $_SESSION["element_id[$i]"] . '<br><br>'; } The output is: array(1) { [0]=> string(1) "1" } array(1) { [0]=> string(1) "1" } Received variable remove_1 Notice: Undefined index: element_id[1] in C:\xampp\htdocs\TopView\cart5.php on line 147 TARGET INDEX TO BE REMOVED:
  2. makamo66

    Simple Form Submission Doesn't Work

    When I remove exit it still doesn't work.
  3. The file test2.php consists of these simple forms: <?php for($i=0; $i<=5; $i++){ echo "<form action='reset4.php' method='get' name='yourForm'>"; echo "<button type='submit' value='delete' name='remove_" . $i . "' class='deletebtn'>X</button>"; echo "</form>"; } ?> It submits to reset4.php which is this simple code: <?php header("Location: test2.php"); exit; for($i=0; $i<=5; $i++){ if (isset($_REQUEST["remove_$i"])){ echo "Deleted"; }} ?> But it doesn't work. The $_REQUEST doesn't populate the address field and it apparently never gets submitted to reset4.php like it should. This is such a simple program, I can't imagine why it doesn't work.
  4. This is the cart array: $cart = array( "1" => "2", "3" => "4", "5" => "6"); var_dump($cart); array(3) { [1]=> string(1) "2" [3]=> string(1) "4" [5]=> string(1) "6" } This is the cart items array: $cart_items = array(); $cart_items["id"] = $id; $cart_items["quantity"] = $quantity; var_dump($cart_items); array(2) { ["id"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } ["quantity"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } } I want the key to be "id" and the value to be "quantity" like it is for the cart array. How do I define my cart_items array using variables?
  5. makamo66

    Update Shopping Cart

    If you were working on a PHP shopping cart using SESSION variables for the cart item's product id and quantity, how would you update the cart if the same product were chosen? If the same product id was posted, how would you add the quantities and show just the same product?
  6. This is the code that I'm using for a shopping cart in order to update the quantity if the same product id is chosen. $cart_items is a two-dimensional session array composed of the id session array and the quantity session array. While looping through $cart_items, I create a temporary array $temp_array so that I can execute some logic on the array without changing the normal output. $temp_array is the $cart_items array without the last items which would be the $_REQUEST id and the $_REQUEST quantity. So the two $_REQUESTs are popped off with array_pop and then the remaining $_SESSIONs are compared to the $_REQUEST id with $j = array_search($_REQUEST["element_id_$i"], $temp_array); $j is now the key that corresponds to the id in $temp_array where the $_REQUEST id has been found. if( $j==$temp_array[$i]) If $j is the key of the item in $temp_array, then the $_REQUEST is unset and the quantities for the item are added together. There are two problems with this code. First of all the var_dump of the $temp_array is always an empty array but I am expecting to get an array with just the last element popped off. Secondly, I get the error "Undefined offset" for the $i variable in this line of the code: if( $j==$temp_array[$i]) foreach($cart_items as $item) { foreach($item["id"] as $key => $val) { foreach($item["quantity"] as $i => $value) { if ($key == $i){ $temp_array = $cart_items; $array = array_pop($temp_array); var_dump($temp_array); if (isset($_REQUEST["element_id_$i"]) && isset($_REQUEST["quantity_$i"])&& isset($value)){ $j = array_search($_REQUEST["element_id_$i"], $temp_array); if( $j==$temp_array[$i]) { echo "<b>SAME PRODUCT ADDED</b>"; $value += $_REQUEST["quantity_$i"]; unset($_REQUEST["element_id_$i"]); }}}}}}
  7. makamo66

    Looping over a multidimensional array

    I retract my original question because I don't need to make my multidimensional-array any larger after all. Please ignore my post.
  8. makamo66

    Looping over a multidimensional array

    The solution can't include nested foreach loops because I will be adding 3 more inner arrays to the multidimensional array and when I do this with nested foreach loops, I need 6 of them.
  9. <?php $_SESSION["cart_item"] = array( 'cart_item' => array( 'id' => $id, 'product_name' => $product_name )); } $cart_items = $_SESSION["cart_item"]; foreach ($cart_items as $cart_item) { echo $cart_item["id"] . $cart_item["product_name"]; } ?> I have tried several variations of the foreach loop like the one above and I mostly get the error message: Notice: Array to string conversion. When I use: var_dump($cart_items); I get the following output: array(1) { ["cart_item"]=> array(2) { ["id"]=> array(2) { [0]=> string(1) "2" [1]=> string(1) "3" } ["product_name"]=> array(2) { [0]=> string(19) "Adult Female Bike" [1]=> string(18) "Kids Unisex Bike" } } }
  10. I want to use array_pop to remove the last item added to the SESSION array. So it would remove the REQUEST item. The problem is that nothing is left in the session array because I pop off the last item each time that the form is submitted. Maybe I need to use array_push to push the popped items back on to the SESSION array after the logic has been applied. I don't understand how array_pop is supposed to work on a SESSION array. Can anyone explain to me what behaviour to expect? I expected to get a SESSION array with the REQUEST item removed but I thought that I would still have all of my SESSION items.
  11. The solution posted to a different forum was: $sql = "INSERT INTO comment_table (name,date_col,website,comment) VALUES (?, ?, ?, ?)";
  12. I changed the code so that it is using prepared statements but I just get new errors now. I altered the table so that the date_col column has the data type of varchar. I get the following warning: mysqli_stmt_bind_param(): Number of variables doesn't match number of parameters in prepared statement if(isset($_REQUEST["submit"]) ) { $date = new DateTime('', new DateTimeZone('America/New_York')); $name = mysqli_real_escape_string($connection, $_REQUEST["name"]); $date_col = $date->format('M d, Y H:i'); $website = $_REQUEST["website"]; $comment = mysqli_real_escape_string($connection, $_REQUEST["comment"]); $sql = "INSERT INTO comment_table (name,date_col,website,comment) VALUES ('$name', '$date_col','$website','$comment')"; if($stmt = mysqli_prepare($connection, $sql)){ // Bind variables to the prepared statement as parameters mysqli_stmt_bind_param($stmt,'ssss',$name,$date_col,$website,$comment); // Attempt to execute the prepared statement if(mysqli_stmt_execute($stmt)){ echo "Records inserted successfully."; } else{ echo "ERROR: Could not execute query: $sql. " . mysqli_error($connection); } } else{ echo "ERROR: Could not prepare query: $sql. " . mysqli_error($connection); } // Close statement mysqli_stmt_close($stmt); // Close connection mysqli_close($connection);
  13. I added a column to my database with SQL as follows: ALTER TABLE comment_table ADD date_col Datetime NOT NULL; I try to insert into the database with the following PHP but nothing gets inserted. if(isset($_POST["submit"])) { $date_col = "test";//this will be DateTime later $name = $_POST["name"]; mysqli_query($connection, "INSERT INTO comment_table (name, date_col) VALUES ('$name', '$date_col')"); } $comsql = "SELECT * FROM comment_table"; $comres = mysqli_query($connection, $comsql); while($comr = mysqli_fetch_assoc($comres)){ ?> <div class="row"> <p>Name: <strong><?php echo $comr['name']; ?></strong>This is the code that is being pointed to as undefined index. <?php echo $comr['date_col']; ?> </p> <?php } ?> </div> The data I'm using for the date_col column is varchar and it should be Datetime but I don't know if that's the reason for the error. I would like to set $date_col equal to a Datetime expression for testing purposes but the formatting I chose wasn't working either.
  14. The code didn't actually make the call to unset the variables until after the not empty conditional was asked. I was misled because var_dump still printed out an empty session array. Once I changed the order of my code, it worked.
  15. When I use the following code, it doesn't print out "else if test" until after I refresh the page. unset($_SESSION["product"]); if ( !empty($_SESSION["product"]) && ($_SESSION["product"] != NULL)) { echo "if test"; } else if { echo "else if test"; } When I use: var_dump($_SESSION["product"]); It shows: array(0) { }
×