I have to resurrect the article that I marked solved this am, 'cause it ain't!
My code is:

<?php
if(isset($_POST['chest'])){
$pin=$_COOKIE['registered'];
$game=$_POST['item'];
foreach($game as $rep){
$link=sqlite_open('data/'.$pin.'.sqlite ',0666,$sqliteerror);
sqlite_query($link,"INSERT INTO holding (item)
VALUES('$rep')");
sqlite_query($link,"UPDATE players SET points = points+10");
header('location:entrance.php');
}

}
include('php/head.php');
?>
<div class="game">
<p><u>Chest</u></p>
<p> The chest has several items inside,<br>
A Food parcel, a small pocket knife, a long scarf, a torch 
and a key on a ring.
</p>
<form action="" method="post">
<div class='form'>
<fieldset>
<legend>Choose Items To Keep</legend>
<div class='labels'>
<?php
$res=sqlite_query($link,"SELECT * FROM chest ");
while($row=sqlite_fetch_array($res)){
    $cont=explode(",",$row['item']);
foreach($cont as $item){
        echo ucwords($item)."<br>";
    }
  }
?>

</div>
<div class='input'>
<input type='checkbox' name='item[]' value='food'><br>
<input type='checkbox' name='item[]' value='knife'><br>
<input type='checkbox' name='item[]' value='torch'><br>
<input type='checkbox' name='item[]' value='scarf'></br>
<input type='checkbox' name='item[]' value='key'></br>
</div>
<div class='send'>
<input type='submit' name='chest' value='Keep'>
</div>
</fieldset>
</div>
</form>
</div>

now, what I wan't to do is, after the insert query on line 7, use the '$rep' variable to delete the same items from the 'chest' table, (line 28),or use array_diff to make a new array so I can update the 'chest' table.
I'v tried every combination of array functions etc that I can think of or find, but I bet I've missed the one that works eh?

Recommended Answers

All 8 Replies

Member Avatar for LastMitch

now, what I wan't to do is, after the insert query on line 7, use the '$rep' variable to delete the same items from the 'chest' table, (line 28),or use array_diff to make a new array so I can update the 'chest' table.

OK, I'm not sure what you are talking about. Maybe you can explain it more. So I can understand it better.

This is line 7:

sqlite_query($link,"INSERT INTO holding (item) VALUES('$rep')");

This is line 28:

$res=sqlite_query($link,"SELECT * FROM chest ");

So you want to create a new query that Delete the item from the 'chest' table?

If not, then explain more since I post the 2 line of code you mention.

So maybe I can see what you are trying to do.

Sorry, I didn't really explain that very well, did I? bad day!
What I would like to do is DELETE the items in the $game=$_POST['item'] var from the
'chest' database column.
The $game var could contain up to 10 items, as a comma sep list.
It's just when I've tried to read the contents of $game, the only output I get is 'Array'
I tried

$result=sqlite_query($link,"SELECT * FROM chest ");
   while($row=sqlite_fetch_array($result)){
    $first=explode(",",$row['dbitem']);
    }
    $game=$_POST['formitem'];
    $test=explode(",",$game);
    $test2="\$delete=array('".$test."');";
   $newarray=array_diff($first,$test2);
   echo $newarray;

but the output from that was an error.
Then I tried with double quotes, single quotes, no quotes, implode etc: I read and re-read the php manual, but still cant see it.

to clarify:
db table content = a,b,c,d
form input contents = a,c
difference = b,d
update table to = b,d

I really am having a bad time with this!
line 7 should read:

$test2="array('".$test."');";

(I think!)

Member Avatar for LastMitch

I realized you post 2 different $item?

$row['dbitem']
$row['item']

I'm gonna to used the last code that you post.

You can try this (I added unset and array_search and array_values).

From this:

$result=sqlite_query($link,"SELECT * FROM chest ");
while($row=sqlite_fetch_array($result)){
$first=explode(",",$row['dbitem']);
}
$game=$_POST['formitem'];
$test=explode(",",$game);
$test2="array('".$test."');";
$newarray=array_diff($first,$test2);
echo $newarray;

to this:

$result=sqlite_query($link,"SELECT * FROM chest ");
while($row=sqlite_fetch_array($result)){
$item=$row['dbitem'];
$first=explode(',', $item);
}

$game=$_POST['formitem'];
$test=array_filter(',', $game);
$test2=unset($test)
$newarray=array_values($first,$test2);
echo $newarray;

I didn't test it. I write it in my head. So if there's an error then it's good.

Let me know if it works or not.

@lastmitch, sorry no, outputs 'unexpected T_UNSET line 9,
added ';' , still same.
I think I'm over-complicating this so I'll start again.
This code:

<form action="" method="post">
   <input type='checkbox' name='item[]' value='food'><br>
   <input type='checkbox' name='item[]' value='knife'><br>
   <input type='checkbox' name='item[]' value='torch'><br>
   <input type='checkbox' name='item[]' value='scarf'></br>
   <input type='checkbox' name='item[]' value='key'></br>
   <input type='submit' name='send' value='Keep'>
</form>
<?php
$sent=$_POST['item'];
echo implode(" ",$sent)."<br>";

when,say, the first two boxes are ticked outputs:

food knife

and this code:

$link=sqlite_open('../data/3001.sqlite ',0666,$sqliteerror);
$result=sqlite_query($link,"SELECT * FROM chest");
while($row=sqlite_fetch_array($result)){
$try=$row['item'];
echo $try." ";

outputs:

food knife torch scarf key

so how come when its all put together, this code:

<form action="" method="post">
   <input type='checkbox' name='item[]' value='food'><br>
   <input type='checkbox' name='item[]' value='knife'><br>
   <input type='checkbox' name='item[]' value='torch'><br>
   <input type='checkbox' name='item[]' value='scarf'></br>
   <input type='checkbox' name='item[]' value='key'></br>
   <input type='submit' name='send' value='Keep'>
</form>
<?php 
if(isset($_POST['send'])){
$sent=$_POST['item'];
echo implode(" ",$sent)."<br>";

$link=sqlite_open('../data/3001.sqlite ',0666,$sqliteerror);
$result=sqlite_query($link,"SELECT * FROM chest");
while($row=sqlite_fetch_array($result)){
$try=$row['item'];
echo $try." ";
}
$new= array_diff($try,$sent);
echo implode(",",$new);
}
?>

ouputs:

food knife
food knife torch scarf key

Warning: array_diff() [function.array-diff]: Argument #1 is not an array in C:\xampp\htdocs\abyss\test\test.php on line 20

Warning: implode() [function.implode]: Invalid arguments passed in C:\xampp\htdocs\abyss\test\test.php on line 21

So I need to turn the output of the sqlite_query into an array that php array_diff will recognise. Is this is where I'm going wrong, because I thought it was an array!

I'll mark this one solved, because I've decided to change the whole project to a completely different style, thanks for the help.

Member Avatar for LastMitch

@TonyG_cyprus

I apologized for replying back late. I was on away. Sorry about that.

This is error:

sorry no, outputs 'unexpected T_UNSET line 9,added ';' , still same.

I forgot to put a semi-colon on $test2=unset($test);

From this

$result=sqlite_query($link,"SELECT * FROM chest ");
while($row=sqlite_fetch_array($result)){
$item=$row['dbitem'];
$first=explode(',', $item);
}
$game=$_POST['formitem'];
$test=array_filter(',', $game);
$test2=unset($test)
$newarray=array_values($first,$test2);
echo $newarray;

to this:

$result=sqlite_query($link,"SELECT * FROM chest ");
while($row=sqlite_fetch_array($result)){
$item=$row['dbitem'];
$first=explode(',', $item);
}
$game=$_POST['formitem'];
$test=array_filter(',', $game);
$test2=unset($test);
$newarray=array_values($first,$test2);
echo $newarray;

Warning: array_diff() [function.array-diff]: Argument #1 is not an array in C:\xampp\htdocs\abyss\test\test.php on line 20

There's something wrong with your ['item'];

It's not an array.

Warning: implode() [function.implode]: Invalid arguments passed in C:\xampp\htdocs\abyss\test\test.php on line 21

The implode() function can't read $new

$new= array_diff($try,$sent);
echo implode(",",$new);

Thanks LastMitch, for your reply, but I've re-written the whole thing now.
changed the layout, and the way it all works. Probably could use less code to
do the same job, but 30-odd pages still comes in at only just over 1Mb, and thats with all the images.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.