Forum Moderators: coopster
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
<?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:)
// 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
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.
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
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.
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
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
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
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
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.
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
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. =)