Add to Database based on Checkbox

Hi guys, i’m a total noob, trying desperately to create a little script just for a fun project in the hope to learn along the way. It’s probably very basic for most out there, but for me it is far from it. So what i have at the moment is simply information from a database displayed with check boxes beside it. What i would like is to add “DiscNo” to MYTABLE if the check box has been selected.

My Code so far :
[php]

<?php $con=mysqli_connect("localhost","kslc_admin","password","kslc_cdg"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT DISTINCT DiscNo, Description FROM CDG ORDER BY DiscNo"); echo " Toggle All
"; echo ""; while($row = mysqli_fetch_array($result)) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
Tag Description DiscNo View Songs
" . $row['Description'] . "" . $row['DiscNo'] . "" . $row['TrackNo'] . "View Tracks
"; mysqli_close($con); ?>[/php]

I have absolutely no idea how to go about this, so any help at all will be greatly appreciated.
Thanks

When you say “ADD DiscNo to MyTable” what do you mean?

You want to allow people to INSERT new discs? Or UPDATE Existing Discs?

Either way, I don’t see where you give people the opportunity to insert or update information in the HTML you provided.

You’re going to need some html Input fields.

Hey thanks for your reply, sorry for poor information, i guess i can’t really explain what i don’t understand. However since posting i have been digging and experimenting.
Basically my code produces a list of Disc Numbers (DiscNo) with a checkbox beside each. What i want is to be able to let people choose 1 or more discs by check box and somehow add those to a column on a db table. I may have got a little further but not sure

[php]

<?php $con=mysqli_connect("localhost","kslc_admin","password","kslc_cdg"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " . mysqli_connect_error(); } $result = mysqli_query($con,"SELECT DISTINCT DiscNo, Description FROM CDG ORDER BY DiscNo"); echo " Toggle All
"; echo ""; echo ""; while($row = mysqli_fetch_array($result)) { echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; echo ""; } echo "
Tag Description DiscNo View Songs
" . $row['Description'] . "" . $row['DiscNo'] . "" . $row['TrackNo'] . "View Tracks
"; echo ""; echo ""; mysqli_close($con); ?>[/php]

Now what happens is that a url is passed to post.php containing the checkbox name (which is also the DiscNo) it looks like this

http://mydomain.com/post.php?SF155=on&SF163=on&SF179=on&submit=Submit+

Where SF155, SF163 & SF179 are the checkbox names which i wish to save to a column on my db table.

I’m not sure if i am going the right direction and really stumped about how to advance… Do i make any sense ?

And no i don’t wish to allow people to insert new discs, basically i am allowing them to create their own list from my existing database of discs.

Ok you’re heading in the right direction…

What is the table you’re saving it to and what are the column names?

Awesome … thank the lord !!

Ehm the table is “members” and the column is “discno”

Thanks again.

I need a little more info

Table Name: Members
Column Name: DiscNo

I’m sure you have more then just 1 column in this table.

Yeh there are more columns but i didn’t think they were relevant “id” “usr” “discno” “pass” “email” …

But really all i need to know is how to pass the checkbox name into “discno”
Obvioulsy i plan to adapt with a WHERE condition to detect the session id of the signed in user but i think i can handle that bit myself … i hope lol

Well it makes a big difference…

Because, from what I’m seeing now from what you shared it looks like you have your database set up incorrectly…

Table Name: Members
Column Name: DiscNo
Column Name: ID
Column Name: Usr
Column Name: Pass
Column Name: email

This tells me that you are going to store the information Comma separated? Which means if you ever wanted to do a query to see how many people choose DiscNo 5 you’re going to have speed optimization issues. It also effects your insert query and update queries, because you’re going to allow people to change their selection?

So I propose you break this out into two tables linked by the User ID.

Table Name: Members
Column Name: ID
Column Name: Usr
Column Name: Pass
Column Name: email

Table Name: Members_DiscNo_Selection
Column Name: User_ID
Column Name: DiscNo
Primary Key (User ID and DiscNo)

Now the database is in 3rd Normal Form and you have a proper relationship with the right constraints on the table.

So populated it will look like this

User ID, DiscNo
1 5
1 7
2 1
2 2
2 3

Then your select queries will need to change to pull back the users selection…

In order to help you, I needed to know how you’re storing the data. Without knowing that I can’t point you in the right direction.

I get where, you are coming from !!! I can see the logic behind that !!

Not realising how important this was i have actually changed my membership script, which creates different tables etc … Would it make sense to provide you with a full copy of my database ?

There’s nothing sensitive in it …

No, I don’t need your database…

I’m not coding it for you, but just trying to help you along where you get stuck, break things into mini-steps, accomplish those mini-steps and move forward. If you have a problem on a mini-step ask for help. People are going to offer more help on a small issue vs a big issue.

Re-designing your database was the first big step, now that you’re done with that, it’s just code.

No i wouldn’t expect you to code it for me at all …I greatly appreciate you have gone over and above already !!! I understand your advise on the database and have redesigned it accordingly, i now know what to do and available documentation is easily enough understood to work this out, but the part i can’t get my head around was the intial bit where i capture the DiscNos from the passed URL after hitting the submit button.

[php]http://mydomain.com/post.php?SF155=on&SF163=on&SF179=on&submit=Submit+[/php]

This

[php]echo “

<input type=“checkbox” name=”. $row[“DiscNo”] ." id=“checkbox”>";
[/php]

Should be set to an array using the []

[php]echo “

<input type=“checkbox” name=”. $row[“DiscNo”] ." id=“checkbox[]”>";[/php]

Then cycle through the results and add them to you database.

[php]<?php

foreach($_POST[‘checkbox’] as $discno) {
echo “discno= $discno
”;
//Do you insert statement here.
}

?>[/php]

Thanks again for your help, i have added the third piece of code to post.php but i haven’t as yet written the INSERT statement as it stands post.php displays this error :

Warning: Invalid argument supplied for foreach() in /home/kslchost/public_html/post.php on line 2

Now maybe i’m just being stupid in thinking that i can run that code on it’s own, i know it won’t do anything without an Insert statement but should it show that error as it is ?

Thanks again

Disregard my last … Having spent sometime checking through my code i found a space which was causing the problem on line 20 just after post before "

[php]echo "<form action=“post.php” method=“post " />”;[/php]

No i’m getting a list of discs on post.php like so

discno= SF176
discno= SF177
discno= SF178
discno= SF179

Is this what i should expect ?

[php]echo “

<input type=“checkbox” name=“checkbox[]” value=”. $row[“DiscNo”] ." id=“checkbox[]”>";[/php]

PHP cares about the input name not the id. And you probably want the value to show what was selected.

Hmmm that conflicts with what Topcoder advised … However i do appreciate your response. The direction which Topcoder provided is now working for me perfectly with the following code …

discs.php
[php] $result = mysqli_query($con,“SELECT DISTINCT DiscNo, Description FROM cdg ORDER BY DiscNo”);
echo “

<input type=“checkbox” onClick=“toggle(this)” /> Toggle All
”;
echo “<form action=“post.php” method=“post” />”;
echo "";

while($row = mysqli_fetch_array($result))
{
echo “

”;
echo “";
echo “”;
echo “”;
echo “”;
echo “”;
echo “”;
}
echo “
Tag Description DiscNo View Songs
<input type=“checkbox” value=”. $row[“DiscNo”] ." name=“checkbox[]”>” . $row[‘Description’] . “” . $row[‘DiscNo’] . “” . $row[‘TrackNo’] . “<a href=“tracks.php?DiscNo=” . $row[“DiscNo”] . “”>View Tracks
”;
echo "<input type=“submit” name=“submit” value=“Submit " />”;
echo “”;

mysqli_close($con);[/php]

post.php

[php]<?php
include(“include/session.php”);
$con=mysqli_connect(“localhost”,“kslc_admin”,“password”,“kslc_cdg”);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_POST[‘checkbox’]))
{
foreach($_POST[‘checkbox’] as $discno) {
echo “discno= $discno
”;

$result = mysqli_query($con,“INSERT INTO members_discno_selection (DiscNo, userid) VALUES (’$discno’, ‘$session->username’)”);
if($result){
echo “Successfully Added
”;
}
else{
echo “Query failed”;
}
}
}
?>[/php]

The only thing i need to do is prevent it from creating duplicate entries … Should i open a new thread ?

No you should just change your INSERT statement, notice the IGNORE Keyword? This will prevent the duplicates from happening.

Change:

[php]$result = mysqli_query($con,“INSERT INTO members_discno_selection (DiscNo, userid) VALUES (’$discno’, ‘$session->username’)”);
[/php]

To:

[php]$result = mysqli_query($con,“INSERT IGNORE INTO members_discno_selection (DiscNo, userid) VALUES (’$discno’, ‘$session->username’)”);[/php]

You truly are awesome !! Thank you once again !!

Sponsor our Newsletter | Privacy Policy | Terms of Service