473,769 Members | 5,757 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Add and Update not working

155 New Member
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(tr im($_POST['state'])))) {
$s = escape_data($_P OST['state']);
} else {
$s = FALSE;
echo '<p><font color="red" size="+1">Pleas e 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,statenam e FROM states where postal = '{$_SESSION['stateid']}'";
$result = mysql_query($qu ery);
while($row = mysql_fetch_arr ay($result, MYSQL_ASSOC))
{
$postal = stripslashes($r ow['postal']);
$statename = stripslashes($r ow['statename']);

echo "<option value'$postal' selected='selec ted'>$statename </option>";
}

$query = "SELECT postal,statenam e FROM states ORDER BY postal";
$result = mysql_query($qu ery) or die(mysql_error ());

while($row = mysql_fetch_arr ay($result, MYSQL_ASSOC))
{
$postal = stripslashes($r ow['postal']);
$statename = stripslashes($r ow['statename']);

echo "
<option value='$postal' >$statename</option>
";
}
?>
</select>[/PHP]Here's the code that processes the form:
[PHP]<?php

if (isset($_POST['submitted']))
{

include('includ es/mysql_connect.p hp');

$cat_id = mysql_real_esca pe_string($_POS T['cat_id']);
$job_position = mysql_real_esca pe_string($_POS T['job_position']);
$city = mysql_real_esca pe_string($_POS T['city']);
$state_id = mysql_real_esca pe_string($_POS T['state_id']);
$display_name = mysql_real_esca pe_string($_POS T['display_name']);
$status = mysql_real_esca pe_string($_POS T['status']);
$content = mysql_real_esca pe_string($_POS T['content']);

$query = "INSERT INTO jobs VALUES ('', '".$_SESSION['user_id']."', '".$_SESSION['company_name']."', '".$display_nam e."', '".$cat_id." ', '".$state_id."' , '".$job_positio n."', '".$status." ', '".$city."', '".$content. "', now())";
$result = mysql_query($qu ery) or die('Error, query failed mysql said <b>'.mysql_erro r().'</b>');
if ($result)
{
echo "<br><span style='color:re d'><strong>Entr y Added!</strong></span><br><br><a href='add_job.p hp'>Enter another Job Ad</a>";
}
else
{
echo "<br><span style='color:re d'><strong>Ther e 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_esca pe_string($_POS T['job_position']);
$city = mysql_real_esca pe_string($_POS T['city']);
$display_name = mysql_real_esca pe_string($_POS T['display_name']);
$cat_id = mysql_real_esca pe_string($_POS T['cat_id']);
$state_id = mysql_real_esca pe_string($_POS T['state_id']);
$status = mysql_real_esca pe_string($_POS T['status']);
$content = mysql_real_esca pe_string($_POS T['content']);

$sql = "UPDATE jobs SET display_name='$ display_name', cat_id='$cat_id ', state_id='$stat e_id', job_position='$ job_position', status='$status ', city='$city', content='$conte nt' WHERE id=$id";
$result = mysql_query($sq l);
if($result)
{
echo "<br><br><p align='center'> <span style='color:re d'>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:re d'>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 1607
MarkoKlacar
296 Recognized Expert Contributor
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 New Member
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,statenam e FROM states where postal = '{$_SESSION['stateid']}'";
$result = mysql_query($qu ery);
while($row = mysql_fetch_arr ay($result, MYSQL_ASSOC))
{
$postal = stripslashes($r ow['postal']);
$statename = stripslashes($r ow['statename']);

echo "<option value'$postal' selected='selec ted'>$statename </option>";
}[/PHP]
Oct 10 '07 #3
MarkoKlacar
296 Recognized Expert Contributor
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
5029
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 cust_no, ded_type_cd, chk_no)
3
3451
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 the necessary records in it when testing it. I get the error "No value given for one or more required parameters." when I try to update the database. Can you tell me what am I doing wrong?
5
4174
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, but to edit a table row, I'd like to use Update. I have the following code in a class: Private Function SQL_CustomerUpdate()
8
2697
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. Example: I have a dataadapter that contains one table with one row. I change the value of the 'FisrtName' column in that row from Jack to John. I call ..update on the dataadapter it goes through fine. Now I change that same column in that same row...
2
3112
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 havnt tested the index yet ) so you can use an .UPDATE( dataTable ) on the data adapter. Otherwise you will get an exception error. Is this statement true? ---- Now me fumbling thru
2
11160
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 dataset to the project and drag a datatable from server explorer onto the dataset design surface. In the configuration wizard, under advanced options the "refresh data table" is disabled ie I am not able to check it (note if the source db is sql...
6
2660
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) { Int32 item_id = Convert.ToInt32(ViewState);
3
4806
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 narrowed the problem down to the department_id parameter which is set from a hidden field. I can confirm this by hard coding the UpdateCommand to: UPDATE configuration SET special_notes=@special_notes WHERE (student_id=@student_id) AND...
3
1795
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: connectionString = "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\progam files\mail.mdb"
0
9583
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10210
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9860
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8869
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7406
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6668
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5445
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3955
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2814
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.