designedfree4u Posted February 28, 2009 Share Posted February 28, 2009 How do I add a number to a certain field? I want to add 1 number to every entry into the field named count ('$_POST[email]','$_POST[count]')";if (!mysql_query($sql,$con)) I tryed: but doesn't work ('$_POST[email]','$_POST[count ++]')";if (!mysql_query($sql,$con)) Any help? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/147248-how-do-i-add-a-number/ Share on other sites More sharing options...
teng84 Posted February 28, 2009 Share Posted February 28, 2009 $_POST[count ++] count ++ <-- will be treated as string here so no ++ or incrementing here you need tto make it like this $counter = 0; $counter ++; $_POST['count'.$counter] or $_POST[$counter] or you might want to loop it for($i = 0; $i<n ; $i++){ echo $_POST[$i] ; } Quote Link to comment https://forums.phpfreaks.com/topic/147248-how-do-i-add-a-number/#findComment-772992 Share on other sites More sharing options...
designedfree4u Posted February 28, 2009 Author Share Posted February 28, 2009 I'm pretty new at this, here is the intire code. i added the new var, like you said, but i'm having trouble incorporating it into my code. $counter = 0; $counter ++; Intire code: <?php $con = mysql_connect("localhost","******","******"); if (!$con) { die('Could not connect: ' . mysql_error()); } $counter = 0; $counter ++; mysql_select_db("jaybirdf_contestvote", $con);$sql="INSERT INTO Jason (email,count) VALUES ('$_POST[email]','$_POST[count]')";if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "1 record added";mysql_close($con) ?> Quote Link to comment https://forums.phpfreaks.com/topic/147248-how-do-i-add-a-number/#findComment-773008 Share on other sites More sharing options...
Maq Posted February 28, 2009 Share Posted February 28, 2009 You're right, you can't use ++ with POST arrays, but you can just do plain old + 1. $count = $_POST['count'] + 1; mysql_select_db("jaybirdf_contestvote", $con); $sql="INSERT INTO Jason (email,count) VALUES ('$_POST[email]','$count')"; Quote Link to comment https://forums.phpfreaks.com/topic/147248-how-do-i-add-a-number/#findComment-773029 Share on other sites More sharing options...
designedfree4u Posted February 28, 2009 Author Share Posted February 28, 2009 well we are getting close. yes it adds the number 1 to that field but i want it to add 1 for every entry. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/147248-how-do-i-add-a-number/#findComment-773037 Share on other sites More sharing options...
Maq Posted February 28, 2009 Share Posted February 28, 2009 Ok... Use the logic I gave you before along with this: $sql="UPDATE Jason SET count = count + 1"; mysql_query($sql) or die(mysql_error()); Quote Link to comment https://forums.phpfreaks.com/topic/147248-how-do-i-add-a-number/#findComment-773043 Share on other sites More sharing options...
designedfree4u Posted February 28, 2009 Author Share Posted February 28, 2009 perfect, Thanks a bunch Quote Link to comment https://forums.phpfreaks.com/topic/147248-how-do-i-add-a-number/#findComment-773049 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.