need some help to get this code working

Discussion in 'PHP' started by dougvcd, Mar 22, 2008.

  1. #1
    i have a login page which when logged in goes to this edit page which is suppose to let you edit a form which gets data from database but when i run it i get no form
    if any one can help
    cheers
    Doug

    
    <?php include("menu_u.php");
    
    if($_POST['action'] == "edit") {
    
    //This gets all the other information from the form
    $state=$_POST['state']; 
    $type=$_POST['type'];
    $name=$_POST['name'];
    $email=$_POST['email'];
    $date=$_POST['date'];
    $area=$_POST['area'];
    $details=$_POST['details'];
    $pname=$_POST['pname'];
    
    // Connects to your Database 
    
     $dbh=mysql_connect("localhost", "cd", "57") or die('I cannot connect to database because: ' .mysql_error()) ; 
    mysql_select_db("cers"); 
    //Writes the information to the database 
    mysql_query("UPDATE dogs SET state=\"$state\", type=\"$type\", name=\"$name\", email=\"$email\", date=\"$date\", area=\"$area\" , details=\"$details\" WHERE ID =\"$ID\" ");
     
    
    echo "Success! Your information has been Updated. PleaseWait till you are redirected to the homepage.";
    					echo'<meta http-equiv="REFRESH" content="2;url=logout.php">';
    					
     }
    else {
    
      $hm = authenticate($_COOKIE[dog]); 
      $hm2 = mysql_num_rows($hm); 
    
    if ($hm2 > 0) 
    { 
    
    $connection = @mysql_connect("localhost", "cd", "57") or die("Couldn't connect.");
    $db = @mysql_select_db("ers", $connection) or die("Couldn't select database.");
    
    $sql = "SELECT * FROM dogs WHERE ID ='$_COOKIE[dog]'";
    
    $result = @mysql_query($sql,$connection) or die("Couldn't execute profile query.");
    $num=mysql_num_rows($result);
    
    while ($row = mysql_fetch_array($result)) {
    $state=$_POST['state']; 
    $type=$_POST['type'];
    $name=$_POST['name'];
    $email=$_POST['email'];
    $date=$_POST['date'];
    $area=$_POST['area'];
    $details=$_POST['details'];
    $pname=$_POST['pname'];
    }
    
    ?>
    
    <form name="form" onsubmit="return ValidateRequiredFields();" action="<?echo $PHP_SELF ?>" method="POST">
    <div id="info" style="width:360px;">
    
        <div align="left">
               <strong>Edit Your Listing</strong><br>
          <br />
       <strong>Type:</strong>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
       <select name="state" value=<? echo $state ?>>
            <option value="lost">Lost</option>
            <option value="found">Found</option>
            </select> <br>
          <br />
       <select name="type" value=<? echo $type ?>>
            <option value="cat">Cat</option>
            <option value="dog">Dog</option>
            </select> <br>
          <br />
          <input name="name" type="text" value="<? echo $name ?>"> 
          <strong>Your Name:</strong>    <br>
          <br />
          <input type="text" name="email" value=<? echo $email ?>> 
          <strong>Email/Tel:</strong>    <br>
          <br />
          <input name="date" type="text" value=<? echo $date ?>> 
          <strong>Date:</strong>    <br> 
          <br />
          <input type="text" name = "area" value=<? echo $area ?>> 
          <strong>Area: </strong><br>
          <br />
          <strong>Details:</strong><br>      
          <textarea name="details" cols="40" rows="5"> <? echo "$details"; ?> </textarea><br> 
          <br/>
      	 <input type="hidden" name="action" value="edit"></div>
          <input type="submit" value="Edit Details"/> 
        
    </form>
    <? }
    
    else
    {
    echo "You must login to do that. <a href=login.php>Click here</a>";
    }
    
    
    
     } ?>
    PHP:
     
    dougvcd, Mar 22, 2008 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    What exactly do you mean you get no form? Do you mean that no data is being preset in your form inputs which would be their current data?

    while ($row = mysql_fetch_array($result)) {
    $state=$_POST['state']; 
    $type=$_POST['type'];
    $name=$_POST['name'];
    $email=$_POST['email'];
    $date=$_POST['date'];
    $area=$_POST['area'];
    $details=$_POST['details'];
    $pname=$_POST['pname'];
    }
    PHP:
    You wouldn't be grabbing the information from $_POST here, you would have to grab it from $row (though if there's more than one row, your variables will only have the last row's information). Since I assume you're only grabbing one row that will be editable, you wouldn't even really need a while loop.
    $row = mysql_fetch_array($result);
    $state=$row['state']; 
    $type=$row['type'];
    $name=$row['name'];
    $email=$row['email'];
    $date=$row['date'];
    $area=$row['area'];
    $details=$row['details'];
    $pname=$row['pname'];
    PHP:
    (this is the stuff right before your form gets outputted by the way; where your while loop is)

    One more thing.. you should run addslashes() on your POST data before inserting it into your database. You don't want to leave yourself open for injections.
    //This gets all the other information from the form
    $state=addslashes($_POST['state']); 
    $type=addslashes($_POST['type']);
    $name=addslashes($_POST['name']);
    $email=addslashes($_POST['email']);
    $date=addslashes($_POST['date']);
    $area=addslashes($_POST['area']);
    $details=addslashes($_POST['details']);
    $pname=addslashes($_POST['pname']);
    PHP:
     
    zerxer, Mar 22, 2008 IP
  3. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    ok done all that but still no form or data
    cheers
    Doug
    as this is for my use only can i do away with the login details etc and just put a record number in to select the record to amend
     
    dougvcd, Mar 22, 2008 IP
  4. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Oh you meant it's not displaying the form at all. Well, it must have something to do with your two lines of logging in (the $hm variables). I don't know what your authenticate function is but that is probably doing something wrong.
     
    zerxer, Mar 22, 2008 IP
  5. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    ok can somebody look and tell me how to do it with out having to login
    have made changes as suggested
    cheers
    Doug
    <?php include("menu_u.php");
    
    if($_POST['action'] == "edit") {
    
    //This gets all the other information from the form
    $state=addslashes($_POST['state']); 
    $type=addslashes($_POST['type']);
    $name=addslashes($_POST['name']);
    $email=addslashes($_POST['email']);
    $date=addslashes($_POST['date']);
    $area=addslashes($_POST['area']);
    $details=addslashes($_POST['details']);
    $pname=$_POST['pname'];
    
    // Connects to your Database 
    
     $dbh=mysql_connect("localhost", "vcd", "57") or die('I cannot connect to database because: ' .mysql_error()) ; 
    mysql_select_db("ers"); 
    //Writes the information to the database 
    mysql_query("UPDATE dogs SET state=\"$state\", type=\"$type\", name=\"$name\", email=\"$email\", date=\"$date\", area=\"$area\" , details=\"$details\" WHERE ID =\"$ID\" ");
     
    
    echo "Success! Your information has been Updated. PleaseWait till you are redirected to the homepage.";
    					echo'<meta http-equiv="REFRESH" content="2;url=logout.php">';
    					
     }
    else
    { 
    
     $dbh=mysql_connect("localhost", "vcd", "57") or die("Couldn't connect.");
     mysql_select_db("ers") or die("Couldn't select database.");
    
    $sql = "SELECT * FROM dogs WHERE ID ='2'";
    
    $result = $dbh=mysql_query($sql) or die("Couldn't execute profile query.");
    $num=mysql_num_rows($result);
    
    $row = mysql_fetch_array($result);
    $state=$row['state']; 
    $type=$row['type'];
    $name=$row['name'];
    $email=$row['email'];
    $date=$row['date'];
    $area=$row['area'];
    $details=$row['details'];
    $pname=$row['pname'];
    
    
    ?>
    
    <form name="form" onsubmit="return ValidateRequiredFields();" action="<?echo $PHP_SELF ?>" method="POST">
    <div id="info" style="width:360px;">
    
        <div align="left">
               <strong>Edit Your Listing</strong><br>
          <br /> 
       <select name="state" value=<? echo $state ?>>
            <option value="lost">Lost</option>
            <option value="found">Found</option>
            </select> <br>
          <br />
       <select name="type" value=<? echo $type ?>>
            <option value="cat">Cat</option>
            <option value="dog">Dog</option>
            </select> <br>
          <br />
          <input name="name" type="text" value="<? echo $name ?>"> 
          <strong>Your Name:</strong>    <br>
          <br />
          <input type="text" name="email" value=<? echo $email ?>> 
          <strong>Email/Tel:</strong>    <br>
          <br />
          <input name="date" type="text" value=<? echo $date ?>> 
          <strong>Date:</strong>    <br> 
          <br />
          <input type="text" name = "area" value=<? echo $area ?>> 
          <strong>Area: </strong><br>
          <br />
          <strong>Details:</strong><br>      
          <textarea name="details" cols="40" rows="5"> <? echo "$details"; ?> </textarea><br> 
          <br/>
      	 <input type="hidden" name="action" value="edit"></div>
          <input type="submit" value="Edit Details"/> 
        
    </form>
    PHP:
    </body>
    </html>
     
    dougvcd, Mar 22, 2008 IP
  6. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #6
    That should work fine how it is now except you'd still need to put <?php } ?> all the way at the end after your </form> so that it doesn't error saying unexpected $end.

    EDIT: Also, here's some shorthand for you. When you just want to echo one variable or something simple out into a block of HTML, you wouldn't have to type something like <?php echo $name ?>, you could just do <?=$name?> and it'll have the same effect.
     
    zerxer, Mar 22, 2008 IP
  7. dougvcd

    dougvcd Peon

    Messages:
    267
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    no dont get any thing at all now
    thanks any way
    Doug
     
    dougvcd, Mar 22, 2008 IP