0

HI, guyz

Wordrin if sum one could help! at the moment got code which allow user to input and display in result. but mean time wana allow user to delete current input data by providing delete button next to displayd data..........
plz could some one tell me how to,what commend do i need and where to put as i tried many but nothig workin for me .

view code!

<?php
$dbcnx=@mysql_connect("localhost", "abc", "xxxx");
/* Connect to the database server */
if (!dbcnx) {
die('<p>Unable to connect ot the database Server. </p>');
}
/*Displays a message if unable*/
/* Now select the test*/
if (! @mysql_select_db('refxx')){
die ( '<p>Cannot selet test database.</p>' );
}


?>

<a>
<?php

if(isset($_GET)):

/* if a record is to be added */
?>

<form action="<?=$_SERVER?>" method="post" >
<table summary="DATA Entry Table">
<caption>Enter details</caption>

<!-- get data -->


<tr><td>authorname</td><td><input name="authorname" type="text" size="40"
Value="authorname" /></td></tr>
<tr><td>publishyear</td><td><input name="publishyear" type="text" size="40"
Value="publishyear" /></td></tr>
<tr><td>booktitle</td><td><input name="booktitle" type="text" size="40"
Value="booktitle" /></td></tr>
<tr><td>edition</td><td><input name="edition" type="text" size="40"
Value="edition" /></td></tr>
<tr><td>place</td><td><input name="place" type="text" size="40"
Value="place" /></td></tr>
<tr><td>publisher</td><td><input name="publisher" type="text" size="40"
Value="publisher" /></td></tr>
</table>
<input type="submit" name="submitdata" value="Generate" /><input type="reset" value="Clear Form" />

</form>

<?php


else:
/* Connect to Database server*/
$dbcnx=@mysql_connect("localhost", "abc", "xxxx");
/* Connect to the database server */
if (!dbcnx) {
die('<p>Unable to connect ot the database Server. </p>');
}
/*Displays a message if unable*/
/* Now select the test*/
if (! @mysql_select_db('refxx')){
die ( '<p>Cannot select test database.</p>' );
}


/* Adds record to database */

if (isset($_POST)) {

$id = $_POST;

$authorname = $_POST;

$publishyear = $_POST;

$booktitle = $_POST;

$edition = $_POST;

$place = $_POST;

$publisher = $_POST;

$sql = "INSERT INTO book set

id='$id', authorname='$authorname', publishyear='$publishyear', booktitle='$booktitle', edition='$edition', place='$place', publisher='$publisher'";


/* Confirm data entered */

if (@mysql_query($sql)) {

echo('<p></p>');

} else {

echo('<p>Error adding record: ' . mysql_error() . '</p>');

}
}
//SELECT * from abc ORDER BY id DESC LIMIT 1;
$result = @mysql_query('select * from tablename ORDER BY id DESC LIMIT 1');

if (!$result) {

die('<p>Error performing query: ' . mysql_error() . '</p>');

}
?>

<table summary="Results" border="4" width="650">
table summary="Events Time" border="4" width="900"
<caption>Your Book Reference is:</caption><br /><br /><br /><br />

<!-- code to display result -->

<?php

while ( $row = mysql_fetch_array($result) ) {

echo ('<tr><td>' . $row . '</td><td>' . $row . '</td><td>' . $row . '</td><td>' . $row . '</td><td>' . $row . '</td><td>' . $row . '</td><td>' . $row . '</td></tr>');


}

echo('<p><a href=" ' . $SERVER . '?addrecord=1">Generate New Reference</a></p>');


endif;
?>

Thanks.

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by sops21
0

just use the delete method using the rows id

mysql_query("DELETE FROM table WHERE id='1'");

Edited by fobos: n/a

0

Thanks,
but thats manual delete function, need some thing auto delete record function by id, at the moment user only can enter and view data but can not delete. need some function which allow user to click button to delete record.

0

If you want to keep the same method that you are using for adding a record, you can do something like this:

echo "<tr><td>" . $row['id'] . "</td><td>" . $row['authorname'] . "</td><td>" . $row['publishyear'] . "</td><td>" . $row['booktitle'] . "</td><td>" . $row['edition'] . "</td><td>" . $row['place'] . "</td><td>" . $row['publisher'] . "</td><td><a href='" . $SERVER['PHP_SELF'] . "'?deleterecord=".$row['id']."'>Delete Record</a></td></tr>";

