Forum Moderators: coopster

Message Too Old, No Replies

Form question

         

transmutated

3:15 am on Jul 15, 2005 (gmt 0)

10+ Year Member



Alright, need a hand. I'm trying to setup an update form for a singular row in MySQL. So, essentially...a user goes to a page that dumps the contents of the database onto the page. Now beside each row is a link that goes to modify.php?rowID=VALUE. Clinking a link brings up the right rowID for the particular link, but the form remains empty. If any one is interested in seeing my code...PM me. But I think this is a rather simple problem...I just dont get why it isnt transfering the rowID info to the next page to place it in the form.

jd01

3:35 am on Jul 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're right, the problem probably is simple, but, without the code, I don't think most people will be able to point out what is wrong...

Is it a forgotten echo?
How *should* the row id information be passed? with a variable? how is the variable defined? if there is no variable how is the information being passed?
Is it a $_POST definition instead of $_GET?
Is there a call to a database that should be made?
Is there another value that should be included?
Is there an include file that is not opening?
And on, and on...

Justin

maxi million

6:44 am on Jul 15, 2005 (gmt 0)

10+ Year Member



in modify.php there has to be db query which will again fetch the row where $_GET['rowID'], unless you are using a function for that already in the previous page (in which case you may simply call that function again).
and in the input fields (or somewhere) the fetched contents have to be echoed. in case of input fields its got to be like:
<input name="textboxone" type="text" id="textboxone" value="<?php echo $value;?>">

transmutated

9:03 pm on Jul 15, 2005 (gmt 0)

10+ Year Member



Alright, here is modify.php

<?php
if (isset($_GET['rowID'])) {
$rowID = $_GET['rowID'];
$query = "SELECT first, last, id, dept FROM employee WHERE rowID='$rowID'";
$result = mysql_query($query);
list($first,$last,$id,$dept) = mysql_fetch_row($result);
include "modifyform.php";
}
?>

And here is the form info from modifyform.php:

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input type="hidden" name="rowID" value="<?php echo $rowID;?>">

And here is the php in modifyform.php:

<?php
if (isset($_POST['submit'])) {
$rowID = $_POST['rowID'];
$first = $_POST['last'];
$last = $_POST['first'];
$id = $_POST['id'];
$dept = $_POST['dept'];

$query = "UPDATE employee SET first='$first', last='$last', id='$id', dept='$dept' WHERE rowID='$rowID'";
$result = mysql_query($query);

if ($result)
echo "<p>The Employee has been successfully updated.</p>";
else
echo "<p>There was a problem updating the Employee.</p>";
}

?>

Never really messed with PHP much...I was following some tutorial. I think there might be a problem with not actually connecting to the DB(login, pass, db)...but, whenever I add that to either script...it doesnt even load the form. So, there that is. Help:)

jd01

10:27 pm on Jul 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
if (isset($_GET['rowID'])) {
$rowID = $_GET['rowID'];

// first, lets get a db connection
$db = mysql_connect('mysql.yourhost.com', 'YourUserName', 'YourPass');
mysql_select_db('YourDataBase',$db) or die ( mysql_error() . "\n" );

$query = "SELECT first, last, id, dept FROM employee WHERE rowID='$rowID'";
$result = mysql_query($query);

// 2nd lets try something I use more (you can switch back later if you like =)
$empolyee = mysql_fetch_array($result);

// 3rd lets do some error checking, which will print out some more of the error, so maybe there will be a better clue as to where things are going wrong.
if (!$result = mysql_query($query)) { die("Could not execute this query - ERRNO:".mysql_errno()." -- ERROR:".mysql_error()." -- QUERY:".$query); }

// 4th lets make sure we have a connection if there is not an error
else echo "Connected!";

include "modifyform.php";
}
?>

And here is the form info from modifyform.php:

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
<input type="hidden" name="rowID" value="<?php echo $rowID;?>">

