I'm trying to contruct a simple .php file that will all people to view, edit and add files...depending on the $action variable...IE: script.php?action=add
Here's the code I've got...I've got two problems...1: the add form won't work...it simply loads the original .php pages upon submission without sending the data to the DB...and 2: I'm trying to use an else-if command so that I can display a record from the DB in this format: script.php?action=view&id=1 - I can't quite get this to work...I have the command set like this:
$db = @mysql_connect("localhost", "USERNAME", "PASSWORD");
if (!$db) {
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
}
mysql_select_db("mycodingdb",$db);
if (! @mysql_select_db("mycodingdb") ) {
echo( "<p>Unable to locate the " .
"database at this time.</p>" );
}
$db = @mysql_connect("localhost", "USERNAME", "PASSWORD");
if (!$db) {
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}
mysql_select_db("mycodingdb",$db);
if (! @mysql_select_db("mycodingdb") ) {
echo( "<p>Unable to locate the " .
"database at this time.</p>" );
exit();
}
// SQL query to retrieve all information
// associated with any entries in the
// database that have an ID equal to $id
$sql = "SELECT * FROM programmers " .
"WHERE ID=$id";
// Just in case there is more than one
// entry with the given ID, loop through
// "all of them" (there will usually only
// be one) so that only the values from
// the last one are retained.
echo( "Welcome to the Programmers For Hire database. Please make a selection:<p>
<b> --- <a href='$PHP_SELF?action=view'>View All Records</a></b><br>
<b> --- <a href='$PHP_SELF?action=add'>Add a Record</a></b><br>
<b> --- <a href='$PHP_SELF?action=edit'>Edit a Record</a></b><br>");
Thanks...that helped some of the problems...but I've got something else now...
I can't just use the two if commands back to back...because in the case of the second if, both are true...so it displays the output from both...so I need a way to narrow the 1st if (if $action == "view") down so that it won't apply in any other scenario...I'm using this, to try to display the "view" page is the "action" variable is set to "view" and there is a blank ID variable (none specified in the URL):
I tried changing that...and I made Kevin's change, but I still have the same two problems I mentioned at first...perhaps the PHP_SELF path won't work because at the page the form is at the URL is programmers.php?action=add ?
$db = @mysql_connect("localhost", "USERNAME", "PASSWORD");
if (!$db) {
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
}
mysql_select_db("mycodingdb",$db);
if (! @mysql_select_db("mycodingdb") ) {
echo( "<p>Unable to locate the " .
"database at this time.</p>" );
}
$db = @mysql_connect("localhost", "USERNAME", "PASSWORD");
if (!$db) {
echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
exit();
}
mysql_select_db("mycodingdb",$db);
if (! @mysql_select_db("mycodingdb") ) {
echo( "<p>Unable to locate the " .
"database at this time.</p>" );
exit();
}
// SQL query to retrieve all information
// associated with any entries in the
// database that have an ID equal to $id
$sql = "SELECT * FROM programmers " .
"WHERE ID=$id";
// Just in case there is more than one
// entry with the given ID, loop through
// "all of them" (there will usually only
// be one) so that only the values from
// the last one are retained.
echo( "Welcome to the Programmers For Hire database. Please make a selection:<p>
<b> --- <a href='$PHP_SELF?action=view'>View All Records</a></b><br>
<b> --- <a href='$PHP_SELF?action=add'>Add a Record</a></b><br>
<b> --- <a href='$PHP_SELF?action=edit'>Edit a Record</a></b><br>");
I try to put everything in one script normally. If programmers.php is another script then PHP_SELF will not work. If this is all one script there is no code to add the record to the DB.
if ($action = "view" && !isset($ID)) {
do ordinary view stuff
}
if ($action = "view" && isset($ID)) {
do view stuf when $ID is set
}
if ($action = "add") {
do add stuff
}
if ($action = "edit) {
do edit stuff
}
or you could use else if statements. like this ..
if ($action = "view" && !isset($ID)) {
do ordinary view stuff
}
else if ($action = "view" && isset($ID)) {
do view stuf when $ID is set
}
else if ($action = "add") {
do add stuff
}
else ($action = "edit) {
do edit stuff
}
The first thing you do in your page should be connect to DB, before any if statements otherwise your writing code too many times, when once will suffice.
Also before the first if statement put an
if ($submit = "Submit") {
Add new record to db
}
this will be checked each time the script is entered, note the value for each submit on you page will need to be different so you can differenciate between them.
Variable names in PHP are case sensitive. Thus, if you pass ?id=5 in the query string of your URL, the value of $id will be 5, but $ID is not set to anything. Be sure you are using consistent cases in your variable names.
If this still fails to resolve your problem, could you tell us what SQL error you are getting?
Bookmarks