//Get the record
$recordtodelete = $_GET['deleterecord'];

//Delete Query
mysql_query("DELETE FROM table WHERE id='$recordtodelete'");
0

seems their is still something missing, as this is not workin either?

0

I've inserted my solution into you existing code. (Altho I didn't run it to see if it works) This also assumes that $row is the id of the row in the database as well.

<?php 
$dbcnx=@mysql_connect("localhost", "abc", "xxxx");
/* Connect to the database server */
if (!dbcnx) {
die('<p>Unable to connect ot the database Server. </p>');
}
/*Displays a message if unable*/
/* Now select the test*/
if (! @mysql_select_db('refxx')){
die ( '<p>Cannot selet test database.</p>' );
}


?>

<a>
<?php

if(isset($_GET['addrecord'])):

/* if a record is to be added */
?>

<form action="<?=$_SERVER['PHP_SELF']?>" method="post" > 
<table summary="DATA Entry Table">
<caption>Enter details</caption> 

<!-- get data -->


<tr><td>authorname</td><td><input name="authorname" type="text" size="40"
Value="authorname" /></td></tr>
<tr><td>publishyear</td><td><input name="publishyear" type="text" size="40"
Value="publishyear" /></td></tr>
<tr><td>booktitle</td><td><input name="booktitle" type="text" size="40"
Value="booktitle" /></td></tr>
<tr><td>edition</td><td><input name="edition" type="text" size="40"
Value="edition" /></td></tr>
<tr><td>place</td><td><input name="place" type="text" size="40"
Value="place" /></td></tr>
<tr><td>publisher</td><td><input name="publisher" type="text" size="40"
Value="publisher" /></td></tr>
</table>
<input type="submit" name="submitdata" value="Generate" /><input type="reset" value="Clear Form" />

</form> 

<?php


else:
/* Connect to Database server*/
$dbcnx=@mysql_connect("localhost", "abc", "xxxx");
/* Connect to the database server */
if (!dbcnx) {
die('<p>Unable to connect ot the database Server. </p>');
}
/*Displays a message if unable*/
/* Now select the test*/
if (! @mysql_select_db('refxx')){
die ( '<p>Cannot select test database.</p>' );
}

// check if we are deleting

if(isset($_GET['deleterecord']){
// delete the record
$recordtodelete = $_GET['deleterecord'];
mysql_query("DELETE FROM table WHERE id='$recordtodelete'") or die(mysql_error());

}


/* Adds record to database */

if (isset($_POST['submitdata'])) {

$id = $_POST['id'];

$authorname = $_POST['authorname'];

$publishyear = $_POST['publishyear'];

$booktitle = $_POST['booktitle'];

$edition = $_POST['edition'];

$place = $_POST['place'];

$publisher = $_POST['publisher'];

$sql = "INSERT INTO book set

id='$id', authorname='$authorname', publishyear='$publishyear', booktitle='$booktitle', edition='$edition', place='$place', publisher='$publisher'";


/* Confirm data entered */

if (@mysql_query($sql)) {

echo('<p></p>');

} else {

echo('<p>Error adding record: ' . mysql_error() . '</p>');

}
}
//SELECT * from abc ORDER BY id DESC LIMIT 1;
$result = @mysql_query('select * from tablename ORDER BY id DESC LIMIT 1');

if (!$result) {

die('<p>Error performing query: ' . mysql_error() . '</p>');

}
?>

<table summary="Results" border="4" width="650">
table summary="Events Time" border="4" width="900"
<caption>Your Book Reference is:</caption><br /><br /><br /><br />

<!-- code to display result -->

<?php 

while ( $row = mysql_fetch_array($result) ) {

echo "<tr><td>" . $row['id'] . "</td><td>" . $row['authorname'] . "</td><td>" . $row['publishyear'] . "</td><td>" . $row['booktitle'] . "</td><td>" . $row['edition'] . "</td><td>" . $row['place'] . "</td><td>" . $row['publisher'] . "</td><td><a href='" . $SERVER['PHP_SELF'] . "'?deleterecord=".$row['id']."'>Delete Record</a></td></tr>";


}

echo('<p><a href=" ' . $SERVER['PHP_SELF'] . '?addrecord=1">Generate New Reference</a></p>');


endif;
?>
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.