...

View Full Version : Variable writing over itself in table



mwm
01-25-2007, 03:14 PM
I have a simple form that updates customer info. On the page call cifcustedit.php there is a javascript calendar that pops to choose a date to be inserted into the db table. That works great. Here lies the problem:
1) If you just change the cdate by itself in goes into the table just fine, but when you change that along with another variable in the form it puts the cdate at the default of 00-00-0000.
2) Other times when another var is changed it echo's back the incorrect date ie. you put in from the calander 1-04-2007 and when echoed back in the form the next time you view it, it reads 04-20-2001?

I have no idea how to solve this, any help or suggestion is realy appreciated.
here is the code.



<?
session_start();
include "conn.php";
$id=$_POST['id'];
?>
<html>
<body><head></head>

<script language="javascript" src="http://xx.xx.xx/customer/cal2.js">


</script>


<body background="white" text= "#000000" link= "#A52A2A" vlink ="#A52A2A" alink= "#A52A2A"></body>
<form action="custeditupdate.php" method="POST" name = "cifcustedit" >
<h1><p align=center></p></h1>
<h2><p align=center>Edit Customer Information</p></
<h3></h3><pre><p align =center><a href="managerstart.php">Main</a>
</center></pre>
<font color=#A52A2A>*</font>Required
<hr>

<?

$query = ("SELECT id,custname,custadd,city,state,zip,custphone,workphone,cellphone,community,lot,DATE_FORMAT(cdate,'%m-%d-%Y') FROM customers where id = '$id' and community ='$community'");
$result = mysql_query($query);
$row=mysql_fetch_row($result);


echo "<input type = hidden name = id value ='$id'>";

?>
<br>
<h3>
<p align=center><font color=#A52A2A>*</font>Customer Name <input type="text" name="custname" value = "<? echo $row['1']?>" size=30 maxlength=30><font color=#A52A2A>*</font>Address <input type="text" name="custadd" value = "<? echo $row['2']?>" size=30 maxlength=30><font color=#A52A2A><p align = center>*</font>City <input type="text" name="city" value = "<? echo $row['3']?>" size=25 maxlength=25><font color=#A52A2A>*</font>State <input type="text" name="state" value = "<? echo $row['4']?>" size=2 maxlength=2><font color=#A52A2A>*</font>Zip <input type="text" name="zip" value = "<? echo $row['5']?>" size=5 maxlength=5><font color=#A52A2A><p align = center>*</font>Phone <input type="text" name="custphone" value = "<? echo $row['6']?>" size=12 maxlength=12>Work Phone <input type="text" name="workphone" value = "<? echo $row['7']?>" size=12 maxlength=12> Cell Phone <input type="text" name="cellphone" value = "<? echo $row['8']?>" size=12 maxlength=12><p align = center>
<font color=#A52A2A>*</font>Community <input type="text" name="community" value = "<? echo $row['9']?>" size=20 maxlength=20>
<font color=#A52A2A>*</font>Lot/Building <input type="text" name="lot" value = "<? echo $row['10']?>"size=10 maxlength=10></p><script language="javascript" src="http://69.212.88.203/customer/cal_conf2.js"></script>
<font color=#A52A2A><p align = center>*</font>Close Date <input type="text" name="cdate" value = "<?echo $row['11']?>" size="10"><a href="javascript:showCal('Calendar4')"> Select Date</a>
<br></center>
</h3>
<p> <input type=Submit name=Submit style="font-size:15" value="Update"> <p>


</form>


</body>
</html>




<?
session_start();
include ("conn.php");

echo "<pre>";
print_r($_POST);
echo "</pre>";


$query = "UPDATE customers Set community = '$community',custname ='$custname',custadd ='$custadd',city = '$city', state = '$state', zip ='$zip',custphone = '$custphone', workphone = '$workphone',cellphone = '$cellphone', lot= '$lot', cdate = '$cdate' WHERE id = '$id'";

$result = mysql_query($query) or die ("Could not Update");

echo "All records have been change for $custname";


?>
<html>
<center><h3>
<body background="white" "text= "#000000" link= "#A52A2A" vlink ="#A52A2A" alink= "#A52A2A">
</h3>
<p><a href="managerstart.php">Main</a></center>
</html>

