0

I'm trying to make a form that will allow a user to edit records in the database.

My main.php is a table where the user can click on a record to edit / delete:

    echo "<td>".$row["fname"].", ".$row["first_name"]."</center></td>";
    echo "<td><center>".$row["gender"]."</center></td>";
    echo "<td><center>".$row["city"]."</center></td>";
    echo "<td><center>".$row["extra"]."</center></td>";
    echo '<td><center><a href="form.php?id='.$row["p_ID"].'"><img src="edit_icon.png"/></a><center></td>';
    echo '<td><center><a href="delete.php?id='.$row["p_ID"].'"><img src="delete.gif"/></a><center></td>';
    echo "</tr>";
    }
?>

<input type="hidden" name="p_ID" value="<?php echo $row["p_ID"]?>"></input>
<input type="hidden" name="lname" value="<?php echo $row["lname"]?>"></input>
etc..


When the user clicks the edit icon, it redirects to the form page where I need the values ($row['fname']) to show up in their respective fields. I've tried suggested solutions but I still don't know how to accomplish this correctly. I keep getting errors. This is what I've tried with my form.php

@$submit=$_POST['submit'];
@$lname=$_GET['lname'];
@$gender=$_GET['gender'];
@$city=$_GET['city'];
@$extra=$_GET['extra'];
?>

Last name <input type="text" name="lname" value= <?php echo $lname ?>><br><br>
Gender <input type="radio" name="gender" value="M" <?php if($gender=="M") {echo "checked";} else {echo " ";} ?>/>M&nbsp;
<input type="radio" name="gender" value="F" <?php if($gender=="F") {echo "checked";} else {echo " ";} ?>/>F<br><br>
City <select name="city">
    <option value="x">Select</option>
    <?php
    $db=mysql_connect("localhost","root") or die('Not connected : ' . mysql_error());
    mysql_select_db("my_db",$db) or die (mysql_error());
    $SQL="SELECT * FROM cities"; 
    $result=mysql_query($SQL) or die(mysql_error());
    $num_results=mysql_num_rows($result);
    mysql_close($db);
    for ($i=0;$i<$num_results;$i++)
        {
        $row=mysql_fetch_array($result);
        echo"<option value='".$row['city_id']."'", $row['city_id']==$row['city_ID']? " selected='selected'" : '',">".$row['city']."</option>";
        }
    ?>
    </select><br><br>

Extra <input type="checkbox" name="extra" value="yes" <?php if($extra=="yes") {echo "checked";} else {echo " ";} ?>/><br><br>


And I'm not really concerned with any SQL injections or anything right now, I just need this working. I'd be grateful for any help!

Errors: Undefined index for everything

10
  • 1
    I just need this working famous last words :) Anyway, you should quote the exact error(s) you are getting. Commented Jun 16, 2012 at 23:32
  • You shouldnt close the db... especially not before you've looped through the result set... But we need the error messages you are getting... Also show the code where you fetch the values from $_GET and put them in local varibales like $lname. Commented Jun 16, 2012 at 23:37
  • You don't seem to be closing off your <?php?> tags before you start writing some html. Commented Jun 16, 2012 at 23:46
  • @keto23 Oh sorry, I do close them it's just that I tried to shorten my sample codes on here so as to not clutter. I'll fix that right now Commented Jun 16, 2012 at 23:48
  • @Pekka I get undefined index errors Commented Jun 16, 2012 at 23:49

2 Answers 2

1

You are closing the connection before mysql_query is used in mysql_fetch_array.

You are trying to fetch the rows incorrectly.

mysql_close isn't needed unless you have a lot of processing after the query because the connection is closed after the script has finished running anyway.

Also, declare your variables like this to remove the undefined index errors:

$submit = isset($_POST['submit']) ? mysql_real_escape_string($_POST['submit']) : "";

Note: this also protects against sql injection but mysql_real_escape_string() will only work after you connect.

<?php

$db=mysql_connect("localhost","root") or die('Not connected : ' . mysql_error());
mysql_select_db("my_db",$db) or die (mysql_error());

$submit = isset($_POST['submit']) ? mysql_real_escape_string($_POST['submit']) : "";
//other vars here

$SQL="SELECT * FROM cities"; 
$result=mysql_query($SQL) or die(mysql_error());
while($row = mysql_fetch_array($result)){
  echo "<option value='".$row['city_id']."'>".$row['city']."</option>";
}
mysql_close(); // now you can close the connection
?>
Sign up to request clarification or add additional context in comments.

2 Comments

Good thought. But SQL injection protection isn't required here because none of those variables touch the database in the query the OP has right now.
Thank you! This got rid of my undefined index errors but my form still doesn't record default values from the database
0

What you need is form submission check. Here is the general form of a single-page update script:

<?php
if(isset($_POST['submit'])) {
    // Form was submitted, process it and display message
    exit(); // Prevent form from being displayed again
}
?>
<!doctype html>
<html>
<body>
   <form method="POST" action="form.php">
       <!-- fields -->
       <input type="submit" name="submit" value="Submit" />
   </form>
</body>
</html>

1 Comment

Thank you for your time! But I'm a beginner and it confuses me to use only one form. I need the forms, process and main separated :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.