Redlightpacket Posted January 19, 2009 Share Posted January 19, 2009 I get Undefined index: variable or idea1 error when I come to the "simple page" by clicking on a common id on the "idea page". What's causing it is the $idea = addslashes($_POST['idea1']);-->I'm refering on the "simple page" here. But When I submit the data to the "simple page" again the Undefined index:variable error goes off the screen. Here is the "simple page": <html> <style type="text/css"> <!-- .style1 { font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 36px; } .style2 { font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 14px; font-weight: bold; } .style3 { font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 14px; font-weight: bold; } .style4 { font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 14px; font-weight: bold; } .style5 { font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 14px; font-weight: bold; } --> </style> <?PHP $id = $_GET['id']; ?> <form method="post" action="http://localhost/simple.php?<?php echo'id='.$id;?>"> <pre class="style1">Message</pre> <pre> <script language="Javascript"> <!-- function doClear(theText) { if (theText.value == theText.defaultValue) { theText.value = "" } } //--> </script> <span class="style5">Idea :</span> <textarea name="idea1" cols="55" onFocus="doClear(this)">Talk about whatever. </textarea> <input name="Submit1" type="submit" value="submit" style="width: 117px"> </pre> <pre><--BACK <a href="http://localhost/idea.php">Home</a></pre>; </html> <?php /////////////////////////////////////////////////////// //--------------INPUT--------------------------------- /////////////////////////////////////////////////////// //------------------------------------------- // Connects to database and selects database //------------------------------------------- define('DB_HOST', 'localhost'); define('DB_USER', 'chris'); define('DB_PASS', ''); define('DB_NAME', 'ideadb'); define('DEBUG', true); $con = mysql_connect(DB_HOST, DB_USER, DB_PASS); if(!$con) { // error connecting if(DEBUG === true) { die('Database connection error: ' . mysql_error()); } else { die('Failed to connect to the database.'); } } if(!mysql_select_db(DB_NAME, $con)) { if(DEBUG === true) { die('Failed to select database, error: ' . mysql_error()); } else { die('Failed to connect to database.'); } } /////////////////////////////////////////////////////////////////////////// $idea = addslashes($_POST['idea1']); ///////////////////////////////////////////////////////////////////////// // Creates a table if no table exists ////////////////////////////////////////////////////////////////////////// $array = array('id', $id); $comma_separated = join("", $array); $sql="SELECT * FROM $comma_separated"; $result=@mysql_query($sql); if (!$result) { echo "No table exists"; //Combine $id to a word idtb $array = array('id', $id); $comma_separated = join("", $array); echo $comma_separated; $query = "CREATE TABLE IF NOT EXISTS $comma_separated(id SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY, idea VARCHAR(450))"; $result_create = mysql_query($query) or die("Query failed: ".mysql_error()); } $query = "INSERT INTO $comma_separated(id, idea) VALUES (NULL, '$idea')"; $result = mysql_query($query) or die("Query failed: ".mysql_error()); ///////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////// //--------------OUTPUT--------------------------------- /////////////////////////////////////////////////////// //Display Ideas on Webpage. //Need database code to do this with. $query = "SELECT * FROM $comma_separated"; $result = mysql_query($query) or die("Query failed: " . mysql_error()); while ($row = mysql_fetch_assoc($result)) { echo'<table border="1"> <tr> <td>';echo $row["id"];echo'</td> <td>';echo $row["idea"]; echo'</td> </tr> </table> '; } /////////////////////////////////////////////////////// ?> Here is the "idea page" <html> <style type="text/css"> <!-- .style1 { font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 36px; } .style2 { font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 14px; font-weight: bold; } .style3 { font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 14px; font-weight: bold; } .style4 { font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 14px; font-weight: bold; } .style5 { font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 14px; font-weight: bold; } --> </style> <form method="post" action="http://localhost/simple.php">; <pre class="style1">Message</pre> <pre> <script language="Javascript"> <!-- function doClear(theText) { if (theText.value == theText.defaultValue) { theText.value = "" } } //--> </script> <span class="style5">Idea :</span><textarea name="idea" cols="55" onFocus="doClear(this)">Talk about whatever. </textarea> <input name="Submit1" type="submit" value="submit" style="width: 117px"> </pre> <pre><--BACK <a href="index.php">Home</a></pre> </html> <?php /////////////////////////////////////////////////////// //--------------INPUT--------------------------------- /////////////////////////////////////////////////////// //------------------------------------------- // Connects to database and selects database //------------------------------------------- define('DB_HOST', 'localhost'); define('DB_USER', 'chris'); define('DB_PASS', ''); define('DB_NAME', 'ideadb'); define('DEBUG', true); $con = mysql_connect(DB_HOST, DB_USER, DB_PASS); if(!$con) { // error connecting if(DEBUG === true) { die('Database connection error: ' . mysql_error()); } else { die('Failed to connect to the database.'); } } if(!mysql_select_db(DB_NAME, $con)) { if(DEBUG === true) { die('Failed to select database, error: ' . mysql_error()); } else { die('Failed to connect to database.'); } } //////////////////////////////////////////////////////// /////////////////////////////////////////////////////// //--------------INPUT--------------------------------- /////////////////////////////////////////////////////// $idea = addslashes($_POST['idea']); //Just to give each table a name by user $query = "INSERT INTO stan(id, idea) VALUES (NULL, '$idea')"; $result = mysql_query($query) or die("Query failed: ".mysql_error()); //////////////////////////////////////////////////////// //Display Ideas on Webpage. //Need database code to do this with. /////////////////////////////////////////////////////// //--------------OUTPUT--------------------------------- /////////////////////////////////////////////////////// $query = "SELECT * FROM stan"; $result = mysql_query($query) or die("Query failed: " . mysql_error()); while ($row = mysql_fetch_assoc($result)) { echo'<table border="1"> <tr> <td>';echo $row["id"];echo'</td> <td>';echo $row["idea"]; echo'</td> <td><a href="'; echo'http://localhost/';echo'simple.php?id='.$row['id'];echo'>';echo $row["id"]; echo'</a></td> </tr> </table> '; } Quote Link to comment https://forums.phpfreaks.com/topic/141465-undefined-index-variable/ Share on other sites More sharing options...
rhodesa Posted January 19, 2009 Share Posted January 19, 2009 the problem is, when you first load the page, $_POST['idea1'] doesn't exist, and it won't until after you post the page. i assume you wouldn't want to run that block of code until AFTER the page is submitted. if that is try, put the following IF statement around all the code that should only run after the submit: if($_SERVER['REQUEST_METHOD'] == 'POST'){ } Quote Link to comment https://forums.phpfreaks.com/topic/141465-undefined-index-variable/#findComment-740471 Share on other sites More sharing options...
Redlightpacket Posted January 19, 2009 Author Share Posted January 19, 2009 I'll try that Quote Link to comment https://forums.phpfreaks.com/topic/141465-undefined-index-variable/#findComment-740472 Share on other sites More sharing options...
Redlightpacket Posted January 19, 2009 Author Share Posted January 19, 2009 like that if($_SERVER['REQUEST_METHOD'] == 'POST'){ $idea = addslashes($_POST['idea']); } Quote Link to comment https://forums.phpfreaks.com/topic/141465-undefined-index-variable/#findComment-740478 Share on other sites More sharing options...
rhodesa Posted January 19, 2009 Share Posted January 19, 2009 no...you will want it around the mysql_query() too. i tried to put it in your code, but it's a mess. why do you have more then one HTML page in there? Quote Link to comment https://forums.phpfreaks.com/topic/141465-undefined-index-variable/#findComment-740480 Share on other sites More sharing options...
Redlightpacket Posted January 19, 2009 Author Share Posted January 19, 2009 see i'm kind of a beginner at programming,. what should i do Quote Link to comment https://forums.phpfreaks.com/topic/141465-undefined-index-variable/#findComment-740484 Share on other sites More sharing options...
rhodesa Posted January 19, 2009 Share Posted January 19, 2009 what are you trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/141465-undefined-index-variable/#findComment-740487 Share on other sites More sharing options...
Redlightpacket Posted January 19, 2009 Author Share Posted January 19, 2009 I'm trying to make a comment system Quote Link to comment https://forums.phpfreaks.com/topic/141465-undefined-index-variable/#findComment-740488 Share on other sites More sharing options...
Redlightpacket Posted January 19, 2009 Author Share Posted January 19, 2009 oh, I was trying to explain to you what i was talking about Quote Link to comment https://forums.phpfreaks.com/topic/141465-undefined-index-variable/#findComment-740491 Share on other sites More sharing options...
Redlightpacket Posted January 19, 2009 Author Share Posted January 19, 2009 there is simple.php and idea.php. simple.php displays the stuff on the screen Quote Link to comment https://forums.phpfreaks.com/topic/141465-undefined-index-variable/#findComment-740493 Share on other sites More sharing options...
Redlightpacket Posted January 19, 2009 Author Share Posted January 19, 2009 it worked Quote Link to comment https://forums.phpfreaks.com/topic/141465-undefined-index-variable/#findComment-740499 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.