CFMaBiSmAd
01-25-2007, 03:29 PM
Your code is unconditionally executing everything each time the page is served (when the page is first visited and when the form is submitted - assuming that the URL in the form action="..." is this same page.)

Play computer and step through your code and ask what happens when it gets to the UPDATE query each time?

Also, you have multiple session_start() statements. The second one has no effect and in fact generates an error that gets logged to your server's log file, causing a slight speed penalty.

mwm
01-25-2007, 03:43 PM
Please forgive my stuipity. But I'm not sure what you mean. I have use <pre> to see what is being pass and that appears to correct. Maybe you would be willing to point me in the right direction?

Thanks

aedrin
01-25-2007, 04:01 PM
CFMaBiSmAd:

I believe these were meant to be 2 seperate PHP files.

If you look in the middle, there is a
tag.

mwm:

I'm not seeing immediately what could be going wrong.

Though one suggestion that I have is this. I find it easier to work with Unix timestamps than dates in MySQL. There's a lot of information on timestamps but the general idea is that a timestamp is the number of seconds since a certain standardized date (January 1, 1970).

You store these in a column of type INTEGER. To get a unix timestamp from a time that was entered by a user, you can use the strtotime() function in your PHP code. This will take a wide range of formats and then turn it into a timestamp.

If you can't, or don't want to change the database, upon updating the row you could do this:

[PHP]
$sql = "UPDATE ... SET cdate = FROM_UNIXTIME(" . strtotime($cdate) . ") WHERE ...";


To format the timestamp into a date to display, you simply use the date() function, like so.



echo date('Y/m/d H:i', $row['cdate']);
// This would output something like: 2007/01/25 08:10


There are several other MySQL functions that will help with Unix timestamps.

CFMaBiSmAd
01-25-2007, 05:13 PM
aedrin, you are probably right about it being two files/pieces of code. All the more reason why the original poster should review his post and edit it if necessary to make sure it is an accurate representation of what he intended.

mwm
01-25-2007, 05:41 PM
Yes its to scripts, sorry about that.

I have a simple form that updates customer info. On the page call cifcustedit.php there is a javascript calendar that pops to choose a date to be inserted into the db table. That works great. Here lies the problem:
1) If you just change the cdate by itself in goes into the table just fine, but when you change that along with another variable in the form it puts the cdate at the default of 00-00-0000.
2) Other times when another var is changed it echo's back the incorrect date ie. you put in from the calander 1-04-2007 and when echoed back in the form the next time you view it, it reads 04-20-2001?

I have no idea how to solve this, any help or suggestion is realy appreciated.
here is the code.

first script


<?
session_start();
include "conn.php";
$id=$_POST['id'];
?>
<html>
<body><head></head>

<script language="javascript" src="http://xx.xx.xx/customer/cal2.js">


</script>


<body background="white" text= "#000000" link= "#A52A2A" vlink ="#A52A2A" alink= "#A52A2A"></body>
<form action="custeditupdate.php" method="POST" name = "cifcustedit" >
<h1><p align=center></p></h1>
<h2><p align=center>Edit Customer Information</p></
<h3></h3><pre><p align =center><a href="managerstart.php">Main</a>
</center></pre>
<font color=#A52A2A>*</font>Required
<hr>

<?

$query = ("SELECT id,custname,custadd,city,state,zip,custphone,workphone,cellphone,community,lot,DATE_FORMAT(cdate,'%m-%d-%Y') FROM customers where id = '$id' and community ='$community'");
$result = mysql_query($query);
$row=mysql_fetch_row($result);


echo "<input type = hidden name = id value ='$id'>";

