fozze Posted February 14, 2011 Share Posted February 14, 2011 this is probably going to be really simple to fix but i cant figure it out :/ on one page i have a form which uses php to populate a dropdown box, now this form will allow the user to add 1 to the person which was selected in the dropdown box. The code for this looks like this.. <form action="addpoint.php" method="post"> <select> <?php $sql="SELECT id,name FROM man"; $result =mysql_query($sql); while ($data=mysql_fetch_assoc($result)){ ?> <option value ="<?php echo $data['id'] ?>" ><?php echo $data['name'] ?></option> <?php } ?> </select> <input type="submit" value="Add Point"/> </form> the php to process this form looks like this.. <?php $sql="UPDATE man SET points = (points + 1) WHERE name = ('$_POST[name]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "$_POST[name] has been added"; mysql_close($con) ?> The problem i have is that i dont think its pulling the name from the form so therefore it wont effect the database, ive tried the sql like this.. UPDATE man SET points = (points + 1) WHERE name = 'joe' and it works fine, any help would be gratefully received Owen Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 14, 2011 Share Posted February 14, 2011 Your <select> tag needs a name= attribute. Quote Link to comment Share on other sites More sharing options...
ruddie Posted February 14, 2011 Share Posted February 14, 2011 First off.. you do know you can simply echo the whole form to right..? Since right now I see you using <?php ?> tags for every variable you would like to echo, which I find very annoying (although if you prefer this, feel free to do it this way) About your problem.. It seems you forgot that it actually is a variable, which means you must call the query like this: UPDATE man SET points = (points + 1) WHERE name = '".$_POST[name]."'"; The dots are basically the problem, when you set a variable in an echo, or query simply must always have those dots. Don't worry though, I think a lot of people forget about these things all the time =P Edit: yes, select tags are required to Quote Link to comment Share on other sites More sharing options...
fozze Posted February 14, 2011 Author Share Posted February 14, 2011 thank you for helping all sorted now can believe i forgot to add a name attribute :/ grr and as for the dots never even new i needed them :s lol thanks for the help all working now =) Owen Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted February 14, 2011 Share Posted February 14, 2011 About your problem.. It seems you forgot that it actually is a variable, which means you must call the query like this: UPDATE man SET points = (points + 1) WHERE name = '".$_POST[name]."'"; The dots are basically the problem, when you set a variable in an echo, or query simply must always have those dots. Don't worry though, I think a lot of people forget about these things all the time =P The string concatenation, or 'dots', is not needed, and does nothing but lead to more typo errors. The string is double quoted, and the variable will be interpolated as-is. The only change I'd make (and it technically isn't really needed), would be to quote the array index then use complex notation. I do that mainly for consistency. $sql="UPDATE man SET points = ( points + 1 ) WHERE name = ( '{$_POST['name']}' )"; Quote Link to comment 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.