// 5th we need to make sure we echo back all the values for the form
<input type="text" name="last" value="<?php echo $employee['last'];?>">
<input type="text" name="first" value="<?php echo $employee['first'];?>">
<input type="text" name="id" value="<?php echo $employee['id'];?>">
<input type="text" name="dept" value="<?php echo $employee['dept'];?>">

// 6th make sure we are posting the submit
<input type="submit" name="submit" value="Submit">
</form>

And here is the php in modifyform.php:

<?php
if (isset($_POST['submit'])) {
$rowID = $_POST['rowID'];
$first = $_POST['last'];
$last = $_POST['first'];
$id = $_POST['id'];
$dept = $_POST['dept'];

// 7th lets change our variables to make sure there is no storage conflict or anything else goofy going on.
$query2 = "UPDATE employee SET first='$first', last='$last', id='$id', dept='$dept' WHERE rowID='$rowID'";
$result2 = mysql_query($query2);

// 8th lets check for errors again
if (!$result2 = mysql_query($query2)) { die("Could not execute this query - ERRNO:".mysql_errno()." -- ERROR:".mysql_error()." -- QUERY:".$query2); }

// 9th again check to see if it worked
else echo "It Worked!";

// if ($result)
// echo "<p>The Employee has been successfully updated.</p>";
// else
// echo "<p>There was a problem updating the Employee.</p>";

// Added: to avoid any issues, let's unset the submit when the connection is complete.
unset($submit);
}

?>

Obviously, this will need to be shortened, edited a little to your liking... My main objective when writing is to make it all work, and get some comment along the way for each portion working or not working.

This should give an idea if the script is working, and if not, where it is breaking (hopefully some use for the future, too.)

Hope this helps and gives you some ideas... I think I found that SQL error check here at WebmasterWorld a while ago, thanks to the original poster, it is a great little piece of code.

Justin

transmutated

12:25 am on Jul 16, 2005 (gmt 0)

10+ Year Member



Alright, tried it all and still nothing. I first updated the modify.php, then tried it to no avail. Then updated modifyform.php and nothing. Hmmm, alright, so here is the next thing. The page that dumps all of this info out...here is the php for that. Maybe it has something o do with that.
Here that is:
<?php

include "mysql.class.php";

$mysqldb = new mysql("localhost","yada","yada","hr");
$mysqldb->connect();
$mysqldb->select();

$mysqldb->query("SELECT rowID, first as First, last as Last, id as ID, dept as Deptartment FROM employee ORDER BY dept");

$actions = '<a href="modify.php?rowID=VALUE">Modify</a> ¦ <a href="view.php?rowid=VALUE">View Detail</a>';

echo $mysqldb->getResultAsTable($actions);

?>

That looks like an easy enough script that runs good, generates all of the correct urls and what not. If need be, I can post the mysql.class.php, but its kinda big.

transmutated

12:33 am on Jul 16, 2005 (gmt 0)

10+ Year Member



Oh, and I forgot to mention. When you click on the modify link...it brings up the form and all and it states that it is connected but the form remains empty. Now, in hopes of getting another error message, I filled out the form and hit submit. It returned no error message and I was greated with a blank screen. Thanks for the suggestions:) They are greatly appreciated!

maxi million

6:01 am on Jul 16, 2005 (gmt 0)

10+ Year Member



ok...it looks like a very strange problem really...but you have
<?php echo $_SERVER['PHP_SELF'];?>
as your form action. i know theres no problem with that, but since you are echoing it, it may be useful if you do view source for modify.php. it SHOULD say form action="modify.php", nevertheless check it just in case it says "modifyform.php". if it does, that would be the problem

jd01

9:30 am on Jul 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How about we add another error check so we can find out where the information is getting lost...

Since we know there is a connection, and we have a result, print out the result:

// 2nd lets try something I use more (you can switch back later if you like =)
$empolyee = mysql_fetch_array($result);

// 3rd lets do some error checking, which will print out some more of the error, so maybe there will be a better clue as to where things are going wrong.
if (!$result = mysql_query($query)) { die("Could not execute this query - ERRNO:".mysql_errno()." -- ERROR:".mysql_error()." -- QUERY:".$query); }

