Jump to content

Recommended Posts

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>
';

}

Link to comment
https://forums.phpfreaks.com/topic/141465-undefined-index-variable/
Share on other sites

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'){


}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.