Solved

PHP Form doesn't update in MySQL

Posted on 2011-03-06
4
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 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