phpnoob1991 Posted September 23, 2012 Share Posted September 23, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/268689-trouble-with-_post-array/ Share on other sites More sharing options...
trq Posted September 23, 2012 Share Posted September 23, 2012 echo your query so you can see what is actually happening. Quote Link to comment https://forums.phpfreaks.com/topic/268689-trouble-with-_post-array/#findComment-1380231 Share on other sites More sharing options...
phpnoob1991 Posted September 23, 2012 Author Share Posted September 23, 2012 echo your query so you can see what is actually happening. Eeek, sorry, how would I do that? I am fairly new to PHP and MySQL, and am learning on the go. Would I just use the regular "echo"? Quote Link to comment https://forums.phpfreaks.com/topic/268689-trouble-with-_post-array/#findComment-1380235 Share on other sites More sharing options...
trq Posted September 23, 2012 Share Posted September 23, 2012 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; Quote Link to comment https://forums.phpfreaks.com/topic/268689-trouble-with-_post-array/#findComment-1380237 Share on other sites More sharing options...
phpnoob1991 Posted September 23, 2012 Author Share Posted September 23, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/268689-trouble-with-_post-array/#findComment-1380240 Share on other sites More sharing options...
thara Posted September 23, 2012 Share Posted September 23, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/268689-trouble-with-_post-array/#findComment-1380243 Share on other sites More sharing options...
phpnoob1991 Posted September 23, 2012 Author Share Posted September 23, 2012 (edited) 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 September 23, 2012 by phpnoob1991 Quote Link to comment https://forums.phpfreaks.com/topic/268689-trouble-with-_post-array/#findComment-1380244 Share on other sites More sharing options...
thara Posted September 23, 2012 Share Posted September 23, 2012 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... Quote Link to comment https://forums.phpfreaks.com/topic/268689-trouble-with-_post-array/#findComment-1380251 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.