arrrgghhh - php & mysql not playing nice together..!
Posted: Fri Oct 04, 2002 11:14 am
Hi guys - I could use some help.
Here's the situation: I've got 10 php pages:
add.php, delete.php, edit.php, footer.php (just html footer), header.php (just html head), index.php (as a default will just list the entries of the db on server), list.php, save.php, update.php, and vars.php. (holds the variables for the database connection, & name, & table name).
The pages' name pretty much indicates its function.
Here's the link : http://www.mc2ads.com/prologi/editbox/index.php
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 "edit" 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 "edit", 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("SELECT * FROM $db_table WHERE id=$editid");
$edit = mysql_fetch_array($result);
?>
Also, the "delete" 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?
I'll post all of the code for all of the pages and maybe someone else can see what I'm just not getting because I'm about to go completely postal.
----------------------------------------------------------------------------------
add.php
----------------------------------------------------------------------------------
<form method="post" action="index.php">
<input type="hidden" name="todo" value="save">
<table>
<tr>
<td>Load Date:</td>
<td><input type="text" name="loaddate"></td>
</tr>
<tr>
<td>Originating City:</td>
<td><input type="text" name="origcity"></td>
</tr>
<tr>
<td>Originating State:</td>
<td><input type="text" name="origstate"></td>
</tr>
<tr>
<td>Destination City:</td>
<td><input type="text" name="destcity"></td>
</tr>
<tr>
<td>Destination State:</td>
<td><input type="text" name="deststate"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="add"></td>
</tr>
</table>
</form>
----------------------------------------------------------------------------------
delete.php
----------------------------------------------------------------------------------
<?
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$result = mysql_query("DELETE FROM $db_table WHERE id=$deleteid");
?>
----------------------------------------------------------------------------------
edit.php
----------------------------------------------------------------------------------
<?
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$result = mysql_query("SELECT * FROM $db_table WHERE id=$editid");
$edit = mysql_fetch_array($result);
?>
<form method="post" action="index.php">
<input type="hidden" name="todo" value="update">
<input type="hidden" name="id" value="<? echo $edit["id"] ?>">
<table>
<tr>
<td>Load Date:</td>
<td><input type="text" name="loaddate" value="<? print $edit["loaddate"] ?>"></td>
</tr>
<tr>
<td>Originating City:</td>
<td><input type="text" name="origcity" value="<? print $edit["origcity"] ?>"></td>
</tr>
<tr>
<td>Originating State:</td>
<td><input type="text" name="origstate" value="<? print $edit["origstate"] ?>"></td>
</tr>
<tr>
<td>Destination City:</td>
<td><input type="text" name="destcity" value="<? print $edit["destcity"] ?>"></td>
</tr>
<tr>
<td>Destination State:</td>
<td><input type="text" name="deststate" value="<? print $edit["deststate"] ?>"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="update"></td>
</tr>
</table>
</form>
----------------------------------------------------------------------------------
footer.php
----------------------------------------------------------------------------------
</BODY>
</HTML>
----------------------------------------------------------------------------------
header.php
----------------------------------------------------------------------------------
<HTML>
<HEAD>
<TITLE>
<?
echo "$title";
?>
</TITLE>
</HEAD>
<BODY>
----------------------------------------------------------------------------------
index.php
----------------------------------------------------------------------------------
<?
require("vars.php"); //for database connection vars
switch ($todo) {
case "add":
//dispaly a blank form
$title="Add new entry"; //used by header
require("header.php"); // your page header
require("add.php"); // submits to index.php?todo=save
require("footer.php"); // your page footer
break;
case "edit":
// pull the data from db and plugs into form
$title = "Edit Existing Entry"; // used by header
require("header.php"); // your page header
require("edit.php"); // edits $editid, submits to index.php?todo=update
require("footer.php"); // your page footer
break;
case "delete":
require("delete.php"); // deletes $deleteid from the db
header("Location: index.php"); // go to listing
break;
case "save":
require("save.php"); // add new entry to db
header("Location: index.php"); // go to listing
break;
case "update":
require("update.php"); // update the db where id=$id
header("Location: index.php"); // go to listing
break;
default:
$title = "Viewing All Entries"; // used by header
require("header.php"); // your page header
require("list.php"); //show all entries
require("footer.php"); // your page footer
break;
}
?>
----------------------------------------------------------------------------------
list.php
----------------------------------------------------------------------------------
<link href="style.css" rel="stylesheet" media="screen">
<div align="center">
<span class="headers">Test Database Template</span>
<p>
<span class="bodystyle"><a href="index.php?todo=add">Add a Record</a></span><br>
<br>
</p>
<table width="650" cellspacing="2" border="0" cellpadding="1">
<tr bgcolor="#DADADA" align="center">
<td>ID</td>
<td>Load Date</td>
<td>Originating City</td>
<td>Originating State</td>
<td>Destination City</td>
<td>Destination State</td>
<td>Delete</td>
<td>Edit</td>
</tr>
<?php
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$result = mysql_query("select * from $db_table");
while($r=mysql_fetch_array($result))
{
$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="mailto:' . $email . '">' . $email . '</a></td>
<td><a href="index.php?todo=delete&deleteid=' . $id . '">delete</td>
<td><a href="index.php?todo=edit&editid=' . $id . '">edit</td>
</tr>';
}
?>
</table>
<br>
<span class="bodystyle"> <a href="#top">Top of Page</a> </span>
</div>
----------------------------------------------------------------------------------
save.php
----------------------------------------------------------------------------------
<?
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$result = mysql_query("INSERT INTO $db_table (id,loaddate,origcity,origstate,destcity,deststate) VALUES ('NULL', '$loaddate', '$origcity', '$origstate', '$destcity', '$deststate')");
?>
----------------------------------------------------------------------------------
update.php
----------------------------------------------------------------------------------
<?
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$result = mysql_query("UPDATE $db_table SET loaddate=$loaddate,origcity=$origcity,origstate=$origstate,destcity=$destcity,deststate=$deststate WHERE id=$id");
?>
----------------------------------------------------------------------------------
vars.php
----------------------------------------------------------------------------------
This page just holds the actual variables, which I can't post for obvious security reasons but we have already established that all of these are correct and working because we can connect and add records using the variable names.
I hope I haven't forgotten anything...oh yeah - HELP!
Here's the situation: I've got 10 php pages:
add.php, delete.php, edit.php, footer.php (just html footer), header.php (just html head), index.php (as a default will just list the entries of the db on server), list.php, save.php, update.php, and vars.php. (holds the variables for the database connection, & name, & table name).
The pages' name pretty much indicates its function.
Here's the link : http://www.mc2ads.com/prologi/editbox/index.php
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 "edit" 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 "edit", 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("SELECT * FROM $db_table WHERE id=$editid");
$edit = mysql_fetch_array($result);
?>
Also, the "delete" 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?
I'll post all of the code for all of the pages and maybe someone else can see what I'm just not getting because I'm about to go completely postal.
----------------------------------------------------------------------------------
add.php
----------------------------------------------------------------------------------
<form method="post" action="index.php">
<input type="hidden" name="todo" value="save">
<table>
<tr>
<td>Load Date:</td>
<td><input type="text" name="loaddate"></td>
</tr>
<tr>
<td>Originating City:</td>
<td><input type="text" name="origcity"></td>
</tr>
<tr>
<td>Originating State:</td>
<td><input type="text" name="origstate"></td>
</tr>
<tr>
<td>Destination City:</td>
<td><input type="text" name="destcity"></td>
</tr>
<tr>
<td>Destination State:</td>
<td><input type="text" name="deststate"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="add"></td>
</tr>
</table>
</form>
----------------------------------------------------------------------------------
delete.php
----------------------------------------------------------------------------------
<?
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$result = mysql_query("DELETE FROM $db_table WHERE id=$deleteid");
?>
----------------------------------------------------------------------------------
edit.php
----------------------------------------------------------------------------------
<?
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$result = mysql_query("SELECT * FROM $db_table WHERE id=$editid");
$edit = mysql_fetch_array($result);
?>
<form method="post" action="index.php">
<input type="hidden" name="todo" value="update">
<input type="hidden" name="id" value="<? echo $edit["id"] ?>">
<table>
<tr>
<td>Load Date:</td>
<td><input type="text" name="loaddate" value="<? print $edit["loaddate"] ?>"></td>
</tr>
<tr>
<td>Originating City:</td>
<td><input type="text" name="origcity" value="<? print $edit["origcity"] ?>"></td>
</tr>
<tr>
<td>Originating State:</td>
<td><input type="text" name="origstate" value="<? print $edit["origstate"] ?>"></td>
</tr>
<tr>
<td>Destination City:</td>
<td><input type="text" name="destcity" value="<? print $edit["destcity"] ?>"></td>
</tr>
<tr>
<td>Destination State:</td>
<td><input type="text" name="deststate" value="<? print $edit["deststate"] ?>"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" name="submit" value="update"></td>
</tr>
</table>
</form>
----------------------------------------------------------------------------------
footer.php
----------------------------------------------------------------------------------
</BODY>
</HTML>
----------------------------------------------------------------------------------
header.php
----------------------------------------------------------------------------------
<HTML>
<HEAD>
<TITLE>
<?
echo "$title";
?>
</TITLE>
</HEAD>
<BODY>
----------------------------------------------------------------------------------
index.php
----------------------------------------------------------------------------------
<?
require("vars.php"); //for database connection vars
switch ($todo) {
case "add":
//dispaly a blank form
$title="Add new entry"; //used by header
require("header.php"); // your page header
require("add.php"); // submits to index.php?todo=save
require("footer.php"); // your page footer
break;
case "edit":
// pull the data from db and plugs into form
$title = "Edit Existing Entry"; // used by header
require("header.php"); // your page header
require("edit.php"); // edits $editid, submits to index.php?todo=update
require("footer.php"); // your page footer
break;
case "delete":
require("delete.php"); // deletes $deleteid from the db
header("Location: index.php"); // go to listing
break;
case "save":
require("save.php"); // add new entry to db
header("Location: index.php"); // go to listing
break;
case "update":
require("update.php"); // update the db where id=$id
header("Location: index.php"); // go to listing
break;
default:
$title = "Viewing All Entries"; // used by header
require("header.php"); // your page header
require("list.php"); //show all entries
require("footer.php"); // your page footer
break;
}
?>
----------------------------------------------------------------------------------
list.php
----------------------------------------------------------------------------------
<link href="style.css" rel="stylesheet" media="screen">
<div align="center">
<span class="headers">Test Database Template</span>
<p>
<span class="bodystyle"><a href="index.php?todo=add">Add a Record</a></span><br>
<br>
</p>
<table width="650" cellspacing="2" border="0" cellpadding="1">
<tr bgcolor="#DADADA" align="center">
<td>ID</td>
<td>Load Date</td>
<td>Originating City</td>
<td>Originating State</td>
<td>Destination City</td>
<td>Destination State</td>
<td>Delete</td>
<td>Edit</td>
</tr>
<?php
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$result = mysql_query("select * from $db_table");
while($r=mysql_fetch_array($result))
{
$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="mailto:' . $email . '">' . $email . '</a></td>
<td><a href="index.php?todo=delete&deleteid=' . $id . '">delete</td>
<td><a href="index.php?todo=edit&editid=' . $id . '">edit</td>
</tr>';
}
?>
</table>
<br>
<span class="bodystyle"> <a href="#top">Top of Page</a> </span>
</div>
----------------------------------------------------------------------------------
save.php
----------------------------------------------------------------------------------
<?
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$result = mysql_query("INSERT INTO $db_table (id,loaddate,origcity,origstate,destcity,deststate) VALUES ('NULL', '$loaddate', '$origcity', '$origstate', '$destcity', '$deststate')");
?>
----------------------------------------------------------------------------------
update.php
----------------------------------------------------------------------------------
<?
mysql_connect($db_host, $db_user, $db_pass);
mysql_select_db($db_name);
$result = mysql_query("UPDATE $db_table SET loaddate=$loaddate,origcity=$origcity,origstate=$origstate,destcity=$destcity,deststate=$deststate WHERE id=$id");
?>
----------------------------------------------------------------------------------
vars.php
----------------------------------------------------------------------------------
This page just holds the actual variables, which I can't post for obvious security reasons but we have already established that all of these are correct and working because we can connect and add records using the variable names.
I hope I haven't forgotten anything...oh yeah - HELP!