Jump to content

[SOLVED] Brain teaser


Recommended Posts

ok i am trying to make the navbar for my site editable from a another page, and i have the code right(I think), but its a brain teaser, i cant seem to understand (i guess you could say) how to update each and every link.

 

heres my code for the update page

 

<?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);

	while($row = mysql_fetch_assoc($result)) {

		echo'<tr><td>Link ', $row['id'], ' <input type="text" size="25" maxlength="200" name="linkname', $row['id'], '" value="', $row ['linkname'], '"></td><td> <input type="text" size="25" maxlength="200" name="link', $row['id'], '" value="', $row ['link'], '"> </td></tr>';

	}
	?>
        <tr><td><input type="submit" name="update" value="Change Navbar"></td></tr>
</table>
</form>




<?php

        }
        //Or else it has been submitted...
        else {
            //Get information from the forms secure it all.
            
             $linkname1= secure($_POST['linkname1']);
		 $link1 = secure($_POST['link1']);
		 $linkname2= secure($_POST['linkname2']);
		 $link2 = secure($_POST['link2']);
		 $linkname3= secure($_POST['linkname3']);
		 $link3 = secure($_POST['link3']);
		 $linkname4= secure($_POST['linkname4']);
		 $link4 = secure($_POST['link4']);
		 $linkname5= secure($_POST['linkname5']);
		 $link5 = secure($_POST['link5']);
		 $linkname6= secure($_POST['linkname6']);
		 $link6 = secure($_POST['link6']);
		 $linkname7= secure($_POST['linkname7']);
		 $link7 = secure($_POST['link7']);

            $update = @mysql_query("UPDATE `nav` SET linkname1 = '$linkname1', link1 = '$link1', linkname2 = '$linkname2', link2 = '$link2', linkname3 = '$linkname3', link3 = '$link3', linkname4 = '$linkname4', link4 = '$link4', linkname5 = '$linkname5', link5 = '$link5', linkname6 = '$linkname6', link6 = '$link6', linkname7 = '$linkname7', link7 = '$link7'");



            if ($update)
                echo 'The NavBar has been changed, <a href="index.php?action=general">Click Here</a> to go back.';

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

            //A query to update everything


	}
}
}


?>

 

heres the stucture of my SQL Table

 

CREATE TABLE `nav` (

  `id` int(11) NOT NULL auto_increment,

  `linkname` varchar(50) NOT NULL,

  `link` varchar(100) NOT NULL,

  PRIMARY KEY  (`id`)

) ENGINE=MyISAM  DEFAULT CHARSET=latin1

 

and the id goes from 1 to 7

Link to comment
Share on other sites

Well, putting the bad database design aside, do this - for the name attribute for the input fields, use linkname[$row['id']] and link[$row['id']].

 

Then in your else case (where the form is submitted), you can do this -

<?php
$linknames = $_POST['linkname'];
$links = $_POST['link'];

$errors = array();
foreach ($linknames as $key => $value) {
     // optional, but this checks if everything is okay and not empty
     if (isset($links[$key]) && !empty($links[$key]) && !empty($value)) {
          $query = mysql_query(sprintf('UPDATE nav SET linkname%1$d = "%2$s" link%1$d = "%3$s" WHERE id = %1$d LIMIT 1;', $key, $value, $links[$key]));
          if (!$query) $errors[] = array('id' => $key, 'msg' => mysql_error());
     }
}

if ($errors = count($errors)) echo sprintf('%d were found.', $errors);

Link to comment
Share on other sites

The query you're executing doesn't correspond to query you're executing. I'm going to assume the CREATE TABLE statement is correct because it looks like something that the phpMyAdmin export could have generated.

 

So using the array structure Ken suggested, something like this:

 

foreach ($_POST['linkname'] as $id => $linkname) {
if (!isset($_POST['link'][$id])) {
	continue;
}

mysql_query(sprintf("UPDATE nav SET linkname = '%s', link = '%s' WHERE id = %d",
	mysql_real_escape_string($linkname),
	mysql_real_escape_string($_POST['link'][$id]),
	$id
));
}

Link to comment
Share on other sites

like this?

 

