Solved

PHP Form doesn't update in MySQL

Posted on 2011-03-06
4
264 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

914 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

15 Experts available now in Live!

Get 1:1 Help Now