Jump to content

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

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.