Hello all,
Newbie and running out of steam here.
I have a form passing the checkbox value to a db.
The db is essentially 2 fields news and id (auto-increment).
I'm pretty baffled as how to extract the data.
Ideally I would like to give each form submission a unique ID -
That's probably for another post.
<form method="post" action="processcheck.php">
<p><input type="checkbox" name="news[]" value="newspaper1"> Guardian<br />
<input type="checkbox" name="news[]" value="independent"> Independent<br />
<input type="checkbox" name="news[]" value="observer"> Observer<br />
<input type="checkbox" name="news[]" value="scotsman"> Scotsman<br />
<input type="checkbox" name="news[]" value="herald"> Herald</p>
<div class="row">
<span class="formlabel"></span>
<span class="forminput"><input type="submit" value="Submit" class="submit" /> <input type="reset" class="submit" /></span>
</div>
</form>
processcheck.php
<?php
$dbh = mysql_connect('localhost', 'xxxxxx', 'xxxxxx') or die(mysql_error());
mysql_select_db('xxxxxx') or die(mysql_error());
?>
<?php
$news = implode(",",$_POST['news']);
for($i = 0; $i < sizeof($news); $i++) {
$query = "INSERT INTO cBox (news) VALUES ('".$news."')";
$result = mysql_query($query) or die(mysql_error());
}
mysql_close($dbh);
// Echo to the browser.
if(count($news) == 0){echo "<p>No Checkbox selected !</p>";}
if (count($news) > 0) {
for ($i=0;$i<count($news);$i++) {
echo "<li>$news[$i] \n";
}
}
?>
Now I can see the db being populated with "array".
I created a page for testing and to extract the db data - This is where I'm baffled.
getnews.php
<?php
//Connecting to the db here
?>
<?php
$result = mysql_query("SELECT * news FROM cBox");
echo "<hr />".$result."<hr />";
while($info = mysql_fetch_array($news, MYSQL_ASSOC)) {
//echo "<hr />".$info."<hr />";
//echo "<hr />".$news."<hr />";
//exit();
$news = explode(",", $info['news']);
foreach ($news as $value){
echo $value;
echo "<br/>";
//echo "<li>$value\n";
}
}
?>