Jump to content

Archived

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

ludwigvb

Form multi update problem

Recommended Posts

Hello,

I have made a form where I can update all the entries of my mysql database table.
I have made it so it makes the fields automaticly.
How can I get the variables back to update the mysql table?

Here is the code:
(read the comments to know what I mean)

[code]<?php

include 'config.php';
include 'menu.php' ;

$db = mysql_connect($server_name,$user,$password);
@mysql_select_db($database) or die( "Unable to select database");

$sql="SELECT * FROM links ORDER BY cat_id ASC, name ASC";
mysql_query("SET NAMES 'utf8'");
$result=mysql_query($sql);

// Count table rows
$count=mysql_num_rows($result);
$rows=mysql_fetch_array($result) ;
?>
<table width="500" border="0" cellspacing="1" cellpadding="0" align="center">
<form name="form1" method="post" action="">
<tr>
<td align="center">
<?php echo $lang_links_edit ; ?>
<table width="90%" border="0" cellspacing="1" cellpadding="0">

<?php
echo'
<tr>
<td colspan="10" align="center"><a href="links_edit.php">'. $lang_links_edit .'</a></td>
</tr>
<tr class="odd">
<td align="center"><strong>'. $lang_cat_id .'</strong></td>
<td align="center"><strong>'. $lang_url .'</strong></td>
<td align="center"><strong>'. $lang_links_name .'</strong></td>
<td align="center"><strong>'. $lang_link_cat_name .'</strong></td>
<td align="center"><strong>'. $lang_link_submiter .'</strong></td>
<td align="center"><strong>'. $lang_link_subemail .'</strong></td>
<td align="center"><strong>'. $lang_link_back .'</strong></td>
<td align="center"><strong>'. $lang_desc .'</strong></td>
<td align="center"><strong>'. $lang_link_approval .'</strong></td>
<td align="center"><strong>'. $lang_delete .'</strong></td>
</tr> ';

$i=0 ;
while($i < $count){

$id=mysql_result($result,$i,"id");
$url=mysql_result($result,$i,"url");
$description=mysql_result($result,$i,"description");
$name=mysql_result($result,$i,"name");
$submiter=mysql_result($result,$i,"submiter");
$email=mysql_result($result,$i,"email");
$link_back=mysql_result($result,$i,"link_back");
$cat_id=mysql_result($result,$i,"cat_id");
$approved=mysql_result($result,$i,"approved");
?>
<tr>
<td align="center"><? echo $id ; ?></td>
<td align="center"><input name="url_<?php echo $id ; ?>" type="text" id="url" value="<? echo $url ; ?>"></td>
<td align="center"><input name="name_<?php echo $id ; ?>" type="text" id="name" value="<? echo $name ; ?>"></td>

<td align="center">
<select name="cat_id_<?php echo $id ; ?>">

<?php // GET CURRENT CATEGORY NAME

// $db = mysql_connect($server_name,$user,$password);
// @mysql_select_db($database) or die( "Unable to select database");

$id_ap = $id ;
$sql_edit="SELECT * FROM links WHERE id='$id_ap'";
mysql_query("SET NAMES 'utf8'");
$result_edit=mysql_query($sql_edit);
$row_edit=mysql_fetch_array($result_edit);

$cat_id_now= $row_edit['cat_id'];
$sql_cat_name="SELECT * FROM categories WHERE cat_id='$cat_id_now'";
mysql_query("SET NAMES 'utf8'");
$result_cat_name=mysql_query($sql_cat_name);
$row_cat_name=mysql_fetch_array($result_cat_name);

echo '<option value="'.$row_cat_name['cat_id'].'">'.$row_cat_name['cat_name'].'</option>';

// GET CATEGORIES NAME TO CREATE LIST
$sql2="SELECT * FROM categories";
$result2=mysql_query($sql2);

$count_cats=mysql_num_rows($result2);

$j=0;
while ($j < $count_cats) {

$cat_ids=mysql_result($result2,$j,"cat_id");
$cat_names=mysql_result($result2,$j,"cat_name");

echo '

<option value="'.$cat_ids.'">'.$cat_names.'</option>

';
$j++ ;
}

echo '</select></td>';
?>

<td align="center"><input name="submiter_<?php echo $id ; ?>" type="text" id="submiter" value="<? echo $submiter ; ?>"></td>
<td align="center"><input name="email_<?php echo $id ; ?>" type="text" id="email" value="<? echo $email ; ?>"></td>
<td align="center"><input name="link_back_<?php echo $id ; ?>" type="text" id="link_back" value="<? echo $link_back ; ?>"></td>
<td align="center"><input name="description_<?php echo $id ; ?>" type="text" id="description" value="<? echo $description ; ?>"></td>


<td align="center"><input name="approved_<?php echo $id ; ?>" type="checkbox" id="approved" value="1" <?php if ($approved == "1") { echo "checked=\"checked\""; } ?>></td>



<td align="center"><input name="del_<?php echo $id ; ?>" type="checkbox" id="del" value="1"></td>

</tr>
<?php
$i++ ;
}
?>
<tr>
<td colspan="10" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</td>
</tr>
</form>
</table>
<?php
// Check if button name "Submit" is active, do this
if($Submit){
for($i=0;$i<$count;$i++){


// WHAT SHOULD I PUT HERE TO CREATE THE VARIABLES THAT GET POSTED FROM THE FORM???? <<<<<<<<<<<<=======================
$id = $id_.''.$id ;
$url = $url_.''.$id ;
$description = $description_.''.$id ;
$name = $name_.''.$id ;
$submiter = $submiter_.''.$id ;
$email = $email_.''.$id ;
$link_back = $link_back_.''.$id ;
$cat_id = $cat_id_.''.$id ;
$approved = $approved_.''.$id ;
$del = $del_.''.$id ;
// WHAT SHOULD I PUT HERE TO CREATE THE VARIABLES THAT GET POSTED FROM THE FORM???? <<<<<<<<<<<<=======================


echo $id .','.$url .','.$description .','. $name .','. $submiter .','. $email .','. $link_back .','. $cat_id .','. $approved .','. $del .'<br>' ;

$sql3="UPDATE links SET id='$id', url='$url', description='$description', name='$name', submiter='$submiter', email='$email', link_back='$link_back', cat_id='$cat_id', approved='$approved' WHERE id='$id'";
mysql_query("SET NAMES 'utf8'") or DIE("ERROR");
$result3=mysql_query($sql3) or DIE("ERROR");
}

// echo '<META HTTP-EQUIV="Refresh"
//      CONTENT="0; URL=links_edit.php">';
}

mysql_close();
?>[/code]

Share this post


Link to post
Share on other sites
you're inputs are stored in the $_POST array.  You need to have an action set for your form.  The action can be the same page or another page.  Either way, you get your POSTed variables like this-

$id=$_POST['id_'.$id];
$url-$_POST['url_'.$id];
$description=$_POST['description_'.$id];
etc.

you can use the same page by creating a conditional as follows
if(isset($_POST))
{
    //create you output page
}
else
{
    //create your form page
}

Share this post


Link to post
Share on other sites
As far as I know, if you just want to dump them into variables that are named corrresponding to the POST key, this would work as well (it's faster than individual assignments, or referencing the $_POST global a number of times):

[code]
<?php
.....

foreach ($_POST as $key => $val)
{
  $$key = $val;  // Pushes the value of $key to it's own variable, named accordingly, and assigns $val to it.
}

.....
?>
[/code]

That should give you a full spectrum of POST variables, all separate and easy to reference.

You can also use the $_REQUEST global, but if my memory serves me, this was less secure than $_POST.  I'll have to check documentation to refresh myself.

Share this post


Link to post
Share on other sites

×

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.