therock1011 Posted April 19, 2010 Share Posted April 19, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/199047-edit-file-actually-adds-instead-of-editing/ Share on other sites More sharing options...
therock1011 Posted April 19, 2010 Author Share Posted April 19, 2010 any help on this anyone could really use some help on this or suggestions or input etc. Would really be appreciated. Thanks!!!! Quote Link to comment https://forums.phpfreaks.com/topic/199047-edit-file-actually-adds-instead-of-editing/#findComment-1044885 Share on other sites More sharing options...
PFMaBiSmAd Posted April 19, 2010 Share Posted April 19, 2010 The code you posted only contains UPDATE queries, therefore either that is not all the relevant code on the page or that is not the code you are using. Quote Link to comment https://forums.phpfreaks.com/topic/199047-edit-file-actually-adds-instead-of-editing/#findComment-1044887 Share on other sites More sharing options...
therock1011 Posted April 19, 2010 Author Share Posted April 19, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/199047-edit-file-actually-adds-instead-of-editing/#findComment-1044895 Share on other sites More sharing options...
PFMaBiSmAd Posted April 20, 2010 Share Posted April 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/199047-edit-file-actually-adds-instead-of-editing/#findComment-1044922 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.