Jump to content

I need assistance adding a link to a table to edit a row. please help.


lostdata

Recommended Posts

Hello, I'm just starting to learning how to use PHP and MYSQL and I'm having an issue that has halted my progress entirly on the project I am working on. I can not for the life of me find a tutioral or guide that simply tell you how to add an "edit" link that will allow you to open a record that is displayed in a table so that it can be updated. I'll just cut to the meat of it and post what I have to work with, to keep it simple I have made a very, very small DB just to work on this with here. Could Someone be kind enough to show me what I need to add to my displayall.php and what file I might need to create to resolve this.

 

I'm using XAMPP 1.7.3, just for development. Unmodified and Apache and MYSQL are running as services.

MYSQL version  displayed is  Ver 14.14 Distrib 5.1.41, for Win32 (ia32)

PHP Version 5.3.1

Apache Ver. 2.2.14

 

I have created a database named "foo" for this, containing one table, "people" whis has two fields, "IDNumber" auto int (primary key) and "name" VARCHAR limited to 30

 

In /htdocs/foo I have mysqlconnect.php (obvious what it contains), new.php a form that uses insert.php to add a new person, and displayall.php which I want a 3rd column with a hyperlink that says "edit" to open the selected record in a form so it can be well...edited.

 

Heres the code I have used on the the said files.

 

new.php:

<html>

<head><title> Add New Person</title></head>

<body>

<b>Add New Person.</b><br>

<form action="/foo/insert.php" method="post">

<table border=1>

<tr><td>Name</td>  <td>  <input type="text" name="Name"></td></tr>

</select>

</table>

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

</form><br>

</body>

</html>

 

insert.php:

<?php

require("mysqlconnect.php");

$Name=$_POST['Name'];

mysql_query("INSERT INTO people VALUES('','$Name')");

mysql_close();

?>

 

displayall.php:

<?php

$con = mysql_connect("localhost","root","");

if (!$con)

  {

  die('Could not connect: ' . mysql_error());

  }

 

mysql_select_db("foo", $con);

$result = mysql_query("SELECT * FROM people");

 

echo "<table border='1'>

<tr>

<th>IDNumber </th>

<th>Name</th>

</tr>";

 

//I'm almost positive this is where the a href link should go, just not sure of its format or the file it should link\call

while($row = mysql_fetch_array($result))

  {

  echo "<tr>";

  echo "<td>" . $row['IDNumber'] . "</td>";

  echo "<td>" . $row['Name'] . "</td>";

  echo "</tr>";

  }

echo "</table>";

mysql_close($con);

?>

 

 

So thats what I have, what exactly am I missing?

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

while($row = mysql_fetch_array($result))

  {

  echo "<tr>";

  echo "<td>" . $row['IDNumber'] . "</td>";

  echo "<td>" . $row['Name'] . "</td>";

  echo "<td><a href='editPage.php?edit=" . $row['IDNumber'] . ">Edit</a>"</td>;

  echo "</tr>";

  }

echo "</table>";

mysql_close($con);

 

Now just pick up the $_GET array on the edit page and you're good to  go

Link to comment
Share on other sites

adding  echo "<td><a href='editPage.php?edit=" . $row['IDNumber'] . ">Edit</a>"</td>; is creating a unique problem for me.

 

Only every other row/record is showing up in the displayall.php now. I created a diagnostic page using printf($_get); and when the link one the buggered display all page is clicked the diag page is showing that it is actaully picking up the next record not the one that is in the while loop. and its also the record that is not showing up in the display all.

 

I'm lost at why this is occurring, it seems that it should work completely.

Link to comment
Share on other sites

Forgot to properly close the attribute value <a>

 

while($row = mysql_fetch_array($result))
{
  echo '<tr>',
       '<td>', $row['IDNumber'], '</td>',
       '<td>', $row['Name'], '</td>',
       '<td><a href="editPage.php?edit=', $row['IDNumber'], '">Edit</a></td>',
       '</tr>';
}
echo '</table>';
mysql_close($con);

Link to comment
Share on other sites

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE in C:\xampp\htdocs\foo\displayall.php on line 26

with the above change to the href link <TD>

 

My four most common uses for abatabase  are view records, add new records, edit existing records, and deleting records.

I have the first two down no problem. But my problem with editing existing records is becoming more and more frustrating. The only tutorials I can find on this subject are so overly cluttered with unneeded code that they are useless to me, I dont want to create a 100 line isset array, don't need to worry about 200 lines of form validations or sessions, and defiantly absolutely do not need and CSS. I feel like these tutorials are the equivalent of teaching me how to build a motherboard just so i can open a bloomin text file.  /vent

 

Does anyone know of a site that has basic tutorial for what I am trying to do? which is the absolute striped down no frill no thrills, add edit delete mysql records through php forms... otherwise I'm going to have to buy a copy of MSAccess ... Which I despise, but frankly it works.

Link to comment
Share on other sites

Well, finally my database project is finished. MsAccess makes forms so freaking easy... point click, define that its attached to the primary key, get on with the rest of the database. I still plan on using php and mysql in the future but the over complicated nature of instructions for the absolute bare minimum basics of database interaction is a mighty bad downfall as far as I'm concerned.

Link to comment
Share on other sites

I still plan on using php and mysql in the future but the over complicated nature of instructions for the absolute bare minimum basics of database interaction is a mighty bad downfall as far as I'm concerned.

 

There are programmers for a reason. To do the things you can't on a software/hardware-level.

Link to comment
Share on other sites

You two that offered me assistance, don't take my frustration the wrong way. You have my genuine thanks, really. I had been beating my head against the wall with that one edit form for the last two weeks. The project I am working on is to replace a database program a family member is using, which they are paying a ungodly amount per month to use and is very very inefficient. It copies the entire client database over from the server to the remote users station every time it is open. with only a few thousand records having to wait about 5 minutes for the program to open is unacceptable in my eyes. My family member doesn't see it that he is being robbed. I have been programming access since the late nineties, took the advanced collage classes and all. But since I have learned how much more efficient php/mysql is over Access I will be learning how to integrate the two. I've lost a single battle, the war goes on.

 

I'm going over the tutorials here and checking some scripts from hotscripts today looking for something that works similar to what I neeed to know, then I will strip it down to the basics, and find out exactly what I need too do/ what I am doing wrong with this darn edit form stuff.

 

thanks again and cheers.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.