PDA

View Full Version : PHP database update question




barr08
Jul 3, 2007, 11:33 AM
Hi, I am working on a project for a MySQL class, and I am creating a sample database in which I need to create a table, add info to this table (name, email, etc.), view the table, and update fields that already exist on the table, using PHP. I have the first three working, but I am having trouble updating fields. I want to be able to enter a name in an html form, and enter information in text boxes to replace the existing information in the database.

My files are located here:
http://nemo.mwd.hartford.edu/~bbarr/IIT310/Final/FinalPHPDB/

This one creates the field:
http://nemo.mwd.hartford.edu/~bbarr/IIT310/Final/FinalPHPDB/dataEntry.html

This views the info:
http://nemo.mwd.hartford.edu/~bbarr/IIT310/Final/FinalPHPDB/retrieveDisplayAll.php

I want to be able to edit the four fields. Any help or input would be great. I may have explained this in a confusing way, so I'll try and clarify if you have any questions. I am not much of a programmer, I focus on the design aspect of my web design major, but I figured this stuff would be good to have at least a working knowledge of.

Thanks for looking!



antibact1
Jul 3, 2007, 12:26 PM
Assuming that you just want to have them enter a name and details, if the name does not exist then add all details, otherwise, find the name and replace the details.

$query = "select * from table where name = $name";
$result = mysql_query($query);

if (mysql_num_rows($result) == 0)
{
/* New add to the system */
$insert_query = "insert into ....";
}
else
{
/* Update */
$row = mysql_fetch_assoc($result);

/* Get the old ID */
$id = $row["id"];

/* Create the update query */
$update_query = "update table set ...... where id = $id";
}

This also assumes the name is unique. Otherwise, you will potentially have multiple rows to update.

barr08
Jul 3, 2007, 12:59 PM
Thanks, i'll try this out, it might be easier to edit the fields based on a unique account number (000x) i have asigned to each account, that way I know I am editing a unique field.

antibact1
Jul 3, 2007, 01:10 PM
Thanks, i'll try this out, it might be easier to edit the fields based on a unique account number (000x) i have asigned to each account, that way I know I am editing a unique field.

If you want to do it that way, in your item listing page, add an edit link that supplies the item ID through GET. Then, when you are displaying the item to the user in a form allowing them to edit the details, add a hidden field containing the id so you will have it in POST.

A link will be in the form http://site.com/edit_item&id=5

if (isset($_GET["id"]))
{
/* Retrieve the item from the database and prepopulate the form with the existing values */
$id = $_GET["id"];
$query = "select * from table where id = $id";

$row = mysql_fetch_assoc($row);

/* Display the form with the hidden ID field */

}
else if (isset($_POST["id"]))
{

/* Retrieve the new details from the form and update the database entry */
$id = $_POST["id"];

$query = "update table .... where id = $id";

}

Note that this contains no error checking (ie. an invalid ID can be supplied). Also, it may be useful to check if the values being updated are in fact changed.

barr08
Jul 5, 2007, 05:09 PM
OK I have tried the first solution posted by antibact1, and I think I am doing something wrong. I have it up at: http://nemo.mwd.hartford.edu/~bbarr/IIT310/Final/FinalPHPDB/
under updateData.php. My code is basically what antibact1 gave me.


$hookup=mysql_connect($server, $user, $pass);

$dbNow=@mysql_select_db($currentDB, $hookup);

$query = "select * from $currentTable where uname = $uname";
$result = mysql_query($query);

if (mysql_num_rows($result) == 0)
{

/* New add to the system */
$insert_query = "insert into $currentTable";

}
else
{

/* Update */
$row = mysql_fetch_assoc($result);

/* Get the old ID */
$id = $row["id"];

/* Create the update query */
$update_query = "update $currentTable set ...... where id = $id";

}

Any ideas? I know the "....." right above has to be something, but I am not sure what. I am getting an error on the 15th line, "
if (mysql_num_rows($result) == 0)"

Thanks

antibact1
Jul 5, 2007, 06:03 PM
Sorry the code I gave was just a template.

There are several other things you do. Here would be a full example with no error checking.

$hookup=mysql_connect($server, $user, $pass);

$dbNow=@mysql_select_db($currentDB, $hookup);

/* Pull all of the form variables and set them */
$uname = $_POST["uname"];
$email = $_POST["email"];
$acnt = $_POST["acnt"];
$cost = $_POST["cost"];

