silverglade Posted September 24, 2009 Share Posted September 24, 2009 hi, i spent a few hours trying to get this right and i give up, i just cant do it. i am making an application that inserts, updates, deletes rows in a sql table(delete using javascript code in the script where onclick deletes a row), then displays the results in the form of an html table. the way i have it coded now, i can first update a row, insert a row, and then delete a row or rows, but once i delete rows, after that, i cant insert or update any rows. any help GREATLY appreciated on this one. thank you. derek here is the code to the entire application in php. <?php include("connections.php"); //////////////////////////////////////// //////////////////////////////////////// /// query db and loop through rows example $id = $_POST['id']; $field2 = $_POST['data2']; $field3 = $_POST['data3']; $field4 = $_POST['data4']; $field5 = $_POST['data5']; $field6 = $_POST['data6']; $id = $_GET['id']; //gets the id value from javascript code below /*echo "<pre>"; //displays array and variables to check whats going on print_r($_POST); echo "</pre>";*/ //if id is set from the onclick javascript below, if ($id) { mysql_query("DELETE FROM table1 WHERE record_id ='$id'"); } if(isset($_POST['Submit'])){ //if submit has been set, or clicked. if($id){ //has id been set? mysql_query("UPDATE table1 SET field2='$field2',field3='$field3',field4='$field4',field5='$field5',field6='$field6' WHERE record_id ='$id'"); }else{ if(!$id) { //if id has not been set, insert data into next row. mysql_query("INSERT INTO table1 (field2,field3,field4,field5,field6)VALUES('$field2','$field3','$field4','$field5','$field6')"); }//end if }//end else }//end isset /// query db and loop through rows example //selects everything from table and assigns to the variable $query $query = mysql_query("SELECT * FROM table1"); $table = "<table width=\"40%\" border=\"1\"> <tr> <td>heading1</td> <td>heading2</td> <td>heading3</td> <td>heading4</td> <td>heading5</td> <td>heading6</td> </tr>"; while($row = mysql_fetch_array($query)){ $table .= " <tr> <td onclick=\"document.location.href='?id=".$row['record_id']."'\" >".$row['record_id']."</td> <td>".$row['field2']."</td> <td>".$row['field3']."</td> <td>".$row['field4']."</td> <td>".$row['field5']."</td> <td>".$row['field6']."</td> </tr>"; } $table .= "</table>"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Update Statement</title> <style type="text/css"> <!-- .style1 {color: #990000} .style2 {color: #0066FF} .style3 {color: #0000CC} .style4 {color: #993399} --> </style> </head> <body> <p><?php echo $table; ?></p> <p><span class="style2">*If updating a row, please enter the ID of the row you want to update, and enter the data</span><br /> <br /> <span class="style3">*If adding a new row of data , just leave the ID space blank</span><br /> <br /> <span class="style4">*To delete a row of data, click on the id of the row to delete</span><br /> </p> <form id="form1" name="form1" method="post" action=""> <table width="31%" border="0"> <tr> <td>ID</td> <td><input name="id" type="text" id="id" /></td> </tr> <tr> <td width="32%">Data2</td> <td width="68%"><input name="data2" type="text" id="data2" /></td> </tr> <tr> <td>Data3</td> <td><input name="data3" type="text" id="data3" /></td> </tr> <tr> <td>Data4</td> <td><input name="data4" type="text" id="data4" /></td> </tr> <tr> <td>Data5</td> <td><input name="data5" type="text" id="data5" /></td> </tr> <tr> <td>Data6</td> <td><input name="data6" type="text" id="data6" /></td> </tr> <tr> <td> </td> <td><input type="submit" name="Submit" value="Submit" /></td> </tr> </table> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/175428-solved-please-need-help-with-this-sql-html-table-editor-im-making-in-php/ Share on other sites More sharing options...
Pickle Posted September 25, 2009 Share Posted September 25, 2009 hi silverglade the first thing that i can see is that, before anything, you are asking the script to delete rows whenever the id variable is set. not sure thats the best way to do this. i think you should pass another variable when the user clicks the delete link e.g. &action=delete and then you know that someones actually clicked the delete link. It seems you are doing everything in one document so things can end up getting confused. you should try and make every action clear if you know what i mean. also i cant see where you are actually running any of the queries, wheres this bit: mysql_query($query) hope this helps. Link to comment https://forums.phpfreaks.com/topic/175428-solved-please-need-help-with-this-sql-html-table-editor-im-making-in-php/#findComment-924770 Share on other sites More sharing options...
silverglade Posted September 25, 2009 Author Share Posted September 25, 2009 thanks very much. i finally figured it out. i changed my variable names, and it worked. here is the code . the program displays a sql table in html table format, and updates a row somewhat, inserts a row, deletes a row, and displays the results, all on the same page. man that was hard. thank you for the feedback. derek <?php include("connections.php"); //////////////////////////////////////// //////////////////////////////////////// /// query db and loop through rows example $id = $_POST['id']; $field2 = $_POST['data2']; $field3 = $_POST['data3']; $field4 = $_POST['data4']; $field5 = $_POST['data5']; $field6 = $_POST['data6']; $remove = $_GET['remove']; //gets the id value from javascript code below and for some reason //when i put the value in a same name variable it worked /*echo "<pre>"; //displays array and variables to check whats going on print_r($_POST); echo "</pre>";*/ //if remove is set , and caught by the $_GET from the onclick javascript below, then delete row if ($remove) { mysql_query("DELETE FROM table1 WHERE record_id ='$remove'"); } if(isset($_POST['Submit'])){ //if submit has been set, or clicked. if($id){ //has id been set? mysql_query("UPDATE table1 SET field2='$field2',field3='$field3',field4='$field4',field5='$field5',field6='$field6' WHERE record_id ='$id'"); }else{ if(!$id) { //if id has not been set, insert data into next row. mysql_query("INSERT INTO table1 (field2,field3,field4,field5,field6)VALUES('$field2','$field3','$field4','$field5','$field6')"); }//end if }//end else }//end isset /// query db and loop through rows example //selects everything from table and assigns to the variable $query $query = mysql_query("SELECT * FROM table1"); $table = "<table width=\"40%\" border=\"1\"> <tr> <td>heading1</td> <td>heading2</td> <td>heading3</td> <td>heading4</td> <td>heading5</td> <td>heading6</td> </tr>"; ///javascript code links to same page, uses remove var to hold the row clicked //i dont understand however , this ".$row['record_id']."'\" >".$row['record_id']."</td> while($row = mysql_fetch_array($query)){ $table .= " <tr> <td onclick=\"document.location.href='?remove=".$row['record_id']."'\" >".$row['record_id']."</td> <td>".$row['field2']."</td> <td>".$row['field3']."</td> <td>".$row['field4']."</td> <td>".$row['field5']."</td> <td>".$row['field6']."</td> </tr>"; } $table .= "</table>"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Update Statement</title> <style type="text/css"> <!-- .style1 {color: #990000} .style2 {color: #0066FF} .style3 {color: #0000CC} .style4 {color: #993399} --> </style> </head> <body> <p><?php echo $table; ?></p> <p><span class="style2">*If updating a row, please enter the ID of the row you want to update, and enter the data</span><br /> <br /> <span class="style3">*If adding a new row of data , just leave the ID space blank</span><br /> <br /> <span class="style4">*To delete a row of data, click on the id of the row to delete</span><br /> </p> <form id="form1" name="form1" method="post" action=""> <table width="31%" border="0"> <tr> <td>ID</td> <td><input name="id" type="text" id="id" /></td> </tr> <tr> <td width="32%">Data2</td> <td width="68%"><input name="data2" type="text" id="data2" /></td> </tr> <tr> <td>Data3</td> <td><input name="data3" type="text" id="data3" /></td> </tr> <tr> <td>Data4</td> <td><input name="data4" type="text" id="data4" /></td> </tr> <tr> <td>Data5</td> <td><input name="data5" type="text" id="data5" /></td> </tr> <tr> <td>Data6</td> <td><input name="data6" type="text" id="data6" /></td> </tr> <tr> <td> </td> <td><input type="submit" name="Submit" value="Submit" /></td> </tr> </table> </form> </body> </html> Link to comment https://forums.phpfreaks.com/topic/175428-solved-please-need-help-with-this-sql-html-table-editor-im-making-in-php/#findComment-924940 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.