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.). Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 23, 2010 Share Posted October 23, 2010 UPDATE table SET field = (field + 1) WHERE . . . Quote Link to comment 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. Quote Link to comment 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 . . . Quote Link to comment 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 Quote Link to comment 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 . . . Quote Link to comment 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...? Quote Link to comment 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. Quote Link to comment 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>'; } } Quote Link to comment 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 Quote Link to comment 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"; Quote Link to comment 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 Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted October 24, 2010 Share Posted October 24, 2010 Is there a new error? Quote Link to comment Share on other sites More sharing options...
son.of.the.morning Posted October 24, 2010 Author Share Posted October 24, 2010 no :S 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.