?>
<br>
<h3>
<p align=center><font color=#A52A2A>*</font>Customer Name <input type="text" name="custname" value = "<? echo $row['1']?>" size=30 maxlength=30><font color=#A52A2A>*</font>Address <input type="text" name="custadd" value = "<? echo $row['2']?>" size=30 maxlength=30><font color=#A52A2A><p align = center>*</font>City <input type="text" name="city" value = "<? echo $row['3']?>" size=25 maxlength=25><font color=#A52A2A>*</font>State <input type="text" name="state" value = "<? echo $row['4']?>" size=2 maxlength=2><font color=#A52A2A>*</font>Zip <input type="text" name="zip" value = "<? echo $row['5']?>" size=5 maxlength=5><font color=#A52A2A><p align = center>*</font>Phone <input type="text" name="custphone" value = "<? echo $row['6']?>" size=12 maxlength=12>Work Phone <input type="text" name="workphone" value = "<? echo $row['7']?>" size=12 maxlength=12> Cell Phone <input type="text" name="cellphone" value = "<? echo $row['8']?>" size=12 maxlength=12><p align = center>
<font color=#A52A2A>*</font>Community <input type="text" name="community" value = "<? echo $row['9']?>" size=20 maxlength=20>
<font color=#A52A2A>*</font>Lot/Building <input type="text" name="lot" value = "<? echo $row['10']?>"size=10 maxlength=10></p><script language="javascript" src="http://69.212.88.203/customer/cal_conf2.js"></script>
<font color=#A52A2A><p align = center>*</font>Close Date <input type="text" name="cdate" value = "<?echo $row['11']?>" size="10"><a href="javascript:showCal('Calendar4')"> Select Date</a>
<br></center>
</h3>
<p> <input type=Submit name=Submit style="font-size:15" value="Update"> <p>


</form>


</body>
</html>


Second script


<?
session_start();
include "conn.php";
$id=$_POST['id'];
?>
<html>
<body><head></head>

<script language="javascript" src="http://xx.xx.xx/customer/cal2.js">


</script>


<body background="white" text= "#000000" link= "#A52A2A" vlink ="#A52A2A" alink= "#A52A2A"></body>
<form action="custeditupdate.php" method="POST" name = "cifcustedit" >
<h1><p align=center></p></h1>
<h2><p align=center>Edit Customer Information</p></
<h3></h3><pre><p align =center><a href="managerstart.php">Main</a>
</center></pre>
<font color=#A52A2A>*</font>Required
<hr>

<?

$query = ("SELECT id,custname,custadd,city,state,zip,custphone,workphone,cellphone,community,lot,DATE_FORMAT(cdate,'%m-%d-%Y') FROM customers where id = '$id' and community ='$community'");
$result = mysql_query($query);
$row=mysql_fetch_row($result);


echo "<input type = hidden name = id value ='$id'>";

?>
<br>
<h3>
<p align=center><font color=#A52A2A>*</font>Customer Name <input type="text" name="custname" value = "<? echo $row['1']?>" size=30 maxlength=30><font color=#A52A2A>*</font>Address <input type="text" name="custadd" value = "<? echo $row['2']?>" size=30 maxlength=30><font color=#A52A2A><p align = center>*</font>City <input type="text" name="city" value = "<? echo $row['3']?>" size=25 maxlength=25><font color=#A52A2A>*</font>State <input type="text" name="state" value = "<? echo $row['4']?>" size=2 maxlength=2><font color=#A52A2A>*</font>Zip <input type="text" name="zip" value = "<? echo $row['5']?>" size=5 maxlength=5><font color=#A52A2A><p align = center>*</font>Phone <input type="text" name="custphone" value = "<? echo $row['6']?>" size=12 maxlength=12>Work Phone <input type="text" name="workphone" value = "<? echo $row['7']?>" size=12 maxlength=12> Cell Phone <input type="text" name="cellphone" value = "<? echo $row['8']?>" size=12 maxlength=12><p align = center>
<font color=#A52A2A>*</font>Community <input type="text" name="community" value = "<? echo $row['9']?>" size=20 maxlength=20>
<font color=#A52A2A>*</font>Lot/Building <input type="text" name="lot" value = "<? echo $row['10']?>"size=10 maxlength=10></p><script language="javascript" src="http://69.212.88.203/customer/cal_conf2.js"></script>
<font color=#A52A2A><p align = center>*</font>Close Date <input type="text" name="cdate" value = "<?echo $row['11']?>" size="10"><a href="javascript:showCal('Calendar4')"> Select Date</a>
<br></center>
</h3>
<p> <input type=Submit name=Submit style="font-size:15" value="Update"> <p>


</form>


</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum