Jump to content

Problem re-updating database with ajax & php - Need help


websoft08

Recommended Posts

Hi guys,

 

I'm having a problem with this bit of code that is supposed to update my database with a text

 

field and dropdown selection using ajax.

 

The first part of the code has the Ajax function

 

The second part has the Edit fields that include 1) a text field & an associated dropdown 2) a

 

second text field & it's associated dropdown 3) a save button that calls the ajax function

 

The third part of the code has the Display fields that shows the updated result of the ajax and

 

mysql update.

 

The last part of the code (on a seperate page) has all the database updates that are called when

 

the ajax function is called.

 

The issue is that normally the code works perfectly and the database and the Display field is

 

updated using the ajax just fine, BUT if I enter in the same data in both text fields (screename

 

& screename2), example "a" and "a"  which updates the database ok and then remove the data from

 

both text fields which still updates the database ok and then try to RE-ADD the same data again

 

"a" and "a" the database doesn't get updated. It refuses to update UNLESS i use different data

 

for one of the text fields.

 

Then (assuming I use different data) it will update the database, BUT it won't allow me to then

 

delete the data and do an update.

 

I tried a bunch of different options to resolve the problem, but it never seems to go away no

 

matter what I do, can you help?

 

Thanks!

Will  ???

 

Code below (comments are not in the real code, just added here):

 

          /* TEST.PHP page */

 

          /*Ajax browser support code*/

 

          <script language="javascript" type="text/javascript">

          <!--

          function ajaxFunction()

          {

 

          var ajaxRequest;

          try

          {

          ajaxRequest = new XMLHttpRequest();

          }

          catch (e)

          {

            try

            {

            ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");

            }

            catch (e)

            {

              try

              {

              ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");

              }

              catch (e)

              {

              alert("Your browser broke!");

              return false;

              }

            }

          }

 

 

          ajaxRequest.onreadystatechange = function()

          {

          if(ajaxRequest.readyState == 4)

          {

          document.getElementById("display_screen_names").innerHTML = ajaxRequest.responseText;

          }

          }

 

          var x = document.getElementById("screename").value;

          var y =

 

document.edit_contact.type.options[document.edit_contact.type.selectedIndex].value;

          var x2 = document.getElementById("screename2").value;

          var y2 =

 

document.edit_contact.type2.options[document.edit_contact.type2.selectedIndex].value;

 

          ajaxRequest.open("GET", "/modules/contact_info/test2.php?screename=" + x + "&type=" +

 

y + "&screename2=" + x2 + "&type2=" + y2 + "&uid=" + uid, true);

          ajaxRequest.send(null);

          }

 

          //-->

          </script>

 

 

          /* EDIT AREA */

 

       

          /* This retieves a userid (uid) from another page */

          $uid = $_GET['uid'];

 

 

          /* This is the 1st text field that pulls the 1st "screename" info from the database */

 

          <input type="text" class="txt" size="20" name="screename" value="<?

 

mysql_connect("localhost", "testuser", "") or die(mysql_error() );

          mysql_select_db("mysite") or die(mysql_error() );

 

          $get_screename = mysql_query("SELECT screename FROM mysite_screename WHERE uid=$uid");

          $show_screename = mysql_fetch_array($get_screename);

          echo $show_screename[screename];

 

          ?>"/>

 

 

 

          /*  This is the 1st dropdown that pulls the 1st "type" info from the database  */

 

          <?

          mysql_connect("localhost", "testuser", "") or die(mysql_error() );

          mysql_select_db("mysite") or die(mysql_error() );

 

          $get_type = mysql_query("SELECT type FROM mysite_screename WHERE uid=$uid");

          $show_type = mysql_fetch_array($get_type);

 

          if ($show_type[type] == Yahoo)

          {

          $yahoo = 'selected=yes';

          }

          if ($show_type[type] == MSN)

          {

          $msn = 'selected=yes';

          }

          if ($show_type[type] == AIM)

          {

          $aim = 'selected=yes';

          }

          if ($show_type[type] == GoogleTalk)

          {

          $googletalk = 'selected=yes';

          }

          if ($show_type[type] == Skype)

          {

          $skype = 'selected=yes';

          }

          if ($show_type[type] == ICQ)

          {

          $icq = 'selected=yes';

          }

          ?>

 

          <select name="type" class="select4">

          <option value="Yahoo" <? echo $yahoo ?> >Yahoo</option>

          <option value="MSN" <? echo $msn ?> >MSN</option>

          <option value="AIM" <? echo $aim ?> >AIM</option>

          <option value="GoogleTalk" <? echo $googletalk ?> >Google Talk</option>

          <option value="Skype" <? echo $skype ?> >Skype</option>

          <option value="ICQ" <? echo $icq ?> >ICQ</option>

          </select>

 

 

          <br />

 

 

          /* This is the 2nd text field that pulls the 2nd "screename" info from the database */

 

          <input type="text" class="txt" size="20" name="screename2" value="<?

 

