UPDATE command not working.
10-12-2007, 12:45 AM
|
UPDATE command not working.
|
Posts: 10
Name: Ken Van Someren
|
I am a newbie when it comes to PHP and MYSQL and have followed a tutorial to build some pages that will allow me to add, search, and display the results, from a table that I have created.
The problem I am having is that I have now modified the tutorial a bit, to accomodate what I am wanting to do regarding the UPDATE syntax.
I am pretty sure I am not doing this the most efficient way, but it was a way I could figure out how to do it.......atleast for now.
Scenario - I have a table with a list of all meetings held in a community dealing with a specific topic.
I am using a login script to restrict access to making the changes. The following is listed at the top of each of hte pages.
Code:
<?php
session_start();
require_once('myconnect.php');
include('functions.php');
include('settings.php');
checkLogin('1 2');
?>
Step 1 - View a list of all the meetings and note the CODE number listed (This is the auto increment, primary field in the table)
Step 2 - Enter the 2 or 3 digit code in the field available on Page 1. This is the code used for this portion
Code:
<form action="UpdateMeetings.php" method="post">
<div><H2><center>Meeting Code:<input type="text" size="3" maxlength="3" name="Code"></center></H2></div> <BR> <BR>
<div><center><input type="submit" name="Submit" value="Request Meeting Information"></center></div><BR>
</Form>
When you submit this code it goes to a page called UpdateMeetings.php, which lists all the details of the meeting you requested. This page allows you to make the required changes to the fields and then you press update.
Here is the code that I believe plays a part of this.
This first part is located in the <HEAD> portion of the page
Code:
<?php
$Code = $_POST['Code'];
//Updater.php
?>
Code:
<?
$query="SELECT * FROM Meetings WHERE Code='$Code'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$Program=mysql_result($result,$i,"Program");
$City=mysql_result($result,$i,"City");
$Day=mysql_result($result,$i,"Day");
$Time=mysql_result($result,$i,"Time");
$Address=mysql_result($result,$i,"Address");
$Building=mysql_result($result,$i,"Building");
$Type=mysql_result($result,$i,"Type");
$GRName=mysql_result($result,$i,"GRName");
$Notes=mysql_result($result,$i,"Notes");
$Other=mysql_result($result,$i,"Other");
?>
<Center>
<form action="updated.php">
<input type="hidden" name="Code" value="<? echo "$Code"; ?>"><br><br>
Program: <input type="text" name="Program" value="<? echo "$Program"?>"><br><br>
City: <input type="text" name="City" value="<? echo "$City"?>"><br><br>
Day of Meeting: <input type="text" name="Day" value="<? echo "$Day"?>"><br><br>
Meeting Time: <input type="text" name="Time" value="<? echo "$Time"?>"><br><br>
Address: <input type="text" name="Address" value="<? echo "$Address"?>"><br><br>
Building Name: <input type="text" name="Building" value="<? echo "$Building"?>"><br><br>
Meeting Type: <input type="text" name="Type" value="<? echo "$Type"?>"><br><br>
Group Name: <input type="text" name="GRName" value="<? echo "$GRName"?>"><br><br>
Notes: <input type="text" name="Notes" value="<? echo "$Notes"?>"><br><br>
Other: <input type="text" name="Other" value="<? echo "$Other"?>"><br><br>
<input type="Submit" value="Update Meeting"><br><br>
</form>
</center>
<?
++$i;
}
?>
When you press the submit button, it sends it to the third and final page, which is suppose to update the DB. I get the success message, but the DB itself is not being updated.
Here is the final portion of the script
Code:
<?php
//$Code = $_POST['Code'];
//Updater.php
?>
</head>
<body>
<?php
$Code=$_POST['Code'];
$Program=$_POST['Program'];
$City=$_POST['City'];
$Day=$_POST['Day'];
$Time=$_POST['Time'];
$Address=$_POST['Address'];
$Building=$_POST['Building'];
$Type=$_POST['Type'];
$GRName=$_POST['GRName'];
$Notes=$_POST['Notes'];
$Other=$_POST['Other'];
?>
<?
$query="UPDATE Meetings SET Program='$ud_Program', City='$ud_City', Day='$ud_Day', Time='$ud_Time', Address='$ud_Address', Building='$ud_Building', Type='$ud_Type', GRName='$ud_GRName', Notes='$ud_Notes', Other='$ud_Other' WHERE Code='$Code'";
Echo "Congratulations!! <BR>Your meeting has been successfully modified and the new information will now appear in the listing.";
echo "<meta http-equiv=Refresh content=6;url=members.php>";
mysql_close();
?>
Any help in figuring out why it doesn't actually update the DB would be appreciated. I have built a similar page to delete entries from the DB. It is built basically the same, using the DELETE syntax and I have the same problem, where it won't actually delete the entry, although I get a success message, so I am thinking it is the same problem for both of them.
Thanks in advance,
|
|
|
|
10-12-2007, 04:33 AM
|
Re: UPDATE command not working.
|
Posts: 42,385
Name: Chris Hirst
Location: Blackpool. UK
|
echo out the SQL command before you run it so you can see where the error is likely to be.
(PHP problem most likely so moving this thread)
__________________
Chris. ->> Please login or register to view this content. Registration is FREE <<-
A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
|
|
|
|
10-12-2007, 06:50 AM
|
Re: UPDATE command not working.
|
Posts: 10
Name: Ken Van Someren
|
Thank You for the comments thus far. I was missing the mysql_query() and have updated the updated.php form to include it and it now shows as follows:
Code:
$query="UPDATE Meetings SET Program='$Program', City='$City', Day='$Day', Time='$Time', Address='$Address', Building='$Building', Type='$Type', GRName='$GRName', Notes='$Notes', Other='$Other' WHERE Code='$Code'";
mysql_query($query) or die(mysql_error());
Echo "Congratulations!! <BR>Your meeting has been successfully modified and the new information will now appear in the listing.";
//echo "<meta http-equiv=Refresh content=6;url=members.php>";
mysql_close();
?>
I don't get any errors, but I do get a list of warnings, all pertaining to the following lines of code:
The warning is
Quote:
Notice: Undefined index: Code in directory/location/public_html/updated.php on line 35
It lists this for each of the following
|
Code:
<?php
$Code=$_POST['Code'];
$Program=$_POST['Program'];
$City=$_POST['City'];
$Day=$_POST['Day'];
$Time=$_POST['Time'];
$Address=$_POST['Address'];
$Building=$_POST['Building'];
$Type=$_POST['Type'];
$GRName=$_POST['GRName'];
$Notes=$_POST['Notes'];
$Other=$_POST['Other'];
?>
I am not understanding what it is looking for here. Sorry for the lack of knowledge, but I am a newbie to all of this.
|
|
|
|
10-12-2007, 08:27 AM
|
Re: UPDATE command not working.
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
A notice is a warning that does not block your script execution.
In your case, it tells you that he didn't found an index named "Code" in the $_POST array. Thus, it have no value.
__________________
Only a biker knows why a dog sticks his head out the window.
|
|
|
|
10-12-2007, 11:31 AM
|
Re: UPDATE command not working.
|
Posts: 10
Name: Ken Van Someren
|
So this is a warning, not an error, but for some reason, the UPDATE is still not working. Is it related to the warning(s) that I am getting?
|
|
|
|
10-12-2007, 11:49 AM
|
Re: UPDATE command not working.
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
yes, the warning says "there is no 'code' variable in the post", so if you base your query on those, it will be malformed, for sure.
First, try to correct your form, and then work on your query.
BTW, you can dump the content of your POST easily with this little snippet:
PHP Code:
<?php
echo "<pre>".print_r($_POST,1)."</pre>"; ?>
It will show you everything that is contained into your $_POST variable.
__________________
Only a biker knows why a dog sticks his head out the window.
|
|
|
|
10-13-2007, 12:52 PM
|
Re: UPDATE command not working.
|
Posts: 10
Name: Ken Van Someren
|
I have been struggling with this code for the last day, and am not making any progress, so I am back to hopefully get some more help. I am still getting the "Undefined Variable" warning, and am not sure what is missing, or where it is missing, that this is happening.
It is giving me an undefined variable for this line, which is being carried forward on from page 1
Code:
<?php
$ID = $_POST['Contact'];
//Updater.php
?>
I have changed the code a bit (mostly the words, etc) so I will repost it, in case the changes made will make a difference.
This script is broken out over 3 pages, and I am wondering if somehow I am not carrying the information from the first page to the second page and then to the third page. From what I have been able to understand (remembering I am a real newbie here), if in page 1 (Updater.php) in the form, I name the user input field "contact", then I should be able to carry that variable to the second page by using the command listed above.
It is my understanding that if I now use the $ID on the second page, it will input the data that was found in the form on page 1 (Contact)
I am also getting the "Undefined Variable" error for each of the lines in the form located on page 2 (UpdateMeetings.php), yet I am thinking that I have defined them with the following lines, which are found on this page.
Code:
$Code=mysql_result($result,$i,"Code");
$Program=mysql_result($result,$i,"Program");
$City=mysql_result($result,$i,"City");
$Day=mysql_result($result,$i,"Day");
$Time=mysql_result($result,$i,"Time");
$Address=mysql_result($result,$i,"Address");
$Building=mysql_result($result,$i,"Building");
$Type=mysql_result($result,$i,"Type");
$GRName=mysql_result($result,$i,"GRName");
$Notes=mysql_result($result,$i,"Notes");
$Other=mysql_result($result,$i,"Other");
As a result of these warnings, I am also not getting my DB updated when it runs the UPDATE command.
Listed below is the complete script(s) I am using, and hopefully someone can help me figure out where I have gone wrong...........
Page 1 (Updater.php)
Code:
<?php
session_start();
require_once('myconnect.php');
include('functions.php');
include('settings.php');
checkLogin('1 2');
<form action="UpdateMeetings.php">
<div><H2><center>Meeting Code:<input type="text" size="3" maxlength="3" name="Contact"></center></H2></div> <BR> <BR>
<div><center><input type="submit" name="Submit" value="Request Meeting Information"></center></div><BR>
</Form>
?>
Page 2 (UpdateMeetings.php)
Code:
<?php error_reporting(E_ALL); ?>
<?php
session_start();
require_once('myconnect.php');
include('functions.php');
include('settings.php');
checkLogin('1 2');
?>
<?php
$ID = $_POST['Contact'];
//Updater.php
?>
<?php
$query="SELECT * FROM Meetings WHERE Code='$ID'";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$Code=mysql_result($result,$i,"Code");
$Program=mysql_result($result,$i,"Program");
$City=mysql_result($result,$i,"City");
$Day=mysql_result($result,$i,"Day");
$Time=mysql_result($result,$i,"Time");
$Address=mysql_result($result,$i,"Address");
$Building=mysql_result($result,$i,"Building");
$Type=mysql_result($result,$i,"Type");
$GRName=mysql_result($result,$i,"GRName");
$Notes=mysql_result($result,$i,"Notes");
$Other=mysql_result($result,$i,"Other");
}
?>
<Center>
<form action="updated.php" method="post">
<input type="hidden" name="Code" value="<?php echo $_POST['Contact'];?>"><br><br>
Program: <input type="text" name="Program" value="<?php echo "$Program"?>"><br><br>
City: <input type="text" name="City" value="<?php echo "$City"?>"><br><br>
Day of Meeting: <input type="text" name="Day" value="<?php echo "$Day"?>"><br><br>
Meeting Time: <input type="text" name="Time" value="<?php echo "$Time"?>"><br><br>
Address: <input type="text" name="Address" value="<?php echo "$Address"?>"><br><br>
Building Name: <input type="text" name="Building" value="<?php echo "$Building"?>"><br><br>
Meeting Type: <input type="text" name="Type" value="<?php echo "$Type"?>"><br><br>
Group Name: <input type="text" name="GRName" value="<?php echo "$GRName"?>"><br><br>
Notes: <input type="text" name="Notes" value="<?php echo "$Notes"?>"><br><br>
Other: <input type="text" name="Other" value="<?php echo "$Other"?>"><br><br>
<input type="Submit" name="Submit" value="Update Meeting"><br><br>
</form>
</center>
<?php
++$i;
?>
Page 3 (updated.php)
Code:
<?php error_reporting(E_ALL); ?>
<?php
session_start();
require_once('myconnect.php');
include('functions.php');
include('settings.php');
checkLogin('1 2');
?>
<?php
$Code=$_POST['Code'];
$Program=$_POST['Program'];
$City=$_POST['City'];
$Day=$_POST['Day'];
$Time=$_POST['Time'];
$Address=$_POST['Address'];
$Building=$_POST['Building'];
$Type=$_POST['Type'];
$GRName=$_POST['GRName'];
$Notes=$_POST['Notes'];
$Other=$_POST['Other'];
?>
<?php
$query="UPDATE 'Meetings' SET Program='$Program', City='$City', Day='$Day', Time='$Time', Address='$Address', Building='$Building', Type='$Type', GRName='$GRName', Notes='$Notes', Other='$Other' WHERE Code='$Code'";
mysql_query($query) or die(mysql_error());
Echo "Congratulations!! <BR>Your meeting has been successfully modified and the new information will now appear in the listing.";
//echo "<meta http-equiv=Refresh content=6;url=members.php>";
mysql_close();
?>
|
|
|
|
10-13-2007, 01:29 PM
|
Re: UPDATE command not working.
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
I just noticed that you don't specify your form method.
I believe it's a GET request that's used by default.
Try to add the method in your form:
HTML Code:
<form action="UpdateMeetings.php" method="post">
This should let you get your form value in the $_POST variable.
__________________
Only a biker knows why a dog sticks his head out the window.
|
|
|
|
10-13-2007, 02:33 PM
|
Re: UPDATE command not working.
|
Posts: 10
Name: Ken Van Someren
|
I didn't realize that GET was the default method. That is good to know.
I made the change and now for some reason, when I run the script where I enter the DB auto increment number (Contact) and the page attempts to bring up the existing data, it is timing out. If I remove the method="post" command in the form from the first page, it works again.
I am wondering if I have mixed up some POST and GET commands, although when I looked through it, I don't see where I have done this.
Can you confirm that my thoughts that the following command should work for getting the data to follow from one page to the next.
<?php
$Code = $_POST['Code'];
//Updater.php
?>
Back to the drawing board. It is getting frustrating enough that it is almost time to delete the files and start again........ or just give up.... I am getting frustrated and confused, but I guess this is to be expected when trying to learn a new language. Thanks again for the help
|
|
|
|
10-13-2007, 04:27 PM
|
Re: UPDATE command not working.
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
Quote:
|
Can you confirm that my thoughts that the following command should work for getting the data to follow from one page to the next.
|
Not exactly...
This will get the value that was present in the form, but only on the page declared as the "action" from the form.
If you want to carry it further, you either need to store it in your session ($_SESSION), or to pass it through a GET call ( myNewPage.php?code=$CODE&some=$some&thing=$thing).
Otherwise, you won't see anything between your flow.
Basically:
A form send datas from 1 page to another (it may be itself. I often made forms post to themselves, and processing the result inthe same page)
If you want to carry datas around many pages, store them in the session array.
If you want to pass datas from 1 page to only 1 other and you don't have to pass arrays or objects, pass the values via GET calls from 1 page to another.
__________________
Only a biker knows why a dog sticks his head out the window.
|
|
|
|
10-13-2007, 05:46 PM
|
Re: UPDATE command not working.
|
Posts: 10
Name: Ken Van Someren
|
This is starting to make more sense.
Is it possible to switch between POST and GET for different pages. By this I mean, can I use POST to go from Page 1 to Page 2 and then use GET when I need to move the info from Page 2 to Page 3?
In this particular script, I am starting to think I may be better to change it around and shorten it to only 2 pages. I think I should be able to combine pages 2 and 3.
Any ideas as to what the problem would be that Page 2 won't load once I use the method="Post" in the form?
|
|
|
|
10-13-2007, 06:35 PM
|
Re: UPDATE command not working.
|
Posts: 3,621
Name: Thierry
Location: I'm the uber Spaminator !
|
Hmmmm, is the code you posted the real actual code ?
Because, if it is, you included the HTML form code into a PHP tag. But PHP should have issued an error, so I assume it's not the case.
Code:
<?php
session_start();
require_once('myconnect.php');
include('functions.php');
include('settings.php');
checkLogin('1 2');
?>
<form action="UpdateMeetings.php" method="post">
<div><H2><center>Meeting Code:<input type="text" size="3" maxlength="3" name="Contact"></center></H2></div> <BR> <BR>
<div><center><input type="submit" name="Submit" value="Request Meeting Information"></center></div><BR>
</form>
Frankly, no. I don't see why it would not work by adding the method="post" to your form definition.
__________________
Only a biker knows why a dog sticks his head out the window.
|
|
|
|
10-14-2007, 02:30 AM
|
Re: UPDATE command not working.
|
Posts: 10
Name: Ken Van Someren
|
I have not been able to determine the problem with this, so I have scrapped it, started over and rewrote it to only use 2 pages. I appear to be closer, but am still having a couple of problems. I will try figuring it out and if I am still stuck, I will seek some help with the new format.
I don't think the HTML Form code is inside the PHP tags as there is an end the PHP tag just above the form action.
Thanks for your time in trying to help me out on this, it is appreciated.
|
|
|
|
|
« Reply to UPDATE command not working.
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|