Jump to content

Recommended Posts

I'm working on a admin file to edit a plan however my editplan.php file actually adds a whole new plan duplicating the the existing plan. Can someone please have a look at my code to see where I am going wrong. Any help is appreciated. Here is the code of my editplan.php file:

 


<?php
  require_once('../appvars.php');
  require_once('../connectvars.php');
  
    // Connects to the database
  $dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);

  if (isset($_POST['submit'])) {
    // Grab the going mobile data from the POST
    $id = $_POST['id'];
    $model = $_POST['model'];
    $price = $_POST['price'];
    $minutes = $_POST['minutes'];
    $monthly = $_POST['monthly'];
    $old_image = $_POST['old_image'];
    $new_image = $_FILES['new_image']['name'];
    $new_image_type = $_FILES['new_image']['type'];
    $new_image_size = $_FILES['new_image']['size'];
$required = array('model', 'price', 'minutes', 'monthly');
    $error = false;

    // Validates and moves the uploaded image file, if necessary
    if (!empty($new_image)) {
      if ((($new_image_type == 'image/gif') || ($new_image_type == 'image/jpeg') || ($new_image_type == 'image/jpg') ||
        ($new_image_type == 'image/png')) && ($new_image_size > 0) && ($new_image_size <= GM_MAXFILESIZE)) {
        if ($_FILES['file']['error'] == 0) {
          // Moves the file to the target upload folder
          $target = "../" . GM_UPLOADPATH . basename($new_image);
          if (move_uploaded_file($_FILES['new_image']['tmp_name'], $target)) {
            // The new image file move was successful, now make sure any old image is deleted
            if (!empty($old_image) && ($old_image != $new_image)) {
              @unlink(GM_UPLOADPATH . $old_image);
            }
          }
          else {
            // The new image file move failed, so delete the temporary file and set the error flag
            @unlink($_FILES['new_image']['tmp_name']);
            $error = true;
            echo '<p class="error">Sorry, there was a problem uploading the image.</p>';
          }
        }
      }
      else {
        // The new image file is not valid, so delete the temporary file and set the error flag
        @unlink($_FILES['new_image']['tmp_name']);
        $error = true;
        echo '<p class="error">The image must be a GIF, JPEG, JPG or PNG image file no greater than ' . (MM_MAXFILESIZE / 1024) .
          ' KB in size.</p>';
      }
    }

    // Update the plan info in the database
if (isset($_POST['submit'])) {
	if ($_POST['confirm'] == 'Yes') {
		if (!$error) {
      		 if (!empty($model) && !empty($price) && !empty($minutes) && !empty($monthly)) {
        // Only set the image column if there is a new image
        		if (!empty($new_image)) {
          $query = "UPDATE goingmobile SET model = '$model', price = '$price', minutes = '$minutes', monthly = '$monthly', image = '$new_image' WHERE id = $id";
        }
        else {
          $query = "UPDATE goingmobile SET model = '$model', price = '$price', minutes = '$minutes', monthly = '$monthly' WHERE id = $id";
        }
        mysqli_query($dbc, $query);

        // Confirm success with the admin user
        echo '<p>The Going Mobile plan for ' . $model . ' was successfully Edited.</p>';

        mysqli_close($dbc);
        exit();
      }
      else {
        echo '<p class="error">You must enter all of the plan info (image is optional).</p>';
      }
    }
  } // End of check for form submission
  	else if (isset($id) && isset($model) && isset($price) && isset($minutes) && isset($monthly) && isset($image)) {
    echo '<p>Are you sure you want to edit the following Mobile Plan?</p>';
    echo '<p><strong>Plan Model: </strong>' . $model . '<br /><strong>Plan Price: </strong>' . $price .
      '<br /><strong>Plan Minutes: </strong>' . $minutes . '<br /><strong>Monthly Fees: </strong>' . $monthly . '</p>';
    echo '<form method="post" action="editplan.php">';
    echo '<input type="radio" name="confirm" value="Yes" /> Yes ';
    echo '<input type="radio" name="confirm" value="No" checked="checked" /> No <br />';
    echo '<input type="submit" value="Submit" name="submit" />';
    echo '<input type="hidden" name="id" value="' . $id . '" />';
    echo '<input type="hidden" name="model" value="' . $model . '" />';
    echo '<input type="hidden" name="price" value="' . $price . '" />';
echo '<input type="hidden" name="minutes" value="' . $minutes . '" />';
echo '<input type="hidden" name="monthly" value="' . $monthly . '" />';
echo '<input type="hidden" name="image" value="' . $image . '" />';
    echo '</form>';
	}
}
}
?>
  
  <hr />
  <form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo GM_MAXFILESIZE; ?>" />
    <label for="name">Phone Model:</label>
    <input type="text" id="model" name="model" value="<?php if (!empty($model)) echo $model; ?>" /><br />
    <label for="score">Phone Price:</label>
    <input type="text" id="price" name="price" value="<?php if (!empty($price)) echo $price; ?>" /><br />
    <label for="name">Plan Minutes:</label>
    <input type="text" id="minutes" name="minutes" value="<?php if (!empty($minutes)) echo $minutes; ?>" /><br />
    <label for="name">Monthly Fee:</label>
    <input type="text" id="monthly" name="monthly" value="<?php if (!empty($monthly)) echo $monthly; ?>" /><br />
    <label for="image">Model Image:</label>
    <input type="file" id="image" name="image" /><br />
    <input type="submit" value="Edit" name="submit" />
  </form>
  <hr />
</body>
</html>

 

 

Thanks

Hello PFMaBiSmAd thanks for replying.

 

Yes this is all the code from the file and is the code i am using.

 

It only contains update queries as this is what I am trying to do update a plan in the database. Change say the plan price or the plan image and so on. Should there maybe also be a select query somewhere?

 

Any input would really help as I am stuck on this one as I am new to php.

 

Appreciate any help. Thanks.

my editplan.php file actually adds a whole new plan duplicating the existing plan.

 

Cannot really help you with what that means without seeing a specific example of what is happening that leads you to believe that something is being added instead of being updated. For all we know you could be cross joining two tables in a SELECT query and getting two rows in a result set instead of one.

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.