Jump to content

I have a problem updating db table twice in on session


938660

Recommended Posts

I have a problem with the below code. Basically its a form that pulls data from the database of a particular user. You can also edit the information here - the problem is, it will only let me edit the data and update the database table once in my session. Someone said the code is right, but it it be somthing to do with the 'synchronisation' or somthing. The session works correctly in this process but ie all session data gets updated, but the update statement doesnt work on the 2nd attempt of an update.

 

if (!$_POST['submit']){

$sql = "SELECT * FROM users WHERE username = '$username'"; // Database checks
$result = mysql_query($sql) or die(mysql_error());
$num=mysql_num_rows($result);
$row = mysql_fetch_array($result);

$database_title = $row["title"];
$database_firstname = $row["firstname"];
$database_lastname = $row["lastname"];
$database_address1 = $row["address1"];
$database_address2 = $row["address2"];
$database_city = $row["city"];
$database_postcode = $row["postcode"];
$database_country = $row["country"];
$database_email = $row["email"];


?>
      </p>

<form method="post"  action="<? echo $PHP_SELF?>">

<h3>Enter Details Below</h3>

<fieldset>

<label>
<span><b>Title:</b></span>
<span class="price"><input name="title" type="text" id="title" value="<? echo $_SESSION['title'];?>" size="24" />
</span>
<br><br>
<span><b>First Name:</b></span>
<span class="price"><input name="firstname" type="text" id="firstname" value="<? echo $_SESSION['firstname'];?>" size="24" />
</span>
<br><br>
<span><b>Last Name:</b></span>
<span class="price"><input name="lastname" type="text" id="lastname" value="<? echo $_SESSION['lastname'];?>" size="24" />
</span>
<br><br>
<span><b>Address Line 1:</b></span>
<span class="price"><input name="address1" type="text" id="address1" value="<? echo $_SESSION['address1'];?>" size="24" />
</span>
<br><br>
<span><b>Address Line 2:</b></span>
<span class="price"><input name="address2" type="text" id="address2" value="<? echo $_SESSION['address2'];?>" size="24" />
</span>
<br><br>
<span><b>City/Town:</b></span>
<span class="price"><input name="city" type="text" id="city" value="<? echo $_SESSION['city'];?>" size="24" />
</span>
<br><br>
<span><b>Post Code:</b></span>
<span class="price"><input name="postcode" type="text" id="postcode" value="<? echo $_SESSION['postcode'];?>" size="24" />
</span>
<br><br>
<span><b>Country:</b></span>
<span class="price"><input name="country" type="text" id="country" value="<? echo $_SESSION['country'];?>" size="24" />
</span>
<br><br>
<span><b>Email:</b></span>
<span class="price">
<input name="email" type="text" id="email" value="<? echo $_SESSION['email'];?>" size="24" />
</span>
<br><br><br>
<input name="submit" type="submit" id="submit" value="Edit" />
</fieldset>
</form>
<?
} else {
$sql = "UPDATE users SET title = '$_POST[title]', firstname = '$_POST[firstname]', lastname = '$_POST[lastname]', address1 = '$_POST[address1]', address2 = '$_POST[address2]', city = '$_POST[city]', postcode = '$_POST[postcode]', country = '$_POST[country]', email = '$_POST[email]' WHERE username = '$_SESSION[username]'";
$result = mysql_query($sql) or die(mysql_error());




$_SESSION['username'] = $_POST['username'];
$_SESSION['password'] =  $_POST['password'];
$_SESSION['title'] =  $_POST['title'];
$_SESSION['firstname'] =  $_POST['firstname'];
$_SESSION['lastname'] =  $_POST['lastname'];
$_SESSION['address1'] =  $_POST['address1'];
$_SESSION['address2'] =  $_POST['address2'];
$_SESSION['city'] =  $_POST['city'];
$_SESSION['postcode'] =  $_POST['postcode'];
$_SESSION['country'] =  $_POST['country'];
$_SESSION['email'] =  $_POST['email'];
$_SESSION['logged_in'] = 1;

[code /]

Suggestions:

 

1) Use $_SERVER['PHP_SELF'] and not $PHP_SELF.

 

2) Always escape data before inserting/updating the table. See mysql_real_escape_string().

 

FYI - MySQL will not update the table when the data is the same. One piece of information has to be different before an update will occur.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.