Hello,
Can anyone please help me with a problem I’m having which I have simplified down from my original problem.
I have users logged into a website and when they are logged in I have a session that remembers their unique ID Username they entered when logging in throughout the ‘login based’ website.
Now when they are logged in they can access a form which saves data to a table in a MYSQL database called ‘offers’
The table is set-up as follows:
offer_id int(3) No auto_increment
name varchar(200) No
title varchar(200) No
full_details longtext No
redemption longtext No
restrictions longtext No
user varchar(40) No 0 //Want the $Username saved in this field
Now in this form coded below there are textboxes where these users can enter data which is saved to the above table.
But what it does not do – and what I want it to do is grab the session username that the user is logged in with and place it in the user field in the MYSQL database every time a record is saved and submit is hit.
I can get the username they are logged in with through a session to appear in the text box when the page loads and they will not change this textbox field, but can’t get it to save to the ‘offers’ table, when submit is hit:
Can anyone please advise as I’m going slightly bonkers here!?
<?php
// login stuff
include("****.php");
include("****.php");
$msg = "";
$name = "";
$title = "";
$full_details = "";
$redemption = "";
$restrictions = "";
$user = "$username";
if(isset($_POST['Submit']))
{
$name = $_POST['name'];
$title = $_POST['title'];
$full_details = $_POST['full_details'];
$redemption = $_POST['redemption'];
$restrictions = $_POST['restrictions'];
$user = $_POST['username'];
if(!isset($_GET['offer_id']))
{
$result = mysql_query("Insert into offers(name,title,full_details,redemption,restrictions,user) values('$name','$title','$full_details','$redemption','$restrictions','$user')");
$msg = "New record is saved";
}
else
{
$result = mysql_query("Update offers set name='$name', title='$title', full_details='$full_details', redemption='$redemption', restrictions='$restrictions', user='$user' where offer_id=".$_GET['offer_id']);
$msg = "Record is updated";
}
}
if(isset($_GET['offer_id']))
{
$result = mysql_query("Select * From offers where offer_id=".$_GET['offer_id'],$link);
$row = mysql_fetch_array($result, MYSQL_BOTH);
$name = $row['name'];
$title = $row['title'];
$full_details = $row['full_details'];
$redemption = $row['redemption'];
$restrictions = $row['restrictions'];
$user = $row['user'];
}
if(mysql_error())
{
echo mysql_error() ."<br>\n";
}
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Members-Only Page </title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1
</head>
<link rel="stylesheet" href="layout.css">
<link rel="stylesheet" href="layout.css">
<body>
<p> </p>
<p> <?=$username?> please enter a deal for the website below.</p>
<table width="95%" border="0">
<tr bgcolor="#FFFF00">
<td><?php echo $msg?></td>
</tr>
</table>
<form name="form1" method="post" action="">
<table width="95%" border="0">
<tr valign="middle">
<td height="20" colspan="2"> <img src="../images/1.jpg" width="20" height="20">
Username:</td>
</tr>
<tr>
<td colspan="2">
<p>
<input name="user" type="text" id="user" value="<?php echo $username?>" size="40">
</p>
<p><font color="#990000">** When the page loads the above field contains
the username they are logged in with (and they do not overwrite this)
, but when I hit submit below and all the fields are filled in it saves
a record to the database, but does not save the $username into the $user
field each time - just leaves it blank. **</font></p>
</td>
</tr>
</table>
<br>
<table width="95%" border="0">
<tr valign="middle">
<td height="20" colspan="2"> <img src="../images/2.jpg" width="20" height="20">
Please Enter Your Company Name:</td>
</tr>
<tr>
<td colspan="2">
<p>
<input name="name" type="text" id="name" value="<?php echo $name?>" size="40">
<font color="#990000"> ** This saves fine into the 'offers' field when
data is entered and submit is hit**</font></p>
</td>
</tr>
</table>
<br>
<table width="95%" border="0">
<tr valign="middle">
<td height="20" colspan="2"><img src="../images/3.jpg" width="20" height="20">
Please give a title for the offer:</td>
</tr>
<tr>
<td colspan="2">
<input name="title" type="text" id="title" value="<?php echo $title?>" size="40">
<font color="#990000">** This saves fine into the 'offers' field when
data is entered and submit is hit**</font> </td>
</tr>
</table>
<br>
<table width="95%" border="0">
<tr valign="middle">
<td height="20" colspan="2"><img src="../images/4.jpg" width="20" height="20">
Please give full details of the offer:</td>
</tr>
<tr>
<td colspan="2">
<textarea name="full_details" id="full_details" cols="60" rows="3"><?php echo $full_details?></textarea>
<font color="#990000">** This saves fine into the 'offers' field when
data is entered and submit is hit**</font></td>
</tr>
</table>
<br>
<table width="95%" border="0">
<tr valign="middle">
<td height="20" colspan="2"><img src="../images/5.jpg" width="20" height="20">
Method of Redemption:</td>
</tr>
<tr>
<td colspan="2">
<textarea name="redemption" id="redemption" cols="60" rows="3"><?php echo $redemption?></textarea>
<font color="#990000">** This saves fine into the 'offers' field when
data is entered and submit is hit**</font></td>
</tr>
</table>
<br>
<table width="95%" border="0">
<tr valign="middle">
<td height="20" colspan="2"><img src="../images/6.jpg" width="20" height="20">
Details of Restrictions:</td>
</tr>
<tr>
<td colspan="2">
<textarea name="restrictions" id="restrictions" cols="60" rows="3"><?php echo $restrictions?></textarea>
<font color="#990000">** This saves fine into the 'offers' field when
data is entered and submit is hit**</font></td>
</tr>
</table>
<p>
<input type="submit" name="Submit" value="Submit" class="buttons">
<input type="reset" name="Submit2" value="Reset" class="buttons">
</p>
<p><a href="listing.php">Back to Listings</a> | <a href="protectedpage.php">Back
to Index</a> | <a href="logout.php">Logout</a></p>
</form>
<?php
include ('./includes/footer.html');
?>