$query = "select * from $currentTable where uname = $uname";
$result = mysql_query($query);

if (mysql_num_rows($result) == 0)
{
/* New add to the system */
$insert_query = "insert into $currentTable (uname, email, acnt, cost) values ($uname, $email, $acnt, $cost)";

$result = mysql_query($insert_query);

}
else
{
/* Update */
$row = mysql_fetch_assoc($result);

/* Get the old ID */
$id = $row["id"];

/* Create the update query */
$update_query = "update $currentTable set uname = $uname, email = $email, acnt = $acnt, cost = $cost where id = $id";

$result = mysql_query($update_query);

}

Note that none of the queries escape any special characters that may mess up your query. It also assumes that each of the parameters has been set. Ideally, you would check each of the parameters to determine that they have been set, and construct your queries that way.

If you need more help, just let me know.

barr08
Jul 5, 2007, 07:06 PM
Oh it was a template. I feel so stupid...

I'm going to give this a shot, thanks for your help.

barr08
Jul 5, 2007, 07:23 PM
OK so I am still getting an error on the rows line. The error is:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/MWD/bbarr/www/IIT310/Final/FinalPHPDB/updateData.php on line 21

I recognize this error from other programs, i think it is pretty broad. Do you have any idea what it could mean in this code?

antibact1
Jul 5, 2007, 07:54 PM
No worries.

That error means the query is not executing successfully. So when you pass the result resource to mysql_num_rows, it is not a valid result. Is the variable $currentTable defined prior to executing any of that code. Another suggestion is to execute

echo $query;

after the query to take a look at the query. Also, you may want to do a

echo mysql_error();

to get a better idea of what the error is.

macfaninpdx
Jul 6, 2007, 09:41 AM
You are referencing a variable, $currentTable, that is not defined anywhere. So PHP will use an empty value in it's place, making the $query variable look something like:
"select * from where uname = <some_user>"

If the mysql table you are selecting from will not change, you can safely replace the $currentTable variable with the name of the table. If it needs to be dynamic, then make sure to set the $currentTable variable prior to using it in the query statement.

In the case where the table name won't change, the line setting the query variable would look like this:
$query = "select * from <table_name> where uname = '$uname'";
And you would of course replace <table_name> above with the actual name of your table. Make sense?

Oh, and one more potential gotcha. You will need to add single-quotes around each variable where text will be used in the query. So for example $insert_query = "insert into <table_name> (uname, email, acnt, cost) values ('$uname', '$email', '$acnt', '$cost')"; and $update_query = "update <table_name> set uname = '$uname', email = '$email', acnt = '$acnt', cost = '$cost' where id = '$id'";
Technically the $id above deosn't need to be wrapped in single-quotes, but it won't hurt. I hope this helps. Let us know if you have any more questions.

barr08
Jul 9, 2007, 09:28 AM
You are referencing a variable, $currentTable, that is not defined anywhere. So PHP will use an empty value in it's place, making the $query variable look something like:
"select * from where uname = <some_user>"

If the mysql table you are selecting from will not change, you can safely replace the $currentTable variable with the name of the table. If it needs to be dynamic, then make sure to set the $currentTable variable prior to using it in the query statement.

In the case where the table name won't change, the line setting the query variable would look like this:
$query = "select * from <table_name> where uname = '$uname'";
And you would of course replace <table_name> above with the actual name of your table. Make sense?



I have $currentTable defined in the program, it is above the section of the code that I put in this thread. Is this something I still need to do? I am still getting the error on the mysql_num_rows() line.

macfaninpdx
Jul 9, 2007, 03:22 PM
I am still getting the error on the mysql_num_rows() line.
Put the single-quotes around the $uname variabe, as mentioned above. And you might as well put the single-quotes around the rest of the variables in your mysql queries, because they will give you errors as well.

barr08
Jul 9, 2007, 04:27 PM
Put the single-quotes around the $uname variabe, as mentioned above. And you might as well put the single-quotes around the rest of the variables in your mysql queries, because they will give you errors as well.

OK so I did this and it is still happening :mad: ! Any other ideas? My code now looks like this:


<?php
$server="localhost";
$user="bbarr";
$pass="******";
$currentDB="bbarr";
$currentTable="boyfight3";

$hookup=mysql_connect($server, $user, $pass);

$dbNow=@mysql_select_db($currentDB, $hookup);

/* Pull all of the form variables and set them */
$uname = $_POST["uname"];
$email = $_POST["email"];
$acnt = $_POST["acnt"];
$cost = $_POST["cost"];

