Jump to content

Query not working


herghost

Recommended Posts

Evening everybody, hoping someone can see my error here!

$sql="INSERT INTO transactions (t_ref, m_affid, m_name, order_val, order_date, order_status, u_id, u_comm) VALUES('$t_ref', '$m_affid', '$m_name', '$order_val', '$order_date', '$order_status', '$u_id', '$u_comm')";
				if (!mysql_query($sql))
  					{ 
   						mysql_error();
  					}
		else

				{
					$newq = mysql_query("SELECT * FROM userbalance WHERE u_id = '$u_id'");
					echo mysql_error();
					$row = mysql_fetch_assoc($newq);
					$cpending = $row['pending'];
					$npending = $cpending + $u_comm;
					$updatepending = "INSERT into userbalance (pending)
					VALUES ('$npending')
					WHERE u_id = '$u_id'";

					if (!mysql_query($updatepending))
  					{ 
   						mysql_error();
  					}

				}

 

Basically  the 1st query works and inserts the data correctly, however once it gets to $newq = mysql_query("SELECT * FROM userbalance WHERE u_id = '$u_id'"); this query does not insert anything, no errors.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/219139-query-not-working/
Share on other sites

Hi Pickachu,

 

Thanks for you speedy reply as always,

 

{
					$newq = mysql_query("SELECT * FROM userbalance WHERE u_id = '$u_id'");
					echo mysql_error();
					$row = mysql_fetch_assoc($newq);
					$cpending = $row['pending'];
					echo  "<br>" . $cpending . "<br />";

					$npending = $cpending + $u_comm;
					echo  "<br>" . $npending . "<br />";

					echo $u_id;
					$updatepending = "INSERT into userbalance (pending)
					VALUES ('$npending')
					WHERE u_id = '$u_id'";

					if (!mysql_query($updatepending))
  					{ 
   						mysql_error();
  					}

				}

 

Yes cpending, npending and u_id all echo correctly, just the query is not working.

 

Ive double checked the table is definitely called userbalance, and the two fields involved are definitely called pending and u_id!

Link to comment
https://forums.phpfreaks.com/topic/219139-query-not-working/#findComment-1136395
Share on other sites

ah, if I add an echo to the mysql_error I get:

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 'WHERE u_id = '23'' at line 1

 

Can I not use a where statement like this in an insert into query?

 

Link to comment
https://forums.phpfreaks.com/topic/219139-query-not-working/#findComment-1136398
Share on other sites

Turns out no, no I cant!

 

Changed it to an update but still not inserting values:

{
					$newq = mysql_query("SELECT * FROM userbalance WHERE u_id = '$u_id'");
					echo mysql_error();
					$row = mysql_fetch_assoc($newq);
					$cpending = $row['pending'];
					echo  "<br>" . $cpending . "<br />";

					$npending = $cpending + $u_comm;
					echo  "<br>" . $npending . "<br />";

					echo $u_id;

					$updatepending = "UPDATE userbalance SET pending= '$npending' WHERE u_id = '$u_id'";


						echo mysql_error();


				}

 

Link to comment
https://forums.phpfreaks.com/topic/219139-query-not-working/#findComment-1136402
Share on other sites

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.