Forum Moderators: coopster

Message Too Old, No Replies

EDIT and DELETE Functions

How to Edit n Delete database values via PHP

         

shruti

2:56 pm on Jul 25, 2008 (gmt 0)

10+ Year Member



this is my "showcomments.php" file which displays all the comments of the chosen date(chosen in other page).

Now I have Delcom() and Editcom() functions in this page.
What do I write in these functions to make them work.

<?php

$host = "#*$!";
$user = "#*$!";
$pass = "#*$!";
$dbname = "#*$!";

$connection = mysql_connect("localhost", "root", "") or die("Cannot connect to MySQL server: " . mysql_error());
$db_selected = mysql_select_db('#*$!', $connection);

$mth = $_POST['month'];
$dt = $_POST['date'];
$yr = $_POST['year'];

$whole1="$yr-$mth-$dt";

//echo "$whole1";

$data = mysql_query("SELECT * FROM comments where date = '$whole1'");
Print "<table border=2 cellspacing=2 align=center bordercolor=#FFffff cellpadding=10 width=100% style='bordercolor:#CC0066;font-family:Tahoma;text-align:center;color:#CC0066'>";


$num_data = mysql_num_rows($data);
echo "<tr><td bgcolor='#CCCCCC' style='width:60.75pt;text-align:left;font-size:small;font-family:Tahoma;font-weight:bold;padding:0in 0in 0in 0in'>CELLID</td> ";
echo "<td bgcolor='#CCCCCC' style='width:60.75pt;text-align:left;font-size:small;font-family:Tahoma;font-weight:bold;padding:0in 0in 0in 0in'>PROBLEM</td> ";
echo "<td bgcolor='#CCCCCC' style='width:60.75pt;text-align:left;font-size:small;font-family:Tahoma;font-weight:bold;padding:0in 0in 0in 0in'>SOLUTION</td> ";
echo "<td bgcolor='#CCCCCC' style='width:60.75pt;text-align:left;font-size:small;font-family:Tahoma;font-weight:bold;padding:0in 0in 0in 0in'>COMMENTS</td>";
echo "<td bgcolor='#CCCCCC' style='width:60.75pt;text-align:left;font-size:small;font-family:Tahoma;font-weight:bold;padding:0in 0in 0in 0in'></td>";
echo "<td bgcolor='#CCCCCC' style='width:60.75pt;text-align:left;font-size:small;font-family:Tahoma;font-weight:bold;padding:0in 0in 0in 0in'></td>
</tr> ";


for ($i=0; $i <$num_data; $i++)
{

$row = mysql_fetch_array($data);

echo "<tr><td style='color: #666666;text-align:left;font-size:small;font-family:Tahoma'>" .$row['CELLID'] . "</td> ";
echo "<td style='color: #666666;text-align:left;font-size:small;font-family:Tahoma'>" .$row['problem'] . "</td> ";
echo "<td style='color: #666666;text-align:left;font-size:small;font-family:Tahoma'>" .$row['solution'] . "</td> ";
echo "<td style='color: #666666;text-align:left;font-size:small;font-family:Tahoma'>" .$row['comments'] . "</td>";
echo "<td><input type=button value=Delete onclick=delCom(i)></td>";
echo "<td><input type=button value=Edit onclick=editCom(i)></td></tr>";
}

?>


==========================================================

My data base table "comments" is

CELLID , problem ,solution , comments ,userid , date .
===========================================================

shruti

4:38 pm on Jul 25, 2008 (gmt 0)

10+ Year Member



Please Help me!

shruti

6:49 pm on Jul 29, 2008 (gmt 0)

10+ Year Member



Why is no one helping me now?

coopster

7:14 pm on Jul 29, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Your functions are JavaScript functions. What do you intend to do within the JavaScript functions?

webfoo

7:31 pm on Jul 29, 2008 (gmt 0)

10+ Year Member



The code you have calls the literal javascript function editCom(i), not the value of the current reccord, just the letter i. You need to modify that line to this:

echo "<td><input type='button' value='Edit' onclick='editCom(" . $i . ")'></td></tr>"; 

This will pass the number of the current record to the JavaScript function. PHP and JavaScript work seperately.

shruti

8:40 pm on Jul 29, 2008 (gmt 0)

10+ Year Member



