Solved

PHP Form doesn't update in MySQL

Posted on 2011-03-06
4
263 Views
Last Modified: 2012-05-11
Hi,

When I hit "Submit" in this form it doesn't update the records while If I changed

$sql1="UPDATE my_table SET checked='$checked[$i]', text='$text[$i]' WHERE id='$id[$i]'";

Open in new window

to
$sql1="UPDATE my_table SET checked='C', text=D' WHERE id='345'";

Open in new window


It updates the record 345 just fine.


PS: the IDs are not auto increments. It's like 124, 455, 655, 686, 1023, etc...

I would really appreciate your help since it's making me crazy over.

Thanks
<?php ob_start(); ?>
<p>
<table class="container">
<form name="form1" method="post" action="">
<tr>
<td>ID</td>
<td>Checked</td>
<td>Text</td>
</tr>
<?php
mysql_connect("localhost", "username", "password");
mysql_select_db("mydatabase");
$sql = "select * from my_table order by id desc";
$query = mysql_query($sql);
while ($row = mysql_fetch_array($query)) {
?>
<tr>
<td><? $id[]=$row['id']; ?><?php echo $row['id'] ?></td>
<td><input name="checked[]" type="text" id="checked" value="<?php echo $row['checked']; ?>" maxlength="1" class="checked"></td>
<td><input name="text[]" type="text" id="text" value="<?php echo $row['text']; ?>" maxlength="1" class="checked"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="10" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</form>
</table>
<?php
if(isset($_POST['Submit'])){
for($i=0;$i<$count;$i++){
$sql1="UPDATE my_table SET checked='$checked[$i]', text='$text[$i]' WHERE id='$id[$i]'";
$result2 = mysql_query($sql1);
}
}
if($result2){
header("location:update.php");
ob_end_flush();
}
mysql_close(); ?>

Open in new window

0
Comment
Question by:Dido123
  • 2
4 Comments
 
LVL 7

Accepted Solution

by:
StraySod earned 500 total points
ID: 35045895
Hi,

I think you should use $_POST['checked'][$i] and $_POST['text'][$i] instead of $checked/$text vars and

$sql1="UPDATE my_table SET checked='{$_POST['checked'][$i]}', text='{$_POST['text'][$i]}' WHERE id='{$id[$i]}'";

don't forget to add curly braces {} around the array vars. It won't be recognized otherwise or you can concatenate more strings using dot operator like this:

$sql1="UPDATE my_table SET checked='" . {$_POST['checked'][$i] . "', text='". $_POST['text'][$i] . "' WHERE id='" . $id[$i] . "'";
 
0
 
LVL 11

Expert Comment

by:Ovid Burke
ID: 35046264
The variable $count is not defined, so I suspect that $i would have no value in your iteration.

Try:

$count = count($_POST['checked']);
for($i = 1; $i <= $count; $++) {

    // code to be executed

}

Open in new window


Note that because you are collecting the value of $i from a database record, it is unlikely that it would ever be zero. Hence your iteration may begin at 1.
0
 

Author Closing Comment

by:Dido123
ID: 35050453
It worked! :)


Thank you so much.
0
 
LVL 7

Expert Comment

by:StraySod
ID: 35055563
You're welcome!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Adding Extra Information box 4 26
PHP Sum Column in Table 3 28
Phone Dialer 5 38
confirm popup before record delete takes place 87 25
Creating and Managing Databases with phpMyAdmin in cPanel.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now