HOw to create editable tables in html using php ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ial523
Forum Newbie
Posts: 1
Joined: Fri Mar 11, 2016 8:50 pm

HOw to create editable tables in html using php ?

Post by ial523 »

I want to create an editable table in html using php, which will allow to retrieve data from database and display them and also allow to update, delete by selecting a row of the table. And also add new rows..(something similar to the image I've attached)
.
Currently I have a table called "contacts" in the db with fname, address and contact no columns.
.
when I run this, it shows the following error, what am I doing wrong?

Fatal error: Call to a member function fetch_object() on a non-object in C:\xampp\htdocs\newNew\jol.php on line 30
.
The code I have so far is below;
.

Code: Select all

<html>
<body>

<?php
     include ("dbConnect.php");
$sql="select * from contacts";

$result=mysql_query($sql);
$i = 0
?>

<form method=post action="<?php echo $_SERVER['PHP_SELF']; ?>">

<table style="font-family"arial; font-size:15" width="600" border="1" cellpadding="1" cellspacing="1" align="center">

<font face=arial size=5 color=blue>

<tr bgcolor=#009933>
<th><b>NAME</b></th>
<th><b>ADDRESS</b></th>
<th><b>PHONE NO</b></th>

<th><b>UPDATE</b></th>
<th><b>DELETE</b></th>
<th><b>CHECK</b></th>
</tr>

<?php

while ($row = $result->fetch_object())

{
echo "<tr>";

echo "<td>";
echo '<input type="text" value="'.$abc['fname'].'" name="name'.$i.'"readonly/>';
echo "</td>";

echo "<td>";
echo '<input type="text" value="'.$abc['address'].'" name="add'.$i.'"/>';
echo "</td>";

echo "<td>";
echo '<input type="text" value="'.$abc['contactno'].'" name="add'.$i.'"/>';
echo "</td>";

echo "<td>";
echo '<input type="submit" value="update" name="adress'.$i.'" />' ;


if (isset($_POST['address'.$i.'']))

{
if (isset($_POST['check'.$i.'']))
{
$jin = $_POST['add'.$i.''];
$cont = $_POST['add'.$i.''];
$nam = $_POST['add'.$i.''];

$update="update contacts set address='$jin' , contactno='$cont' where fname='$name'" ;

$qry = mysql_queery($update);
if (!$qry){echo "Updation Failed!";} else
{
echo "updated successfully";
}

}
else {echo "Please select a checkbox";}
}

echo "</td>" ;

echo "<td>" ;
echo '<input type="submit" value="delete" name="delete'.$i.'" />' ;
if (isset($_POST['delete'.$i.'']))

{
if (isset($_POST['check'.$i.'']))
{
$nam = $_POST['name'.$i.''];

$delete="delete from contacts where fname='$nam'" ;

$qry = mysql_queery($delete);
if (!$qry){echo "Deletion Failed!";} else
{

echo "deleted successfully !";
}
}

else {echo "Please select a checkbox";}
}

echo "</td>" ;

echo "<td>";
echo '<input type="checkbox" name="check'.$i.'" />' ;
echo "</td>" ;

echo "</tr>" ;

$i++;
}

echo "<tr>" ;
echo "<td>" ;
echo '<input type="text" name="txtname" />' ;
echo "</td>";

echo "<td>" ;
echo '<input type="text" name="txtaddress" />' ;
echo "</td>";

echo "<td>" ;
echo '<input type="text" name="txtphone" />' ;
echo "</td>";

echo "<td>" ;
echo '<input type="submit" value="add" name="add" />' ;

if (isset($_POST['add']))
{
$jin = $_POST['txtname'.$i.''];
$cont = $_POST['txtaddress'.$i.''];
$nam = $_POST['txtphone'.$i.''];

if (!$jin or !$cont or !$nam)
{echo "Fill all fields" ;}

else
{

$update="insert into contact (fname, address, contactno) values ('$jin', '$con', '$nam')"; 

$qry = mysql_query($update);
if (!qry){echo "Adding failed!";}
else
{
header ("location: jol.php");}
}

}

?>

</font>

</form>
</table>

</body>

<html>
Attachments
111.jpg
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: HOw to create editable tables in html using php ?

Post by Celauran »

The mysql_ functions have all been deprecated for literally longer than I can remember and were removed from the language in PHP 7, so you really shouldn't be using them.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: HOw to create editable tables in html using php ?

Post by Christopher »

Yes, use the mysqli or PDO database extensions. They are just as easy to use. There are examples in the PHP documentation.
ial523 wrote:Fatal error: Call to a member function fetch_object() on a non-object in C:\xampp\htdocs\newNew\jol.php on line 30
This error means that there was an error in the query, so a result object was not returned. Use PDO.
(#10850)
Post Reply