Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

bad control structures? 3

Status
Not open for further replies.

streetbmx

Programmer
Dec 6, 2001
16
0
0
US
i am trying to make a simple database template(?). adding and deleting from the mysql database worked fine untill I tried to add edit, the edit wont work. also it always says 'Record has been deleted from database', its only supposed to say that when you delete from it. I also tried to use a switch to control everything but nothing worked after that. can someone look at my code and tell me whats wrong? (my webhost doesnt support php so its just a text file.)
 
Looking at your code, I have to recommend you start using seperate files for seperate tasks. I didn't notcie any errors though. Using seperate file for each task is the best way to do it though. Everything is handled by the index.php file. By default, the index.php file will just list all the entries.

This is untested - it should work though... just make each of the files. This uses mostly your code, I just reorganized it a bit.

index.php:
Code:
<?
require(&quot;vars.php&quot;);
Code:
// for database connection vars
Code:
switch($todo)
{
 case
Code:
&quot;add&quot;
Code:
;
Code:
// display a blank form
Code:
  $title = &quot;
Code:
Add New Entry
Code:
&quot;;
Code:
// used by header
Code:
  require(&quot;header.php&quot;);
Code:
// your page header
Code:
  require(&quot;add.php&quot;);
Code:
// submits to index.php?todo=save
Code:
  require(&quot;footer.php&quot;);
Code:
// your page footer
Code:
  break;

 case
Code:
&quot;edit&quot;
Code:
:
Code:
// pull the data from db and plugs into form
Code:
  $title = &quot;
Code:
Edit Existing Entry
Code:
&quot;;
Code:
// used by header
Code:
  require(&quot;header.php&quot;);
Code:
// your page header
Code:
  require(&quot;edit.php&quot;);
Code:
// edits $editid, submits to index.php?todo=update
Code:
  require(&quot;footer.php&quot;);
Code:
// your page footer
Code:
  break;

 case
Code:
&quot;delete&quot;
Code:
:
  require(&quot;delete.php&quot;);
Code:
// deletes $deleteid from the db
Code:
  header(&quot;Location: index.php&quot;);
Code:
// go to listing
Code:
  break;

 case
Code:
&quot;save&quot;
Code:
:
  require(&quot;save.php&quot;);
Code:
// add new entry to db
Code:
  header(&quot;Location: index.php&quot;);
Code:
// go to listing
Code:
  break;

 case
Code:
&quot;update&quot;
Code:
:
  require(&quot;update.php&quot;);
Code:
// update the db where id=$id
Code:
  header(&quot;Location: index.php&quot;);
Code:
// go to listing
Code:
  break;

 default:
  $title = &quot;
Code:
Viewing All Entries
Code:
&quot;;
Code:
// used by header
Code:
  require(&quot;header.php&quot;);
Code:
// your page header
Code:
  require(&quot;list.php&quot;);
Code:
//show all entries
Code:
  require(&quot;footer.php&quot;);
Code:
// your page footer
Code:
  break;
}
?>

vars.php
Code:
<?
$db_host = &quot;
Code:
localhost
Code:
&quot;;
$db_user = &quot;
Code:
YOUR_USERNAME
Code:
&quot;;
$db_pass = &quot;
Code:
YOUR_PASSWORD
Code:
&quot;;
$db_name = &quot;
Code:
address
Code:
&quot;;
$db_table = &quot;
Code:
addresses
Code:
&quot;;
?>

header.php
Code:
<HTML>
<HEAD>
 <TITLE><?
 echo &quot;
Code:
$title
Code:
&quot;;
 ?></TITLE>
</HEAD>
<BODY>

add.php:
Code:
<form method=&quot;post&quot; action=&quot;index.php&quot;>
<input type=&quot;hidden&quot; name=&quot;todo&quot; value=&quot;save&quot;>
<table>
	<tr>
		<td>First Name:</td>
		<td><input type=&quot;text&quot; name=&quot;f_name&quot;></td>
	</tr>
	<tr>
		<td>Last Name:</td>
		<td><input type=&quot;text&quot; name=&quot;l_name&quot;></td>
	</tr>
	<tr>
		<td>Phone:</td>
		<td><input type=&quot;text&quot; name=&quot;phone&quot;></td>
	</tr>
	<tr>
		<td>Email:</td>
		<td><input type=&quot;text&quot; name=&quot;email&quot;></td>
	</tr>
	<tr>
		<td></td>
		<td><input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;ADD&quot;></td>
	</tr>