mysql_connect("localhost", "testuser", "") or die(mysql_error() );

          mysql_select_db("mysite") or die(mysql_error() );

 

          $get_screename = mysql_query("SELECT screename2 FROM mysite_screename WHERE

 

uid=$uid");

          $show_screename = mysql_fetch_array($get_screename);

          echo $show_screename[screename2];

 

          ?>"/>

 

 

 

          /* This is the 2nd dropdown that pulls the 2nd "type" info from the database */?>

 

          <?

          mysql_connect("localhost", "testuser", "") or die(mysql_error() );

          mysql_select_db("mysite") or die(mysql_error() );

 

          $get_type = mysql_query("SELECT type2 FROM mysite_screename WHERE uid=$uid");

          $show_type = mysql_fetch_array($get_type);

 

          if ($show_type[type2] == Yahoo)

          {

          $yahoo2 = 'selected=yes';

          }

          if ($show_type[type2] == MSN)

          {

          $msn2 = 'selected=yes';

          }

          if ($show_type[type2] == AIM)

          {

          $aim2 = 'selected=yes';

          }

          if ($show_type[type2] == GoogleTalk)

          {

          $googletalk2 = 'selected=yes';

          }

          if ($show_type[type2] == Skype)

          {

          $skype2 = 'selected=yes';

          }

          if ($show_type[type2] == ICQ)

          {

          $icq2 = 'selected=yes';

          }

          ?>

 

          <select name="type2" class="select4">

          <option value="Yahoo" <? echo $yahoo2 ?> >Yahoo</option>

          <option value="MSN" <? echo $msn2 ?> >MSN</option>

          <option value="AIM" <? echo $aim2 ?> >AIM</option>

          <option value="GoogleTalk" <? echo $googletalk2 ?> >Google Talk</option>

          <option value="Skype" <? echo $skype2 ?> >Skype</option>

          <option value="ICQ" <? echo $icq2 ?> >ICQ</option>

          </select>

 

          </td>

          </tr>

  </table>

</td>

</tr>

 

        /* Button that calls the AJAX function */

 

<input value="Save" type="button" onclick="ajaxFunction();">

 

 

 

 

/* DISPLAY AREA */

 

 

  /* This div contains & displays the fields with the default mysql values for screen names &

 

displays the updated fields from the AJAX code */

 

  <div id="display_screen_names">

 

  <?

  mysql_connect("localhost", "testuser", "") or die(mysql_error() );

  mysql_select_db("mysite") or die(mysql_error() );

 

  $get_screename_and_type = mysql_query("SELECT screename, type, screename2, type2 FROM

 

mysite_screename WHERE uid=$uid ORDER BY uid");

  $show_screename_and_type = mysql_fetch_array($get_screename_and_type);

 

 

  if ($show_screename_and_type[screename] != "")

  {

  echo  "<b>" . $show_screename_and_type[type] . "</b>" . ":" .

 

$show_screename_and_type[screename] . " ";

  }

 

  if ($show_screename_and_type[screename2] != "")

  { 

  echo "<b>" . $show_screename_and_type[type2] . "</b>" . ":" .

 

$show_screename_and_type[screename2] ;

  }

 

  ?>

 

  </div>

 

 

 

/* TEST2.PHP page */

 

 

<?php

 

/* These variables we take using GET from the test.php page */

 

$uid = $_GET['uid'];

$screename = $_GET["screename"];

$type = $_GET["type"];

$screename2 = $_GET["screename2"];

$type2 = $_GET["type2"];

 

/* Connection to the mysql & mysite database */

 

mysql_connect("localhost", "testuser", "") or die(mysql_error() );

mysql_select_db("mysite") or die(mysql_error() );

 

 

/*  Checks if the $uid exists, if it does exist then it updates the row, if it doesn't exist it

 

inserts a new row */

 

$check_uid = "SELECT uid FROM mysite_screename WHERE uid='$uid' ORDER BY uid;";

$answer_uid = mysql_query($check_uid);

if (mysql_num_rows($answer_uid) > 0)

{

  if ($screename != NULL)

  {

  /*  Updates the existing row if a value is being passed for screename */

  mysql_query("UPDATE mysite_screename SET screename='$screename', type='$type' WHERE uid='$uid'

 

ORDER BY uid ");

  }

  else

  {

  /*  Updates the existing row with NULL if a value is not being passed */

  mysql_query("UPDATE mysite_screename SET screename=NULL, type=NULL WHERE uid='$uid' ORDER BY

 

uid ");

  }

 

  if ($screename2 != NULL)

  {

  /*  Updates the existing row if a value is being passed for screename & screename2 */

  mysql_query("UPDATE mysite_screename SET screename2='$screename2', type2='$type2' WHERE

 

uid='$uid' ORDER BY uid ");

  }

  else

  {

  /*  Updates the existing row with NULL if a value is not being passed */

  mysql_query("UPDATE mysite_screename SET screename2=NULL, type2=NULL WHERE uid='$uid' ORDER BY

 

uid ");

  }

}

 

else

{

/*  Inserts a new row */

mysql_query("INSERT INTO mysite_screename (id, uid, screename, type, screename2, type2) VALUES

 

(NULL , '$uid', '$screename', '$type', '$screename2', '$type2')") or die(mysql_error() );

}

 

 

/*  Passes screen name info to ajax code which displays changes instantly */

 

$get_screename_info = mysql_query("SELECT screename, type, screename2, type2 FROM

 

mysite_screename WHERE uid=$uid ORDER BY uid");

$show_screename_info = mysql_fetch_array($get_screename_info);

 

if ($show_screename_info[screename] != "")

{

echo  "<b>" . $show_screename_info[type] . "</b>" . ":" . $show_screename_info[screename] . " ";

}

 

if ($show_screename_info[screename2] != "")

echo "<b>" . $show_screename_info[type2] . "</b>" . ":" . $show_screename_info[screename2] ;

}

 

 

?>

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.