Jump to content

Trouble With $_Post Array


phpnoob1991

Recommended Posts

I have a MySQL database with the table "chapters". This table has three columns: id, name, password. I am automatically outputting the contents of this table via an HTML form (it's an update page where the user can edit all of the values). Here's the code for the output:

 

<table>

<?php
while ($row=mysql_fetch_array($result)) {
echo "<tr id='chapter-{$row['id']}' chapter-id='{$row['id']}'>";
echo "<td>";
echo "<input type='text' name='chapter[][id]' value='{$row['id']}'>";
echo "</td>";
echo "<td>";
echo "<input type='text' name='chapter[][name]' value='{$row['name']}'>";
echo "</td>";
echo "<td>";
echo "<input type='text' name='chapter[][password]' value='New Password'>";
echo "</td>";
echo "<td>";
echo "<img class='delete-chapter' src='reject-button.png' alt='Delete Button'>";
echo "</td>";
echo "</tr>";
}
?>

</table>

 

I am passing all of that information into ChaptersUpdate.php via a submit button that's on the bottom of that form. The idea is that ChaptersUpdate.php should look at all of the information within $_POST and update the "chapters" table so that it reflects the new values that the user put in. Here's the PHP code:

 

<?php

include 'database_login.php';

foreach ($_POST['chapter'] as $chapter_id => $chapter_info)
{

mysql_query("UPDATE chapters SET name={$chapter_info['name']}, password={$chapter_info['password']} WHERE id={$chapter_info['id']}");

}

?>

 

Only problem is that ChaptersUpdate.php just doesn't work...the "chapters" table goes completely unchanged. There is no error_log being generated, so I really don't think there's a problem connecting to the database.

 

Any ideas why it's not working?

 

Thanks in advance!

Link to comment
Share on other sites

Store your query in a variable then simply echo it. eg;

 

 

$sql = "UPDATE chapters SET name={$chapter_info['name']}, password={$chapter_info['password']} WHERE id={$chapter_info['id']}";

echo $sql;

 

WOW, clearly there is a BIG problem somewhere. This is what I get:

 

UPDATE chapters SET name=, password= WHERE id=132UPDATE chapters SET name=MIT, password= WHERE id=UPDATE chapters SET name=, password=New Password WHERE id=UPDATE chapters SET name=, password= WHERE id=133UPDATE chapters SET name=Harvard, password= WHERE id=UPDATE chapters SET name=, password=New Password WHERE id=

 

I don't understand, is the problem with the way I set up the multidimensional array or with the way I am handing it in ChaptersUpdate.php?

Link to comment
Share on other sites

echo $_POST array in ChaptersUpdate.php page and let us know whats in it...

 

you can use

print_r($_POST);

to check your $_POST array in ChaptersUpdate.php

 

This is the result (original HTML form contained two rows from the database):

 

Array ( [chapter] => Array ( [0] => Array
( [id] => 132 ) [1] => Array ( [name] => MIT ) [2] => Array
( [password] => New Password ) [3] => Array
( [id] => 133 ) [4] => Array ( [name] => Harvard ) [5] => Array
( [password] => New Password ) ) )

 

Seeing this, I am beginning to think the problem is with the way I am setting up the multidimensional array...

 

I was originally hoping for something like this:

 

Array ([chapter] => Array (

[0] => Array

([id] =>132

[name] =>some name

[password] => some password)

[1] => Array

([id] => 133

[name] => some name

[password] => some password))

Edited by phpnoob1991
Link to comment
Share on other sites

post your query that comes to $result variable.

 

it seems you dont need to use multidiamentional array.. suppose you have a table that contain user information such as name, password, etc. so when you need to update user information on your update.php page you need only update one particular user's information...

Link to comment
Share on other sites

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.