Jump to content

Undefined variable


MandragoraSprout

Recommended Posts

i have a problem , can u help  me please?:( sorry for my english.

 

well..in the begining of the document i define variable id, like this:

<?php if (isset($_POST['id'])){$id = $_POST['id'];} ?>

 

now the second part of the code, where from i want to make changes in my database:

 

   

 <?php
if (isset($title) && isset($description) && isset($text) && isset($author))
{ $result = mysql_query ("UPDATE articles SET title='$title', description='$description', text='$text', author='$author' WHERE id='$id'");

if ($result == 'true') {echo "<p>article updated!</p>";}
else {echo "<p>article is not updated!</p>";}


}  
else

{
echo "<p>error!.</p>";
}    ?>

 

i get error in row UPDATE - "Undefined variable id!"

at the same time condition in var $result is done and i see notice "article updated!"

but there is no changes in database..

 

what can i do? any ideas?

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

you say that in the beginning of your file you have the following

<?php if (isset($_POST['id'])){$id = $_POST['id'];} ?>

 

but if $_POST['id'] is not set then your variable $id is not defined. so you need to write something like this:

 

if (isset($_POST['id'])){
     $id = $_POST['id'];
}else{
     $id = "";
}

 

then you should only start the next part of your script if you have an id. so:

 

if($id != ""){
   //do everything in here
}

 

hope that helps

Link to comment
https://forums.phpfreaks.com/topic/175396-undefined-variable/#findComment-924383
Share on other sites

if (isset($_POST['id'])){

    $id = $_POST['id'];

}else{

    $id = "";

}

 

you can make this check more easy like this

$id = (isset($_POST['id']) ): $_POST['id'] ?  '  '

 

Fewer lines, yes... more easy is in the eye of the beholder... personally, i prefer the first. i must admit, though, that i never took the time to learn what the ':' and '?' mean exactly..

Link to comment
https://forums.phpfreaks.com/topic/175396-undefined-variable/#findComment-924854
Share on other sites

if (isset($_POST['id'])){

    $id = $_POST['id'];

}else{

    $id = "";

}

 

you can make this check more easy like this

$id = (isset($_POST['id']) ): $_POST['id'] ?  '  '

 

 

thats incorrect syntax. It looks like this

$id = (isset($_POST['id'])) ? $_POST['id'] : '';

 

you mixed up the ternary operator with the colon

Link to comment
https://forums.phpfreaks.com/topic/175396-undefined-variable/#findComment-924855
Share on other sites

if (isset($_POST['id'])){

    $id = $_POST['id'];

}else{

    $id = "";

}

 

you can make this check more easy like this

$id = (isset($_POST['id']) ): $_POST['id'] ?  '  '

 

 

thats incorrect syntax. It looks like this

$id = (isset($_POST['id'])) ? $_POST['id'] : '';

 

you mixed up the ternary operator with the colon

 

 

well i replace old code with this

$id = (isset($_POST['id'])) ? $_POST['id'] : '';

 

and there is no error about undefined variable, but now my database can not update :(

 

 

Link to comment
https://forums.phpfreaks.com/topic/175396-undefined-variable/#findComment-924999
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.