Attached PHP, mysql, won't run twice

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Elridan
Forum Newbie
Posts: 3
Joined: Wed Dec 22, 2010 11:08 am

Attached PHP, mysql, won't run twice

Post by Elridan »

:?: I'm working on a member modification page that works off of the javascript "onblur" method. Ideally the way I want it to work is when a person leaves the field they were in the second php script runs and returns to the page a value to the page. Right now it does just that with one problem - it won't run the attached script twice if the values are the same twice.

For instance: If I'm tabbing through the fields several times for whatever reason and accidentally add an "a" to the name field one of those times, i realize this and want to go back to fix it. After I've fixed it I tab out and what should happen is the script runs, the new info is written to the databse, and the script returns new info for the page. What is currently happening is nothing - it doesn't seem to run the script.

Using Chrome I see that it's executing the script but it isn't writing back to the page or updating the database.

Any help or pointers I could get would be very welcome.

Thanks!

Member Edit Page:

Code: Select all

<?php

$title = "TAPCO File Upload Data";

$top1 = "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
<html xmlns='http://www.w3.org/1999/xhtml'>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />
<title>";

$top2 = "</title>
<link href='css.css' rel='stylesheet' type='text/css' />

<script type='text/javascript'>

function attach_file( p_script_url ) {
	// create new script element, set its relative URL, and load it
	script = document.createElement( 'script' );
	script.src = p_script_url;
	document.getElementsByTagName( 'head' )[0].appendChild( script );
}

</script>

</head>

<body>";

$host="localhost"; // Host name 
$username="ftpuploads"; // Mysql username 
$password="password"; // Mysql password 
$db_name="ftpuploads"; // Database name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$unam = $_GET["unam"];

$q = "select * from members where username = '".$unam."'";
$r = mysql_query($q);

$name = mysql_result($r, 0, 'name');
$email = mysql_result($r, 0, 'email');
$sp = mysql_result($r, 0, 'sp');
$sno = mysql_result($r, 0, 'sno');
$snow = "Sales Number: ".str_pad(mysql_result($r, 0, 'sno'), 3, 0, STR_PAD_LEFT);
$fa = mysql_result($r, 0, 'fa');
$user = mysql_result($r, 0, 'user');
$admin = mysql_result($r, 0, 'admin');
$active = mysql_result($r, 0, 'active');

mysql_close();


if ($user == "0" && $admin == "1") {
	$disuoa = "Admin";
}
elseif ($user == "1" && $admin == "0") {
	$disuoa = "User";
}
else {
	die("Something is wrong, both admin and user have the same value");
}

if ($active == "1") {
	$disactive = "Active";
}
else {
	$disactive = "Inactive";
}

if ($sp == "0") {
	$spcell = "<option value='1'>Yes</option>
               <option value='0' selected>No</option>";
}
else {
	$spcell = "<option value='1' selected>Yes</option>
               <option value='0'>No</option>";
}

if ($fa == "0") {
	$facell = "<option value='1'>Yes</option>
               <option value='0' selected>No</option>";
}
else {
	$facell = "<option value='1' selected>Yes</option>
               <option value='0'>No</option>";
}

if ($user == "0" && $admin == "1") {
	$uoacell = "<option value='User'>User</option>
				<option value='Admin' selected>Admin</option>";
}
else {
	$uoacell = "<option value='User' selected>User</option>
				<option value='Admin'>Admin</option>";
}

if ($active == "0") {
	$actcell = "<option value='1'>Yes</option>
               <option value='0' selected>No</option>";
}
else {
	$actcell = "<option value='1' selected>Yes</option>
               <option value='0'>No</option>";
}

echo $top1.$title.$top2;	

echo "<span id='disname'>".$name."</span><br />
	<span id='disuname'>".$unam."</span><br />
<span id='disemail'>".$email."</span><br />
<br /><span id='dissno'>".$snow."</span><br />
<br /><span id='disuoa'>".$disuoa."</span><br />
<br /><span id='disactive'>".$disactive."</span><br /><br />";

echo "<form>

<table>
	<tr>
    	<td>
            Name
        </td>
        <td>
            Password
        </td>
        <td>
            Sales Person?
        </td>
        <td>
            Sales Number
        </td>
        <td>
            Full Access?
        </td>
        <td>
            User or Admin?
        </td>
        <td>
            Active?
        </td>
    </tr>
	<tr>
    	<td>
            <input type='text' name='name' value='".$name."' onblur=\"javascript:attach_file('updatefield.php?span=disname&unam=".$unam."&fchan=name&val='+this.value)\" />
        </td>
        <td>
            <span id='passres'><a href=\"javascript:attach_file('resetpassword.php?uname=".$unam."&email=".$email."')\">Reset and Send</a></span>
        </td>
        <td>
            <select name='sp' onblur=\"javascript:attach_file('updatefield.php?span=&unam=".$unam."&fchan=sp&val='+this.value, this.value)\">
				".$spcell."
            </select>
        </td>
        <td>
            <input type='text' name='sno' onblur=\"javascript:attach_file('updatefield.php?span=dissno&unam=".$unam."&fchan=sno&val='+this.value)\" />
        </td>
        <td>
            <select name='fa'>
				".$facell."
            </select>
        </td>
        <td>
            <select name='uoa'>
				".$uoacell."
            </select>
        </td>
        <td>
            <select name='active'>
				".$actcell."
            </select>
        </td>
</table>

</form>
";



?>
updatefield page:

Code: Select all

<?

$host="localhost"; // Host name 
$username="ftpuploads"; // Mysql username 
$password="password"; // Mysql password 
$db_name="ftpuploads"; // Database name 

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

header( 'Content-Type: text/javascript' );

$span = $_GET['span'];
$username = $_GET['unam'];
$fchan = $_GET['fchan'];
$val = $_GET['val'];

$q = "select ".$fchan." from members where username = '".$username."'";
$r = mysql_query($q);

$fchanOrVal = mysql_result($r, 0, name);

if ($fchanOrVal == $val) {
	die();
}
else {

$qu = "update members set ".$fchan." = '".$val."' where username = '".$username."'";
$ru = mysql_query($qu);

echo "if ('".$span."' != '') {
	fchan_span_obj = document.getElementById( '".$span."' );
	fchan_span_obj.innerHTML = '".$val."';
}
else if ('".$span."' == '') {

}";

}

mysql_close();

?>
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Attached PHP, mysql, won't run twice

Post by Jonah Bron »

If you ask me, it doesn't seem like a good idea to save data onBlur. What's wrong with a normal form?
Elridan
Forum Newbie
Posts: 3
Joined: Wed Dec 22, 2010 11:08 am

Re: Attached PHP, mysql, won't run twice

Post by Elridan »

Jonah Bron wrote:If you ask me, it doesn't seem like a good idea to save data onBlur. What's wrong with a normal form?
It was just an idea at the time. I will probably end up switching to a form, however I would still like to know why it isn't running every time a change is made. Do you have any ideas?
Elridan
Forum Newbie
Posts: 3
Joined: Wed Dec 22, 2010 11:08 am

Re: Attached PHP, mysql, won't run twice

Post by Elridan »

I've figured out the problem. The code would only run once with the same values under the same session. After inserting session_start() and session_destroy() at the beginning and end of the actual edit code it works just fine.
Post Reply