Jump to content

Archived

This topic is now archived and is closed to further replies.

croix

How to go about deleting records via PHP {Solved}

Recommended Posts

OK, Im pretty sure I can figure out how to code the page that will actually do the deletion, but im having problems figuring out how to get the information there.

I have a query page, which returns a list of records, including an Edit button for each record. The edit button passes along the ID for that record, and the new page displays the record in editable fields.

There is currently an Update button on that page, that when clicked, takes any new information in the forms and updates the record with the new information.

I would like to have something along the lines of another button (a Delete button) or even a text link saying "Did you want to delete this record?" on the same page that has the Update button.


Problem is, I can't figure out how to pass along the info I need. The Update button works because the form action points to the next page.



I know this is probably a simple solution, but Im stumped... How get my form to be able to be sent to 2 different pages depending on what button is pressed?

Share this post


Link to post
Share on other sites
Why not have another, smaller, form inside the page? IE:

[code]<form action="..." method="post">
<input type="hidden" name="result_id" value="<?=$_GET['id'];?>">
<input type="submit" value="Delete this entry">
</form>[/code]

Share this post


Link to post
Share on other sites
haha i knew there was going to be a simple answer to this.. lemme give that a try

Share this post


Link to post
Share on other sites
[code]
<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');

include("dbconnection.php");
$id=$_GET['customerID'];
$query = "SELECT * FROM customers WHERE customerID= $id ";
$result = odbc_exec($cnx, $query);
     while($row = odbc_fetch_array($result))
             {
$firstname = $row['firstname'];
$lastname = $row['lastname'];
$address1 = $row['address1'];
$address2 = $row['address2'];
$city = $row['city'];
$state = $row['state'];
$zip = $row['zip'];
$email = $row['email'];
$referral = $row['referral'];
$promo = $row['promo'];
$lubricant = $row['lubricant'];

$results = "
  <div id=\"table1\"><center>
<form action=\"cust_update.php\" method=\"post\">
  <div align=\"left\"><input type=\"hidden\" name=\"customerID\" value=\"$id\">
    <table width=349 height=\"91\" border=\"0\">
      <tr>
        <td width=174 height=\"81\"><p>First Name:<br>
              <input type=\"text\" name=\"firstname\" value=\"$firstname\">
        </p>          </td>
        <td width=165><p>Last Name:<br>
              <input type=\"text\" name=\"lastname\" value=\"$lastname\">
        </p>          </td>
      </tr>
      <tr>
        <td>Address:<br>
    <input type=\"text\" name=\"address1\" value=\"$address1\"></td>
        <td>Address 2:<br>
    <input type=\"text\" name=\"address2\" value=\"$address2\"></td>
      </tr>
      <tr>
        <td></td>
        <td></td>
      </tr>
      <tr>
        <td><p></p>
          <p>City:<br>
                <input type=\"text\" name=\"city\" value=\"$city\">
          </p></td>
        <td><p></p>
          <p>State:<br>
                <input type=\"text\" name=\"state\" value=\"$state\">
          </p></td>
      </tr>
      <tr>
        <td><p></p>
          <p>Zip:<br>
                <input type=\"text\" name=\"zip\" value=\"$zip\">
          </p></td>
        <td></td>
      </tr>
      <tr>
        <td><p></p>
          <p>Email:<br>
                <input type=\"text\" name=\"email\" value=\"$email\">
          </p></td>
        <td></td>
      </tr>
      <tr>
        <td><p></p>
          <p>Referral:<br>
                <input type=\"text\" name=\"referral\" value=\"$referral\">
          </p></td>
        <td></td>
      </tr>
      <tr>
        <td><p></p>
          <p>Promo:<br>
                <input type=\"text\" name=\"promo\" value=\"$promo\">
          </p></td>
        <td><p></p>
          <p>Lubricant:<br>
                <input type=\"text\" name=\"lubricant\" value=\"$lubricant\">
          </p></td>
      </tr>
      <tr>
        <td></td>
        <td></td>
      </tr>
    </table>
    <p><br>
        <input type=\"Submit\" value=\"Update\">
        </p></form><br>

<form action=\"cust_delete.php\" method=\"post\">
<input type=\"hidden\" name=\"cust_id\" value=\"<?=$_GET['customerID'];?>\">
<input type=\"submit\" value=\"Delete\">
</form>

  </div>

</center>
</div>
";
}
?>
[/code]

so thats what i tried, and what i get is a blank page.
if i just take out the second form, it works fine.

I think the value for the second submit button is wrong, but what should it be if not delete?

***edit
looking at the way the forum posted the bottom of the code, im thinking something is wrong there.

Share this post


Link to post
Share on other sites
a little googling on the value of buttons tells me that that couldnt be the problem.

however, Dreamweaver has the red error underline on type=\Submit\" in the second form, and hovering over tells me "Unsupported value for the type attribute of the input tag" and lists all teh browsers its unsupported on.

Share this post


Link to post
Share on other sites
Change
[code]
<form action=\"cust_delete.php\" method=\"post\">
<input type=\"hidden\" name=\"cust_id\" value=\"<?=$_GET['customerID'];?>\">
<input type=\"submit\" value=\"Delete\">
[/code]

to

[code]
<form action=\"cust_delete.php\" method=\"post\">
<input type=\"hidden\" name=\"cust_id\" value=\"{$_GET['customerID']}\">
<input type=\"submit\" value=\"Delete\">
[/code]

You are already in a php string (not html code)

Share this post


Link to post
Share on other sites
[code]
<?php

ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On');

include("dbconnection.php");

//echo "
//";
//print_r($_POST);
//echo "
//";

$customerID=$_GET['customerID'];


//foreach($_GET as $key => $value)
//{

//  ${$key} = $value;
//}

$query="DELETE FROM customers VALUES * WHERE customerID = $customerID";
odbc_exec($cnx, $query);
if (!odbc_exec($cnx, $query))
{
echo odbc_errormsg($cnx);
}
else
{
echo "Record Deleted!";
}
odbc_close($cnx);

header("Refresh: 2; URL=http://sliquid.com/protected/cust_search.php");
echo " "; // NN4 requires that we output something...
exit();
?>[/code]

ok, now im getting the button, but using it gives me this error:
[b]Notice: Undefined variable: customerID in d:\inetpub\sliquid\wwwRoot\protected\cust_delete.php on line 16

Warning: odbc_exec(): SQL error: [Microsoft][ODBC Microsoft Access Driver] Syntax error in FROM clause., SQL state 37000 in SQLExecDirect in d:\inetpub\sliquid\wwwRoot\protected\cust_delete.php on line 16

Warning: odbc_exec(): SQL error: [Microsoft][ODBC Microsoft Access Driver] Syntax error in FROM clause., SQL state 37000 in SQLExecDirect in d:\inetpub\sliquid\wwwRoot\protected\cust_delete.php on line 16
[Microsoft][ODBC Microsoft Access Driver] Syntax error in FROM clause.[/b]

so obviously i need to fix my undefined variable first off. why is it undefined? is it not receiving the GET variable from the last page?

Share this post


Link to post
Share on other sites
[quote]...is it not receiving the GET variable from the last page?[/quote]

Probably not as your form method is POST.

Also correct syntax for the delete query is

DELETE FROM customers WHERE customerID = $customerID

Share this post


Link to post
Share on other sites
working now.

made your suggested changes, as well as changing the value on the edit page to $id instead of $customerID

Share this post


Link to post
Share on other sites

×

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.