I've wrestled with this code all day and I just can't figure out what the problem is.
I have this same code on the add_job.php page and the edit_job.php page and neither one is enter in the correct information into the database.
But first here's a synopsis of what happens up to this point:
The registration script adds users to the users table and a row in that table is named stateid. The information that goes into this stateid row is selected from a select list from the states table. The rows in this table are postal and statename and they've already been filled in. The statename is the name of a State like (Georgia) and postal is the 2 or 3 letter abbreviation of that state, i.e. (GA). I used 3 letters for a couple of states because I had problems with them in queries, like Indiana (IN) is now IND and Oregon (OR) id now ORG.
The registration script checks the state before entering it into the database:
[PHP]// Check for a state.
if (eregi ('^[[:alpha:]\.\' \-]{2,3}$', stripslashes(trim($_POST['state'])))) {
$s = escape_data($_POST['state']);
} else {
$s = FALSE;
echo '<p><font color="red" size="+1">Please enter your state!</font></p>';
}[/PHP]I don't know if this is right or not, but it does enter the correct state abbreviation into the users table row stateid.
The login script checks the database table users for correct email and password, and if it matches sets the session variable [PHP]$_SESSION['stateid'] = $row[8];[/PHP] correctly -in this case AL for Alabama.
OK, now we're ready to add a job to the jobs table and this is where our problems begins. All the other information enters in correctly, just the 2-3 letter postal code for the state gets screwed up. Here is the code to handle the state:
[PHP]<?php
$query = "SELECT postal,statename FROM states where postal = '{$_SESSION['stateid']}'";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$postal = stripslashes($row['postal']);
$statename = stripslashes($row['statename']);
echo "<option value'$postal' selected='selected'>$statename</option>";
}
$query = "SELECT postal,statename FROM states ORDER BY postal";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$postal = stripslashes($row['postal']);
$statename = stripslashes($row['statename']);
echo "
<option value='$postal'>$statename</option>
";
}
?>
</select>[/PHP]Here's the code that processes the form:
[PHP]<?php
if (isset($_POST['submitted']))
{
include('includes/mysql_connect.php');
$cat_id = mysql_real_escape_string($_POST['cat_id']);
$job_position = mysql_real_escape_string($_POST['job_position']);
$city = mysql_real_escape_string($_POST['city']);
$state_id = mysql_real_escape_string($_POST['state_id']);
$display_name = mysql_real_escape_string($_POST['display_name']);
$status = mysql_real_escape_string($_POST['status']);
$content = mysql_real_escape_string($_POST['content']);
$query = "INSERT INTO jobs VALUES ('', '".$_SESSION['user_id']."', '".$_SESSION['company_name']."', '".$display_name."', '".$cat_id."', '".$state_id."', '".$job_position."', '".$status."', '".$city."', '".$content."', now())";
$result = mysql_query($query) or die('Error, query failed mysql said <b>'.mysql_error().'</b>');
if ($result)
{
echo "<br><span style='color:red'><strong>Entry Added!</strong></span><br><br><a href='add_job.php'>Enter another Job Ad</a>";
}
else
{
echo "<br><span style='color:red'><strong>There was an error! The category was not created.</strong></span>";
}
include('bottom.php'); // Include the HTML footer.
exit();
mysql_close();
}
?>[/PHP]
The edit script uses this same state select code, and here is the update processing code :
[PHP]<?php
if ($_POST["submit"])
{
$job_position = mysql_real_escape_string($_POST['job_position']);
$city = mysql_real_escape_string($_POST['city']);
$display_name = mysql_real_escape_string($_POST['display_name']);
$cat_id = mysql_real_escape_string($_POST['cat_id']);
$state_id = mysql_real_escape_string($_POST['state_id']);
$status = mysql_real_escape_string($_POST['status']);
$content = mysql_real_escape_string($_POST['content']);
$sql = "UPDATE jobs SET display_name='$display_name', cat_id='$cat_id', state_id='$state_id', job_position='$job_position', status='$status', city='$city', content='$content' WHERE id=$id";
$result = mysql_query($sql);
if($result)
{
echo "<br><br><p align='center'><span style='color:red'>Job ad updated.</span><br><br><a href='edit_jobs.php'>Edit another job ad</a></p>";
}
else
{
echo "<br><br><p align='center'><span style='color:red'>There was an error! Job ad did not update.</p>";
}
}
}
?>[/PHP]
What it's doing is putting the full state name of the stateid session in the state_id row instead of the 2-3 letter abbreviation. As much of it as it can since it's varchar (4).
I'd really like to put this one to bed, so if anyone can see where I'm making my mistake, I'd appreciate you're pointing it out to me.
Thanks.
David