...

View Full Version : EDIT a ROW problem .. please help



caymaniac
01-15-2007, 11:59 PM
OK

so I am now at the making an edit page ... i went to start with to edit a row .. i think that is all that is needed.
Unfortunately .. i get the info displayed .. and i can submit it .. but i never get the information actually updated .. can someone take a look and see if they can tell where i have gone wrong ?


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<?
//connect to mysql
// Database information required to connect to database
$host="localhost";
$name = "test";
$pass = "******";
$dbname = "testing";

// Connect to Database and select the database to use
$dbi = mysql_connect($host, $name,$pass) or
die("I cannot connect to the database. Error :" . mysql_error());
mysql_select_db($dbname,$dbi);

//select which database you want to edit
mysql_select_db("iunearth");
// make del an action
if($action=="del")
{
mysql_query("DELETE FROM hotels WHERE id=$id;");
}
//If cmd has not been initialized
if(!isset($cmd))
{
//display all the news
$result = mysql_query("select * from hotels order by id");

//run the while loop that grabs all the news scripts
while($r=mysql_fetch_array($result))
{
//grab the title and the ID of the news
$name=$r["name"];//take out the title
$id=$r["id"];//take out the id

//make the title a link
echo "$name - <a href='edit.php?cmd=edit&id=$id'>Edit</a> - <a href='edit.php?action=del&id=$id'>delete</a>";
echo "<br>";
}
}
?>
<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
if (!isset($_POST["submit"]))
{
$id = $_GET["id"];
$sql = "SELECT * FROM hotels WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>

<form action="edit.php" method="post">
<input type=hidden name="id" value="<?php echo $myrow["id"] ?>">

Hotel:<INPUT TYPE="TEXT" NAME="name" VALUE="<?php echo $myrow["name"] ?>" SIZE=50><br>
Long:<INPUT TYPE="TEXT" NAME="long" VALUE="<? echo $myrow["long"] ?>"SIZE=14><br>
Lat:<INPUT TYPE="TEXT" NAME="lat" VALUE="<?php echo $myrow["lat"] ?>" SIZE=14><br>
City:<INPUT TYPE="TEXT" NAME="city" VALUE="<?php echo $myrow["city"] ?>" SIZE=20><br>
Country:<INPUT TYPE="TEXT" NAME="country" VALUE="<? echo $myrow["country"] ?>"SIZE=20><br>
Wesbite:<INPUT TYPE="TEXT" NAME="website" VALUE="<?php echo $myrow["website"] ?>" SIZE=100> (no http://)<br>
Phone:<INPUT TYPE="TEXT" NAME="phone" VALUE="<? echo $myrow["phone"] ?>"SIZE=20><br>
Rating:<INPUT TYPE="INT" NAME="rating" VALUE="<?php echo $myrow["rating"] ?>" SIZE=5><br>

<input type="hidden" name="cmd" value="edit">

<input type="submit" name="submit" value="submit">

</form>

<? } ?>
<?
if ($_POST["submit"])
{
$id = $_POST["id"];
$name = $_POST["name"];
$long = $_POST["long"];
$lat = $_POST["lat"];
$city = $_POST["city"];
$country = $_POST["country"];
$website = $_POST["website"];
$phone = $_POST["phone"];
$rating = $_POST["rating"];

$sql = "UPDATE hotels SET name='$name',long='$long',lat='$lat', city='$city', country='$country', website='$website', phone='$phone', rating='$rating' WHERE id=$id";
$result = mysql_query($sql);

// Use an IF statement to check and see if $result was true
// and the info was updated.
if($result)
{
echo "Update successful";
}
// use else to execute the code if $result was false
else
{
echo "Update failed, please try again";
}
}
}
?>
<p><a href="output.php">BROWSE ALL</a> :: <a href="edit.php">EDIT / DELETE</a> :: <a href="input.php">ADD NEW</a> </p>
</body>
</html>

thanks in advance

ps started a new thread as the old one regarding similar stuff had a title that was misleading and it looked like my problems were solved.

