Jump to content

[SOLVED] UPDATE Query can wrong!


ballhogjoni

Recommended Posts

Can some one help me figure this out. I have this update query below and it seems to not be working. I have the $id info coming from a XML page. My parser works and my INSERT query works, just not my UPDATE. What I want to do is just run this script when I need to upate certain parts of the rows in the table of my db.

<?php
$query = mysql_query("SELECT cid FROM cardOffers WHERE cid = ".$id['CID']."");
  $doesitexist = mysql_num_rows($query);
  if ($doesitexist > 0) {
mysql_query("UPDATE cardOffers SET creditcard = '".$id['CREDITCARD']."', category = '".$id['CATEGORY']."', issuer = '".$id['ISSUER']."', link = '".$id['LINK']."', picturehtmlcard = '".$id['PICTUREHTMLCARD']."', ba = '".$id['BA']."', bb = '".$id['BB']."', bc = '".$id['BC']."', bd = '".$id['BD']."', be = '".$id['BE']."', bf = '".$id['BF']."', bg = '".$id['BG']."', bh = '".$id['BH']."', cardtype = '".$id['CARDTYPE']."', introductoryrate = '".$id['INTRODUCTORYRATE']."', timeperiod = '".$id['TIMEPERIOD']."', appliedto = '".$id['APPLIEDTO']."', introdetails = '".$id['INTRODETAILS']."', aprpurchnum = '".$id['APRPURCHNUM']."', annual = '".$id['ANNUAL']."', review = '".$id['REVIEW']."', introductoryratealpha = '".$id['INTRODUCTORYRATEALPHA']."' WHERE cid = '".$id['CID']."'");
  	echo $id['CID'].' was updated<br>';
  } else {
  	mysql_query("INSERT INTO cardOffers (cid,creditcard,category,issuer,link,picturehtmlcard,ba,bb,bc,bd,be,bf,bg,bh,cardtype,introductoryrate,timeperiod,appliedto,introdetails,aprpurchnum,annual,review,introductoryratealpha) VALUES ('".$id['CID']."','".$id['CREDITCARD']."','".$id['CATEGORY']."','".$id['ISSUER']."','".$id['LINK']."','".$id['PICTUREHTMLCARD']."','".$id['BA']."','".$id['BB']."','".$id['BC']."','".$id['BD']."','".$id['BE']."','".$id['BF']."','".$id['BG']."','".$id['BH']."','".$id['CARDTYPE']."','".$id['INTRODUCTORYRATE']."','".$id['TIMEPERIOD']."','".$id['APPLIEDTO']."','".$id['INTRODETAILS']."','".$id['APRPURCHNUM']."','".$id['ANNUAL']."','".$id['REVIEW']."','".$id['INTRODUCTORYRATEALPHA']."')");
  	echo $id['CID'].' <font size="4" color="#FF0000">was new</font><br>';
  }
?>

Link to comment
Share on other sites

Try building the query as a string and echo'ing it to the screen to see exactly what your query is - it may be failing for something simple like a mismatched datatype.

 

Something like this:

<?php
  $debug="UPDATE cardOffers SET creditcard = '".$id['CREDITCARD']."', category = '".$id['CATEGORY']."', issuer = '".$id['ISSUER']."', link = '".$id['LINK']."', picturehtmlcard = '".$id['PICTUREHTMLCARD']."', ba = '".$id['BA']."', bb = '".$id['BB']."', bc = '".$id['BC']."', bd = '".$id['BD']."', be = '".$id['BE']."', bf = '".$id['BF']."', bg = '".$id['BG']."', bh = '".$id['BH']."', cardtype = '".$id['CARDTYPE']."', introductoryrate = '".$id['INTRODUCTORYRATE']."', timeperiod = '".$id['TIMEPERIOD']."', appliedto = '".$id['APPLIEDTO']."', introdetails = '".$id['INTRODETAILS']."', aprpurchnum = '".$id['APRPURCHNUM']."', annual = '".$id['ANNUAL']."', review = '".$id['REVIEW']."', introductoryratealpha = '".$id['INTRODUCTORYRATEALPHA']."' WHERE cid = '".$id['CID']."'";
  echo $debug;
?>

 