</table>
</form>

footer.php
Code:
</BODY>
</HTML>

edit.php:
Code:
<?
 mysql_connect($db_host, $db_user, $db_pass);
 mysql_select_db($db_name); 
 $result = mysql_query(&quot;SELECT * FROM addresses WHERE id=$editid&quot;);
 $edit = mysql_fetch_array($result);
?>
<form method=&quot;post&quot; action=&quot;index.php&quot;>
<input type=&quot;hidden&quot; name=&quot;todo&quot; value=&quot;update&quot;>
<input type=&quot;hidden&quot; name=&quot;id&quot; value=&quot;<? echo $edit[&quot;id&quot;] ?>&quot;>
<table>
	<tr>
		<td>First Name:</td>
		<td><input type=&quot;text&quot; name=&quot;f_name&quot; value=&quot;<? echo $edit[&quot;f_name&quot;] ?>&quot;></td>
	</tr>
	<tr>
		<td>Last Name:</td>
		<td><input type=&quot;text&quot; name=&quot;l_name&quot; value=&quot;<? echo $edit[&quot;l_name&quot;] ?>&quot;></td>
	</tr>
	<tr>
		<td>Phone:</td>
		<td><input type=&quot;text&quot; name=&quot;phone&quot; value=&quot;<? echo $edit[&quot;phone&quot;] ?>&quot;></td>
	</tr>
	<tr>
		<td>Email:</td>
		<td><input type=&quot;text&quot; name=&quot;email&quot; value=&quot;<? echo $edit[&quot;email&quot;] ?>&quot;></td>
	</tr>
	<tr>
		<td></td>
		<td><input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;update&quot;></td>
	</tr>
</table>
</form>

delete.php:
Code:
<?
 mysql_connect($db_host, $db_user, $db_pass);
 mysql_select_db($db_name); 
 $result = mysql_query(&quot;DELETE FROM addresses WHERE id=$deleteid&quot;);
?>

save.php:
Code:
<?
 mysql_connect($db_host, $db_user, $db_pass);
 mysql_select_db($db_name); 
 $result = mysql_query(&quot;INSERT INTO addresses (id,f_name,l_name,phone,email) VALUES ('NULL', '$f_name', '$l_name', '$phone', '$email')&quot;);
?>

update.php:
Code:
<?
 mysql_connect($db_host, $db_user, $db_pass);
 mysql_select_db($db_name); 
 $result = mysql_query(&quot;UPDATE addresses SET f_name=$f_name,l_name=$l_name,phone=$phone,email=$email WHERE id=$id&quot;);
?>

list.php:
Code:
<div align=&quot;center&quot;>
<h2>Database Template</h2>
<a href=&quot;index.php?todo=add&quot;>Add a Record</a>
<br>
<br>
<table border=&quot;1&quot;>
	<tr bgcolor=&quot;#DADADA&quot; align=&quot;center&quot;>
		<td><strong>ID</strong></td>
		<td><strong>First Name</strong></td>
		<td><strong>Last Name</strong></td>
		<td><strong>Phone</strong></td>
		<td><strong>Email</strong></td>
		<td><strong>Delete</strong></td>
		<td><strong>Edit</strong></td>
	</tr>
<?php
 mysql_connect($db_host, $db_user, $db_pass);
 mysql_select_db($db_name); 
 $result = mysql_query(&quot;select * from addresses&quot;);
 while($r=mysql_fetch_array($result))
 {
  $id=$r[&quot;id&quot;];
  $f_name=$r[&quot;f_name&quot;];
  $l_name=$r[&quot;l_name&quot;];
  $phone=$r[&quot;phone&quot;];
  $email=$r[&quot;email&quot;];
?>
	<tr>
		<td><? echo $id ?></td>
		<td><? echo $f_name ?></td>
		<td><? echo $l_name ?></td>
		<td><? echo $phone ?></td>
		<td><a href=&quot;mailto:<? echo $email ?>&quot;><? echo $email ?></a></td>
		<td><a href=&quot;index.php?todo=delete&deleteid=<? echo $id ?>&quot;>delete</td>
		<td><a href=&quot;index.php?todo=edit&editid=<? echo $id ?>&quot;>edit</td>
	</tr>
<?
	}
?>
</table>
<br>
<a href=&quot;#top&quot;>Top of Page</a>
</div>

I hope this helps! This is usually how I make my programs. It seems a lot easier to keep track of things! Lemme know if you have any questions.

Good luck! -gerrygerry
Go To
 
