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 strongm 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
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.)
 
Well I'll be...
Did as you suggested. It fixed that error, took me down another 10 or so lines to a few others, which were probably stray tabs that I put in thinking it wouldn't affect it.

Then I got a few more on the other pages that I took care off, now they are ok - but I cannot add a record. I don't get an error after I submit, I get nothing.

I'll need to take another look....
 
Ok - Now I'm getting errors on the list.php pg around lines 21-22 which are at the beginning of the while loop :

<?php
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$result = mysql_query(&quot;select * from $db_table&quot;);
while($r=mysql_fetch_array($result))
{
$id=$r[&quot;id&quot;];
$loaddate=$r[&quot;loaddate&quot;];
$origcity=$r[&quot;origcity&quot;];
$origstate=$r[&quot;origstate&quot;];
$destcity=$r[&quot;destcity&quot;];
$deststate=$r[&quot;deststate&quot;];

?>
<tr>
<td><? echo $id ?></td>
        <td><? echo $loaddate ?></td>
        <td><? echo $origcity ?></td>
        <td><? echo $origstate ?></td>
<td><? echo $destcity ?></td>
<td><? echo $deststate ?></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>
<?
}
?>

the error message is :Warning: Supplied argument is not a valid MySQL result resource in /usr/local/etc/httpd/htdocs/prologi/editbox/list.php on line 21
 
If I close the bracket before the series of echo stmnt, that fixes that error , but it just gives me another on the stray
<?
}
?>
 
Does the db_table variable contain what you think it does? Try echoing the query to see if it's correct. //Daniel
 
DiamondLil,

I have found that staying in PHP mode and using print or echo statements for output makes debugging PHP a lot easier.

[tt]
Code:
<?php

mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$result = mysql_query(&quot;select * from $db_table&quot;);
while($r=mysql_fetch_array($result[b], MYSQL_ASSOC[/b]))
{
	$id			=	$r['id'];
	$loaddate	=	$r['loaddate'];
	$origcity	=	$r['origcity'];
	$origstate	=	$r['origstate'];
	$destcity	=	$r['destcity'];
	$deststate	=	$r['deststate'];

	print '
	<tr>
		<td>' . $id . '</td>
		<td>' . $loaddate . '</td>
		<td>' . $origcity . '</td>
		<td>' . $origstate . '</td>
		<td>' . $destcity . '</td>
		<td>' . $deststate . '</td>
		<td><a href=&quot;mailto:' . $email . '&quot;>' . $email . '</a></td>
		<td><a href=&quot;index.php?todo=delete&deleteid=' . $id . '&quot;>delete</td>
		<td><a href=&quot;index.php?todo=edit&editid=' . $id . '&quot;>edit</td>
	</tr>';
}

?>
[tt] ______________________________________________________________________
TANSTAAFL!
 
Ok, actually vars.php did have an error, I fixed it, and still I get :

Warning: Supplied argument is not a valid MySQL result resource in usr/local/etc/httpd/htdocs/prologi/editbox/list.php on line 21

list.php page :
<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>Load Date</strong></td>
        <td><strong>Originating City</strong></td>
        <td><strong>Originating State</strong></td>
        <td><strong>Destination City</strong></td>
<td><strong>Destination State</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 $db_table&quot;);
while($r=mysql_fetch_array($result, MYSQL_ASSOC))
{
$id            =    $r['id'];
$loaddate    =    $r['loaddate'];
$origcity    =    $r['origcity'];
$origstate    =    $r['origstate'];
$destcity    =    $r['destcity'];
$deststate    =    $r['deststate'];
print '
<tr>
        <td>' . $id . '</td>
        <td>' . $loaddate . '</td>
        <td>' . $origcity . '</td>
        <td>' . $origstate . '</td>
        <td>' . $destcity . '</td>
        <td>' . $deststate . '</td>
        <td><a href=&quot;mailto:' . $email . '&quot;>' . $email . '</a></td>
        <td><a href=&quot;index.php?todo=delete&deleteid=' . $id . '&quot;>delete</td>
        <td><a href=&quot;index.php?todo=edit&editid=' . $id . '&quot;>edit</td>
</tr>';
}
?>
</table>
<br>
<a href=&quot;#top&quot;>Top of Page</a>
</div>

The records are not being added to the table. Line 21 is: while($r=mysql_fetch_array($result, MYSQL_ASSOC))

-L
 
Try echo'ing your variables. Does db_table, db_host, db_name, db_user and db_pass have the correct values? //Daniel
 
Hi,
That's how I had it before (see 7 posts, 8 from this one, ago). Yes, I just double-checked and all the variables are correct.
-L
 
ok - now, no error, but i'm not adding records. Can add records through telnet/mysql, though.
 
You do know that your file only selects and output records, right? It has no insertion anywhere. //Daniel
 
Yes, this page. But I do have an add.php that theoretically is supposed to be adding records.
 