<?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);

	while($row = mysql_fetch_assoc($result)) {

		echo'<tr><td>Link ', $row['id'], ' <input type="text" size="25" maxlength="200" name="linkname', $row['id'], '" value="', $row ['linkname'], '"></td><td> <input type="text" size="25" maxlength="200" name="link', $row['id'], '" value="', $row ['link'], '"> </td></tr>';

	}
	?>
        <tr><td><input type="submit" name="update" value="Change Navbar"></td></tr>
</table>
</form>




<?php

        }
        //Or else it has been submitted...
        else {
            //Get information from the forms secure it all.
            
             $linkname1= secure($_POST['linkname1']);
		 $link1 = secure($_POST['link1']);
		 $linkname2= secure($_POST['linkname2']);
		 $link2 = secure($_POST['link2']);
		 $linkname3= secure($_POST['linkname3']);
		 $link3 = secure($_POST['link3']);
		 $linkname4= secure($_POST['linkname4']);
		 $link4 = secure($_POST['link4']);
		 $linkname5= secure($_POST['linkname5']);
		 $link5 = secure($_POST['link5']);
		 $linkname6= secure($_POST['linkname6']);
		 $link6 = secure($_POST['link6']);
		 $linkname7= secure($_POST['linkname7']);
		 $link7 = secure($_POST['link7']);

            foreach ($_POST['linkname'] as $id => $linkname) {
if (!isset($_POST['link'][$id])) {
	continue;
}

$update = mysql_query(sprintf("UPDATE nav SET linkname = '%s', link = '%s' WHERE id = %d",
mysql_real_escape_string($linkname),
mysql_real_escape_string($_POST['link'][$id]),
$id
));

}
            if ($update)
                echo 'The NavBar has been changed, <a href="index.php?action=general">Click Here</a> to go back.';

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

            //A query to update everything


	}
}
}


?>

 

if thats right i'm getting an error but it doesn't say what error

Link to comment
Share on other sites

ok heres what i got and the same thing happens

 

 

<?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);

	while($row = mysql_fetch_assoc($result)) {

		echo'<tr><td>Link ', $row['id'], ' <input type="text" size="25" maxlength="200" name="linkname"',[$row['id']], '" value="', $row ['linkname'], '"></td><td> <input type="text" size="25" maxlength="200" name="link"', [$row['id']], '" value="', $row ['link'], '"> </td></tr>';

	}
	?>
        <tr><td><input type="submit" name="update" value="Change Navbar"></td></tr>
</table>
</form>




<?php

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

$errors = array();
foreach ($linknames as $key => $value) {
     // optional, but this checks if everything is okay and not empty
     if (isset($links[$key]) && !empty($links[$key]) && !empty($value)) {
          $query = mysql_query(sprintf('UPDATE nav SET linkname%1$d = "%2$s" link%1$d = "%3$s" WHERE id = %1$d LIMIT 1;', $key, $value, $links[$key]));
          if (!$query) $errors[] = array('id' => $key, 'msg' => mysql_error());
     }
}

if ($errors = count($errors)) echo sprintf('%d were found.', $errors);
            //A query to update everything


	}
}
}


?>

Link to comment
Share on other sites

ok i looked over the script and heres what i came up with it doesn't work either,

 

<?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%">
<tr><td>Site Name <input type="text" size="25" maxlength="200" name="sitename" value="<?php echo
"$sitename"; ?>"> </td></tr>
<tr><td>Site URL <input type="text" size="25" maxlength="200" name="siteurl" value="<?php echo
"$siteurl"; ?>"> </td></tr>
<tr><td>Site Description <textarea class="textbox" cols="40" rows="10" name="sitedesc"><?php echo
"$sitedesc"; ?></textarea> </td></tr>
<tr><td>Admin email <input type="text" size="25" maxlength="200" name="aemail" value="<?php echo
"$aemail"; ?>"> </td></tr>
<tr><td>Contcat Us Link <input type="text" size="25" maxlength="200" name="emailink" value="<?php echo
"$emailink"; ?>"> </td></tr>

<tr><td><input type="submit" name="update" value="Change Settings"></td></tr>
</table>

</form>

<?php

        }
        //Or else it has been submitted...
        else {
            //Get information from the forms secure it all.
            
            $sitename= secure($_POST['sitename']);
            $siteurl = secure($_POST['siteurl']);
            $sitedesc = secure($_POST['sitedesc']);
            $aemail = secure($_POST['aemail']);
            $emailink = secure($_POST['emailink']);

            $update = @mysql_query("UPDATE `info` SET sitename = '$sitename', siturl = '$siteurl' sitedesc = '$sitedesc', aemail = '$aemail', emailink = '$emailink' WHERE id = '1'");
            
    
            
            if ($update)
                echo 'The settings has been changed, <a href="index.php?action=general">Click Here</a> to go back.';
                
            else
               echo "Error message = ".mysql_error(); 

            //A query to update everything


        }
    }
}


?> 

Link to comment
Share on other sites

changed the while statement a bit

<?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>Link ' . $row['id'] . '<input type="text" size="25" maxlength="200" name="linkname' . [$row['id']] . '" value="' . $row ['linkname'] .'"></td><td> <input type="text" size="25" maxlength="200" name="link"' . [$row['id']] . '" value="' . $row ['link'] .'"> </td></tr>';
        }  
        ?>
        <tr><td><input type="submit" name="update" value="Change Navbar"></td></tr>
    </table>
</form>
<?php
}
//Or else it has been submitted...
else {
//Get information from the forms secure it all.
            
$linknames = $_POST['linkname'];
$links = $_POST['link'];

$errors = array();
foreach ($linknames as $key => $value) {
     // optional, but this checks if everything is okay and not empty
     if (isset($links[$key]) && !empty($links[$key]) && !empty($value)) {
          $query = mysql_query(sprintf('UPDATE nav SET linkname%1$d = "%2$s" link%1$d = "%3$s" WHERE id = %1$d LIMIT 1;', $key, $value, $links[$key]));
          if (!$query) $errors[] = array('id' => $key, 'msg' => mysql_error());
     }
}

if ($errors = count($errors)) echo sprintf('%d were found.', $errors);
            //A query to update everything


        }
    }
}


?> 

Link to comment
Share on other sites

i searched for a syntax checker and found one it says

 

Parse error:  parse error, unexpected '[' in nav.php on line 18

 

heres the line

 

 echo '<tr><td>Link ' . $row['id'] . '<input type="text" size="25" maxlength="200" name="linkname' . [$row['id']] . '" value="' . $row ['linkname'] .'"></td><td> <input type="text" size="25" maxlength="200" name="link"' . [$row['id']] . '" value="' . $row ['link'] .'"> </td></tr>';

Link to comment
Share on other sites

Fixed it

 

HEres the old code

 

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

 

and the after(look at the name piece of the input statements)

<?php
while($row = mysql_fetch_assoc($result)) {
            echo '<tr><td>Link ' . $row['id'] . '<input type="text" size="25" maxlength="200" name="linkname' . $row['id'] . '" value="' . $row ['linkname'] .'"></td><td> <input type="text" size="25" maxlength="200" name="link"' . $row['id'] . '" value="' . $row ['link'] .'"> </td></tr>';
?>

 

extra "[, ]" lol

 

Problem Solved

 

but now it's not changing anything in the database

 

<?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>Link ' . $row['id'] . '<input type="text" size="25" maxlength="200" name="linkname' . $row['id'] . '" value="' . $row ['linkname'] .'"></td><td> <input type="text" size="25" maxlength="200" name="link"' . $row['id'] . '" value="' . $row ['link'] .'"> </td></tr>';
        }  
        ?>
        <tr><td><input type="submit" name="update" value="Change Navbar"></td></tr>
    </table>
</form>
<?php
}
//Or else it has been submitted...
else {
//Get information from the forms secure it all.
            
$linknames = $_POST['linkname'];
$links = $_POST['link'];

$errors = array();
foreach ($linknames as $key => $value) {
     // optional, but this checks if everything is okay and not empty
     if (isset($links[$key]) && !empty($links[$key]) && !empty($value)) {
          $query = mysql_query(sprintf('UPDATE nav SET linkname%1$d = "%2$s" link%1$d = "%3$s" WHERE id = %1$d LIMIT 1;', $key, $value, $links[$key]));
          if (!$query) $errors[] = array('id' => $key, 'msg' => mysql_error());
     }
}

if ($errors = count($errors)) echo sprintf('%d were found.', $errors);
            //A query to update everything


        }
    }
}


?> 

[/code]

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.