// 4th lets make sure we have a connection if there is not an error
else echo "Connected!";

// new line
print_r($employee);

This should tell us what is stored in the array employee... if these are not the results you expected, or somehow empty, then we know the error is above here in the connection or in the actual row we are selecting from in the database... if the results are in the array, then we know we are losing them somewhere lower in the code.

Justin

transmutated

11:26 pm on Jul 17, 2005 (gmt 0)

10+ Year Member



Alright, tried both of the suggestions and nothing new comes of it. Sorry for being such a pain...its just really frustrating for me becuase it looks like all of the elements are there I just cant get it to go any where. So, where do we go from here?

jd01

11:33 pm on Jul 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So, you get Connected!, but no print out of the $employees... (This actually tells us a lot). If this is correct, we start working our way up the code, because now we know that we are connecting to the database, but not getting any results out...

Now we will check the full db queries and see what they are:

if (isset($_GET['rowID'])) {
$rowID = $_GET['rowID'];

//new line
echo $rowID;

// first, lets get a db connection
$db = mysql_connect('mysql.yourhost.com', 'YourUserName', 'YourPass');

//new line
echo $db;

mysql_select_db('YourDataBase',$db) or die ( mysql_error() . "\n" );

$query = "SELECT first, last, id, dept FROM employee WHERE rowID='$rowID'";

//new line
echo $query;

$result = mysql_query($query);

//new line
echo $result;

This should give you a good look at what you are asking for and what you are getting back... if any of these are empty, then we know where the problem is. If they are not we will keep moving up the code until we find it =)

Justin

Added: BTW I think it is much more productive to teach you how to trouble shoot than to attempt to flat out give you answers... you should be able to use these techniques indefinitely - Trust me trouble shooting is a good part of the life of scripting.

transmutated

12:12 am on Jul 22, 2005 (gmt 0)

10+ Year Member



Alright, so here is the update.

After inserting the new echo commands, it generated this at the top of the page...

Resource id #2SELECT first, last, id, dept FROM employee WHERE rowID='1'Resource id #3Connected!

Not sure how all of that got generated...but it seems like information like this could be really usefull in the future. If you could...can you explain more Justin;)

Thanks alot for this!

Clifford

jd01

1:34 am on Jul 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sure - What we did is take a look at the actual information that is being passed at each step along the way... I forgot to tell you to add some commenting and new lines - silly me!

I have added the new lines and some comments that we will show when we run this again, so you can see exactly what is coming from where.

This is *very* useful for trouble shooting, because you can normally get right to the issue.

if (isset($_GET['rowID'])) {
$rowID = $_GET['rowID'];

// Show what row we have stored in the $rowID, so we can verify the row exists and has information stored in it in the actual database.
echo "This is the rowID we are trying to select from the database: $rowID <br>";

// SQL information
$db = mysql_connect('mysql.yourhost.com', 'YourUserName', 'YourPass');

// Basically, checking to make sure that there is nothing odd happening...
echo "Make sure we have a resource for connecting to the database: $db <br>";

mysql_select_db('YourDataBase',$db) or die ( mysql_error() . "\n" );

// ADDED: changed this line to * all, to make sure it is not some silly spacing issue or col names.
$query = "SELECT * FROM employee WHERE rowID='$rowID'";

// Show what we are asking the database for, if this does not match a proper SQL query, or if there is not a table, row, or information entered that we are asking for, we will get no results.
echo "This is the actual query we are using to get the information from the database: $query <br>";

$result = mysql_query($query);

// Again, basically, checking to make sure that there is nothing odd happening...
echo "Make sure this is registered as a valid resorce ID: $result <br>";

// New Line: Select the array that the query should produce, if this line is empty, then we know we have made the connection to the wrong place in the database or there is no information stored in the database row we are selecting.
$result2 = mysql_fetch_array($result);

echo "This is what we have stored as results, after making the query: ";
print_r($result);
echo "<br>";

From what you posted last, it looks to me like you are fine with the connection, but there is no information in the database in the row you are requesting... I would double check the query against the database EG is there information in row 1 of the database, or is that an empty row, with only the number set?

You might also use your database admin interface, and add a row, make note of the number and then change to this line:

$query = "SELECT first, last, id, dept FROM employee WHERE rowID='2'";

Obviously 2 would be whatever row number you just added.

Hope this is more clear & that I haven't missed too much. I am very visual, so when I try to answer questions here sometimes it's an effort to try not to leave too much out =)

