Solved

PHP Form doesn't update in MySQL

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
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 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 …

770 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