Click to See Complete Forum and Search --> : Edit MySQL with PHP


monarch_684
08-15-2007, 12:27 PM
I have no clue why this is not working so can some see if they spot a problem.


$id = $_GET["id"];

$connection = mysql_connect("localhost", "xxx", "xxx");
@mysql_select_db("xxx", $connection) or die( "Unable to select database" . mysql_error());

$query = "SELECT * FROM rates WHERE id = '{$id}'";
$result = mysql_query($query)
or die ("Couldn't query data" . mysql_error());

echo "<form method='post' action='editsubmit.php?id=".$row['id']."'>\n";
echo "<table>\n";

while ($row = mysql_fetch_array($result)) {
echo "<tr align='center'>\n";
echo "<td>", $row['id'], "</td>";
echo "<td>", $row['name'], "</td>\n";
echo "<td><input type='text' value='", $row['d_e'], "' size='10' name='d' /></td>\n";
echo "<td><input type='text' value='", $row['c'], "' size='10' name='c' /></td>\n";
echo "<td><input type='text' value='", $row['b'], "'size='10' name='b' /></td>\n";
echo "<td><input type='text' value='", $row['a'], "' size='10' name='a' /></td>\n";
echo "</tr>\n";
}

echo "</table>\n";
echo "<p><input type='submit' value='Update' /></p>";
echo "</form>";
mysql_close();
?>


The
echo "<form method='post' action='editsubmit.php?id=".$row['id']."'>\n";

does not place the id in the url. Is there something that I might of missed.

Webjedikungfu
08-15-2007, 12:57 PM
Trade your two connection lines with these. Maybe I'm way off, but your connecting method looks strange. If I'm wrong just yell at me.


mysql_connect("localhost", "xxx", "xxx") or die ("didn't connect to mysql");
mysql_select_db("xxx") or die ("no database");
Change your line to this:
$query = "SELECT * FROM rates WHERE id =$id";

OK I see a bunch wrong with it, we'll fix you up.

monarch_684
08-15-2007, 01:33 PM
Made the changes and the same result. Everything does correctly except the action on the form where it calls for the id. For some reason it is not getting the id.

Webjedikungfu
08-15-2007, 01:46 PM
OK, I just attempted to clean it up and correct syntax. I have not tested it plus I'm not yet sure what it's doing in relation to other scripts.

Try this (just place your database info back where your xxx's are)
<?php

$id = $_GET["id"];

mysql_connect("localhost", "xxx", "xxx") or die ("didn't connect to mysql");
mysql_select_db("xxx") or die ("no database");

echo "<form method=\"post\" action=\"editsubmit.php?id=$id\">";
echo "<table>\n";

$result = mysql_query("SELECT * FROM rates WHERE id ='$id'");

while($row=mysql_fetch_array($result)) {


$id = $row["id"];
$name = $row["name"];
$d_e = $row["d_e"];
$c = $row["c"];
$b = $row["b"];
$a = $row["a"];


echo "<tr align=\"center\">";
echo "<td>$id </td>";
echo "<td> $name</td>";
echo "<td><input type=\"text\" value=\"$d_e\" size=\"10\" name=\"d\" /></td>";
echo "<td><input type=\"text\" value=\"$c \" size=\"10\" name=\"c\" /></td>";
echo "<td><input type=\"text\" value=\"$b\"size=\"10\" name=\"b\" /></td>";
echo "<td><input type=\"text\" value=\"$a\" size=\"10\" name=\"a\" /></td>";
echo "</tr>";
}// close While

echo "</table>";
echo "<p><input type=\"submit\" value=\"Update\" /></p>";
echo "</form>";
mysql_close();
?>

Webjedikungfu
08-15-2007, 02:50 PM
Are you trying to update data in a database? I think you are.

If so this script is way off.

Regardless this snippet placed in the very top of your script will tell you if the id is being passed. If that is not happening nothing else matters in the script.

if (!isset($_GET["id"])) {

echo "The id is not coming into the script at all";
exit();
}


What is this scripts job/purpose?

monarch_684
08-15-2007, 02:55 PM
Thanks a bunch. That did it. But what do mean that this script is way off?

monarch_684
08-15-2007, 03:03 PM
Sorry for the question the last post my pc seems to be displaying pages weird. This is for a database and it will get updated 4 to 5 times a year.

Webjedikungfu
08-15-2007, 03:03 PM
I'm sorry I'm just confused sbout what the scripts purpose is? Do tell.

Is it just to display certain user info from the database? Nothing more?

monarch_684
08-15-2007, 03:12 PM
I am using this to display interest rates for a credit union website so that they can go in a easily up there rates without have to mess with HTML all the time, plus these rates will be displayed in certain areas of the website as well. Make sense

Webjedikungfu
08-15-2007, 03:26 PM
Yes so you need this script to actually update the database. And then that new data gets displayed on the site somewhere right?

This script will only show data, never update it. If you want to update the database with this script you must add a few things.

monarch_684
08-15-2007, 03:31 PM
Right, I already have an update script that works but the script that was giving me the problem was not passing the id so that the update script would not work.

Webjedikungfu
08-15-2007, 03:34 PM
Oh gotcha. I get it