Justin

transmutated

2:39 am on Jul 23, 2005 (gmt 0)

10+ Year Member



Alright, here is the print out after the modify.php link is clicked.

This is the rowID we are trying to select from the database: 1
Make sure we have a resource for connecting to the database: Resource id #2
This is the actual query we are using to get the information from the database: SELECT * FROM employee WHERE rowID='1'
Make sure this is registered as a valid resorce ID: Resource id #3
This is what we have stored as results, after making the query: Resource id #3

Same as befor, nothing is brought over. I then cleared out the table employee and added one entry through PHPmyadmin. I then set the query to just do rowID=1. That was also a negative. This is mind numbing. I'm still lost...grrrrrrrrrr

jd01

7:09 am on Jul 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I left a 2 off of this line:

print_r($result);

Should be:

print_r($result2);

A few things to look at and think about though:
1. What is the row number that shows in phpMyAdmin for the row you added?
2. Is this the row number you are trying to access?
3. Do you have the id col set to (minimum) index, or (usually) primary?
4. In phpMyAdmin, when you select a row from the DB and you have the option to generate the php, do, then compare that query to your own... if they are different, copy and paste the code into your php and then run the script. If you get results, remove the row number and insert your variable, and try again.

Hope this helps, sorry it is taking so long...

Justin

transmutated

5:26 pm on Jul 26, 2005 (gmt 0)

10+ Year Member



Alright, I added the 2 to the end of the result deal. Well...here is what it posted:

This is the rowID we are trying to select from the database: 1
Make sure we have a resource for connecting to the database: Resource id #2
This is the actual query we are using to get the information from the database: SELECT * FROM employee WHERE rowID='1'
Make sure this is registered as a valid resorce ID: Resource id #3
This is what we have stored as results, after making the query: Array ( [0] => 1 [rowID] => 1 [1] => yada [first] => yada [2] => aday [last] => aday [3] => 00001-0001 [id] => 00001-0001 [4] => a [dept] => a )

So, it looks as though it is being sent over, but the form is rejecting it. Well, I'll mess around with it more tonight. Tell me what you think.

jd01

9:17 pm on Jul 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cool, we're about there...

Let's do some renaming and make sure we don't have any conflicts or variables getting set to "" anywhere EG in the if(isset($_POST['submit'])) line:

Let's also make sure we are echoing an array we know we have results in:
Array ( [0] => 1 [rowID] => 1 [1] => yada [first] => yada [2] => aday [last] => aday [3] => 00001-0001 [id] => 00001-0001 [4] => a [dept] => a )

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">

// you might also try <form action="<?php $PHP_SELF;?>" method="post">

<input type="hidden" name="rowID" value="<?php echo $result2['rowID']; ?>">

// Added a unique variable to the form for editing, so there can be no conflict between submits, or other variables passed.

<input type="hidden" name="edit" value="Y">

<input type="text" name="last" value="<?php echo $result2['last']; ?>">
<input type="text" name="first" value="<?php echo $result2['first']; ?>">
<input type="text" name="id" value="<?php echo $result2['id']; ?>">
<input type="text" name="dept" value="<?php echo $result2['dept']; ?>">

<input type="submit" name="submit" value="Submit">
</form>

<?php

// edited to check for a unique variable instead of the more generic 'submit'

