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.).

 

 

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 :(

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.

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>';
    }
}

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.