Page 1 of 1

HOw to create editable tables in html using php ?

Posted: Fri Mar 11, 2016 9:00 pm
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>

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

Posted: Sat Mar 12, 2016 7:26 am
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.

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

Posted: Sat Mar 12, 2016 10:25 pm
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.