ok - smarty.. ;)
As I had mentioned before I was using gerrygerry's code from earlier in this thread:
------------
add.php:
------------
<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>Load Date:</td>
        <td><input type=&quot;text&quot; name=&quot;loaddate&quot;></td>
    </tr>
    <tr>
        <td>Originating City:</td>
        <td><input type=&quot;text&quot; name=&quot;origcity&quot;></td>
    </tr>
    <tr>
        <td>Originating State:</td>
        <td><input type=&quot;text&quot; name=&quot;origstate&quot;></td>
    </tr>
    <tr>
        <td>Destination City:</td>
        <td><input type=&quot;text&quot; name=&quot;destcity&quot;></td>
    </tr>
    <tr>
        <td>Destination State:</td>
        <td><input type=&quot;text&quot; name=&quot;deststate&quot;></td>
    </tr>
    <tr>
        <td></td>
        <td><input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;add&quot;></td>
    </tr>
</table>
</form>

----------------
update.php:
----------------
<?
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$result = mysql_query(&quot;UPDATE $db_table SET loaddate=$loaddate,origcity=$origcity,origstate=$origstate,destcity=$destcity,deststate=$deststate WHERE id=$id&quot;);
?>

---------------
edit.php:
---------------
<?
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$result = mysql_query(&quot;SELECT * FROM $db_table 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>Load Date:</td>
        <td><input type=&quot;text&quot; name=&quot;loaddate&quot; value=&quot;<? echo $edit[&quot;loaddate&quot;] ?>&quot;></td>
    </tr>
    <tr>
        <td>Originating City:</td>
        <td><input type=&quot;text&quot; name=&quot;origcity&quot; value=&quot;<? echo $edit[&quot;origcity&quot;] ?>&quot;></td>
    </tr>
    <tr>
        <td>Originating State:</td>
        <td><input type=&quot;text&quot; name=&quot;origstate&quot; value=&quot;<? echo $edit[&quot;origstate&quot;] ?>&quot;></td>
    </tr>
    <tr>
        <td>Destination City:</td>
        <td><input type=&quot;text&quot; name=&quot;destcity&quot; value=&quot;<? echo $edit[&quot;destcity&quot;] ?>&quot;></td>
    </tr>
<tr>
        <td>Destination State:</td>
        <td><input type=&quot;text&quot; name=&quot;deststate&quot; value=&quot;<? echo $edit[&quot;deststate&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>

----------
save.php
-----------
<?
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$result = mysql_query(&quot;INSERT INTO $db_table (id,loaddate,origcity,origstate,destcity,deststate) VALUES ('NULL', '$loaddate', '$origcity', '$origstate', '$destcity', '$deststate')&quot;);
?>

-----------
index.php
-----------
<?
require(&quot;vars.php&quot;); //for database connection vars
switch ($todo) {
case &quot;add&quot;:
//dispaly 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
break;
}
?>


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



link:

As I was pasting the code here, I noticed something else <oops, goofy error> I fixed it. And now - we're adding records! Still getting occasional Warning: Supplied argument is not a valid MySQL result resource errors. Let me go through the pages again with what you and gerrygerry told me about invisible characters and see if that's it. Bear with me....
 
aargg. help. please.
(this is even harder because I'm supposed to be working on two completely different projects, preventing me from concentrating on one of them until it's done...)

The link:
The code is the same as in last post, because even though I have gone through each of the pages, I'm not finding any stray characters. I've deleted and re-typed certain pieces.

I can add records so I know that I'm connecting & that the variables are ok, but when I click on &quot;edit&quot; on index.php, it does not grab the values of the cells and plug them into the form fields for the edit.php pg.

When I hit &quot;edit&quot;, I'm getting : Warning: Supplied argument is not a valid MySQL result resource in /usr/local/etc/httpd/htdocs/prologi/editbox/edit.php on line 5.

Here are the first 6 lines of edit.php:

<?
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$result = mysql_query(&quot;SELECT * FROM $db_table WHERE id=$editid&quot;);
$edit = mysql_fetch_array($result);
?>

Also, the &quot;delete&quot; link isn't working. It looks like it is reading delete.php and refreshing the index.php pg, but it's not deleting the info. It's not giving me an error message.

I'm editing the pages in SimpleText so I know that it's not a GoLive wysiwyg messing with the code thing.

Any ideas?
 
arg. At least the index and list pgs WERE working until I added a stylesheet and broke it somewhere.
 
Now I am back to where I was (same errors) as before the stylesheet. Add records: good, delete & edit : ng.
 
? help ?

still having problems if anyone can help. when I click the &quot;edit&quot; link of the index.php, I'm getting an error that directs me to line 37 (the last line of edit.php) which isn't even php.

The last few lines of edit.php are:
        <td><input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;update&quot;></td>
</tr>
</table>
</form>

and there are no stray characters, nothing...

help?
 
That usually indicates that you have a missing } or something similar. Have you tried putting ;'s after all your echo's (<?=$variable?> works the same way and is better IMO). //Daniel
 
hi daniel.

So you are saying that :
<input type=&quot;text&quot; name=&quot;loaddate&quot; value=&quot;<? echo $edit[&quot;loaddate&quot;] ?>&quot;>

should be:
<input type=&quot;text&quot; name=&quot;loaddate&quot; value=&quot;<? echo $edit[&quot;loaddate&quot;]; ?>&quot;>


-l
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top