causeimblonde Posted May 30, 2007 Share Posted May 30, 2007 For some reason, all the coding on my website just went HAYWIRE. I keep getting error messages that say Notice: Undefined variable: balh blah blah. I've managed to fix most of it by adding a single quote around the variable, but I'm stuck on this one... I get the message Notice: Undefined variable: action in website.com on line 201 Here is what I have on line 201... if ($action == "list" || $action == "") If I put single quotes around the 1st "action", then I get the message again, but if I put single quotes around the second action as well, the page goes blank and NOTHING shows up. Does anyone know what is going on here? Literally an hour ago everything was fine, now everything is crazy. ANY help would be TRULY appreciated! thank you thank you thank you! jen Quote Link to comment https://forums.phpfreaks.com/topic/53628-help/ Share on other sites More sharing options...
per1os Posted May 30, 2007 Share Posted May 30, 2007 if (isset($action) && ($action == "list" || $action == "")) www.php.net/isset Checks to see if a variable has been defined or not. On another note you can disable notice's as they are not fatal. Quote Link to comment https://forums.phpfreaks.com/topic/53628-help/#findComment-265074 Share on other sites More sharing options...
causeimblonde Posted May 30, 2007 Author Share Posted May 30, 2007 thank you, first of all, for replying to me! I tried that snippet of code and it, also, turned the page blank. Any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/53628-help/#findComment-265083 Share on other sites More sharing options...
per1os Posted May 30, 2007 Share Posted May 30, 2007 Post more code? Quote Link to comment https://forums.phpfreaks.com/topic/53628-help/#findComment-265087 Share on other sites More sharing options...
causeimblonde Posted May 30, 2007 Author Share Posted May 30, 2007 This is my entire code <? $host="xxxxx $user="xxxxx $password="xxxxx $dbname="xxxxx $dbtable="xxxxx $homepage="enternew.php"; $dbh=mysql_connect ("xxxxx", "xxxxx", "xxxxx") or die ('I cannot connect to the database because: ' . mysql_error()); mysql_select_db ("xxxxx"); ?> <? print "<table cellspacing='5' cellpadding='0'>\n"; print "<tr>\n"; print "<td></td>\n"; print "<td>\n\n"; // PREADD CONTENT if ($action == "preadd") { ?> You are adding a new story:<p> <form action="<? print $homepage ?>" method="post" enctype="multipart/form-data"> <table width="50%" border="0" cellspacing="0" cellpadding="5"> <tr> <td>Date</td> <td><input name="date" type="text" id="date"></td> <td>Category</td> <td><input name="category" type="text" id="category"></td> </tr> <tr> <td>First Name</td> <td><input name="firstname" type="text" id="firstname"></td> <td>Last Name</td> <td><input name="lastname" type="text" id="lastname"></td> </tr> <tr> <td>Source</td> <td colspan="3"><input name="source" type="text" id="source" size="68"></td> </tr> <tr> <td>Image</td> <td colspan="3"><input name="image" type="text" id="image" size="68"></td> </tr> <tr> <td>Title</td> <td colspan="3"><input name="title" type="text" id="title" size="68"></td> </tr> <tr> <td>Subtitle</td> <td colspan="3"><input name="subtitle" type="text" id="subtitle" size="68"></td> </tr> <tr> <td>First</td> <td colspan="3"><textarea name="first" cols="52" rows="5" id="first"></textarea></td> </tr> <tr> <td>Link</td> <td colspan="3" valign="top"><input name="presslink" type="text" id="presslink" size="68"></td> </tr> <tr> <td>Body</td> <td colspan="3" valign="top"><textarea name="body" cols="52" rows="20" id="body"></textarea></td> </tr> <tr> <td></td> <td> <input type="submit" value="Add Story"> </td> </tr> </table> <input type="hidden" name="action" value="add"> </form> <? } // ADD CONTENT if ($action == "add") { $body = addslashes($body); $sql = "insert into $dbtable (date,category,firstname,lastname,source,image,title,subtitle,first,presslink,body) values ('$date','$category','$firstname','$lastname','$source','$image','$title','$subtitle','$first','$presslink','$body');"; $result = mysql_query($sql, $dbh) or die( mysql_error() ); if ($result) { print "You have added a story entitled <b>$title</b>.<br>\n"; } else { print "Error: Could Not Add Story.<br>"; } print "<br>If no errors are encountered, please check the appropriate area of the website to make sure it appears correctly.<p>"; print "<b><a href='$homepage'>Return to Admin</a></b>"; } // EDIT CONTENT if ($action == "edit") { $sql = "select * from $dbtable where id=$id;"; $result = mysql_query($sql, $dbh) or die( mysql_error() ); $date = mysql_result($result, 0, "date"); $category = mysql_result($result, 0, "category"); $firstname = mysql_result($result, 0, "firstname"); $lastname = mysql_result($result, 0, "lastname"); $source = mysql_result($result, 0, "source"); $image = mysql_result($result, 0, "image"); $title = mysql_result($result, 0, "title"); $subtitle = mysql_result($result, 0, "subtitle"); $first = mysql_result($result, 0, "first"); $presslink = mysql_result($result, 0, "presslink"); $body = mysql_result($result, 0, "body"); ?> You are editing a story: <p> <form action="<? print $homepage ?>" method="post" enctype="multipart/form-data"> <table width="50%" border="0" cellspacing="0" cellpadding="5"> <tr> <td>Date</td> <td><input name="date" type="text" id="date" value="<? print $date ?>"></td> <td>Category</td> <td><input name="category" type="text" id="category" value="<? print $category ?>"></td> </tr> <tr> <td>FirstName</td> <td><input name="firstname" type="text" id="firstname" value="<? print $firstname?>"></td> <td>LastName</td> <td><input name="lastname" type="text" id="lastname" value="<? print $lastname ?>"></td> </tr> <tr> <td>Source</td> <td colspan="3"><input name="source" type="text" id="source" value="<? print $source ?>" size="68"></td> </tr> <tr> <td>Image</td> <td colspan="3"><input name="image" type="text" id="image" value="<? print $image ?>" size="68"></td> </tr> <tr> <td>Title</td> <td colspan="3"><input name="title" type="text" value="<? print $title ?>" id="title" size="68"></td> </tr> <tr> <td>Subtitle</td> <td colspan="3"><input name="subtitle" type="text" id="subtitle" value="<? print $subtitle ?>" size="68"></td> </tr> <tr> <td>First</td> <td colspan="3"><textarea name="first" cols="52" rows="5" id="first"><? print $first ?></textarea></td> </tr> <tr> <td>Link</td> <td colspan="3" valign="top"><input name="presslink" type="text" id="presslink" value="<? print $presslink ?>" size="68"></td> </tr> <tr> <td>Body</td> <td colspan="3" valign="top"><textarea name="body" cols="52" rows="20" id="body"><? print $body ?></textarea></td> </tr> <tr> <td></td> <td> <input type="submit" value="Add Story" > </td> </tr> </table> <input type="hidden" name="id" value="<? print $id ?>"> <input type="hidden" name="action" value="update"> </form> <? } // UPDATE CONTENT if ($action == "update") { $body = addslashes($body); $sql = "update $dbtable set date='$date',category='$category',source='$source', firstname='$firstname',lastname='$lastname',image='$image',title='$title',subtitle='$subtitle',first='$first',presslink='$presslink',body='$body' where id='$id'"; $result = mysql_query($sql, $dbh) or die( mysql_error() ); if ($result) { print "You have edited a story entitled <b>$title</b>.<br>\n"; } else { print "Error: Could Not Edit Story<br>"; } print "<br>If no errors are encountered, please check the appropriate area of the website to make sure it appears correctly.<p>"; print "<b><a href='$homepage'>Return to Admin</a></b>"; } // DELETE CONTENT if ($action == "delete_check") { print "Are you sure you want to PERMANENTLY DELETE the story entitled <b>$title</b>?<p>\n\n"; print "<b><a href='$homepage?action=delete&id=$id'>Yes</a></b><br><br>"; print "<b><a href='$homepage'>No - Return to Admin</a></b>"; } if ($action == "delete") { $sql = "delete from $dbtable where id='$id';"; $result = mysql_query($sql, $dbh) or die( mysql_error() ); if ($result) { print "You have permanently deleted the story entitled <b>$title</b>.<p>"; } else { print "Error: Could Not Delete Story.<p>"; } print "<b><a href='$homepage'>Return to Admin</a></b>"; } // LIST ENTRIES (DEFAULT) if ($action == "list" || $action == "") { ?> [<a href="<? print $homepage ?>?action=preadd">Add New Story</a>] <p> <? $sql = "select id, date, title from $dbtable order by date DESC;"; $result = mysql_query($sql, $dbh) or die( mysql_error() ); ?> <table border="1" cellspacing="0" cellpadding="5"> <tr bgcolor="#CCCCCC"> <td>News ID</td> <td>Date</td> <td>Title</td> <td>Functions</td> </tr> <? for ($x = 0; $x < mysql_num_rows($result); $x++) { $id = mysql_result($result, $x, "id"); $date = mysql_result($result, $x, "date"); $title = mysql_result($result, $x, "title"); print " <tr>\n"; print " <td bgcolor='#aaaaaa' align='center'>"; print "$id"; print "</td>\n"; print "<td bgcolor='#bbbbbb'>"; print "$date"; print "</td>\n"; print "<td bgcolor='#bbbbbb'>"; print "$title"; print "</td>\n"; print "<td>"; print "[<a href='$homepage?action=edit&id=$id&date=$date&title=$title'>Edit</a>] [<a href='$homepage?action=delete_check&id=$id&date=$date&title=$title'>Delete</a>]"; print "</td>\n"; print " </tr>\n\n"; } print "</table>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/53628-help/#findComment-265093 Share on other sites More sharing options...
kenrbnsn Posted May 30, 2007 Share Posted May 30, 2007 Please edit you post to put the tag before the code and after. You have unterminated strings all over your code and many lines without semi-colons. Ken Quote Link to comment https://forums.phpfreaks.com/topic/53628-help/#findComment-265098 Share on other sites More sharing options...
per1os Posted May 30, 2007 Share Posted May 30, 2007 just an fyi, the [ code ] tags are your friends. Try this instead of what I put above. if (!isset($action) || ($action == "list" || $action == "")) That should get you your desired results. Quote Link to comment https://forums.phpfreaks.com/topic/53628-help/#findComment-265103 Share on other sites More sharing options...
causeimblonde Posted May 30, 2007 Author Share Posted May 30, 2007 Sorry about the []...i used and i guess that didn't work. Ok, so that last snippet definately worked in the sense that it took away all my errors...but now none of the actions actually work. I click on "Edit" or "Add New" and i just get blank pages in return. Is this whole code completly out of date? Is that where I'm going wrong? thank you again, j Quote Link to comment https://forums.phpfreaks.com/topic/53628-help/#findComment-265111 Share on other sites More sharing options...
per1os Posted May 30, 2007 Share Posted May 30, 2007 Not to say out of date, just really poorly done. I would of used a case/switch statement, and before any of the if statements added this to be sure that incase register_globals are off (which they should be) the script works: <?php // database stuff above $action = 'list'; if (isset($_GET['action'])) { $action = $_GET['action']; } // if statements below. That way you know the list if will always run if no action is passed in. If you want to stick to the if statements I would use If/elseif clauses too. Quote Link to comment https://forums.phpfreaks.com/topic/53628-help/#findComment-265115 Share on other sites More sharing options...
causeimblonde Posted May 30, 2007 Author Share Posted May 30, 2007 This is really the only way I was shown how to use PHP for my website. Someone gave me this code and showed me how to change the fields to reflect my database fields. If there's a better way to do it, I would love a point in the right direction. THank you again for your help, and I apologize for my naivite on the subject. -j Quote Link to comment https://forums.phpfreaks.com/topic/53628-help/#findComment-265120 Share on other sites More sharing options...
per1os Posted May 30, 2007 Share Posted May 30, 2007 <?php // database stuff above $action = 'list'; if (isset($_GET['action'])) { $action = $_GET['action']; } switch (strtolower($action)) { default: case 'list': // place list code etc between here break; case 'preadd': // place preadd code etc between here. break; case 'add': // place add code etc between here. break; case 'edit': // place edit code etc between here. break; case 'delete': // place delete code etc between here. break; case 'delete_check': // place delete_check code etc between here. break; } ?> Makes it a little bit easier to read/maintain I think. Quote Link to comment https://forums.phpfreaks.com/topic/53628-help/#findComment-265125 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.