Jump to content

Recommended Posts

I have a dynamic form that I am writing a script for to allow people to modify the fields of the form. I am taking all of the information that they have entered into the post array.

 

Where I am having a problem is that it is not showing all of the fields in the post array. One of the fields is an image field and the name of the image does not show up in the array when I do print_r or anything.

 

One other thing that I would like to know is how to get rid of the submit key and value from the array. I have tried using the unset method but it still shows up in the array.

 

<?php

session_start();
include ('includes/connection.php');
connect();

$table = $_GET['table'];
$id = $_GET['id'];
$errors = array();

$mod_sql = "SELECT * FROM $table WHERE id = '$id'";
$mod_sql_result = mysql_query($mod_sql);

$info = mysql_fetch_assoc($mod_sql_result);

$mod_form = "<form name='insert_table' id='insert_table' action='" . $_SERVER['PHP_SELF'] . "?id=$id&table=$table' method='post' enctype='multipart/form-data'>
<fieldset>
<legend>table information</legend>
<input TYPE='hidden' VALUE='" . $table . "' />";

foreach ($info as $key => $val) {
    $header = str_replace("_", " ", $key);

    if ($key == 'id') {
$mod_form .= "<p>" . $header . ": <input name='" . $key . "' id='" . $key . "' type='text' value='" . $val . "' readonly='readonly' /></p>";
    }
    else if ($key == 'image') {
$mod_form .= "<p>Current image: <img style='vertical-align: text-top;' src='../db_images/ad_images/" . $val . "' /><input type='checkbox' name='delete_image'  id='delete_image' value='" . $val . "' />Delete this image?</p>
            <p>" . $header . ": <input name='" . $key . "' id='" . $key . "' type='file' /></p>";
    }
    else {
$mod_form .= "<p>" . $header . ": <input name='" . $key . "' id='" . $key . "' type='text' value='" . $val . "' /></p>";
    }
}

$mod_form .= "
<p>
<input type='submit' name='submit_mod' id='submit_mod' />
</p>
</fieldset>
</form>
";

echo $mod_form;

if (isset($_POST['submit_mod'])) {
    $table = $_POST['table'];
    $allowed_ext = array('jpg', 'jpeg', 'png', 'gif');
    $file_name = $_FILES['image']['name'];
    $file_ext = (explode(".", $file_name));
    $file_ext = strtolower(end($file_ext));
    $file_size = $_FILES['image']['size'];
    $file_tmp = $_FILES['image']['tmp_name'];

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

$delete_image_name = $_POST['delete_image'];
unlink('../db_images/ad_images/' . $delete_image_name);
    }

    if ($file_name != " ") {
$new_image = $file_name;
move_uploaded_file($file_tmp, '../db_images/ad_images/' . $file_name);
    }
// This is the section of code that I am having trouble with
foreach ($_POST as $key => $value) {

if ($key == 'image') {
    $value = $new_image;
}
$insert_query = "UPDATE $table SET `$key` = '$value' WHERE id = $id";
$insert_action = mysql_query($insert_query);
    }

    echo $insert_query;
}

if (mysql_error()) {
    $error = "<strong>MySQL error</strong>:<br />" . mysql_errno() . ": " . mysql_error() . "\n<br>When executing:<br>\n$insert_query\n<br>";
    $page = $_SERVER["REQUEST_URI"];

    $log = "INSERT INTO db_error (`error_id`, `error_page`,`error_text`,`query`) VALUES ('','$page','" . mysql_real_escape_string($error) . "','" . mysql_real_escape_string($mod_sql) . "')";
    $insert_log = mysql_query($log);

    echo $error;

    if ($insert_log) {
echo "<p>Error has been added to the error log</p><p>The error id number is " . mysql_insert_id() . "";
    }
    else {
echo "Error adding to error log<br />" . mysql_errno() . ": " . mysql_error();
    }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/261629-need-help-with-post-array/
Share on other sites

The image field is infact a file

use

print_r($_FILES);

for that one,

 

as for the submit button

unset should work..

if not just skip it

for example

unset($_POST['submit_mod']);
//OR
foreach ($_POST as $key => $value) {
if($key=='submit_mod') continue;

 

Hope this helps

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.