Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Dec 2006
    Posts
    36
    Thanks
    1
    Thanked 1 Time in 1 Post

    Question EDIT a ROW problem .. please help

    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 ?
    Code:
    <!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.

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    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()!

  • #3
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I also just noticed you are doing this:

    PHP Code:
    if ($_POST['submit']) {} 
    Which isn't really what you want. You want

    PHP Code:
    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)

  • #4
    New Coder
    Join Date
    Dec 2006
    Posts
    36
    Thanks
    1
    Thanked 1 Time in 1 Post
    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 ?

  • #5
    New Coder
    Join Date
    Dec 2006
    Posts
    36
    Thanks
    1
    Thanked 1 Time in 1 Post
    ok i put some code in to print all errors

    i get this
    Code:
    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 ?

  • #6
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    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.
    PHP Code:
    $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.

  • #7
    New Coder
    Join Date
    Dec 2006
    Posts
    36
    Thanks
    1
    Thanked 1 Time in 1 Post
    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

  • #8
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,634
    Thanks
    4
    Thanked 148 Times in 139 Posts
    Your error is most likely here:
    Code:
    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.

  • #9
    New Coder
    Join Date
    Dec 2006
    Posts
    36
    Thanks
    1
    Thanked 1 Time in 1 Post
    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.

  • #10
    Senior Coder koyama's Avatar
    Join Date
    Dec 2006
    Location
    Copenhagen, Denmark
    Posts
    1,246
    Thanks
    1
    Thanked 5 Times in 5 Posts
    One of your queries contains a column named 'long'. This is a reserved keyword in MySQL. You must enclose it in backticks: `long`

  • #11
    New Coder
    Join Date
    Dec 2006
    Posts
    36
    Thanks
    1
    Thanked 1 Time in 1 Post

    Question New Start

    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
    Code:
    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
    Code:
    <?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',phone='$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
    Last edited by caymaniac; 01-26-2007 at 07:50 PM.

  • #12
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I apologize for not being more explanatory with my suggestion. Here's what you need to do every time you call mysql_query():

    PHP Code:
    $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!

  • #13
    New Coder
    Join Date
    Dec 2006
    Posts
    36
    Thanks
    1
    Thanked 1 Time in 1 Post

    Question added the erros now i get an error !

    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

    Code:
    <?
    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',phone='$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
    
    
    
    }
    
    
    
    ?>

  • #14
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    You're missing a right squiggly bracket } on that "if" statement.

  • #15
    New Coder
    Join Date
    Dec 2006
    Posts
    36
    Thanks
    1
    Thanked 1 Time in 1 Post
    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.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •