son.of.the.morning Posted October 23, 2010 Share Posted October 23, 2010 Following from my previous post, i have a questionnaire and within each question it has the options to pick from raddio button (strongly agree, agree, disagree, N/A). For each value of each question i want to pass it into the relevant Field in my database but rather than overwriting the current value i want to be added on to it. (For example if the current value is 2, once the new value (1) has added it will make the new value of 3.). Link to comment https://forums.phpfreaks.com/topic/216661-adding-1-to-the-current-value-of-a-field-in-a-mysql-db/ Share on other sites More sharing options...
Pikachu2000 Posted October 23, 2010 Share Posted October 23, 2010 UPDATE table SET field = (field + 1) WHERE . . . Link to comment https://forums.phpfreaks.com/topic/216661-adding-1-to-the-current-value-of-a-field-in-a-mysql-db/#findComment-1125674 Share on other sites More sharing options...
son.of.the.morning Posted October 23, 2010 Author Share Posted October 23, 2010 does the 'feild' have to be the name of a feild. Link to comment https://forums.phpfreaks.com/topic/216661-adding-1-to-the-current-value-of-a-field-in-a-mysql-db/#findComment-1125705 Share on other sites More sharing options...
Pikachu2000 Posted October 23, 2010 Share Posted October 23, 2010 Yup, sure does. table also needs to be the actual name of the table . . . Link to comment https://forums.phpfreaks.com/topic/216661-adding-1-to-the-current-value-of-a-field-in-a-mysql-db/#findComment-1125707 Share on other sites More sharing options...
son.of.the.morning Posted October 23, 2010 Author Share Posted October 23, 2010 This is my code... here <?php $host=""; $username=""; $password=""; $db_name=""; $tbl_name="nameEmail"; $tbl_name2="wai-aria"; $name=$_POST['Uname']; $email=$_POST['emailAdd']; $wai=$_POST['wai-aria']; echo $name; echo $email; echo $wai; mysql_connect("$host", "$username", "$password")or die("Cannot connect to database!"); mysql_select_db("$db_name")or die("Cannot select database!"); $sql="INSERT INTO $tbl_name(name, email)VALUES('$name', '$email')"; $result=mysql_query($sql); if ($wai==yes){ $sql="UPDATE $tbl_name2 SET field = (field + 1) WHERE yes=0"; } ?> my table contains 3 rows yes, no and user all of these have one feild each all with the value of 0. I want 1 added to the value of one of these feilds depending on the $wai var value. It's just not wokring for me Link to comment https://forums.phpfreaks.com/topic/216661-adding-1-to-the-current-value-of-a-field-in-a-mysql-db/#findComment-1125708 Share on other sites More sharing options...
Pikachu2000 Posted October 23, 2010 Share Posted October 23, 2010 You really need to edit out your database credentials . . . Link to comment https://forums.phpfreaks.com/topic/216661-adding-1-to-the-current-value-of-a-field-in-a-mysql-db/#findComment-1125711 Share on other sites More sharing options...
son.of.the.morning Posted October 23, 2010 Author Share Posted October 23, 2010 how would i go about doing that...? Link to comment https://forums.phpfreaks.com/topic/216661-adding-1-to-the-current-value-of-a-field-in-a-mysql-db/#findComment-1125712 Share on other sites More sharing options...
Pikachu2000 Posted October 23, 2010 Share Posted October 23, 2010 With the "Modify" button on the upper right side of your post. You've included your DB hostname, username and password in your post . . . Actually, it may be a good idea to change your database password as soon as possible also. Link to comment https://forums.phpfreaks.com/topic/216661-adding-1-to-the-current-value-of-a-field-in-a-mysql-db/#findComment-1125713 Share on other sites More sharing options...
Pikachu2000 Posted October 24, 2010 Share Posted October 24, 2010 See what happens when you replace this: if ($wai==yes){ $sql="UPDATE $tbl_name2 SET field = (field + 1) WHERE yes=0"; } With this: if ($wai== 'yes'){ $sql="UPDATE $tbl_name2 SET field = (field + 1) WHERE yes=0"; if( mysql_query($sql) ) { if( mysql_affected_rows() > 0 ){ echo 'Updated ' . mysql_affected_rows() . 'records.<br>'; } else { echo 'No records were updated<br>'; } } else { echo '<br>Query: ' . $sql . '<br>Failed with error: ' . mysql_error() . '<br>'; } } Link to comment https://forums.phpfreaks.com/topic/216661-adding-1-to-the-current-value-of-a-field-in-a-mysql-db/#findComment-1125880 Share on other sites More sharing options...
son.of.the.morning Posted October 24, 2010 Author Share Posted October 24, 2010 Query: UPDATE wai-aria SET yes = (yes + 1) WHERE yes=0 Failed with error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-aria SET yes = (yes + 1) WHERE yes=0' at line 1 Accessibility & Usability in Rich Internet Applications : Web developers que Link to comment https://forums.phpfreaks.com/topic/216661-adding-1-to-the-current-value-of-a-field-in-a-mysql-db/#findComment-1125885 Share on other sites More sharing options...
Pikachu2000 Posted October 24, 2010 Share Posted October 24, 2010 You can't use a hyphen in a MySQL field/column name unless you enclose it in backticks. $sql="UPDATE `$tbl_name2` SET yes = (yes + 1) WHERE yes=0"; Link to comment https://forums.phpfreaks.com/topic/216661-adding-1-to-the-current-value-of-a-field-in-a-mysql-db/#findComment-1125889 Share on other sites More sharing options...
son.of.the.morning Posted October 24, 2010 Author Share Posted October 24, 2010 I have changed the name of the table to waiValue and input the code you given me and it still isnt effecting it Link to comment https://forums.phpfreaks.com/topic/216661-adding-1-to-the-current-value-of-a-field-in-a-mysql-db/#findComment-1125890 Share on other sites More sharing options...
Pikachu2000 Posted October 24, 2010 Share Posted October 24, 2010 Is there a new error? Link to comment https://forums.phpfreaks.com/topic/216661-adding-1-to-the-current-value-of-a-field-in-a-mysql-db/#findComment-1125893 Share on other sites More sharing options...
son.of.the.morning Posted October 24, 2010 Author Share Posted October 24, 2010 no :S Link to comment https://forums.phpfreaks.com/topic/216661-adding-1-to-the-current-value-of-a-field-in-a-mysql-db/#findComment-1125896 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.