Jump to content

Adding 1 to the current value of a field in a mySQL db


son.of.the.morning

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.