1
    <?php
session_start();
include("configdb.php");
if(!session_is_registered(username)){
header("location:index.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Projects</title>
<link href="style.css" rel="stylesheet" type="text/css" />

<script type="text/javascript">

function un_check(){
for (var i = 0; i < document.frmactive.elements.length; i++) {
var e = document.frmactive.elements[i];
if ((e.name != 'allbox') && (e.type == 'checkbox')) {
e.checked = document.frmactive.allbox.checked;
}
}
}
function Confirm(form){
alert("Project has been activated!");
form.submit();
}
function unConfirm(form){
alert("Project has been Deactivated!");
form.submit();
}
</script>
</head>

<body>
<div id="costDiv">
<div id="divErc"></div>
<div id="costBack">

<?php

if(isset($_POST['checkbox'])){$checkbox = $_POST['checkbox'];
if(isset($_POST['activate'])?$activate = $_POST["activate"]:$deactivate = $_POST["deactivate"])

$id = "('" . implode( "','", $checkbox ) . "');" ;
$sql="UPDATE projects SET p_isActive = '".(isset($activate)?'1':'0')."' WHERE p_id IN $id";
$result = mysql_query($sql) or die(mysql_error());

}

?>
<?php include("hor_menu.php"); ?>


<form name="frmactive" method="post" action="">

<table width="350" border="0" cellspacing="1" cellpadding="5" align="center" style="margin-left:150px ; margin-right:auto ; margin-top:20px ; margin-bottom:auto ; position:absolute ; width:400px">

<tr>
<td align="center" ><input type="checkbox" name="allbox" onclick="un_check(this);" title="Select or Deselct ALL" style="background-color:#ccc;"/></td>

<td align="left"><strong>Project</strong></td>
<td align="left"><strong>Country</strong></td>
<td align="left"><strong>Active</strong></td>

</tr>

<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $rows['p_id']; ?>"/></td>
<td><?php echo $rows['p_name']; ?></td>
<td><?php echo $rows['p_country']; ?></td>
<td><?php if ($rows['p_isActive'] == '1'){ echo'Active';} else{ echo 'Inactive';} ?></td>

</tr>
<?php
}
?>
<tr>
<td colspan="5"><input name="activate" type="submit" id="activate" value="Activate" onClick="Confirm(this.form)" />
<input name="deactivate" type="submit" id="deactivate" value="Deactivate" onClick="unConfirm(this.form)"/></td>
</tr>
</table>


?>
</td>
</tr>
</table>
</form>
</body>
</html>

i have this code.when i check a checkbox project will be either activated or deactivated according to bottom.however this code works perfectly on all browsers except google chrome and safari.can anyone help please.it keeps giving that have an error in my sql syntax espeaciall after the where clause in the update query.thank you

Pascale Abou Abdo
  • 387
  • 1
  • 4
  • 14
  • 2
    I don't think `mysql` has anything related to browser. – Mihai Iorga Aug 23 '12 at 08:34
  • 2
    You should seriously sanitize your incoming data! – Ja͢ck Aug 23 '12 at 08:36
  • 1
    This is not legal: `id="checkbox[]"` get rid of it. – Ariel Aug 23 '12 at 08:37
  • Please output the `$sql` when the error happens. – Ariel Aug 23 '12 at 08:39
  • @Ariel The id is the problem, yeah. Add your own answer and take the credit ;) – Joakim Johansson Aug 23 '12 at 08:48
  • 2
    There is no problem with how Chrome behaves - it's actually Firefox that is too lenient and tries to fix your broken code. Please [read on](http://stackoverflow.com/questions/1397592/difference-between-id-and-name-attributes-in-html) about how `id` and `name` [work](http://stackoverflow.com/questions/7470268/html-input-name-vs-id) - in this case, you actually need `name`. – kapa Aug 23 '12 at 08:55

2 Answers2

0

If you get an SQL error depending on browser, then you most likely need to check your inputs. PHP execution does not differ depending on what browser you're using, other than what data it gets from client side controls like textboxes or triggers.

I suggest you log the $sql variable in some way, you could try just using var_dump, to see how the data changes between browsers.

Joakim Johansson
  • 2,922
  • 1
  • 24
  • 42
  • the update query doesnt read th id..when i used the car_dump,the result was : string 'UPDATE projects SET p_isActive = '0' WHERE p_id IN ' (length=51) – Pascale Abou Abdo Aug 23 '12 at 08:42
  • Then if you do `var_dump($id);` instead you should see that the variable isn't read from the checkbox. Most likely because $checkbox isn't set, because $_POST['checkbox'] doesn't contain anything. Your checkbox has `id="checkbox[]"` like Ariel says, this should be `id="checkbox"` – Joakim Johansson Aug 23 '12 at 08:45
  • it didnt work.the problem is that in firefox the query reads the id and everything works fine.however in chrome the id couldnt be read – Pascale Abou Abdo Aug 23 '12 at 08:51
  • Try doing `var_dump($_POST);` and you'll see all the content submitted by the browser. The name-attribute should set the key for the variable, and if it's not there, inspect your HTML and check what's wrong. – Joakim Johansson Aug 23 '12 at 08:57
  • @pascaleabouabdo The SQL string you posted makes no sense. It doesn't even include the parens you are placing in the `$id` variable. The code you posted, and the code you are running do not match. – Ariel Aug 23 '12 at 10:18
0

This is not legal: id="checkbox[]" get rid of it.

It's not necessary to assign everything an ID. Only assign an ID if you actually use it in the javascript. Otherwise just leave it out.

INPUT fields do need a name attribute, but that works completely differently from an ID.

Ariel
  • 23,798
  • 4
  • 53
  • 68
  • i removed the id..and there is already a name for all input fields..none of the above worked.still the same problem. – Pascale Abou Abdo Aug 23 '12 at 09:15
  • @pascaleabouabdo Please post the results of `print_r($_POST);`, the value of `$id` after you set it, and the value of `$sql` after you set it. – Ariel Aug 23 '12 at 10:19