473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 1591
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: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.