$edit=$_POST['edit'];
if ($edit==="Y")) {
$rowID = $_POST['rowID'];
$first = $_POST['last'];
$last = $_POST['first'];
$id = $_POST['id'];
$dept = $_POST['dept'];

// renamed connections to unique names based on what we are doing, to avoid any conflict, and remind us of where we are in the code.

$edit_query = "UPDATE employee SET first='$first', last='$last', id='$id', dept='$dept' WHERE rowID='$rowID'";
$edit_result = mysql_query($query);

if (!$edit_result = mysql_query($edit_query)) { die("Could not execute this query - ERRNO:".mysql_errno()." -- ERROR:".mysql_error()." -- QUERY:".$edit_query); }

else echo "It Worked!";

// if ($result)
// echo "<p>The Employee has been successfully updated.</p>";
// else
// echo "<p>There was a problem updating the Employee.</p>";

// Added: to avoid any issues, let's unset the submit when the connection is complete.
// Added: Unset of the unique variable, edit
unset($submit,$edit);
}

?>

Justin

transmutated

11:34 pm on Jul 26, 2005 (gmt 0)

10+ Year Member



So, I just updated my code to match the new. When I tried to get it to go...my form didnt show up this time. I decided I probably had a typo so I copied and pasted and it ended with the same result...no form. This all looks right again...so, i am once again clueless.

jd01

12:55 am on Jul 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<?php
$edit=$_POST['edit'];
$rowID = $_GET['rowID']; if(!$rowID) { $rowID = $_POST['rowID']; }
$first = $_POST['last'];
$last = $_POST['first'];
$id = $_POST['id'];
$dept = $_POST['dept'];

if (isset($rowID) && $rowID!=="" && $edit!=="Y") {
$db = mysql_connect('mysql.yourhost.com', 'YourUserName', 'YourPass');

echo "Make sure we have a resource for connecting to the database: $db <br>";

mysql_select_db('YourDataBase',$db) or die ( mysql_error() . "\n" );
$query = "SELECT * FROM employee WHERE rowID='$rowID'";

echo "This is the actual query we are using to get the information from the database: $query <br>";

$result = mysql_query($query);

echo "Make sure this is registered as a valid resorce ID: $result <br>";
$result2 = mysql_fetch_array($result);

echo "This is what we have stored as results, after making the query: ";
print_r($result2);

?>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">

// you might also try <form action="<?php $PHP_SELF;?>" method="post">

<input type="hidden" name="rowID" value="<?php echo $result2['rowID']; ?>">

<input type="hidden" name="edit" value="Y">

<input type="text" name="last" value="<?php echo $result2['last']; ?>">
<input type="text" name="first" value="<?php echo $result2['first']; ?>">
<input type="text" name="id" value="<?php echo $result2['id']; ?>">
<input type="text" name="dept" value="<?php echo $result2['dept']; ?>">

<input type="submit" name="submit" value="Submit">
</form>

<?php }

elseif($edit==="Y")) {

$edit_query = "UPDATE employee SET first='$first', last='$last', id='$id', dept='$dept' WHERE rowID='$rowID'";
$edit_result = mysql_query($edit_query);

if (!$edit_result = mysql_query($edit_query)) { die("Could not execute this query - ERRNO:".mysql_errno()." -- ERROR:".mysql_error()." -- QUERY:".$edit_query); }

else echo "It Worked!";

unset($edit,$rowID,$submit);

} elseif(!$rowID && !$edit) { echo "The row ID and edit are not set"; }?>

Justin

Added: Oops! forgot to close/open the php tags around the form... don't have time to explain why I did it this way right now, but will try to revisit it later - Have a good day. =)

transmutated

2:27 am on Jul 27, 2005 (gmt 0)

10+ Year Member



The form still isnt showing up. Befor I crash out tonight...I'll keep messing with it. I just dont see why the form data isnt being filled in. In looks like the elements are all being brought in and that the form is rejecting them. Push comes to shove...I've learned alot about the PHP deal. Alright, back to the grind of things.