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
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
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
Share on other sites

if your input names are var1[], var2[]...

 

They are accessed via $_GET['var1'][] or $_POST['var1'][]...

 

Therefor, to access a collection of these, use

for ($i = 0; $i < count($_POST['var1']); $i++)
  echo $_POST['var1'][$i] . "<br />\n";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.