Fumigator
01-16-2007, 12:04 AM
Add an error check on each and every query. You may think "this query will always work", but it's the little things, always the little things. I recommend during your testing and debugging phase you add a die() when the query doesn't work, to make it very clear when a query fails. And always display the query text and the contents of mysql_error()!

Fumigator
01-16-2007, 12:07 AM
I also just noticed you are doing this:



if ($_POST['submit']) {}


Which isn't really what you want. You want



if (isset($_POST['submit'])) {}


Which is really what you want.

(I don't really know if this is related to your current issue but it will probably eventually become an issue at some point)

caymaniac
01-16-2007, 01:26 AM
I am literally just beginning php learning

I wouldnt know where to start with error checking

is there a simple bit of code i can just add .. and where does it go ?

caymaniac
01-16-2007, 01:36 AM
ok i put some code in to print all errors

i get this
Notice: Undefined variable: action in C:\apachefriends\xampp\htdocs\iunearth\edit.php on line 27

Notice: Undefined index: cmd in C:\apachefriends\xampp\htdocs\iunearth\edit.php on line 51

Notice: Undefined index: cmd in C:\apachefriends\xampp\htdocs\iunearth\edit.php on line 51

Notice: Undefined index: submit in C:\apachefriends\xampp\htdocs\iunearth\edit.php on line 81



Those are all the errors i get through out the trying to edit process

does that help ?

koyama
01-16-2007, 02:35 AM
It seems that some of your variables are empty. Are you relying on register_globals being on?

What Fumigator is saying is that each time you use mysql_query you could write e.g.


$sql = "DELETE FROM hotels WHERE id='$id'";
mysql_query($sql) or die('Bad query :'.$sql);

This is necessary for debugging since php for security reasons usually doesn't show database related warnings. This way you are able to see where things went wrong.

caymaniac
01-16-2007, 02:48 PM
I will have to have a look through .. but wow ... this is a big learning curve

I am not sure I a up for this

guelphdad
01-16-2007, 03:05 PM
Your error is most likely here:

mysql_query("DELETE FROM hotels WHERE id=$id;");

remove the first ; they are not necessary unless you are doing work from the mysql client.

the last one is necessary because it tells php that you are finished on that line.

note too, it pays to do two things when debugging

1) try hard coding a value in place of a variable

2) when you know that is working, substitute the variable in but this time echo out the value of the variable so that you know it is working.

caymaniac
01-21-2007, 09:27 PM
my delete function is working fine though, I can delete entries no problem.

it's the actual updating the information on editing that isn't working.
I have tried a few other things but it still won't edit and I am still at the same stage as above basically. It's proving to be a real headache .. I am sure once I get it I will be ok for other stuff also.

koyama
01-21-2007, 09:51 PM
One of your queries contains a column named 'long'. This is a reserved keyword in MySQL. You must enclose it in backticks: `long`

caymaniac
01-25-2007, 07:14 PM
OK So i sat down and re wrote some code out after having done some reading.

I still don't have any error coding apart from
error_reporting(E_ALL);
ini_set('display_errors', '1');

However I am getting a number of errors from my new page .. that in my mind should not be a problem. anyways ... here is my code if anyone would tell me why I get the following errors:

Notice: Undefined variable: submit in C:\apachefriends\xampp\htdocs\***\go\inc\accomodation.php on line 7
Notice: Undefined variable: delete in C:\apachefriends\xampp\htdocs\***\go\inc\accomodation.php on line 22
Notice: Undefined variable: id in C:\apachefriends\xampp\htdocs\***\go\inc\accomodation.php on line 33
Notice: Undefined variable: PHPSELF in C:\apachefriends\xampp\htdocs\***\go\inc\accomodation.php on line 41
Notice: Undefined variable: PHPSELF in C:\apachefriends\xampp\htdocs\***\go\inc\accomodation.php on line 41
Notice: Undefined variable: PHPSELF in C:\apachefriends\xampp\htdocs\***\go\inc\accomodation.php on line 41
Notice: Undefined variable: id in C:\apachefriends\xampp\htdocs\***\go\inc\accomodation.php on line 54


