Need help with SELECT UPDATE WHERE

Hello:

I’m trying to write a php script to take data from a form and update numeric values in an SQL database.

I have multiple tables.

In the form I get the E-Mail address & am trying to use this to look up the user ID # in one table.

Then I want to increment a value in another table with an entered value from the form for that user id.

I can either get it to update ALL users or none, not just the one I want.

Here is the section of code I’m struggling with:

$db->query("
SELECT uid FROM userdata WHERE email = $sdataemail");
$uuid = uid;

$db->query("
UPDATE subscriptions
SET AL = AL + $sAL WHERE uid = $uuid");

I would greatly appreciate any help on this to get it working.

Thanks,

Phil

I would probably need to see more code to know for sure, but if your are setting your uuid variable and calling the update in a loop.

Hi:

Here’s the entire script, with sensitive parts X’d out.
It’s called by the form page, which is just an admin hidden page for me to update subscription durations.
I am connecting to the database with no problem.

I’m testing it one step at a time, so all the other updates are commented out for now.
I know this is just something stupid, because it works when I don’t use the WHERE, but then it does the action to all the rows of data, not just the specific one it’s supposed to.

Thanks for your help, I really appreciate your effort.

Phil

<?php //Connect to database. Use one database and several tables instead of multiple databases for simplicity's sake. $db = new mysqli("xxxxxxxxxxxx", "xxxxxxxx", "xxxxxxxxxxx", "xxxxxxxxxxxx"); //Create variables. This will allow us to sanitize user input more easily later. $sdataemail = $_POST['sdataemail']; $sAL = $_POST['sAL']; $sAK = $_POST['sAK']; $sAZ = $_POST['sAZ']; $sAR = $_POST['sAR']; $sCA = $_POST['sCA']; $sCO = $_POST['sCO']; $sCT = $_POST['sCT']; $sDE = $_POST['sDE']; $sDC = $_POST['sDC']; $sFL = $_POST['sFL']; $sGA = $_POST['sGA']; $sHI = $_POST['sHI']; $sID = $_POST['sID']; $sIL = $_POST['sIL']; $sIN = $_POST['sIN']; $sIA = $_POST['sIA']; $sKS = $_POST['sKS']; $sKY = $_POST['sKY']; $sLA = $_POST['sLA']; $sME = $_POST['sME']; $sMD = $_POST['sMD']; $sMA = $_POST['sMA']; $sMI = $_POST['sMI']; $sMN = $_POST['sMN']; $sMS = $_POST['sMS']; $sMO = $_POST['sMO']; $sMI = $_POST['sMI']; $sNE = $_POST['sNE']; $sNV = $_POST['sNV']; $sNH = $_POST['sNH']; $sNJ = $_POST['sNJ']; $sNM = $_POST['sNM']; $sNY = $_POST['sNY']; $sNC = $_POST['sNC']; $sND = $_POST['sND']; $sOH = $_POST['sOH']; $sOK = $_POST['sOK']; $sOR = $_POST['sOR']; $sPA = $_POST['sPA']; $sRI = $_POST['sRI']; $sSC = $_POST['sSC']; $sSD = $_POST['sSD']; $sTN = $_POST['sTN']; $sTX = $_POST['sTX']; $sUT = $_POST['sUT']; $sVT = $_POST['sVT']; $sVA = $_POST['sVA']; $sWA = $_POST['sWA']; $sWV = $_POST['sWV']; $sWI = $_POST['sWI']; $sWY = $_POST['sWY']; //Store values in database $db->query(" SELECT uid FROM userdata WHERE email = $sdataemail"); $uuid = uid; $db->query(" UPDATE subscriptions SET AL = AL + $sAL WHERE uid = $uuid"); // If ($sAK > 0) THEN { // SET AK = AK + $sAK; // WHERE email = $sdataemail;} // If ($sAZ > 0) THEN { // SET AZ = AZ + $sAZ; // WHERE email = $sdataemail;} // If ($sAR > 0) THEN { // SET AR = AR + $sAR; // WHERE email = $sdataemail;} // If ($sCA > 0) THEN { // SET CA = CA + $sCA; // WHERE email = $sdataemail;} // If ($sCO > 0) THEN { // SET CO = CO + $sCO; // WHERE email = $sdataemail;} // If ($sCT > 0) THEN { // SET CT = CT + $sCT; // WHERE email = $sdataemail;} // If ($sDE > 0) THEN { // SET DE = DE + $sDE; // WHERE email = $sdataemail;} // If ($sFL > 0) THEN { // SET FL = FL + $sFL; // WHERE email = $sdataemail;} // If ($sGA > 0) THEN { // SET GA = GA + $sGA; // WHERE email = $sdataemail;} // If ($sHI > 0) THEN { // SET HI = HI + $sHI; // WHERE email = $sdataemail;} // If ($sID > 0) THEN { // SET ID = ID + $sID; // WHERE email = $sdataemail;} // If ($sIL > 0) THEN { // SET IL = IL + $sIL; // WHERE email = $sdataemail;} // If ($sIN > 0) THEN { // SET IN = IN + $sIN; // WHERE email = $sdataemail;} // If ($sIA > 0) THEN { // SET IA = IA + $sIA; // WHERE email = $sdataemail;} // If ($sKS > 0) THEN { // SET KS = KS + $sKS; // WHERE email = $sdataemail;} // If ($sKY > 0) THEN { // SET KY = KY + $sKY; // WHERE email = $sdataemail;} // If ($sLA > 0) THEN { // SET LA = LA + $sLA; // WHERE email = $sdataemail;} // If ($sME > 0) THEN { // SET ME = ME + $sME; // WHERE email = $sdataemail;} // If ($sMD > 0) THEN { // SET MD = MD + $sMD; // WHERE email = $sdataemail;} // If ($sMA > 0) THEN { // SET MA = MA + $sMA; // WHERE email = $sdataemail;} // If ($sMI > 0) THEN { // SET MI = MI + $sMI; // WHERE email = $sdataemail;} // If ($sMN > 0) THEN { // SET MN = MN + $sMN; // WHERE email = $sdataemail;} // If ($sMS > 0) THEN { // SET MS = MS + $sMS; // WHERE email = $sdataemail;} // If ($sMO > 0) THEN { // SET MO = MO + $sMO; // WHERE email = $sdataemail;} // If ($sMT > 0) THEN { // SET MT = MT + $sMT; // WHERE email = $sdataemail;} // If ($sNE > 0) THEN { // SET NE = NE + $sNE; // WHERE email = $sdataemail;} // If ($sNV > 0) THEN { // SET NV = NV + $sNV; // WHERE email = $sdataemail;} // If ($sNH > 0) THEN { // SET NH = NH + $sNH; // WHERE email = $sdataemail;} // If ($sNJ > 0) THEN { // SET NJ = NJ + $sNJ; // WHERE email = $sdataemail;} // If ($sNM > 0) THEN { // SET NM = NM + $sNM; // WHERE email = $sdataemail;} // If ($sNY > 0) THEN { // SET NY = NY + $sNY; // WHERE email = $sdataemail;} // If ($sNC > 0) THEN { // SET NC = NC + $sNC; // WHERE email = $sdataemail;} // If ($sND > 0) THEN { // SET ND = ND + $sND; // WHERE email = $sdataemail;} // If ($sOH > 0) THEN { // SET OH = OH + $sOH; // WHERE email = $sdataemail;} // If ($sOK > 0) THEN { // SET OK = OK + $sOK; // WHERE email = $sdataemail;} // If ($sOR > 0) THEN { // SET OR = OR + $sOR; // WHERE email = $sdataemail;} // If ($sPA > 0) THEN { // SET PA = PA + $sPA; // WHERE email = $sdataemail;} // If ($sRI > 0) THEN { // SET RI = RI + $sRI; // WHERE email = $sdataemail;} // If ($sSC > 0) THEN { // SET SC = SC + $sSC; // WHERE email = $sdataemail;} // If ($sSD > 0) THEN { // SET SD = SD + $sSD; // WHERE email = $sdataemail;} // If ($sTN > 0) THEN { // SET TN = TN + $sTN; // WHERE email = $sdataemail;} // If ($sTX > 0) THEN { // SET TX = TX + $sTX; // WHERE email = $sdataemail;} // If ($sUT > 0) THEN { // SET UT = UT + $sUT; // WHERE email = $sdataemail;} // If ($sVT > 0) THEN { // SET VT = VT + $sVT; // WHERE email = $sdataemail;} // If ($sVA > 0) THEN { // SET VA = VA + $sVA; // WHERE email = $sdataemail;} // If ($sWA > 0) THEN { // SET WA = WA + $sWA; // WHERE email = $sdataemail;} // If ($sWV > 0) THEN { // SET WV = WV + $sWV; // WHERE email = $sdataemail;} // If ($sWI > 0) THEN { // SET WI = WI + $sWI; // WHERE email = $sdataemail;} // If ($sWY > 0) THEN { // SET WY = WY + $sWY; // WHERE email = $sdataemail;} // If ($sDC > 0) THEN { // SET DC = DC + $sDC; // WHERE email = $sdataemail;} //Redirect to main logged in page // header("refresh:1; url=../xxxxxxxxxxxxxxxxx.html"); ?>

I’m sorry I posted this to the wrong section, I’m new on here & got confused.
I’ll make sure to post any future questions in the correct section.
Once again, sorry about that.
Phil

It doesn’t work this way…

[php]$db->query("
SELECT uid FROM userdata WHERE email = $sdataemail");
$uuid = uid;[/php]

it needs to be similar to this…

[php]$result = $db->query(“SELECT uid FROM userdata WHERE email = $sdataemail”);
$row = $result->fetch_assoc();
$uuid = $row[‘uid’];
[/php]

Hi:

I put in your code:

$result = $db->query(“SELECT uid FROM userdata WHERE email = $sdataemail”);
$row = $result->fetch_assoc();
$uuid = $row[‘uid’];

$db->query("
UPDATE subscriptions
SET AL = AL + $sAL WHERE uid = $uuid");

Tested it & got this message:

Fatal error: Call to a member function fetch_assoc() on a non-object in /homepages/2/d546963395/htdocs/scripts/subsdataform.php on line 63

Line 63 is: $row = $result->fetch_assoc();

Thanks,

Phil

I don’t think it’s the code, I think that your query isn’t returning any results or is not correct.

Do this, it should output the query to the webpage, copy it and run it directly against your database.

[php]echo “SELECT uid FROM userdata WHERE email = $sdataemail”;
exit;
$result = $db->query(“SELECT uid FROM userdata WHERE email = $sdataemail”);
$row = $result->fetch_assoc();
$uuid = $row[‘uid’];[/php]

Then run the query directly against your database and see what happens.

Hi:

I ran it in the Admin page of the database and got this result:

SQL query: Documentation

echo “SELECT uid FROM userdata WHERE email = $sdataemail”;

MySQL said: Documentation
#1064 - 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 ‘echo “SELECT uid FROM userdata WHERE email = $sdataemail”’ at line 1

I also ran it from the web page and got:

SELECT uid FROM userdata WHERE email = [email protected]

Where the E-mail address was what had been entered on the form.

So look at the results, we missed something very simple, notice that their isn’t any quotes around the email address? That makes the SQL Statement invalid.

Now try this…

[php] $result = $db->query(“SELECT uid FROM userdata WHERE email = ‘$sdataemail’”);
$row = $result->fetch_assoc();
$uuid = $row[‘uid’][/php]

Awesome!!!
Just had to put the semi-colon on the last line & it worked like a charm!
Thank you so much for your help!
This will make my life soooo much easier dealing with this.

I always forget the semi-colons.

Sponsor our Newsletter | Privacy Policy | Terms of Service