Jump to content

Database Table not Updating


papillonstudios

Recommended Posts

I'm trying to make the navbar for my cms editable from the admin panel, but well no success.

 

heres 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>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

Referring this --- linkname' . $row['id'] it is creating linkname1,linkename2,linkname3 so $linksnames is not an array try

 

print linkname1 [ considering $row['id'] = 1]

 

I think you are trying to make this as an array? Right?

Link to comment
Share on other sites

ok lets make this simple i will explain what i want it to do and well will go form there.

 

I want the script to show 14 fields

 

7 linknames

and

7 links

 

and these fields will show to current linkname and link for a link in the navbar.(stored in database)

 

then you can change the name of a link or the link url itself and click submit and it changes it in the database thus displaying the new link name or url in the navbar.

 

if theres a better way of doing it then please tell

 

Link to comment
Share on other sites

I didn't understand but is following is your requirement

 

link 1 :yahoo http://www.yahoo.com

link 2:blog http://www.dummy.com

 

submit button

 

So when you change yahoo to gmail and link to www.gmail.com in database Link 1 sud get update to gmail-linkname and http://www.gmail.com as link (right)

 

 

If above is the scenario then  i recommend you to use 'update button' in front of all link set i.e

 

link1 yahoo link update link

link2 gmail  link  update link

 

so when you click update button this will identify which link set to get modify .i.e

 

$url= "link=$row['linkname']&linkurl=$row['link']";

 

<a href="samescript.name?$url"/>update</a>

 

so when you click on update link it will hit the same  script and perform the update query

 

if(isset($_GET['link']) && isset($_GET['linkname']))

{

  query to update

}

 

 

I hope this may help. Let me know if  any issues you come across.

 

 

 

Link to comment
Share on other sites

ok i get what you mean and thats a great idea but what would the update query look like?

 

ok heres what the navbar looks like.(currently

 

Home <> Downloads <> Games <> Videos <> LinkName <> LinkName

 

you get the point and then i have the nav.php folder in the admin section.(currently)

 

[LinkName]    [Link]

 

[LinkName]    [Link]

 

[LinkName]    [Link]

 

[LinkName]    [Link

 

[LinkName]    [Link]

 

[LinkName]    [Link]

 

[LinkName]    [Link]

 

[update Links]

 

 

Link to comment
Share on other sites

I got it working, but now i got another most likely easier to solve.

 

It gives me this error when i submit a link(i took your advice and make a change link button for each)

 

heres the error

Error message = You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' url = WHERE id =' at line 1

 

heres my 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 "' . $row[' id'] . '></td></tr>';
        }  
        ?>
    </table>
</form>


<?php

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

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



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

Error message = You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' url = WHERE id =' at line 1

 

url = WHERE id =,

none of the variables are being sent in the url?

...

does your url look like

whatever.php?id=X&url=Z&linkname=Y

 

x, y and z are obviously the user input from the field.

 

you should change the db query to the following in case the variables arent sent.

//check if linkname, url and id are sent in the URL
if (isset($_GET['linkname'], $_GET['url'], $_GET['id']) {
$update = mysql_query("UPDATE nav SET linkname = " . $_GET['linkname'] . " , url = " . $_GET['url'] . " WHERE id = " . $_GET['id'] . ";");
}

 

 

 

 

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.