$query = "select * from $currentTable where uname = '$uname'";
$result = mysql_query($query);

if (mysql_num_rows($result) == 0)
{

/* New add to the system */
$insert_query = "insert into $currentTable (uname, email, acnt, cost) values ('$uname', '$email', '$acnt', '$cost')";

$result = mysql_query($insert_query);

}
else
{

/* Update */
$row = mysql_fetch_assoc($result);

/* Get the old ID */
$id = $row["id"];

/* Create the update query */
$update_query = "update $currentTable set uname = '$uname', email = '$email', acnt = '$acnt', cost = '$cost' where id = '$id'";

$result = mysql_query($update_query);

}


?>

macfaninpdx
Jul 9, 2007, 04:35 PM
OK so I did this and it is still happening :mad: ! Any other ideas? My code now looks like this:

First of all, it would be best if you remove your user name and password from your code above. ;) Second, add the following line right in between the $query= and the $result= lines:
echo $query;
And then add the following after the $result= line:
echo mysql_error();
Let us know what that displays.

barr08
Jul 9, 2007, 04:38 PM
Haha shoot I forgot about the PW. I am so frazzled this is due very soon.

I added the echos, and I got:

select * from boyfight3 where uname = ''select * from boyfight3 where uname = ''
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/MWD/bbarr/www/IIT310/Final/FinalPHPDB/updateData.php on line 23

boyfight3 is the name of my table

macfaninpdx
Jul 9, 2007, 04:50 PM
I added the echos, and I got:

select * from boyfight3 where uname = ''select * from boyfight3 where uname = ''

This confuses me. According to this, the variable $uname is set to "select * from boyfight3 where uname=" which may be your problem. to debug, let's add this to the first line of your code (after the <?php):
print_r($_POST);
exit();In order to view the formatted results, view the source of the returned web page and copy the results there.

barr08
Jul 9, 2007, 04:52 PM
The result is:

Array
(
)

?? lol

thanks for all your help by the way, very clutch!

macfaninpdx
Jul 9, 2007, 04:59 PM
OK, I understand the previous results now, too. Your $_POST is not getting populated by the referring page. Are you trying to load this page directly, or by posting a form from a referring page? Because only the latter will give you results that work. Otherwise, as you are seeing now, the $_POST variable (which is the global variable that holds all form POST information) is not getting populated.

To test the pade by loading directly (instead of using a form submit), enter something like this into the URL:
http://localhost/yourpage.php?uname=somename&email=someemail&acnt=someacnt&cost=somecost(blue text is the part to add)

In other words, you are injecting the variables manually into the URL since they are not being passed as POST variables. Also, remove the print_r and exit() lines previously added.

barr08
Jul 9, 2007, 05:06 PM
OK I removed the two most recent lines, and added the blue text. I am still getting the same error:

select * from boyfight3 where uname = ''select * from boyfight3 where uname = ''

this is the URL I used:

http://nemo.mwd.hartford.edu/~bbarr/IIT310/Final/FinalPHPDB/updateData.php?uname=somename&email=someemail&acnt=cnt&cost=somecost

I have an HTML form for the page, but that returns the same error. I am also not sure if that form is correct.

macfaninpdx
Jul 9, 2007, 05:19 PM
Let's try adding the print_r($_POST) back in, with the updated URL.

barr08
Jul 9, 2007, 05:22 PM
Same error.

Array()

RupertJ
Jul 9, 2007, 05:22 PM
If you pass the variables in the URL like that, they end up in $_GET, not $_POST.

You can use a function like:

function getParam($name) {
if(isset($_GET[$name])) return $_GET[$name];
if(isset($_POST[$name])) return $_POST[$name];
return false;
}

If you want to switch between the two.

macfaninpdx
Jul 9, 2007, 05:23 PM
Try changing it to print_r($_REQUEST)

barr08
Jul 9, 2007, 05:26 PM
After changing it to REQUEST, I got this:


Array
(
[uname] => somename
[email] => someemail
[acnt] => someacnt
[cost] => somecost
[__utmz] => 126326804.1183574340.1.1.utmccn=(referral)|utmcsr=hartford.facebook.com|utmcct=/profile.php|utmcmd=referral
[__utma] => 126326804.1276275613.1183574340.1183574340.1184015861.2
[WRUID] => 0
[__utmc] => 126326804
)