<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

// the actual page

if ($submit) {

//here if no ID then adding else we're editing

if ($id) {
$sql="UPDATE accomodation SET name='$name',type='$type',lon='$lon',lat='$lat',city='$city',country='$country',website='$website',p hone='$phone',rating='$rating' WHERE id=$id";

} else {
$sql="INSERT INTO accomodation (name,type,lon,lat,city,country,website,phone,rating) VALUES ('$name','$type','$lon','$lat','$city','$country','$website,'$phone,'$rating')";
}

// run SQL against the DB

$result=mysql_query($sql);
echo "The Record for $name has been updated!<p>";
} elseif ($delete) {
//delete record
$sql="DELETE FROM accomodation WHERE id=$id";
$result=mysql_query($sql);

echo "$sql Record deleted!<p>";

} else {

// this part happens if we don't press submit

if (!$id) {

// print the list if there is no editing

$result=mysql_query("SELECT * FROM accomodation");
while ($myrow=mysql_fetch_array($result)) {

printf("<a href=\"%s?id=%s\">%s</a> \n", $PHP_SELF, $myrow["id"], $myrow["name"]);
printf("<a href=\"%s?id=%s&delete=yes\">(DELETE)</a><br>", $PHPSELF, $myrow["id"]);
}
}
?>

<p>
<a href="<?php echo $PHP_SELF ?>">ADD A RECORD</a>
<p>

<form method="post" action="<?php echo $PHP_SELF ?>">

<?php

if ($id) {
// editing so select a record
$sql="SELECT * FROM accomodation WHERE id=$id";
$result=mysql_query($sql);
$id=$myrow["id"];
$name=$myrow["name"];
$type=$myrow["type"];
$lon=$myrow["lon"];
$lat=$myrow["lat"];
$city=$myrow["city"];
$coutnry=$myrow["country"];
$website=$myrow["website"];
$phone=$myrow["phone"];
$rating=$myrow["rating"];
//print the id for editing
?>
<input type=hidden name="id" value="<?php echo $id ?>">
<?php
}
?>
Name: <input type"Text" name="name" value="<?php echo $name ?>"><br/>
Type: <input type"Text" name="type" value="<?php echo $type ?>"><br/>
Longitude: <input type"Text" name="lon" value="<?php echo $lon ?>"><br/>
Latitude: <input type"Text" name="lat" value="<?php echo $lat ?>"><br/>
City: <input type"Text" name="city" value="<?php echo $city ?>"><br/>
Country: <input type"text" name="country" value="<?php echo $country ?>"><br/>
Website: <input type"Text" name="website" value="<?php echo $website ?>"><br/>
Phone: <input type"Text" name="phone" value="<?php echo $phone ?>"><br/>
Rating: <input type"Text" name="rating" value="<?php echo $rating ?>"><br/>
<input type="Submit" name"submit" value="Enter Information">
</form>
<?php
}
?>


ps i connect to the database through including other files in one main wile

Fumigator
01-26-2007, 01:45 AM
I apologize for not being more explanatory with my suggestion. Here's what you need to do every time you call mysql_query():



