Jump to content

PHP code help


sasori

Recommended Posts

here's my database structure

user_id ,  first_name , last_name, email, registration_date, password1, 

 

here's my code

<?php
$page_title = "Edit a User";
require('./includes/header.html');

if((isset($_GET['id'])) && (is_numeric($_GET['id']))){
   $id = $_GET['id'];
  }elseif((isset($_POST['id'])) && (is_numeric($_POST['id']))){
   $id = $_POST['id'];
  }else{
    echo '<h1 id="mainhead">Page Error</h1>
        <p class="error">This page has been accessed in error.</p><p><br /><br />
        </p>';
        require('./includes/footer.html');
        exit();
  }

require_once('./includes/mysql_connect.php');


function escape_data($data){
  global $dbc;
  if(ini_get('magic_quotes_gpc')){
    $data = stripslashes($data);
  }else{
    return mysql_real_escape_string(trim($data),$dbc);
  }
}


if(isset($_POST['submitted'])){
  $errors = array();
  if(empty($_POST['first_name'])){
    $errors[] = 'You forgot to enter your first name.';
  }else{
    $fn = escape_data($_POST['first_name']);
  }

    if(empty($_POST['last_name'])){
      $errors[] = 'You forgot to enter  your last name';
    }else{
      $ln = escape_data($_POST['last_name']);
    }

    if(empty($_POST['email'])){
      $errors[] = 'You forgot to enter your email';
    }else{
      $e = escape_data($_POST['email']);
    }

    if(empty($errors)){
      $query = "SELECT user_id FROM users WHERE email = '$e' AND user_id !='$id'";
      $result = mysql_query($query);
      if(mysql_num_rows($result) == 0){
        $query = "UPDATE users SET first_name='$fn', last_name ='$ln', email='$e' WHERE user_id = $id";
        $result = mysql_query($query);
        if(mysql_affected_rows() == 1){
          echo '<h1 id="mainhead">Edit a user</h1>
                <p>The user has been edited</p><p><br /><br /></p>';
        }else{
          echo '<h1 id="manhead">System Error</h1>
                <p class="error">The user could not be edited due to system error.
                We apologize for any inconvenience.</p>';
          echo '<p>'. mysql_error(). '<br /><br />Query: '. $query . '</p>';
          require('./includes/footer.html');
          exit();
        }
      }else{ //Already registered
        echo '<h1 id="mainhead">Error!</h1>
        <p class="error">The email address has already been registered.</p>';
      }
    }else{//report errors
        echo '<h1 id="mainhead">Error!</h1>
        <p class="error">The following error(s) occurred: <br />';
        foreach($errors as $msg){
          echo " - $msg<br />\n";
        }
        echo '</p><p>Please try again.</p><p><br /></p>';
    }//endif report errors
}// end if submit

$query = "SELECT first_name, last_name, email FROM users WHERE user_id='$id'";
$result = mysql_query($query);
if(mysql_num_rows($result) == 1){
  $row = mysql_fetch_array($result,MYSQL_NUM);
  echo '<h2>Edit a User</h2>
  <form action="edit_user.php" method="post">
  <p>First Name:<input type="text" name="first_name" size="15" maxlength="15" value="' . $row['0'] .'"/></p>
  <p>Last Name:<input type="text" name="last_name" size="15" maxlength="30" value="' . $row['1'] .'"/></p>
  <p>Email:<input type="text" name="email" size="20" maxlength="40" value="'. $row['2'] . '"/></p>
  <p><input type="submit" name="submit" value="Submit" />
  <input type="hidden" name="submitted" value="TRUE" />
  <input type="hidden" name="id" value="'. $id .'" />
  </form>';
}else{
  echo '<h1 id="mainhead">Page Error</h1>
  <p class="error">This page has been accessed error.</p><p><br ><br /></p>';
}
mysql_close();
require('./includes/footer.html');
?>

 

the problem is once i try to edit a record through the web browser..it says the data is edited but then it doesn't insert the first name, last name and email address :( what should I do ?

 

keiu5j.jpg

Link to comment
https://forums.phpfreaks.com/topic/153717-php-code-help/
Share on other sites

erm ill take a look at the code hang on

 

EDIT

 

try changing this

 

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

 

to

 

if($id != ''){

 

EDIT AGEN

 

      $query = "SELECT user_id FROM users WHERE email = '$e' AND user_id !='$id'";
      $result = mysql_query($query);
      if(mysql_num_rows($result) == 0){

 

where is the logic in that

 

 

      $query = "SELECT user_id FROM users WHERE email='$e'";
      $result = mysql_query($query);
      if(mysql_num_rows($result) == 1){

Link to comment
https://forums.phpfreaks.com/topic/153717-php-code-help/#findComment-807788
Share on other sites

Archived

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

×
×
  • 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.