Had to drop a star on this for a fine example of programming in an organised fashion :) ***************************************
Party on, dudes!
 
Whoops! I meant to change each of your database queries to have a $db_table every place it said addresses. I did this for portability... if you every change the table name, have to change servers, or username/password, you'll only have to edit your vars.php file. -gerrygerry
Go To
 
gerrygerry !
Are you still around for a question on your code?
--L
 
You could always ask and see if someone else can answer it. //Daniel
 
True, Daniel - I will.
:)

I'm trying to look at doing a test of an updateable area with gerrygerrys *hellashisly * organized code to help me learn this. I first copied that code exacly darnit [other than changing the last break on the switch statements on the index.php pg to an endswitch and a ' ; ' to a ':' after ' add '] - but I'm getting an error (and I was getting the error before I started trying to touch any of his code):

Parse error: parse error, expecting `T_CASE' or `T_DEFAULT' or `'}'' in /usr/local/etc/httpd/htdocs/prologi/editbox/index.php on line 4

Pretty much same code as above - but I shall post it in case I did something dumb and don't realize it:


<?
require(&quot;vars.php&quot;); // for database connection vars
switch ($todo) {
 case &quot;add&quot;:
  // display a blank form
  $title = &quot;Add New Entry&quot;; // used by header
  require(&quot;header.php&quot;); // your page header
  require(&quot;add.php&quot;); // submits to index.php?todo=save
  require(&quot;footer.php&quot;); // your page footer
  break;
 case &quot;edit&quot;:
  // pull the data from db and plugs into form
  $title = &quot;Edit Existing Entry&quot;; // used by header
  require(&quot;header.php&quot;); // your page header
  require(&quot;edit.php&quot;); // edits $editid, submits to index.php?todo=update
  require(&quot;footer.php&quot;); // your page footer
  break;
 case &quot;delete&quot;:
  require(&quot;delete.php&quot;); // deletes $deleteid from the db
  header(&quot;Location: index.php&quot;); // go to listing
  break;
 case &quot;save&quot;:
  require(&quot;save.php&quot;); // add new entry to db
  header(&quot;Location: index.php&quot;); // go to listing
  break;
 case &quot;update&quot;:
  require(&quot;update.php&quot;); // update the db where id=$id
  header(&quot;Location: index.php&quot;); // go to listing
  break;
 default:
  $title = &quot;Viewing All Entries&quot;; // used by header
  require(&quot;header.php&quot;); // your page header
  require(&quot;list.php&quot;); //show all entries
  require(&quot;footer.php&quot;); // your page footer
endswitch;
}
?>


-L
 
Remove the endswitch;. It shouldn't be there. That is only if you use the other &quot;not-so-nice&quot; (IMO) way of scripting in PHP. It looks like this if you're going to use that way:
Code:
switch ($statement):
    case 'Somecase':
        echo &quot;Some text&quot;;
        break;
    default:
        echo &quot;Nothing else matched&quot;;
endswitch;
Note the colon after the switch and that you don't use any {s or }s. //Daniel
 
Im still around... I guess I'm a little too late to help though... thanks for covering for me Daniel! I haven't had much time to post here lately... -gerrygerry

Standard response to one of my posts:
&quot;No where in your entire rantings did you come anywhere close to what can be considered a rational answer. We are all now dumber from having heard that. I award you no points and may God have mercy on your soul.&quot;
 
Hi guys.
Ok, I replaced the endswitch with the break;
but I still get the error:

parse error, expecting `T_CASE' or `T_DEFAULT' or `'}''


-L
 
Could you post the exact error message you are getting? What version of PHP are you using? //Daniel
 
It's at least 4.0.1, I think, definitely not 4.1 yet.

The error is:
Parse error: parse error, expecting `T_CASE' or `T_DEFAULT' or `'}'' in /usr/local/etc/httpd/htdocs/prologi/editbox/index.php on line 4


But I don't know what to DO to on (or around) line 4...

 
It seems to me you might have an invisible control character in your code... sometimes you might accidentallly hit a ctrl+[SOMETHING] and it just isn't visible... try deleting the line, the line before it, and the line after it... then retype them. -gerrygerry

Standard response to one of my posts:
&quot;No where in your entire rantings did you come anywhere close to what can be considered a rational answer. We are all now dumber from having heard that. I award you no points and may God have mercy on your soul.&quot;
 
I agree with gerrygerry on this one. That is the only thing that could cause this kind of trouble. //Daniel
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top