macfaninpdx
Jul 9, 2007, 05:27 PM
If you pass the variables in the URL like that, they end up in $_GET, not $_POST.
Right, I forgot. Thanks. They will still show in $_REQUEST though.

macfaninpdx
Jul 9, 2007, 05:29 PM
After changing it to REQUEST, I got this:

OK, so now that we can inject the variable values, remove the print_r line, and use the long URL to manually enter the variables you want to update in your table. You will need to change the beginning lines of your code from $_POST["uname"] = to $_REQUEST["uname"] =...

barr08
Jul 9, 2007, 05:44 PM
So...

$uname = $_POST["uname"];
$email = $_POST["email"];
$acnt = $_POST["acnt"];
$cost = $_POST["cost"];

Change all the posts to REQUEST?

barr08
Jul 9, 2007, 05:48 PM
I changed that to this:

$uname = $_REQUEST["uname"];
$email = $_REQUEST["email"];
$acnt = $_REQUEST["acnt"];
$cost = $_REQUEST["cost"];

and the url I used to test is this:
http://nemo.mwd.hartford.edu/~bbarr/IIT310/Final/FinalPHPDB/updateData.php?uname=BenCo&email=barr08@gmail.com&acnt=0001&cost=2000

and I got the same error.

macfaninpdx
Jul 9, 2007, 05:51 PM
Change all the posts to REQUEST?
For the purpose of debugging your code, yes. But now that I think about it, maybe we should back up a step. You first started talking about the "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource" warning, and no you know that this is because it was not getting the variable for $uname.

So at this point, it probably makes most sense to try accessing this page from the form originally designed to make the updates, rather than use the url manually. Does that work? If not, what is the code from the referring page?

macfaninpdx
Jul 9, 2007, 05:53 PM
heh, looking back at the code, maybe you should try removing the @ from $dbNow=@mysql_select_db($currentDB, $hookup);

barr08
Jul 9, 2007, 05:55 PM
For the purpose of debugging your code, yes. But now that I think about it, maybe we should back up a step. You first started talking about the "Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource" warning, and no you know that this is because it was not getting the variable for $uname.

So at this point, it probably makes most sense to try accessing this page from the form originally designed to make the updates, rather than use the url manually. Does that work? If not, what is the code from the referring page?

OK good call. I changed the REQUESTS back to POST, and this is my code. As you can plainly see, this stuff isn't exactly my strongest skill, so be gentle...

http://nemo.mwd.hartford.edu/~bbarr/IIT310/Final/FinalPHPDB/dataUpdate.html


<form action="updateData.php" method="post">
Company Name <br/>
<input type="text" name="uname" />
<br />
Contact Email <br />
<input type="text" name="email" />
<br />
Account Number <br/>
<input type="text" name="acnt" />
<br/>
Site Cost <br/>
<input type="text" name="cost" />
<br/>
<input type="submit" value="Send" />

</form>


Still this error:
select * from boyfight3 where uname = 'BenCo'select * from boyfight3 where uname = 'BenCo'

barr08
Jul 9, 2007, 05:58 PM
heh, looking back at the code, maybe you should try removing the @ from $dbNow=@mysql_select_db($currentDB, $hookup);

I removed it, same error.

macfaninpdx
Jul 9, 2007, 06:05 PM
I removed it, same error.
K, the problem is that your code is not connecting to your DB. Try changing your code to:
$hookup=mysql_connect($server, $user, $pass);
if (!$hookup) {
die("Connection failed: " . mysql_error());
}
$dbNow=mysql_select_db($currentDB, $hookup);
if (!$dbNow) {
die("Unable to select database: " . mysql_error());
}
And see what this returns.

BTW - I gotta go for now. I will try to check in later tonight. GL

barr08
Jul 9, 2007, 06:07 PM
I'll try that. Thanks a lot. I will leave my result here and you can take a look whenever you get back.

