472,982 Members | 2,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,982 software developers and data experts.

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 1568
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

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

Similar topics

17
by: kalamos | last post by:
This statement fails update ded_temp a set a.balance = (select sum(b.ln_amt) from ded_temp b where a.cust_no = b.cust_no and a.ded_type_cd = b.ded_type_cd and a.chk_no = b.chk_no group by...
3
by: Shapper | last post by:
Hello, I have created 3 functions to insert, update and delete an Access database record. The Insert and the Delete code are working fine. The update is not. I checked and my database has all...
5
by: HydroSan | last post by:
Having a bit of a problem getting UPDATE working. The project in question is a simple MySQL VB.NET frontend, allowing Insertion, Selection, and others. Well, I've gotten Drop and Insert working,...
8
by: Zorpiedoman | last post by:
I keep getting a concurrency exception the second time I make a change and attempt to update a dataadapter. It appears this is by design, so there must be something I can do to avoid it. ...
2
by: Miro | last post by:
I will ask the question first then fumble thru trying to explain myself so i dont waste too much of your time. Question / Statement - Every mdb table needs a PrimaryKey ( or maybe an index - i...
2
by: travhale | last post by:
in a new project using .net 2005, c#. getting err message "Update requires a valid UpdateCommand when passed DataRow collection with modified rows." source RDBMS is oracle 8i. I add a new...
6
by: Nuzzi | last post by:
Hello All, I have two pages that are very similar. One is working, one is not. Here is the code for both: Page 1 (Working): protected void btn_update_Click(object sender, EventArgs e)...
3
by: Brad Baker | last post by:
I have a formview with a datasource that contains a select and update command. The select statement works fine but the update command doesn't seem to be working. After some troubleshooting I have...
3
by: Dilruba | last post by:
asp, vbscript, Ms Access I am using vbscript to insert & update ms accees. Insert operation is working , but update operation is not working. I have used the following codes: ...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.