MandragoraSprout Posted September 24, 2009 Share Posted September 24, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/175396-undefined-variable/ Share on other sites More sharing options...
Pickle Posted September 24, 2009 Share Posted September 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175396-undefined-variable/#findComment-924383 Share on other sites More sharing options...
MandragoraSprout Posted September 25, 2009 Author Share Posted September 25, 2009 thx Pickle, i'll try this Quote Link to comment https://forums.phpfreaks.com/topic/175396-undefined-variable/#findComment-924631 Share on other sites More sharing options...
hamza Posted September 25, 2009 Share Posted September 25, 2009 if (isset($_POST['id'])){ $id = $_POST['id']; }else{ $id = ""; } you can make this check more easy like this $id = (isset($_POST['id']) ): $_POST['id'] ? ' ' Quote Link to comment https://forums.phpfreaks.com/topic/175396-undefined-variable/#findComment-924846 Share on other sites More sharing options...
tbare Posted September 25, 2009 Share Posted September 25, 2009 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.. Quote Link to comment https://forums.phpfreaks.com/topic/175396-undefined-variable/#findComment-924854 Share on other sites More sharing options...
mikesta707 Posted September 25, 2009 Share Posted September 25, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175396-undefined-variable/#findComment-924855 Share on other sites More sharing options...
MandragoraSprout Posted September 25, 2009 Author Share Posted September 25, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175396-undefined-variable/#findComment-924999 Share on other sites More sharing options...
mikesta707 Posted September 25, 2009 Share Posted September 25, 2009 post current code? Quote Link to comment https://forums.phpfreaks.com/topic/175396-undefined-variable/#findComment-925012 Share on other sites More sharing options...
MandragoraSprout Posted September 25, 2009 Author Share Posted September 25, 2009 post current code? current code is $id = (isset($_POST['id'])) ? $_POST['id'] : ''; do u mean this? Quote Link to comment https://forums.phpfreaks.com/topic/175396-undefined-variable/#findComment-925016 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.