dannyd Posted April 1, 2008 Share Posted April 1, 2008 I pasted my code below which has my whole attributes script which thanks to this site works. Not the pretties piece of code but works. How can I redirect after the delete so it goes to to the page /attributes.php?cadid=cadid page. I always get a headers error if i use a php redirect. Not sure how to work around that. Any ideas ? <?php //table for product attributes $tbl_name = "product_attributes"; // get the required action if(isset($_GET['action']) && !empty($_GET['action'])) { // call code based on action switch(strtolower($_GET['action'])) { //************************** BEGIN ADD ATTRIBUTES TO PRODUCT ******************************************* case 'add': $clid = $_REQUEST['clid']; $clientname = $_REQUEST['clientname']; echo '<b>Add attributes to: ' . $clientname . '<b><BR>'; // start displaying data $sql = 'SELECT * FROM ' . $tbl_name . ' WHERE clid="' . $clid . '" ORDER BY sort ASC'; $result = mysql_query($sql); // get the number of field from the table $num_fields = mysql_num_fields($result); // display results with table fieldnames and "add" link echo '<table border="1" cellspacing="2" cellpadding="5"><tr>'; $field_array=array('Attribute ID','Client ID','Attribute Name','Price', 'URL', 'Sort'); // first the field names from table for($i = 0; $i < $num_fields; $i++) { // echo ' <th>' . ucwords(mysql_field_name($result, $i)) . "</th>\n"; echo '<th bgcolor="#ddf4fd">' . $field_array[$i] . '</th>'; } // append the Action column echo '<th bgcolor="#ddf4fd">Action</th>'; echo " </tr>\n"; // now display all content to screen while($row = mysql_fetch_assoc($result)) { // use implode to create new cell for each item in the row echo " <tr>\n <td>" , implode("</td>\n <td>", $row) . "</td>\n "; // display our action links (edit and delete) echo '<td><a href="?action=edit&attribute_id='.$row['attribute_id'].'&clid=' . $row['clid'] . '&clientname=' . $clientname.'">EDIT</a> | <a href="?action=delete&attribute_id='.$row['attribute_id'].'&clid=' . $row['$clid'] . '&clientname=' . $clientname .'">DELETE</a></td>'."\n </tr>\n"; } // close table echo "</table>\n"; //end displaying data if(isset($_POST['submit']) && $_POST['submit'] == 'Add') { foreach($_POST as $field => $value) { if ($value == '' && $field != 'clid' && $field != 'attribute_id'){ echo '<font color="red">ERROR: Please Make sure fields are not empty</font>'; exit; } if($field != 'submit' && $field != 'attribute_id' && $field != 'clid') { $fields_list[] = "`$field`='" . mysql_real_escape_string($value) . "'"; } } echo 'record added!'; $sql = "INSERT INTO product_attributes (clid,attribute_name,price,url,sort) VALUES('$clid', '$attribute_name', '$price', '$url','$sort')"; $result = mysql_query($sql); //ob_flush(); //header('Location: attributes.php?action=add&clid=$cadid'); //ob_flush(); } // echo '<table width="400"><tr><td><form method="post" action="?action=add&clid=' . $clid . '"><input type="hidden" name="clid" value="' . $clid . '"><br>Attribute:<input type="Text" name="attribute_name"><br>Price:<input type="Text" name="price"><br>URL:<input type="Text" name="url"><br>Order:<input type="Text" name="sort"><br><input type="submit" name="submit" value="Add" /></form></td></tr></table>'; echo '<table width="450"><tr><td><form method="post" action="?action=add&clid=' . $clid . '"><input type="hidden" name="clid" value="' . $clid . '"></td><td><input type="hidden" name="clid" value="' . $clid . '"></td></tr><tr> <td width="75">Attribute Name:</td><td><input type="Text" name="attribute_name"></td></tr><tr><td>Price:</td><td><input type="Text" name="price"></td></tr><tr><td>URL (Link to attribute page):</td><td><input type="Text" name="url"></td></tr><tr> <td>Order:</td> <td><input type="Text" name="sort"></td> </tr> <tr><td width="170"> </td> <td width="218"><form method="post" action="?action=add&clid=' . $clid . '"> <input type="submit" name="submit" value="Add" /></form></td></tr></table>'; break; //************************** END ADD ATTRIBUTES TO PRODUCT ******************************************* //************************** BEGIN EDIT ATTRIBUTES TO PRODUCT ******************************************* case 'edit': $clid = $_REQUEST['clid']; $clientname = $_REQUEST['clientname']; echo '<b>Edit Attributes for: ' . $clientname . '</b><BR>'; // get all data from the table $sql = 'SELECT * FROM product_attributes WHERE clid="' . $clid . '" ORDER BY sort ASC'; $result = mysql_query($sql); // get the number of field from the table $num_fields = mysql_num_fields($result); // display results with table fieldnames and "add" link echo '<a href="?action=add&clid=' . $clid . '&clientname=' . $clientname . '">Add a Record</a>'; echo '<table border="1" cellspacing="2" cellpadding="5"><tr>'."\n"; $field_array=array('Attribute ID','Client ID','Attribute Name','Price', 'URL', 'Sort'); // first the field names from table for($i = 0; $i < $num_fields; $i++) { //echo ' <th>' . ucwords(mysql_field_name($result, $i)) . "</th>\n"; echo '<th bgcolor="#ddf4fd">' . $field_array[$i] . '</th>'; } // append the Action column echo '<th bgcolor="#ddf4fd">Action</th>'; echo '</tr>'; // now display all content to screen while($row = mysql_fetch_assoc($result)) { // use implode to create new cell for each item in the row echo " <tr>\n <td>" , implode("</td>\n <td>", $row) . "</td>\n "; // display our action links (edit and delete) echo '<td><a href="?action=edit&attribute_id='.$row['attribute_id'].'&clid='.$row['clid'].'&clientname=' . $clientname . '">EDIT</a> | <a href="?action=delete&attribute_id='.$row['attribute_id'].'&clid='.$row['clid'].'&attribute_name=' . $row['attribute_name'] . '&clientname=' . $clientname . '">DELETE</a></td>'."\n </tr>\n"; } // close table echo "</table>\n"; //end displaying data; $edit_fields=array('Attribute Name','Price', 'URL', 'Sort'); // check that form has been submitted: if(isset($_POST['submit']) && $_POST['submit'] == 'Update') { $attribute_id = $_POST['attribute_id']; $clid = $_POST['clid']; foreach($_POST as $field => $value) { if($field != 'submit' && $field != 'attribute_id' && $field != 'clid') { $fields_list[] = "`$field`='" . mysql_real_escape_string($value) . "'"; if ($value == ''){ echo $field . '<font color="red">ERROR: Please Make sure fields are not empty</font>'; exit; } } } $sql = 'UPDATE ' . $tbl_name . ' SET ' . implode(', ', $fields_list) . ' WHERE attribute_id=' . $attribute_id; $result = mysql_query($sql); } // form has not been submitted, display form elseif(isset($_GET['attribute_id']) && is_numeric($_GET['attribute_id'])) { // get the id from the url // url example: ?action=edit&id=1 $id = $_GET['attribute_id']; // get data from table based on id $sql = 'SELECT * FROM '.$tbl_name.' WHERE attribute_id='.$attribute_id; // perform query $result = mysql_query($sql); // check that the quewry return one result if(mysql_num_rows($result) == 1) { $row = mysql_fetch_assoc($result); $form = '<form action="?action=edit" method="post">'; foreach($row as $field_name => $field_value) { if ($field_name == 'clid' || $field_name == 'attribute_id') $type = 'hidden'; else $type = 'text'; $form .= '<input size="50" type="' . $type . '" name="'. $field_name .'" value="' . $field_value . '" /><br />'; } $form .= '<input type="submit" name="submit" value="Update" /></form>'; echo $form; } } break; //************************** END EDIT ATTRIBUTES TO PRODUCT ******************************************* //************************** BEGIN DELETE ATTRIBUTES TO PRODUCT ******************************************* case 'delete': echo '<font color="red"><b>' . $attribute_name . ' deleted from ' . $clientname . '</font></b>'; exit; //$sql = "DELETE FROM product_attributes WHERE attribute_id=$attribute_id"; //$result = mysql_query($sql); break; //************************** END DELETE ATTRIBUTES TO PRODUCT ******************************************* //************************** BEGIN DEFAULT ATTRIBUTES TO PRODUCT ******************************************* default: $clientname = $_REQUEST['clientname']; // get all data from the table $sql = 'SELECT * FROM product_attributes WHERE clid=' . $clid; $result = mysql_query($sql); // get the number of fields from the table $num_fields = mysql_num_fields($result); // display results with table fieldnames and "add" link echo '<a href="?action=add&clid=' . $clid . '">Add Record</a>'; echo '<table border="1" cellspacing="2" cellpadding="5"><tr>'."\n"; // first the field names from table for($i = 0; $i < $num_fields; $i++) { //echo ' <th>' . ucwords(mysql_field_name($result, $i)) . "</th>\n"; } // append the Action column echo " <th>Action</th>\n"; echo " </tr>\n"; // now display all content to screen while($row = mysql_fetch_assoc($result)) { // use implode to create new cell for each item in the row echo " <tr>\n <td>" , implode("</td>\n <td>", $row) . "</td> "; // display our action links (edit and delete) echo '<td><a href="?action=edit&attribute_id='.$row['attribute_id'].'&clid='.$row['clid'].'&clientname=' . $clientname . '">EDIT</a> | <a href="?action=delete&attribute_id='.$row['attribute_id'].'&attribute_name=' . $row['attribute_name'] . '&clientname=' . $clientname . '">DELETE</a></td>'."\n </tr>\n"; } // close table echo "</table>\n"; } } //************************** END DEFAULT ATTRIBUTES TO PRODUCT ******************************************* else { $clientname = $_REQUEST['clientname']; echo '<b>Attributes for : ' . $clientname . '</b>'; echo '<BR>'; // get all data from the table $sql = 'SELECT * FROM product_attributes WHERE clid="' . $clid . '" ORDER BY sort ASC'; $result = mysql_query($sql); // get the number of field from the table $num_fields = mysql_num_fields($result); // display results with table fieldnames and "add" link echo '<a href="?action=add&clid=' . $clid . '&clientname=' . $clientname . '">Add Record</a>'; echo '<table border="1" cellspacing="2" cellpadding="5"><tr>'."\n"; $field_array=array('Attribute ID','Client ID','Attribute Name','Price', 'URL', 'Sort'); // first the field names from table for($i = 0; $i < $num_fields; $i++) { //echo ' <th>' . ucwords(mysql_field_name($result, $i)) . "</th>\n"; echo '<th bgcolor="#ddf4fd">' . $field_array[$i] . '</th>'; } // append the Action column echo '<th bgcolor="#ddf4fd">Action</th>'; echo " </tr>\n"; // now display all content to screen while($row = mysql_fetch_assoc($result)) { // use implode to create new cell for each item in the row echo " <tr>\n <td>" , implode("</td>\n <td>", $row) . "</td>\n "; // display our action links (edit and delete) echo '<td><a href="?action=edit&attribute_id='.$row['attribute_id'].'&clid='.$row['clid'].'&clientname=' . $clientname . '">EDIT</a> | <a href="?action=delete&attribute_id='.$row['attribute_id'].'&attribute_name=' . $row['attribute_name'] . '&clientname=' . $clientname . '">DELETE</a></td>'."\n </tr>\n"; } // close table echo "</table>\n"; } ?> Link to comment https://forums.phpfreaks.com/topic/99036-redirecting-after-mysql-insert/ Share on other sites More sharing options...
trq Posted April 1, 2008 Share Posted April 1, 2008 You'll get a header error when you try to call the header function after already outputting to the browser. Re-arange your code so there is nothing displayed prior to calling the header function. There is no point in displaying anything anyway if all your going to do is redirect. Link to comment https://forums.phpfreaks.com/topic/99036-redirecting-after-mysql-insert/#findComment-506815 Share on other sites More sharing options...
dannyd Posted April 2, 2008 Author Share Posted April 2, 2008 Can you give me an example of how I would rearrange the code or where I am outputting text before I call a redirect. So for instance when I delete I want it automatically redirected to the default form instead of displaying the message that somethings been deleted and the user having to use the back button. Link to comment https://forums.phpfreaks.com/topic/99036-redirecting-after-mysql-insert/#findComment-507442 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.