Hello all!
Well after scratching my head for too long, I give in, I've got to ask or I'll go crazy!
I've been fiddling with PHP/MySQL again recently and although I successfully get a search of the db via a form field and have found a way* of adding a checkbox and update multiple records (a popular request I think!), my next hurdle is combining them.
I was hoping I could mash the script together (logically with my limited knowledge) to produce a 3 stage script. Page1 would send Page2 the search term (works so far) and then Page2 would send itself(and the db) those recorded selected for updating.
So far, when I try to I'm getting many errors, and I believe they occur because the script is trying to get information that's not there (from the search). Also it's because I've tried to modify existing code and I'm really guessing now :-(
Here's the code on the second page, the first page simply has a $_POST in it and then the second page collects that info:
<?php
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
if(isset($_POST['projno-search'])) $search = $_POST['projno-search'];
if(isset($_POST['checkbox'])){$checkbox = $_POST['checkbox'];
if(isset($_POST['activate'])?$activate = $_POST["activate"]:$deactivate = $_POST["deactivate"])
$id = "('" . implode( "','", $checkbox ) . "');" ;
$sql="UPDATE $tbl_name SET invoiced = '".(isset($activate)?'Y':'N')."' WHERE entryref IN $id" ;
$result = mysql_query($sql) or die(mysql_error());
}
$sql="SELECT * FROM $tbl_name WHERE projno=$search";
// $sql="SELECT * FROM $tbl_name ";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
?>
<head>
<title>Checkboxes</title>
</head>
<body>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td><form name="frmactive" method="post" action="">
<table width="400" border="0" cellpadding="3" cellspacing="1">
<tr>
<td colspan="5"><input name="activate" type="submit" id="activate" value="Invoiced" />
<input name="deactivate" type="submit" id="deactivate" value="Not Invoiced" /></td>
</tr>
<tr>
<td> </td>
<td colspan="4"><strong>Update multiple rows in mysql with checkbox</strong> </td>
</tr><tr>
<td align="center"><input type="checkbox" name="allbox" /></td>
<td align="center"><strong>Entry Ref</strong></td>
<td align="center"><strong>Project No.</strong></td>
<td align="center"><strong>Description</strong></td>
<td align="center"><strong>Hours</strong></td>
<td align="center"><strong>Invoiced</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['entryref']; ?>"></td>
<td><?php echo $rows['entryref']; ?> </td>
<td><?php echo $rows['projno']; ?> </td>
<td><?php echo $rows['description']; ?> </td>
<td><?php echo $rows['hours']; ?> </td>
<td><?php echo $rows['invoiced']; ?> </td>
</tr>
<?php
}
?>
<tr>
<td colspan="5" align="center"> </td>
</tr>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
Here's the page when I click on one of the invoice buttons (invoiced/not invoiced):
Notice: Undefined variable: search in C:\wamp\www\Version2\klw-checkboxes.php on line 22
Line 22: $sql="SELECT * FROM $tbl_name WHERE projno=$search";
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\wamp\www\Version2\klw-checkboxes.php on line 26
Line 26: $count=mysql_num_rows($result);
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\wamp\www\Version2\klw-checkboxes.php on line 58
Line 58: while($rows=mysql_fetch_array($result)){
It might be worth knowing that security isn't an issue as this script is run offline via WAMPServer (a brilliant freebee for testing!).
Well, I really look forward to any comments, so thank you in advance! I'm guessing it's because I've interferred too much, using my inexperienced hands!
- I found a script close to the script I wanted on the net and tweaked it to work for me.