Reputation: 19
echo "<form method='POST' action=''><br>";
$query = mysql_query("set names 'utf8'");
$query = mysql_query("
SELECT * FROM pages WHERE user_id = '$_SESSION[user_id]'
") or die(mysql_error());
while($pagek = mysql_fetch_array($query)) {
$name = $pagek['page_name'];
$pageid = $pagek['page_id'];
echo '<input type="checkbox" name="page_ids[]" value="'.$pageid.'">'
.$name
."<br>";
}
echo "<br>
<input type='submit'
value='Adatok frissítése'
name ='adat'
class='button small radius center black'
>";
echo "</form>";
foreach($_POST['page_ids'] as $page){
if ($page){
mysql_query("
UPDATE pages
SET page_value = '0'
WHERE user_id = '$_SESSION[user_id]'
");
mysql_query("
UPDATE pages
SET page_value = '1'
WHERE user_id = '$_SESSION[user_id]'
AND page_id = '$page'
");
}
}
I like, then if Which checkbox is checked this mysql update 1, and more than 0.
This code is not good, here in only a site update on getting the rest are zero, and you can always choose a site
Upvotes: 1
Views: 6554
Reputation: 174967
First!
Please, don't use
mysql_*
functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
Seriously, your code contains a serious vulnerability called SQL Injection. If you don't fix it, your website can and will be compromised very easily.
Unchecked checkboxes do not get submitted at all. So basically, your server won't see anything.
To take that into account, what you should do is to set it to 0 by default, unconditionally, and then, if the checkbox was checked, set it to 1.
Yes, it does require you to possible do 2 queries in a request, but it's the only way to ensure you get the result you need.
Upvotes: 3
Reputation: 19
Solve the task in the right solution:
echo "<form method='POST' action=''><br>";
$query = mysql_query("set names 'utf8'");
$query = mysql_query("SELECT * FROM pages WHERE user_id = '$_SESSION[user_id]' ") or die(mysql_error());
if(mysql_num_rows($query) == 0) {
echo "<tr><td colspan=\"3\">Nincs megjeleníthető Facebook oldalad! Kérlek jelentkezz be az FB Login menüpont alatt!</td></tr>";
} else {
echo "<u>Kérem válassza ki melyik oldalaira szeretne képet feltölteni!</u><br><br><br>";
while($pagek = mysql_fetch_array($query)) {
$name = $pagek['page_name'];
$pageid = $pagek['page_id'];
echo '<input type="checkbox" name="page_ids[]" value="'.$pageid.'">'.$name."<br>";
}
echo "<br><input type='submit' value='Adatok frissítése' name ='adat' class='button small radius center black'>";
echo"</form>";
if($_POST['adat']){
mysql_query("UPDATE pages SET page_value = '0' WHERE user_id = '$_SESSION[user_id]' ");
foreach($_POST['page_ids'] as $page){
$pa = $page;
if ($pa){
mysql_query("UPDATE pages SET page_value = '1' WHERE user_id = '$_SESSION[user_id]' AND page_id = '$page' ");
}
}
}
}
}
Upvotes: 0
Reputation: 1324
You could use isset()
to see if the checkbox has been checked.
if(isset($_POST['checkbox']))
{
$update = 1;
}
else
{
$update = 0;
}
Then add $update to your query. Hope this helps.
Upvotes: 3
Reputation: 10717
Try $_SESSION vars like:
UPDATE pages SET page_value = '0' WHERE user_id = {$_SESSION['user_id']}
UPDATE pages SET page_value = '1' WHERE user_id = {$_SESSION['user_id']} AND page_id = '$page'
Use mysql_real_escape_string
for more security:
$pages = mysql_real_escape_string(serialize($_POST['page_ids']));
foreach($pages as $page) {
// action
}
Upvotes: 0