Note that there are some explanatory texts on larger screens.

plurals
  1. POTrouble with updating table with arrays
    primarykey
    data
    text
    <p>After being stuck trying to get a grip of how this works, i've decided to ask for some hints to how I can do this.</p> <p>Let me start by saying I have next to no experience with anything like this. I've just gathered some logic by looking at the code and i'm almost done with my project, except for this issue that i've come by.</p> <p>This is the original code (which i am still using another place in the code, this appears to be working just fine).</p> <pre><code>function showCart() { global $db; $cart = $_SESSION['cart']; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } $output[] = '&lt;form action="kurv.php?action=update" method="post" id="cart"&gt;'; $output[] = '&lt;table&gt;'; foreach ($contents as $id=&gt;$qty) { $sql = 'SELECT * FROM varetabel WHERE varenr = '.$id; $result = $db-&gt;query($sql); $row = $result-&gt;fetch(); extract($row); $output[] = '&lt;tr&gt;'; $output[] = '&lt;td&gt;&lt;a href="kurv.php?action=delete&amp;varenr='.$id.'" class="r"&gt;Slet&lt;/a&gt;&lt;/td&gt;'; $output[] = '&lt;td&gt;'.$varenavn.'&lt;/td&gt;'; $output[] = '&lt;td&gt;DKK '.$pris.'&lt;/td&gt;'; $output[] = '&lt;td&gt;&lt;input type="text" name="qty'.$id.'" value="'.$qty.'" size="3" maxlength="3" /&gt;&lt;/td&gt;'; $output[] = '&lt;td&gt;DKK '.($pris * $qty).'&lt;/td&gt;'; $total += $pris * $qty; $output[] = '&lt;/tr&gt;'; $_SESSION['items'] = $contents; // Antal forskellige varer. $_SESSION['qty'] = $qty; } $output[] = '&lt;/table&gt;'; $output[] = '&lt;p&gt;Pris total: &lt;strong&gt;DKK '.$total.'&lt;/strong&gt;&lt;/p&gt;'; $output[] = '&lt;div&gt;&lt;button type="submit"&gt;Opdatér kurv&lt;/button&gt;&lt;/div&gt;'; $output[] = '&lt;/form&gt;'; } else { $output[] = '&lt;p&gt;Kurven er tom.&lt;/p&gt;'; } return join('',$output); </code></pre> <p>}</p> <p>global $db is connecting to the DB and such, no biggie, and then there is this function from an include:</p> <pre><code> function fetch () { if ( $row=mysql_fetch_array($this-&gt;query,MYSQL_ASSOC) ) { return $row; } else if ( $this-&gt;size() &gt; 0 ) { mysql_data_seek($this-&gt;query,0); return false; } else { return false; } } </code></pre> <p>Now, im trying to use the same method to take the arrays from this code and put them into my database with this code:</p> <pre><code>session_start(); global $db; $items = $_SESSION['items']; $cart = $_SESSION['cart']; $qty = $_SESSION['qty']; if(isset($_SESSION['email'])) { // IF LOGGED IN $sql = mysql_query("SELECT * FROM antalstabel ORDER BY ordrenr DESC LIMIT 1") or die(mysql_error()); $row = mysql_fetch_assoc($sql); $maxordrenr = $row['ordrenr']; $nextnumber = $maxordrenr + 1; $antal = count($items); // COUNTS DIFFERENT ITEMS IN CART. $maxplusantal = $maxordrenr + $antal; $varenrplaceholder = 0; for ($i = $maxordrenr; $i &lt;= $maxplusantal; $i++) { $sql = mysql_query("INSERT INTO antalstabel (ordrenr, varenr) VALUES ('$nextnumber','$varenrplaceholder')") or die(mysql_error()); $nextnumber--; $varenrplaceholder++; } $varenrplaceholder = 0; if ($cart) { $items = explode(',',$cart); $contents = array(); foreach ($items as $item) { $contents[$item] = (isset($contents[$item])) ? $contents[$item] + 1 : 1; } foreach ($contents as $id=&gt;$qty) { $sql = 'UPDATE antalstabel SET varenr = '.$id.' and antal = '.$qty.' WHERE ordrenr = '.$nextnumber.' and varenr = '.$varenrplaceholder.''; $result = $db-&gt;query($sql); $row = $result-&gt;fetch(); extract($row); $varenrplaceholder++; } } </code></pre> <p>But it does not appear to be working properly, the code does run, but it does not update the tabel with the values of the arrays.</p> <p>Am i completely wrong or can anyone help me with this issue, i know it is a lot to ask.</p> <p>my own code creates X rows with the same ID depending on how many different items there is in the cart, I am trying to update those existing rows that i've just created with the values i presume is still in the arrays of the <code>showCart()</code> function.</p> <p>Thank you in advance</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload