Jump to content

Multiple Update


phpretard

Recommended Posts

How do I post the array of information?

 

$_POST['name[$i]']  <<--This what I want to happen.

 

All works of course except the updating part.

 

The "UPDATE" line is wrong.

 

<?php
include ('connect.php');

$sql="SELECT * FROM links";
$result=mysql_query($sql);

$count=mysql_num_rows($result);
?>
<table width="500" border="0" cellspacing="1" cellpadding="0">
<form name="form1" method="post" action="test.php">
<tr> 
<td>
<table width="500" border="0" cellspacing="1" cellpadding="0">


<tr>
<td align="center"><strong>Id</strong></td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>url</strong></td>
<td align="center"><strong>sort</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td align="center"><? $id[]=$rows['id']; ?><? echo $rows['id']; ?></td>
<td align="center"><input name="name[]" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="url[]" type="text" id="url" value="<? echo $rows['url']; ?>"></td>
<td align="center"><input name="sort[]" type="text" id="sort" value="<? echo $rows['sort']; ?>"></td>
</tr>
<?php
}
?>
<tr>
<td colspan="4" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php

if(isset($_POST['Submit'])){
for($i=0;$i<$count;$i++){
$sql1="UPDATE links SET name='$name[$i]', url='$url[$i]', sort='$sort[$i]' WHERE id='$id[$i]'";
$result1=mysql_query($sql1);
}
}

if($result1){
echo "Worky";
}
mysql_close();
?>

Link to comment
https://forums.phpfreaks.com/topic/94303-multiple-update/
Share on other sites

I have to run off to class so can't be too much help, but you need to give your input names explicit names (NOT arrays). For example:

 

<input name="url_<?php echo $i; ?>" .....

 

and then put them together in an array after the form is submitted. Maybe someone can be  more explicity for you, gotta run.

Link to comment
https://forums.phpfreaks.com/topic/94303-multiple-update/#findComment-483016
Share on other sites

You are not calling the input arrays from POST

 

<?php
$name=$_POST['name[]'];
$url=$_POST['url[]'];
?>

 

If the code you have provided above is all in "form1" then i would move the part where you check if form is submited and process dabase updates at the begining of the file.  use if else to then give form if it hasnt been submitted.

 

if $_POST['submit']{

update database

}else{

give form

}

Link to comment
https://forums.phpfreaks.com/topic/94303-multiple-update/#findComment-483034
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.