Not sure if that'd work as I just copied and pasted your code but that's the sort of thing.

Link to comment
Share on other sites

revraz your right, but I don't know what it was. I jsut added the part that you suggested, Yesideez, to my code underneath the UPDATE query and it seems to be updating everything. Weird! So now the code looks like this, and works every time:

 

 

<?php
if ($doesitexist > 0) {
mysql_query("UPDATE cardOffers SET creditcard = '".$id['CREDITCARD']."', category = '".$id['CATEGORY']."', issuer = '".$id['ISSUER']."', link = '".$id['LINK']."', picturehtmlcard = '".$id['PICTUREHTMLCARD']."', ba = '".$id['BA']."', bb = '".$id['BB']."', bc = '".$id['BC']."', bd = '".$id['BD']."', be = '".$id['BE']."', bf = '".$id['BF']."', bg = '".$id['BG']."', bh = '".$id['BH']."', cardtype = '".$id['CARDTYPE']."', introductoryrate = '".$id['INTRODUCTORYRATE']."', timeperiod = '".$id['TIMEPERIOD']."', appliedto = '".$id['APPLIEDTO']."', introdetails = '".$id['INTRODETAILS']."', aprpurchnum = '".$id['APRPURCHNUM']."', annual = '".$id['ANNUAL']."', review = '".$id['REVIEW']."', introductoryratealpha = '".$id['INTRODUCTORYRATEALPHA']."' WHERE cid = '".$id['CID']."'");
  	echo $id['CID'].' was updated<br>';
$debug="UPDATE cardOffers SET creditcard = '".$id['CREDITCARD']."', category = '".$id['CATEGORY']."', issuer = '".$id['ISSUER']."', link = '".$id['LINK']."', picturehtmlcard = '".$id['PICTUREHTMLCARD']."', ba = '".$id['BA']."', bb = '".$id['BB']."', bc = '".$id['BC']."', bd = '".$id['BD']."', be = '".$id['BE']."', bf = '".$id['BF']."', bg = '".$id['BG']."', bh = '".$id['BH']."', cardtype = '".$id['CARDTYPE']."', introductoryrate = '".$id['INTRODUCTORYRATE']."', timeperiod = '".$id['TIMEPERIOD']."', appliedto = '".$id['APPLIEDTO']."', introdetails = '".$id['INTRODETAILS']."', aprpurchnum = '".$id['APRPURCHNUM']."', annual = '".$id['ANNUAL']."', review = '".$id['REVIEW']."', introductoryratealpha = '".$id['INTRODUCTORYRATEALPHA']."' WHERE cid = '".$id['CID']."'";
  echo $debug.'<br>';
  } else {
  	mysql_query("INSERT INTO cardOffers (cid,creditcard,category,issuer,link,picturehtmlcard,ba,bb,bc,bd,be,bf,bg,bh,cardtype,introductoryrate,timeperiod,appliedto,introdetails,aprpurchnum,annual,review,introductoryratealpha) VALUES ('".$id['CID']."','".$id['CREDITCARD']."','".$id['CATEGORY']."','".$id['ISSUER']."','".$id['LINK']."','".$id['PICTUREHTMLCARD']."','".$id['BA']."','".$id['BB']."','".$id['BC']."','".$id['BD']."','".$id['BE']."','".$id['BF']."','".$id['BG']."','".$id['BH']."','".$id['CARDTYPE']."','".$id['INTRODUCTORYRATE']."','".$id['TIMEPERIOD']."','".$id['APPLIEDTO']."','".$id['INTRODETAILS']."','".$id['APRPURCHNUM']."','".$id['ANNUAL']."','".$id['REVIEW']."','".$id['INTRODUCTORYRATEALPHA']."')");
  	echo $id['CID'].' <font size="4" color="#FF0000">was new</font><br>';
  }
?>

Link to comment
Share on other sites

Using die() is OK when writing the script but you should really be using the proper error handling routines. Otherwise people will be using your site and when it experiences problems - it stops working - not good.

 

At least using error handling routines you can give the user a message and giving them an option to carry on with the site somewhere else. Plus it gives you an opportunity to send yourself an email containing the data the user entered and a copy of the query that failed. That way you know your script is failing and what could be causing it.

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.