$result=mysql_query($sql);
if (!$result) {
die("SQL ERROR!<br />Query text: $sql<br />Error text:".mysql_error());
else {
echo "The Record for $name has been updated!<p>";
}


I count three calls to mysql_query() in the code you posted-- you need to do this all three times. You will then start getting what we call helpful error messages! :thumbsup:

caymaniac
01-26-2007, 08:55 PM
Ok so i put the error checking in every query area .. and now i get this :

Parse error: syntax error, unexpected T_ELSE in /home/iunearth/public_html/gotest/inc/accomodation.php on line 29

it looks fine to me


<?
include 'dbconfig.inc';
include 'dbopen.inc';
//select databse
mysql_select_db("***_test",$db);
?>
<?php

if ($submit) {

// here if no ID then adding else we're editing

if ($id) {

$sql = "UPDATE accomodation SET name='$name',type='$type',lon='$lon',lat='$lat',city='$city',country='$country',website='$website',p hone='$phone',rating='$rating' WHERE id=$id";

} else {

$sql = "INSERT INTO accomodation (name,type,lon,lat,city,country,website,phone,rating) VALUES ('$name','$type','$lon','$lat','$city','$country','$website,'$phone,'$rating')";

}

// run SQL against the DB

$result=mysql_query($sql);

if (!$result) {
die("SQL ERROR!<br />Query text: $sql<br />Error text:".mysql_error());
else {

echo "The Record for $name has been updated!<p>";

}
elseif ($delete) {

// delete a record

$sql = "DELETE FROM accomodation WHERE id=$id";

$result=mysql_query($sql);

if (!$result) {
die("SQL ERROR!<br />Query text: $sql<br />Error text:".mysql_error());
else {

echo "$sql Entry deleted!<p>";

} else {

// this part happens if we don't press submit

if (!$id) {

// print the list if there is not editing

$result = mysql_query("SELECT * FROM accomodation",$db);

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

printf("<a href=\"%s?id=%s\">%s</a> \n", $PHP_SELF, $myrow["id"], $myrow["name"]);

printf("<a href=\"%s?id=%s&delete=yes\">(DELETE)</a><br>", $PHP_SELF, $myrow["id"]);

}

}



?>

<P>

<a href="<?php echo $PHP_SELF?>">ADD AN ACCOMODATION</a>

<P>

<form method="post" action="<?php echo $PHP_SELF?>">

<?php



if ($id) {

// editing so select a record

$sql = "SELECT * FROM accomodation WHERE id=$id";

$result=mysql_query($sql);
if (!$result) {
die("SQL ERROR!<br />Query text: $sql<br />Error text:".mysql_error());
else {

$myrow = mysql_fetch_array($result);

$id = $myrow["id"];
$name=$myrow["name"];
$type=$myrow["type"];
$lon=$myrow["lon"];
$lat=$myrow["lat"];
$city=$myrow["city"];
$country=$myrow["country"];
$website=$myrow["website"];
$phone=$myrow["phone"];
$rating=$myrow["rating"];
}
// print the id for editing



?>

<input type=hidden name="id" value="<?php echo $id ?>">

<?php

}



?>

Name: <input type"Text" name="name" value="<?php echo $name ?>"><br/>
Type: <input type"Text" name="type" value="<?php echo $type ?>"><br/>
Longitude: <input type"Text" name="lon" value="<?php echo $lon ?>"><br/>
Latitude: <input type"Text" name="lat" value="<?php echo $lat ?>"><br/>
City: <input type"Text" name="city" value="<?php echo $city ?>"><br/>
Country: <input type"text" name="country" value="<?php echo $country ?>"><br/>
Website: <input type"Text" name="website" value="<?php echo $website ?>"><br/>
Phone: <input type"Text" name="phone" value="<?php echo $phone ?>"><br/>
Rating: <input type"Text" name="rating" value="<?php echo $rating ?>"><br/>

<input type="Submit" name="submit" value="Enter information">

</form>



<?php



}



?>

Fumigator
01-26-2007, 09:53 PM
You're missing a right squiggly bracket } on that "if" statement.

caymaniac
01-26-2007, 11:53 PM
there is no 'if' statement on line 29 ?

sorry i may seem dumb .. but i tried a few combinations and i get errors on them all.

koyama
01-27-2007, 12:26 AM
don't always count on that the actual error is on the line number that the parser gives you. It tells you were it chocked--this may even be several lines after the actual error.

if (!$result) {
die("SQL ERROR!<br />Query text: $sql<br />Error text:".mysql_error());
}
else {
echo "The Record for $name has been updated!<p>";
}

caymaniac
01-27-2007, 06:32 AM
Thank you very much Koyama and Fumigator ...

I got the error coding put on all 3 of my queries ... I now understand what it is where it goes .. and how it works ... and how amazing and usefull it it ..

the moment i had it in place it pushed out my errors for me 1 by one until i had corrected the fact that i had 2 ' missing ..!!!! amazing ...

Thanks so much. :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum