Jump to content

Variable Troubles


Recommended Posts

I have a form 14 fields, heres what the form looks like:

 

LinkName      URL

LinkName      URL

LinkName      URL

LinkName      URL

LinkName      URL

LinkName      URL

 

the form is generated with a while loop.

 

<form method="post">
    <table width="75%">
        <?php
        //Selecting the News From trhe Table news
        $query = "SELECT * FROM `nav`";
        $result = mysql_query($query) or die(mysql_error());

        while($row = mysql_fetch_assoc($result)) {
            echo '<tr><td><input type="text" size="25" maxlength="200" name="linkname" value="' . $row ['linkname'] .'"></td><td> <input type="text" size="25" maxlength="200" name="url" value="' . $row ['url'] .'"> </td><td><input type="submit" name="update" value="Change Link "></td></tr>';
        }  
        ?>
    </table>
</form>

 

and heres all the code

<?php
//Checks to see if theyre allowed to edit their profile
if ($uCan['admin']) {
    //Double security incase the admin hasn't set a guest membergroup
    if ($uId) {

        //If the form hasn't been submitted, show it.
        if (!$_POST['update']) {
?>
<form method="post">
    <table width="75%">
        <?php
        //Selecting the News From trhe Table news
        $query = "SELECT * FROM `nav`";
        $result = mysql_query($query) or die(mysql_error());

        while($row = mysql_fetch_assoc($result)) {
            echo '<tr><td><input type="text" size="25" maxlength="200" name="linkname" value="' . $row ['linkname'] .'"></td><td> <input type="text" size="25" maxlength="200" name="url" value="' . $row ['url'] .'"> </td><td><input type="submit" name="update" value="Change Link "></td></tr>';
        }  
        ?>
    </table>
</form>


<?php

        }
        //Or else it has been submitted...
        else {
            //Get information from the forms secure it all.
            
            $linkname = secure($_POST['linkname']);
		$url = secure($_POST['url']);

		$update = mysql_query("UPDATE nav SET linkname = '" . $_GET['linkname'] . "' , url = '" . $_GET['url'] . "' WHERE id = " . $_GET['id'] . ";");
		echo '<p>' . $linkname. '</p>';
				  echo '<p>' . $url . '</p>';


            if ($update)
                echo 'Link ' . $row['id'] . ' had been updated, click <a href="index.php?action=upnav">here</a> ';

            else
               echo "Error message = ".mysql_error(); 

            //A query to update everything


	}
}
}


?>

 

Now where i declare the 2 variables

$linkname = secure($_GET['linkname']);
 $url = secure($_GET['url']);

is where i want the value of the fields that correspond with the submit button to be put there.

 

When i echo the variables, no matter what button i click it displays the values of the last LinkName and URL.

 

How do i fix it? I will know what i will have to do with the SQL UPDATE.

 

Thanks in advance

Link to comment
https://forums.phpfreaks.com/topic/164794-variable-troubles/
Share on other sites

That is because all your fields have the same name and the last variable overwrites the all other. You better make them arrays like:

 

<input type="text" size="25" maxlength="200" name="linkname"[] value="' . $row ['linkname'] .'">

<input type="text" size="25" maxlength="200" name="url[]" value="' . $row ['url'] .'">

 

So then in the $_POST['linkname'] and $_POST['url] You will have arrays with the the info from all the fields

Link to comment
https://forums.phpfreaks.com/topic/164794-variable-troubles/#findComment-868987
Share on other sites

ok heres what i came up with

 

<?php
//Checks to see if theyre allowed to edit their profile
if ($uCan['admin']) {
    //Double security incase the admin hasn't set a guest membergroup
    if ($uId) {

        //If the form hasn't been submitted, show it.
        if (!$_POST['update']) {
?>
<form method="post">
    <table width="75%">
        <?php
        //Selecting the News From trhe Table news
        $query = "SELECT * FROM `nav`";
        $result = mysql_query($query) or die(mysql_error());

        while($row = mysql_fetch_assoc($result)) {
            echo '<tr><td><input type="text" size="25" maxlength="200" name="linkname"' . $row['id'] . '" value="' . $row ['linkname'] .'"></td><td> <input type="text" size="25" maxlength="200" name="url"' . $row['id'] . '"" value="' . $row ['url'] .'"> </td><td><input type="submit" name="update" value="Change Link "></td></tr>';
        }  
        ?>
    </table>
</form>


<?php

        }
        //Or else it has been submitted...
        else {
            //Get information from the forms secure it all.
            
            $linkname = secure($_POST['linkname']);
		$url = secure($_POST['url']);

		//$update = mysql_query("UPDATE nav SET linkname = '$linkname' , url = '$url' WHERE id = " . $_GET['id'] . ";");
		echo '<p>' . $linkname. '</p>';
				  echo '<p>' . $url . '</p>';


            if ($update)
                echo 'Link ' . $row['id'] . ' had been updated, click <a href="index.php?action=upnav">here</a> ';

            else
               echo "Error message = ".mysql_error(); 

            //A query to update everything


	}
}
}


?>

Link to comment
https://forums.phpfreaks.com/topic/164794-variable-troubles/#findComment-869024
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.