I wrote this function but it does not do anything.
Can someone help me! :((

function getComments(x){

$data = mysql_query("SELECT * from comments where Cellid='$x'")
$num_data = mysql_num_rows($data);

for($i=0; $i < $num_data; $i++) {

$row = mysql_fetch_array($data);

echo "<tr><td style='color: #666666;text-align:left;font-size:small;font-family:Tahoma'>" .$row['CELLID'] . "</td> ";
echo "<td style='color: #666666;text-align:left;font-size:small;font-family:Tahoma'>" .$row['problem'] . "</td> ";
echo "<td style='color: #666666;text-align:left;font-size:small;font-family:Tahoma'>" .$row['solution'] . "</td> ";
echo "<td style='color: #666666;text-align:left;font-size:small;font-family:Tahoma'>" .$row['comments'] . "</td>";
}

}

webfoo

10:21 pm on Jul 29, 2008 (gmt 0)

10+ Year Member



That is a PHP Function. That won't be called when you editCom(i). Also, the $data line is missing a ; at the end.

shruti

12:31 pm on Jul 30, 2008 (gmt 0)

10+ Year Member



Then please advice me what will work, coz nothing is working !

webfoo

1:56 pm on Jul 30, 2008 (gmt 0)

10+ Year Member



Could you post all of the code for us? I think we need to look at the big picture.

shruti

2:57 pm on Jul 30, 2008 (gmt 0)

10+ Year Member



this is my code: I have two function EDIT and DELETE.
When people make comments, they are displayed here with an edit n delete button . So when you click Edit then it lets you edit the comments u have made or when you click delete it deletes the comment.

But you have to make sure that it is the same user deleting his own comments. He cant edit others.


<?php

$host = "#*$!";
$user = "#*$!";
$pass = "#*$!";
$dbname = "#*$!";

$connection = mysql_connect("localhost", "root", "") or die("Cannot connect to MySQL server: " . mysql_error());
$db_selected = mysql_select_db('#*$!', $connection);

$mth = $_POST['month'];
$dt = $_POST['date'];
$yr = $_POST['year'];

$whole1="$yr-$mth-$dt";

//echo "$whole1";

$data = mysql_query("SELECT * FROM comments where date = '$whole1'");
Print "<table border=2 cellspacing=2 align=center bordercolor=#FFffff cellpadding=10 width=100% style='bordercolor:#CC0066;font-family:Tahoma;text-align:center;color:#CC0066'>";

$num_data = mysql_num_rows($data);
echo "<tr><td bgcolor='#CCCCCC' style='width:60.75pt;text-align:left;font-size:small;font-family:Tahoma;font-weight:bold;padding:0in 0in 0in 0in'>CELLID</td> ";
echo "<td bgcolor='#CCCCCC' style='width:60.75pt;text-align:left;font-size:small;font-family:Tahoma;font-weight:bold;padding:0in 0in 0in 0in'>PROBLEM</td> ";
echo "<td bgcolor='#CCCCCC' style='width:60.75pt;text-align:left;font-size:small;font-family:Tahoma;font-weight:bold;padding:0in 0in 0in 0in'>SOLUTION</td> ";
echo "<td bgcolor='#CCCCCC' style='width:60.75pt;text-align:left;font-size:small;font-family:Tahoma;font-weight:bold;padding:0in 0in 0in 0in'>COMMENTS</td>";
echo "<td bgcolor='#CCCCCC' style='width:60.75pt;text-align:left;font-size:small;font-family:Tahoma;font-weight:bold;padding:0in 0in 0in 0in'></td>";
echo "<td bgcolor='#CCCCCC' style='width:60.75pt;text-align:left;font-size:small;font-family:Tahoma;font-weight:bold;padding:0in 0in 0in 0in'></td>
</tr> ";

for ($i=0; $i <$num_data; $i++)
{

$row = mysql_fetch_array($data);

echo "<tr><td style='color: #666666;text-align:left;font-size:small;font-family:Tahoma'>" .$row['CELLID'] . "</td> ";
echo "<td style='color: #666666;text-align:left;font-size:small;font-family:Tahoma'>" .$row['problem'] . "</td> ";
echo "<td style='color: #666666;text-align:left;font-size:small;font-family:Tahoma'>" .$row['solution'] . "</td> ";
echo "<td style='color: #666666;text-align:left;font-size:small;font-family:Tahoma'>" .$row['comments'] . "</td>";
echo "<td><input type=button value=Delete onclick=delCom(i)></td>";
echo "<td><input type=button value=Edit onclick=editCom(i)></td></tr>";
}

?>

==========================================================

My data base table "comments" is

CELLID , problem ,solution , comments ,userid , date .
===========================================================