Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7430177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:05:50+00:00 2026-05-29T09:05:50+00:00

I have 3 pages: search.html. search.php, and edit.php I also have new.php which I

  • 0

I have 3 pages: search.html. search.php, and edit.php

I also have new.php which I can use to create an entry into my MySQL database, and it works perfectly, I can view the data and it’s where it’s supposed to be. Then, I have search.html and search.php which I use to do a very basic search of the database, and it again works perfectly.

Then, in the results (on search.php) I have an edit link that launches edit.php with the data beside the returned results, and it loads edit.php along with the correct data. So I think I’m ok up to this point. But then, when I edit my form and click submit, it overwrites every entry in the database, instead of just the entry that I initially tried to edit. Been messing with this for about an hour now and not sure where I’ve messed up. Code below:

Search.html

<html>
<head>
<title>Search the Database</title>
</head>

<body>
<form action="search.php" method="post">
  Search:
  <input type="text" name="term" />
  <br />
  <input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>

Search.php

<?php
mysql_connect ("myhost", "myuser","mypass")  or die (mysql_error());
mysql_select_db ("mydb");

$term = $_POST['term'];

$sql = mysql_query("select * from mytable where fname like '%$term%'");

while ($row = mysql_fetch_array($sql)){
    echo '<br/> First Name: '.$row['fname'];
    echo '<br/> Last Name: '.$row['lname'];
    echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
    echo '<br/><br/>';
    }

?>

Edit.php

<?php
function renderForm($id, $fname, $lname)
 {
 ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="mystyle.css">
</head>
<body>
<div id="stylized" class="myform">
<form id="form" name="form" action="" method="post">
  <input type="hidden" name="id" value="<?php echo $id; ?>"/>
  <h1>Edit Entry</h1>
  <Label>First Name/Organization </label>
  <input type="text" name="fname" value="<?php echo $fname; ?>"/>
  <Label>Attn. </label>
  <input type="text" name="attn" value="<?php echo $attn; ?>"/>
  <input type="submit" name="submit" value="Submit">
  </div>
</form>
</body>
</html>
<?php
 }

 include('settings.php');

 if (isset($_POST['submit']))
 { 
 if (is_numeric($_POST['id']))
 {
 $id = $_POST['id'];
 $fname = mysql_real_escape_string(htmlspecialchars($_POST['fname']));
 $lname = mysql_real_escape_string(htmlspecialchars($_POST['lname']));

 if ($fname == '' || $lname == '')
 {

 $error = 'ERROR: Please fill in all required fields!';

 renderForm($id, $fname, $lname);
 }
 else
 {

 mysql_query("UPDATE entries SET fname='$fname', lname='$lname'")
 or die(mysql_error()); 

 header("Location: index.html"); 
 }
 }
 else
 {
 echo 'Error!';
 }
 }
 else
 {

 if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
 {
 $id = $_GET['id'];
 $result = mysql_query("SELECT * FROM entries WHERE id=$id")
 or die(mysql_error()); 
 $row = mysql_fetch_array($result);

 if($row)
 {

 $fname = $row['fname'];
 $lname = $row['lname'];

 renderForm($id, $fname, $lname, '');
 }
 else

 {
 echo "No results!";
 }
 }
 else

 {
 echo 'Error!';
 }
 }
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:05:51+00:00Added an answer on May 29, 2026 at 9:05 am

    Mistake

    you have mysql_query("UPDATE entries SET fname='$fname', lname='$lname'")

    What it means is you want to set new first and last name for all entries….

    Learning

    Whenever we update we provide condition for which id you want to change…

    so your query should be

    mysql_query("UPDATE entries SET fname='$fname', lname='$lname' WHERE id=$id")

    For UPDATE it should be always

    UPDATE myTable
    SET field1=new_value_1, field2=new_value_2, .....
    WHERE condition
    

    Good Luck

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a search form on each of my pages. If I use form
We have a page of search results which the user can hit in several
I have three simple html or php file that I'm going to create. What
We are converting all the static html pages in our codebase into php pages.
I am working on creating a PHP script to convert my HTML/CSS/PHP pages into
what I have: a google search form (displayed on all pages) a wordpress page
I have a page of search results, each of which has an icon to
EDIT: Mangling is fixed - primary issue appears to be the php/mysql connection In
I use a index.php to control the access of pages. Here is the mechanism:
Scenario: I have a php page in which I call a python script. Python

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.