dsjoes 0 Report post Posted January 13, 2011 These are the errors i keep getting Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /hermes/bosweb/web230/b2302/ipg.myaccount/test_server/admin/test1.php on line 48Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /hermes/bosweb/web230/b2302/ipg.myaccount/test_server/admin/test1.php on line 67 this is the code<?php// Set Global Vars$HOST = "XXXXXXXX";$USERNAME = "XXXXXXXXXXXX";$PASSWORD = "XXXXXXXX";$DATABASE = "testdocs";$TABLE = "docs";// Establish a connectionmysql_connect($HOST, $USERNAME, $PASSWORD) or die(mysql_error());mysql_select_db($DATABASE) or die(mysql_error());// Query for resultsif(!isset($_POST['delete'])) {$sql = "select * from `$TABLE` order by `id`;";} else {$DELETE_ID = mysql_real_escape_string($_POST['delete']);$result1 = mysql_query("select * from `$TABLE` where `id` = '$DELETE_ID';");$row = mysql_fetch_assoc($result1);mysql_close($result1);// Delete the file on the diskunlink('../admin/testimonial_files/'.$row['Download']);// Build your query, kind of weird but alright.$sql = ("delete from $TABLE where ");for($i=0;$i<count($_POST['checkbox']);$i++){if($i != 0) { $sql.= "AND "; }$sql .= " id='" . $_POST['checkbox'][$i] . "'";}$result = mysql_query($sql);if(isset($_POST['delete'])) { header('Location: index.php');exit;}}?><table align="center" width="400" border="0" cellspacing="1" cellpadding="0"><tr><td><form name="delete" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>"><table width="400" border="1" cellpadding="3" cellspacing="1"><tr><td colspan="6" align="center"><strong>Testimonials</strong> </td></tr><tr><td align="center"><strong>Select</strong></td><td align="center"><strong>ID</strong></td><td align="center"><strong>Name</strong></td><td align="center"><strong>Description</strong></td><td align="center"><strong>Download</strong></td><td align="center"><strong>Last Modified</strong></td></tr><?php $row = mysql_fetch_assoc($result); ?><?php do { ?> <tr> <td align="center"> <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['id']; ?>"> </td> <td align="center"><?php echo $row['id']; ?></td> <td align="center"><?php echo $row['Name']; ?></td> <td align="center"><?php echo $row['Message']; ?></td> <td align="center"><a href="/admin/docs/<?php echo $row['Download']; ?>">Download</a></td> <td align="center"><?php echo $row['Modified']; ?></td> </tr> <tr> <td colspan="6" align="center"><input name="delete" type="submit" id="delete" value="Delete"></td> </tr></table></form></td></tr></table><?php } while($row = mysql_fetch_assoc($result)); ?> and these are the 2 lines the errors are referring to<?php $row = mysql_fetch_assoc($result); ?><?php do { ?><?php } while($row = mysql_fetch_assoc($result)); ?> Share this post Link to post Share on other sites
sonesay 7 Report post Posted January 13, 2011 (edited) These are the errors i keep gettingthis is the code <?php// Set Global Vars$HOST = "XXXXXXXX";$USERNAME = "XXXXXXXXXXXX";$PASSWORD = "XXXXXXXX";$DATABASE = "testdocs";$TABLE = "docs";// Establish a connectionmysql_connect($HOST, $USERNAME, $PASSWORD) or die(mysql_error());mysql_select_db($DATABASE) or die(mysql_error());// Query for resultsif(!isset($_POST['delete'])) {$sql = "select * from `$TABLE` order by `id`;";} else {$DELETE_ID = mysql_real_escape_string($_POST['delete']);$result1 = mysql_query("select * from `$TABLE` where `id` = '$DELETE_ID';");$row = mysql_fetch_assoc($result1);mysql_close($result1);// Delete the file on the diskunlink('../admin/testimonial_files/'.$row['Download']);// Build your query, kind of weird but alright.$sql = ("delete from $TABLE where ");for($i=0;$i<count($_POST['checkbox']);$i++){if($i != 0) { $sql.= "AND "; }$sql .= " id='" . $_POST['checkbox'][$i] . "'";}$result = mysql_query($sql);if(isset($_POST['delete'])) { header('Location: index.php');exit;}}?><table align="center" width="400" border="0" cellspacing="1" cellpadding="0"><tr><td><form name="delete" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>"><table width="400" border="1" cellpadding="3" cellspacing="1"><tr><td colspan="6" align="center"><strong>Testimonials</strong> </td></tr><tr><td align="center"><strong>Select</strong></td><td align="center"><strong>ID</strong></td><td align="center"><strong>Name</strong></td><td align="center"><strong>Description</strong></td><td align="center"><strong>Download</strong></td><td align="center"><strong>Last Modified</strong></td></tr><?php $row = mysql_fetch_assoc($result); ?><?php do { ?> <tr> <td align="center"> <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['id']; ?>"> </td> <td align="center"><?php echo $row['id']; ?></td> <td align="center"><?php echo $row['Name']; ?></td> <td align="center"><?php echo $row['Message']; ?></td> <td align="center"><a href="/admin/docs/<?php echo $row['Download']; ?>">Download</a></td> <td align="center"><?php echo $row['Modified']; ?></td> </tr> <tr> <td colspan="6" align="center"><input name="delete" type="submit" id="delete" value="Delete"></td> </tr></table></form></td></tr></table><?php } while($row = mysql_fetch_assoc($result)); ?> and these are the 2 lines the errors are referring to<?php $row = mysql_fetch_assoc($result); ?><?php do { ?><?php } while($row = mysql_fetch_assoc($result)); ?> It looks like the return from mysql_query() is not valid. You should first check its valid before performing anything on it such as mysql_fetch_assoc()Try this first:$result = mysql_query("select * from `$TABLE` where `id` = '$DELETE_ID';");if (!$result) { die('Invalid query: ' . mysql_error());} else {// do what ever you need to do here.} Good luck. Edited January 13, 2011 by sonesay (see edit history) Share this post Link to post Share on other sites
dsjoes 0 Report post Posted January 14, 2011 It looks like the return from mysql_query() is not valid. You should first check its valid before performing anything on it such as mysql_fetch_assoc()Try this first: $result = mysql_query("select * from `$TABLE` where `id` = '$DELETE_ID';");if (!$result) { die('Invalid query: ' . mysql_error());} else {// do what ever you need to do here.} Good luck. nothing has changed it still shows the same messages Share this post Link to post Share on other sites
sonesay 7 Report post Posted January 14, 2011 nothing has changed it still shows the same messages Can you try a echo("select * from `$TABLE` where `id` = '$DELETE_ID';"); The first thing you should do here is to try and verify you are building the sql correctly. There may be times where the value $DELETE_ID might not be as you expected so take the generated sql string and run it against your mysql database manually to verify it is valid. Any syntax errors you get you will need to fix. Share this post Link to post Share on other sites
dsjoes 0 Report post Posted January 14, 2011 Can you try a echo("select * from `$TABLE` where `id` = '$DELETE_ID';"); The first thing you should do here is to try and verify you are building the sql correctly. There may be times where the value $DELETE_ID might not be as you expected so take the generated sql string and run it against your mysql database manually to verify it is valid. Any syntax errors you get you will need to fix. where do i put it i have tryed under the php but it doesn't echo anything Share this post Link to post Share on other sites
sonesay 7 Report post Posted January 14, 2011 where do i put it i have tryed under the php but it doesn't echo anything Try it here.$DELETE_ID = mysql_real_escape_string($_POST['delete']);$result1 = mysql_query("select * from `$TABLE` where `id` = '$DELETE_ID';");$row = mysql_fetch_assoc($result1);mysql_close($result1);// change to $DELETE_ID = mysql_real_escape_string($_POST['delete']);echo("select * from `$TABLE` where `id` = '$DELETE_ID';");die(); Share this post Link to post Share on other sites
dsjoes 0 Report post Posted January 14, 2011 Try it here. $DELETE_ID = mysql_real_escape_string($_POST['delete']);$result1 = mysql_query("select * from `$TABLE` where `id` = '$DELETE_ID';");$row = mysql_fetch_assoc($result1);mysql_close($result1);// change to $DELETE_ID = mysql_real_escape_string($_POST['delete']);echo("select * from `$TABLE` where `id` = '$DELETE_ID';");die(); still nothing Share this post Link to post Share on other sites
sonesay 7 Report post Posted January 14, 2011 still nothing That's very strange. Can you post your entire script again if its changed a lot? There should be some kind of output or error if something is wrong. Is anything being reported?The only other way to figure it out is to keep going back up a line and outing echo statements to make sure it the code actually reaches that point. You need to figure out exactly where it dying before you can resolve any issues. Share this post Link to post Share on other sites
dsjoes 0 Report post Posted January 14, 2011 That's very strange. Can you post your entire script again if its changed a lot? There should be some kind of output or error if something is wrong. Is anything being reported?The only other way to figure it out is to keep going back up a line and outing echo statements to make sure it the code actually reaches that point. You need to figure out exactly where it dying before you can resolve any issues. <?php// Set Global Vars$HOST = "XXXXXXX";$USERNAME = "XXXX";$PASSWORD = "XXXXX";$DATABASE = "XXXXX";$TABLE = "Testimonials";// Establish a connectionmysql_connect($HOST, $USERNAME, $PASSWORD) or die(mysql_error());mysql_select_db($DATABASE) or die(mysql_error());// Query for resultsif(!isset($_POST['delete'])) {$sql = "select * from `$TABLE` order by `id`;";} else {$DELETE_ID = mysql_real_escape_string($_POST['delete']);echo("select * from `$TABLE` where `id` = '$DELETE_ID';");die();// Delete the file on the diskunlink('../admin/testimonial_files/'.$row['Download']);// Build your query, kind of weird but alright.$sql = ("delete from $TABLE where ");for($i=0;$i<count($_POST['checkbox']);$i++){if($i != 0) { $sql.= "AND "; }$sql .= " id='" . $_POST['checkbox'][$i] . "'";}$result = mysql_query($sql);if(isset($_POST['delete'])) { header('Location: index.php');exit;}}?><table align="center" width="400" border="0" cellspacing="1" cellpadding="0"><tr><td><form name="delete" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>"><table width="400" border="1" cellpadding="3" cellspacing="1"><tr><td colspan="6" align="center"><strong>Testimonials</strong> </td></tr><tr><td align="center"><strong>Select</strong></td><td align="center"><strong>ID</strong></td><td align="center"><strong>Name</strong></td><td align="center"><strong>Description</strong></td><td align="center"><strong>Download</strong></td><td align="center"><strong>Last Modified</strong></td></tr><?php $row = mysql_fetch_assoc($result); ?><?php do { ?> <tr> <td align="center"> <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['id']; ?>"> </td> <td align="center"><?php echo $row['id']; ?></td> <td align="center"><?php echo $row['Name']; ?></td> <td align="center"><?php echo $row['Message']; ?></td> <td align="center"><a href="/admin/testimonial_files/<?php echo $row['Download']; ?>">Download</a></td> <td align="center"><?php echo $row['Modified']; ?></td> </tr> <tr> <td colspan="6" align="center"><input name="delete" type="submit" id="delete" value="Delete"></td> </tr></table></form></td></tr></table><?php } while($row = mysql_fetch_assoc($result)); ?> only messages i get are the ones i posted in the first post nothing new Share this post Link to post Share on other sites
sonesay 7 Report post Posted January 14, 2011 (edited) What is your page suppose to be doing exactly? From the looks of it you are checking if there are any submissions for delete. If there are none which is mostly the case when you frist visit the page then that block is always going to execute. Which explains why the rest of the code fails because the block of code that assigns the $result1 variable never gets executed. You might want to change your if statements around appropriately. if(!isset($_POST['delete'])) {$sql = "select * from `$TABLE` order by `id`;";} else {$DELETE_ID = mysql_real_escape_string($_POST['delete']);echo("select * from `$TABLE` where `id` = '$DELETE_ID';");die();// Delete the file on the diskunlink('../admin/testimonial_files/'.$row['Download']);// Build your query, kind of weird but alright.$sql = ("delete from $TABLE where ");for($i=0;$i<count($_POST['checkbox']);$i++){if($i != 0) { $sql.= "AND "; }$sql .= " id='" . $_POST['checkbox'][$i] . "'";} if you doif(!isset($_POST['delete'])) {$sql = "select * from `$TABLE` order by `id`;";die("post delete not set");} else {$DELETE_ID = mysql_real_escape_string($_POST['delete']);echo("select * from `$TABLE` where `id` = '$DELETE_ID';");die("result should be set here");// Delete the file on the diskunlink('../admin/testimonial_files/'.$row['Download']);// Build your query, kind of weird but alright.$sql = ("delete from $TABLE where ");for($i=0;$i<count($_POST['checkbox']);$i++){if($i != 0) { $sql.= "AND "; }$sql .= " id='" . $_POST['checkbox'][$i] . "'";} that should tell you where its executing. Edited January 14, 2011 by sonesay (see edit history) Share this post Link to post Share on other sites
dsjoes 0 Report post Posted January 14, 2011 What is your page suppose to be doing exactly? From the looks of it you are checking if there are any submissions for delete. If there are none which is mostly the case when you frist visit the page then that block is always going to execute. Which explains why the rest of the code fails because the block of code that assigns the $result1 variable never gets executed. You might want to change your if statements around appropriately. if(!isset($_POST['delete'])) {$sql = "select * from `$TABLE` order by `id`;";} else {$DELETE_ID = mysql_real_escape_string($_POST['delete']);echo("select * from `$TABLE` where `id` = '$DELETE_ID';");die();// Delete the file on the diskunlink('../admin/testimonial_files/'.$row['Download']);// Build your query, kind of weird but alright.$sql = ("delete from $TABLE where ");for($i=0;$i<count($_POST['checkbox']);$i++){if($i != 0) { $sql.= "AND "; }$sql .= " id='" . $_POST['checkbox'][$i] . "'";} if you doif(!isset($_POST['delete'])) {$sql = "select * from `$TABLE` order by `id`;";die("post delete not set");} else {$DELETE_ID = mysql_real_escape_string($_POST['delete']);echo("select * from `$TABLE` where `id` = '$DELETE_ID';");die("result should be set here");// Delete the file on the diskunlink('../admin/testimonial_files/'.$row['Download']);// Build your query, kind of weird but alright.$sql = ("delete from $TABLE where ");for($i=0;$i<count($_POST['checkbox']);$i++){if($i != 0) { $sql.= "AND "; }$sql .= " id='" . $_POST['checkbox'][$i] . "'";} that should tell you where its executing. that gives me the messagepost delete not setthe script is supposed to let me delete a selected row from my sql database and also delete the file from the download column Share this post Link to post Share on other sites
sonesay 7 Report post Posted January 14, 2011 that gives me the messagethe script is supposed to let me delete a selected row from my sql database and also delete the file from the download column Yeah I thought that's what you wanted but I wasn't too sure so I had to ask. all the code for the part where you delete needs to go in the correct if block in your statement. The part where you query for your result and use it in the mysql_fetch_assoc() needs to go in the else block. Try this:<?php// Set Global Vars$HOST = "XXXXXXX";$USERNAME = "XXXX";$PASSWORD = "XXXXX";$DATABASE = "XXXXX";$TABLE = "Testimonials";// Establish a connectionmysql_connect($HOST, $USERNAME, $PASSWORD) or die(mysql_error());mysql_select_db($DATABASE) or die(mysql_error());if (isset($_POST['delete'])) { // delete sql here $DELETE_ID = mysql_real_escape_string($_POST['delete']); unlink('../admin/testimonial_files/' . $row['Download']); $sql = ("delete from $TABLE where "); for ($i = 0; $i < count($_POST['checkbox']); $i++) { if ($i != 0) { $sql.= "AND "; } $sql .= " id='" . $_POST['checkbox'][$i] . "'"; } $result = mysql_query($sql); header('Location: index.php'); exit;} else { // select sql here $sql = "select * from `$TABLE` order by `id`;"; $result = mysql_query($sql);}?><table align="center" width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td><form name="delete" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>"> <table width="400" border="1" cellpadding="3" cellspacing="1"> <tr> <td colspan="6" align="center"><strong>Testimonials</strong> </td> </tr> <tr> <td align="center"><strong>Select</strong></td> <td align="center"><strong>ID</strong></td> <td align="center"><strong>Name</strong></td> <td align="center"><strong>Description</strong></td> <td align="center"><strong>Download</strong></td> <td align="center"><strong>Last Modified</strong></td> </tr><?php $row = mysql_fetch_assoc($result); ?><?php do { ?> <tr> <td align="center"> <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['id']; ?>"> </td> <td align="center"><?php echo $row['id']; ?></td> <td align="center"><?php echo $row['Name']; ?></td> <td align="center"><?php echo $row['Message']; ?></td> <td align="center"><a href="/admin/testimonial_files/<?php echo $row['Download']; ?>">Download</a></td> <td align="center"><?php echo $row['Modified']; ?></td> </tr> <tr> <td colspan="6" align="center"><input name="delete" type="submit" id="delete" value="Delete"></td> </tr> </table> </form> </td> </tr> </table><?php } while ($row = mysql_fetch_assoc($result)); ?> 1 dsjoes reacted to this Share this post Link to post Share on other sites
dsjoes 0 Report post Posted January 15, 2011 (edited) that is now showing the data but it only shows the first row in the table the others are not in it i have attached a pic to show you what i mean and the description part is supposed to be empty i haven't put anything in it yet because of testing the unlink.untitled.bmpi have tryed the deleting bit but i get these two messages Warning: unlink(/testimonial_files/) [function.unlink]: Is a directory in /hermes/bosweb/web230/b2302/ipg.myaccount/test_server/admin/test1.php on line 15Warning: Cannot modify header information - headers already sent by (output started at /hermes/bosweb/web230/b2302/ipg.myaccount/test_server/admin/test1.php:15) in /hermes/bosweb/web230/b2302/ipg.myaccount/test_server/admin/test1.php on line 24 i have tryed with the full file path/hermes/bosweb/web230/b2302/ipg.myaccount/test_server/testimonial_files/ Edited January 15, 2011 by dsjoes (see edit history) Share this post Link to post Share on other sites
dsjoes 0 Report post Posted January 16, 2011 (edited) that is now showing the data but it only shows the first row in the table the others are not in it i have attached a pic to show you what i mean and the description part is supposed to be empty i haven't put anything in it yet because of testing the unlink.untitled.bmpi have tryed the deleting bit but i get these two messages Warning: unlink(/testimonial_files/) [function.unlink]: Is a directory in /hermes/bosweb/web230/b2302/ipg.myaccount/test_server/admin/test1.php on line 15Warning: Cannot modify header information - headers already sent by (output started at /hermes/bosweb/web230/b2302/ipg.myaccount/test_server/admin/test1.php:15) in /hermes/bosweb/web230/b2302/ipg.myaccount/test_server/admin/test1.php on line 24 i have tryed with the full file path/hermes/bosweb/web230/b2302/ipg.myaccount/test_server/testimonial_files/ i have fixed the table not displaying correctly it is just the unlink and the header problem that i am still having problems with.the unlink part is too high up the code because it gets the ['Download'] part from this bit<?php $row = mysql_fetch_assoc($result); ?><?php do { ?> Edited January 16, 2011 by dsjoes (see edit history) Share this post Link to post Share on other sites
sonesay 7 Report post Posted January 17, 2011 (edited) Yeah your right. I didn't look at the entire code and just pasted the whole block in. the unlink('../admin/testimonial_files/' . $row['Download']); line you can delete I think because its not needed? if you look at http://php.net/manual/en/function.unlink.php this method deletes a file and I don't remember you mentioning there was any files you also needed deleting. If you do need to delete a file as well then you can paste that line back into the loop block.Edit: I just looked at your code again and by the looks of it you only want to be deleting (unlinking) the file if the user submits a delete post right? If that is the case then you would need to do:<?php// Set Global Vars$HOST = "database";$USERNAME = "sonint99";$PASSWORD = "C2sHF9Zxq3ULXCTV";$DATABASE = "Random";$TABLE = "Testimonials";// Establish a connectionmysql_connect($HOST, $USERNAME, $PASSWORD) or die(mysql_error());mysql_select_db($DATABASE) or die(mysql_error());//==================================================================// Check if anything is posted//==================================================================if (isset($_POST['delete'])) { $sql = ("delete from $TABLE where "); for ($i = 0; $i < count($_POST['checkbox']); $i++) { if ($i != 0) { $sql.= "OR "; } $sql .= " id='" . $_POST['checkbox'][$i] . "'"; //================================================================== // Select file to unlink based on id posted //================================================================== $sql_file_unlink = "select Download from $TABLE where id = {$_POST['checkbox'][$i]}"; $result_file_unlink = mysql_query($sql_file_unlink); $row_file_link = mysql_fetch_assoc($result_file_unlink); if($row_file_link) { unlink('../admin/testimonial_files/' . $row_file_link['Download']); } //================================================================== } $result = mysql_query($sql); header('Location: index.php'); exit;} else { // select sql here $sql = "select * from `$TABLE` order by `id`;"; $result = mysql_query($sql);}?><table align="center" width="400" border="0" cellspacing="1" cellpadding="0"> <tr> <td colspan="6" align="center"><strong>Testimonials</strong> </td> </tr> <tr> <td><form name="delete" method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>"> <table width="400" border="1" cellpadding="3" cellspacing="1"> <tr> <td align="center"><strong>Select</strong></td> <td align="center"><strong>ID</strong></td> <td align="center"><strong>Name</strong></td> <td align="center"><strong>Description</strong></td> <td align="center"><strong>Download</strong></td> <td align="center"><strong>Last Modified</strong></td> </tr> <?php $row = mysql_fetch_assoc($result); ?><?php do { ?> <tr> <td align="center"> <input name="checkbox[]" type="checkbox" id="checkbox[]" value="<?php echo $row['id']; ?>"> </td> <td align="center"><?php echo $row['id']; ?></td> <td align="center"><?php echo $row['Name']; ?></td> <td align="center"><?php echo $row['Message']; ?></td> <td align="center"><a href="/admin/testimonial_files/<?php echo $row['Download']; ?>">Download</a></td> <td align="center"><?php echo $row['Modified']; ?></td> </tr> <?php } while ($row = mysql_fetch_assoc($result)); ?> <tr> <td colspan="6" align="center"><input name="delete" type="submit" id="delete" value="Delete"></td> </tr> </table> </form> </td> </tr> </table><?php I hope that's what you wanted. I removed some variables as it seems like they arnt being used like the delete id from the submit button. Edited January 17, 2011 by sonesay (see edit history) 1 dsjoes reacted to this Share this post Link to post Share on other sites