Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

PHP Form doesn't update in MySQL

Posted on 2011-03-06
4
Medium Priority
?
272 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
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 dynamically set the form action using jQuery.
Suggested Courses

604 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