barr08
Jul 9, 2007, 06:08 PM
Same error :(

Is the HTML form correct? I just kinda whipped it up without thinking.

macfaninpdx
Jul 9, 2007, 10:22 PM
The form looks fine. I am beginning to wonder if you have SAFE mode turned on in your php.ini file? Enter this into a new file called test.php:
<?php
phpinfo();
?>
and load it from the browser. Look for the line that says SAFE_MODE.

barr08
Jul 10, 2007, 06:41 AM
Safe mode looks like it is turned off. You can view the test file here:

http://nemo.mwd.hartford.edu/~bbarr/IIT310/Final/FinalPHPDB/test.php

I have work until 5, so I won't be able to do anything, but any ideas you might have in the meantime would be fantastic. I will be picking up on this project again at 5.

Thanks

macfaninpdx
Jul 10, 2007, 10:02 AM
OK, it's a new day - we can figure this thing out. In fact, I had a thought. Does the table "boyfight3' already exist? If not, you will need to create it first, before you can select anything from it.

barr08
Jul 10, 2007, 12:32 PM
the table does indeed exist.

macfaninpdx
Jul 10, 2007, 03:04 PM
This is really beginning to perplex me. :confused: Can you try to issue the SQL query in whatever program you used to set up your table in the first place? What is the result? You should copy the SQL statement directly from your code (substituting real values for variables of course) to see exactly what will result.

barr08
Jul 10, 2007, 03:07 PM
This is really beginning to perplex me. :confused: Can you try to issue the SQL query in whatever program you used to set up your table in the first place? What is the result? You should copy the SQL statement directly from your code (substituting real values for variables of course) to see exactly what will result.

I am not sure quite what you mean. I think one problem may be that I don't think I have a table. My database can be viewed at this page:

http://nemo.mwd.hartford.edu/~bbarr/IIT310/Final/FinalPHPDB/retrieveDisplayAll.php

That doesn't look like a table, maybe thats why it isn't working?

If that is indeed a table, and I am just missing something, you should explain what you mean in the post above in newbier terms. I am using dreamweaver to edit my code, if that helps.

macfaninpdx
Jul 10, 2007, 03:20 PM
By table, I am referring to the mySQL table. It has been a long time since I used DreamWeaver, but I seem to recall that there is a DB or Database tab under connections on the property inspector, right? You need to determine if you have set up a table called "boyfight3" in your "bbarr" database with the fields correctly set up.

And FWIW, I copied your code (both form and update) to my local machine, and after I set up the mySQL db, table and fields the code worked fine. So there is no problem with the code - only the communication with the database.

barr08
Jul 10, 2007, 03:22 PM
By table, I am referring to the mySQL table. It has been a long time since I used DreamWeaver, but I seem to recall that there is a DB or Database tab under connections on the property inspector, right? You need to determine if you have set up a table called "boyfight3" in your "bbarr" database with the fields correctly set up.

And FWIW, I copied your code (both form and update) to my local machine, and after I set up the mySQL db, table and fields the code worked fine. So there is no problem with the code - only the communication with the database.

OK cool, I am going to head home from work now so I can get to the code and everything. I will be back in 45 minutes, if you happen to be around, that would be great!

Thanks

macfaninpdx
Jul 10, 2007, 03:40 PM
Are you sure you put this code in your script?

$hookup=mysql_connect($server, $user, $pass);
if (!$hookup) {
die("Connection failed: " . mysql_error());
}
$dbNow=mysql_select_db($currentDB, $hookup);
if (!$dbNow) {
die("Unable to select database: " . mysql_error());
}

I was looking the updateData code a little bit, and if it mysql_connect fails or mysql_select_db fails it will generate the Warning about mysql_num_rows as you are seeing. If you put the above code in and either of those fail, the error will be returned and the mysql_num_rows will not be run.

barr08
Jul 10, 2007, 04:27 PM
OK back.

I have added that code to my program, but I am still getting the same error.

select * from boyfight3 where uname = ''select * from boyfight3 where uname = ''

I don't see why it wouldn't be connecting. The beginning of this code is basically the same as the beginning of all my other programs in this project, which work fine.

This is the beginning of my code, where I added that stuff:


<?php

$server="localhost";
$user="bbarr";
$pass="******";
$currentDB="bbarr";
$currentTable="boyfight3";

$hookup=mysql_connect($server, $user, $pass);
if (!$hookup) {
die("Connection failed: " . mysql_error());
}
$dbNow=mysql_select_db($currentDB, $hookup);
if (!$dbNow) {
die("Unable to select database: " . mysql_error());
}

/* Pull all of the form variables and set them */
$uname = $_POST["uname"];
$email = $_POST["email"];
$acnt = $_POST["acnt"];
$cost = $_POST["cost"];

$query = "select * from $currentTable where uname = '$uname'";
echo $query;
$result = mysql_query($query);
echo $query;


etc.

macfaninpdx
Jul 10, 2007, 04:43 PM
I have added that code to my program, but I am still getting the same error.
Well if script is not exiting after you added that code, then the problem is not with the db connection, nor with the mysql_select_db. Which leaves the query. But it worked fine here, which I think means it is something with your mysql table.

Can you add this, under the $result= mysql_query($query):
if( !$result ) {
echo "Query failed: " . mysql_error();
}

barr08
Jul 10, 2007, 04:49 PM
Code is:


$query = "select * from $currentTable where uname = '$uname'";
echo $query;
if( !$result ) {
echo "Query failed: " . mysql_error();
}
$result = mysql_query($query);
echo $query;


Error is:

select * from boyfight3 where uname = ''Query failed: select * from boyfight3 where uname = ''

was I supposed to take out the first "echo $query;"?

macfaninpdx
Jul 10, 2007, 04:49 PM
From the php.net documentation (link (http://us.php.net/manual/en/function.mysql-query.php)):
mysql_query() will also fail and return FALSE if the user does not have permission to access the table(s) referenced by the query.
Now I'm thinking this may be the problem.

macfaninpdx
Jul 10, 2007, 04:50 PM
was I supposed to take out the first "echo $query;"?
Take out both echo lines, and move the if block to after the $result = ... line.

barr08
Jul 10, 2007, 04:51 PM
Query failed: Unknown column 'uname' in 'where clause'

macfaninpdx
Jul 10, 2007, 04:54 PM
Query failed: Unknown column 'uname' in 'where clause'
Finally, we found the cause of the problem! You do not have the fields set up (or not set up correctly) in your boyfight3 table. Can you set up the fields using DreamWeaver? How did you initially set up the database and the table?

barr08
Jul 10, 2007, 04:56 PM
I have a program that created the table:

http://nemo.mwd.hartford.edu/~bbarr/IIT310/Final/FinalPHPDB/createTable.php


<?php
$server="localhost";
$user="bbarr";
$pass="******";
$currentDB="bbarr";
$testTable="boyfight3";

$hookup=mysql_connect($server, $user, $pass) ;

$dbNow=@mysql_select_db($currentDB, $hookup);

$sql = "CREATE TABLE $testTable (ename varchar(30), email varchar(40), acnt varchar(4), cost varchar(10))";

$result= @mysql_query($sql, $hookup) or die("Table not created." . mysql_error());

if($result) {
print "Table successfully created." ;
}
?>

macfaninpdx
Jul 10, 2007, 05:03 PM
$sql = "CREATE TABLE $testTable (ename varchar(30), email varchar(40), acnt varchar(4), cost varchar(10))";
If you look closely at the CREATE TABLE SQL command, you will notice that the field you created is called ename, not uname as you are trying to select. Change the select statement in your updateData.php file to:
$query = "select * from $currentTable where ename = '$uname'";
and you will have to change it in the insert and update queries as well.

barr08
Jul 10, 2007, 05:04 PM
wow what a terrible typo...one sec

barr08
Jul 10, 2007, 05:08 PM
Awesome! no errors, but it doesn't update the fields.

My first field is BenCo/barr08@gmail.com/0001/1000

and i typed all of that, but instead of 1000, i typed 2000, into the dataUpdate html form, and nothing happened.

view fields:
http://nemo.mwd.hartford.edu/~bbarr/IIT310/Final/FinalPHPDB/retrieveDisplayAll.php

data update form:
http://nemo.mwd.hartford.edu/~bbarr/IIT310/Final/FinalPHPDB/dataUpdate.html

macfaninpdx
Jul 10, 2007, 05:17 PM
Awesome! no errors, but it doesn't update the fields.
Now THAT problem does lie in the code. ;)
Here is your existing code (assuming you changed the uname to ename per above:

else
{

/* Update */
$row = mysql_fetch_assoc($result);

/* Get the old ID */
$id = $row["id"];

/* Create the update query */
$update_query = "update $currentTable set ename = '$uname', email = '$email', acnt = '$acnt', cost = '$cost' where id = '$id'";

$result = mysql_query($update_query);

}
But I'm afraid the bit about "Get the old ID" won't do anything, since you are not using an id field. So you can remove the $id line and it's comment. And you can remove the $row = ... bit as well. So the new else block should become:
else
{
/* Create the update query */
$update_query = "update $currentTable set ename = '$uname', email = '$email', acnt = '$acnt', cost = '$cost' where ename = '$uname'";

$result = mysql_query($update_query);

}

*Note the $update_query line was changed too.

barr08
Jul 10, 2007, 05:25 PM
IT WORKS!

check you pm