Solved

PHP delete contact form problem

Posted on 2007-11-22
5
979 Views
Last Modified: 2013-12-12
This script properly selects and displays a user name to be deleted. However, it does not delete the user from the database on submission. I don't see the problem. Can someone help me work through what I'm missing? Thank you in advance.

<?php
require_once('functions.php');
require_once('includes/conn.php');
session_start();

if (!$_POST['uid']) {
      $msg_no_input = "NO SEARCH TERM WAS HIGH LIGHTED!";
      header("Location:http://www.zzzzzzzzzzzzzzz.org/user_search.php?msg_no_input=" . urlencode($msg_no_input));
      exit;
    } else {      

$uid = $_POST['uid'];
if (isset($_POST['lastname']))
   {
    $email = strtolower(trim($_POST['email']));
      $firstname = ucfirst (trim($_POST['firstname']));
      $lastname = ucfirst (trim($_POST['lastname']));

//build and issue query
$sql = "DELETE t_user FROM t_user
            WHERE t_user.lastname = '$lastname'";

$result = @mysql_query($sql,$connection) or die(mysql_error());

$msg_success = "$_POST[firstname] $_POST[lastname] has been deleted";
      header("Location:http:/www.zzzzzzzzzzzzzz.org/user_add_result.php?msg_success=" . urlencode($msg_success));
      exit;
} else {

//build and issue query
$sql ="SELECT uid, firstname, lastname FROM t_user WHERE uid ='$_POST[uid]'";

$result =@mysql_query($sql,$connection)or die(mysql_error());

//get results for display
while ($row =mysql_fetch_array($result))
 {
 $firstname =$row['firstname'];
 $lastname =$row['lastname'];
  }//end while
 }//end else
}//end if
?>
<form name="user_form" method="post" action="<?php $_SERVER['PHP_SELF']; ?>"/>
<input type="hidden" name="uid" value="<? echo "$_POST[uid]"; ?>">
<input type="text" name="name" value="<? echo "$firstname $lastname"; ?>" size="42" maxlength="42" class="input_area" />
<input type="submit" name="submit"value="Delete" class="button"
     onclick="this.form.action='<?php $_SERVER['PHP_SELF']; ?>'"/>
<input type="submit" class="button" value="Cancel"
        onclick="this.form.action='user_search.php'" />
</form>
0
Comment
Question by:NZ7C
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 20

Expert Comment

by:steelseth12
ID: 20337017
$sql = "DELETE t_user FROM t_user
            WHERE t_user.lastname = '$lastname'";

should be

$sql = "DELETE  FROM t_user
            WHERE t_user.lastname = '$lastname'";
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 20337042
I agree with steelseth12 about what the problem is, but wonder if your query might delete more than one user by mistake sometimes (if they have the same surname)?
0
 

Author Comment

by:NZ7C
ID: 20337085
steelseth12 - Yes, you are right on that - don't know why I did not see it. However, I still cannot get the entry to delete after making the change.

TerryAtOpus - You are right as well - I am just setting this up for the moment. Deletions should really be done by the uid.

Thanks to both of you for offering help. Still scratching my head on it.
0
 
LVL 20

Accepted Solution

by:
steelseth12 earned 500 total points
ID: 20337108
In your form you do not have a textfield called lastname you just have one field called name.

So these will not work
$firstname = ucfirst (trim($_POST['firstname']));
   
$lastname = ucfirst (trim($_POST['lastname']));

You could break up the first name from the surname by doing

$temp_name = explode(" ",trim($_POST['name'],2);
      
        $firstname = ucfirst ($temp_name[0]);
   
        $lastname = ucfirst ($temp_name[1]);

Also you are changing the case of the lastname with the ucfirst() function ... if you have the lastname in lower case in the database it will not match the query.
0
 

Author Comment

by:NZ7C
ID: 20337139
steelseth12:
You're right! Changed the textfield name and now it deletes. Case change does not seem to be an issue though. Thanks so much for the help.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question