468,457 Members | 1,798 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

Add and Update not working

155 100+
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
Oct 10 '07 #1
3 1372
MarkoKlacar
296 Expert 100+
Hi David,

in the first php wrapped code I don't see you starting you session.
I thnk you need to add
[PHP]session_start();[/PHP]
This needs to be done before you actually start reading from the session.
The same thing goes for when you're putting things in to the session, it has to be started.

Now you have to consider why you are using sessions in the first place. I think the most common use of sessions is to store login information etc, I think you can go with POST or GET.

Let me know if this helped.

Cheers.
Oct 10 '07 #2
DavidPr
155 100+
Yes, the add_job.php and edit_job.php pages can only be accessed by logging in, which starts the session. And the session start() is on top of every page.

The session is started on the login.php page:
[PHP]$_SESSION['first_name'] = $row[3];
$_SESSION['user_id'] = $row[0];
$_SESSION['last_name'] = $row[4];
$_SESSION['company_name'] = $row[5];
$_SESSION['city'] = $row[7];
$_SESSION['stateid'] = $row[8];[/PHP]

When I'm adding a new job or editing a job I can select a different state in the drop-down menu other than the one that is selected and the abbreviation code for that state is entered into or updated in the database just fine. Only when you select the state that is pre-selected does is try to put the entire state name in instead of the abbreviation.

So the problem has something to do with this portion of the State Field in the forms:
[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>";
}[/PHP]
Oct 10 '07 #3
MarkoKlacar
296 Expert 100+
Hi David,

I would considering trying the following:

(Row 1)
Be sure $_SESSION['stateid'] contains the correct information.
Try setting the stateid like this
[PHP]$_SESSION['stateid'] = $row['statename or what ever'][/PHP]

Do not mix index numbers with 'text' when you're getting information from an array.

Try this, let me know if I'm way off or if I understood the question correctly.

Cheers
Oct 15 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

17 posts views Thread by kalamos | last post: by
5 posts views Thread by HydroSan | last post: by
8 posts views Thread by Zorpiedoman | last post: by
6 posts views Thread by Nuzzi | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.