Jump to content

Deleting records from database


dodgei

Recommended Posts

I'm using odbc to connect to my ms access database. I would like to display the records from a query and with each line
retrieved I would like to have a picture of a "x" next to the record that will delete that record if clicked.

What I dont understand is how I can single out that one record?
Link to comment
Share on other sites

It does have a unique ID. Its the id field is auto-numbered.

I'm using a loop to display all of the records so if I assign the id to a variable wont the
variable change with each new record retrieved?

The records are going to be retrieved and displayed with a loop.  What can I do so that
when I construct the delete statement it will delete that one record?
Link to comment
Share on other sites

For each record selected from the database you will need to store the row id in a link:

eg:
[code]
<?php
$sql = "select id from table";
$results = @mysql_query($sql) or die("<tt>problem with $sql : " . mysql_error() . "</tt>\n");
while($r = @mysql_fetch_object($results)) {
 echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?action=delete&row_id=" . $r->id . "><img src=\"images/x.gif\" border=\"0\"></a>\n";
}
?>
[/code]


You willl also need something like this in your page to handle the delete

[code]
if(isset($_GET['delete'])) {
 $sql = "delete from table where id = '" . $_GET['row_id'] . "'";
 @mysql_query($sql) or die("<tt>problem with $sql : " . mysql_error() . "</tt>\n");
}
[/code]

Hope this helps

;)
Link to comment
Share on other sites

instead of using

[code]
mysql_query()
[/code]
use:
[code]
odbc_exec()
[/code]
and instead of using
[code]
mysql_fetch_object()
[/code]
use:
[code]
odbc_fetch_object()
[/code]
and for error reporting use:
[code]
odbc_errormsg()
[/code]
instead of using:
[code]
mysql_error()
[/code]



[quote]
Also, am I just querying the record id or can I query all the fields and display the
records with all the fields and also the picture for the delete?
[/quote]

Yes.....

[code]
select * from table...
[/code]

:)
Link to comment
Share on other sites

<?php

$odbc = odbc_connect ('accounting', 'root', '') or die('Could    Not Connect to ODBC Database!');
$sql = "select * from entry";
$results = @odbc_exec($odbc, $sql) or die("<tt>problem with $sql : " . odbc_errormsg() . "</tt>\n");
while($r = @odbc_fetch_object($results))
{
  echo "new";
  echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?action=delete&row_id=" . $r->id . "><img src=\"/cal.gif\" border=\"0\"></a>\n";
 
}
odbc_close($odbc);
?>

this is what I have. I ran the page and it doesnt do anything. It doesnt even write "new". The gif file is in the same directory as the page. I'm not sure if I have the reference right on that one.

Is fetch object function similar to fetch array in that I can reference individual fields with $r['fieldname'];?

Can you see any errors in my code?
Link to comment
Share on other sites

Try this instead of fetch object:

[code]
<?php
$result = odbc_exec($odbc,$sql);
while(odbc_fetch_row($result)) {
  //as long as your first column is the id field
  $rowid = odbc_result($result,1);
  echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?action=delete&row_id=" . $rowid . "><img src=\"/cal.gif\" border=\"0\">\n";
}
?>
[/code]

Sorry for the confusion, it's been a while since I used ODBC.....

:)
Link to comment
Share on other sites

Change you sql statment to "select * from table..."

then....

[code]
$rowid = odbc_result($result,1);
$field_2 = odbc_result($result,2);
$field_3 = odbc_result($result,3);
$field_4 = odbc_result($result,4);
$field_5 = odbc_result($result,5);
.....
[/code]
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.