Jump to content

Help with database editing script


Nothadoth

Recommended Posts

Im using this script to edit the link names and urls in the Links page. There are more than 1 link so the While loop will display all of them.

The linksedit.php page works fine. But when I go to linksedit.php?mode=change (where it runs the script to edit the table rows) it says "You did not fill in the form properly).
So i'm thinking maybe i'll determined my variables wrong - but I can't find where.

Also could someone check for me the way it edits it, will it work? Because there will be more than one row edited.

Thank you very much in advance!

[code]$mode = $_GET['mode'];

if ($mode == "") {
  print "<table border='1' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#000000' width='100%'>
  <tr>
    <td width='100%' height='27' background='".$ROOT."styles/images/contbar.jpg'><center><span class='t1'>CHANGE LINKS</span></center></td>
  </tr>
  <tr>
    <td width='100%'><div align='center'>
  <center>
  <table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' width='95%'>
    <tr>
      <td width='100%'><span class='t2'>";
     
print "<form method='POST' action='aboutedit.php?mode=change'>
<center>";
     
mysql_connect('localhost','hidden','hidden');
mysql_select_db('hidden_igbltduk');  
$linksquery= mysql_query("SELECT * FROM links ORDER BY id DESC");
while($links = mysql_fetch_array($linksquery)) {

print "<table border='0' cellpadding='2' cellspacing='0' style='border-collapse: collapse'>
    <tr>
      <td width='200' height='22' align='center' valign='top'><span class='t2'>
      Link Name</span></td>
      <td height='22'><input type='text' name='name' size='20' value='".$links['name']."'></td>
    </tr>
    <tr>
      <td width='200' height='22' align='center' valign='top'><span class='t2'>
      Link URL</span></td>
      <td height='22'><input type='text' name='url' size='20' value='".$links['url']."'></td>
    </tr>
  </table><br>";
 
  }
 
  print "</center>
  <center>  <input type='submit' value='Submit' name='submitabout'><input type='reset' value='Reset' name='resetabout'></center>
</form>";
   
 
print "</span></td>
    </tr>
  </table>
  </center>
</div>
</td>
  </tr>
</table>
";
} elseif ($mode == "change") {
 
  $name = $_POST['name'];
  $url = $_POST['url'];
 
  if ($_POST['name'] == "" OR $_POST['url'] == "") {
    print "<table border='1' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#000000' width='100%'>
  <tr>
    <td width='100%' height='27' background='".$ROOT."styles/images/contbar.jpg'><center><span class='t1'>ERROR</span></center></td>
  </tr>
  <tr>
    <td width='100%'><div align='center'>
  <center>
  <table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' width='95%'>
    <tr>
      <td width='100%'><span class='t2'>";
     

     
      print "You did not fill in the form properly!";
     

 
print "</span></td>
    </tr>
  </table>
  </center>
</div>
</td>
  </tr>
</table>
";

} else {

  print "<table border='1' cellpadding='0' cellspacing='0' style='border-collapse: collapse' bordercolor='#000000' width='100%'>
  <tr>
    <td width='100%' height='27' background='".$ROOT."styles/images/contbar.jpg'><center><span class='t1'>LINKS PAGE EDITED</span></center></td>
  </tr>
  <tr>
    <td width='100%'><div align='center'>
  <center>
  <table border='0' cellpadding='0' cellspacing='0' style='border-collapse: collapse' width='95%'>
    <tr>
      <td width='100%'><span class='t2'>";
     
mysql_connect('localhost','hidden','hidden');
mysql_select_db('hidden_igbltduk');  
$linksquery= mysql_query("UPDATE links SET name = '$name', url = '$url'");

     
      print "Links page successfully changed!";
     

 
print "</span></td>
    </tr>
  </table>
  </center>
</div>
</td>
  </tr>
</table>
";
}

}[/code]
Link to comment
Share on other sites

Ok first. It is hard to see what you have posted here. Please ether rap the code in <?php ?> or place the php[] code blocks around it. It makes it easier on us.

Now I looked through your code and the only thing that I see that may be a issue is this.

[code=php:0]  if ($_POST['name'] == "" OR $_POST['url'] == "") {[/code]

give this a try

[code=php:0] if ((!$name) || (!$url)) {[/code]

You defined $name as $_POST['name'] and $url as $_POST['url'] so there is no need to use the full post in the if statment.


The way that I like to code things like this is with a function and switch statement. Here is an example of what I am talking about.

[code]
<?php
session_start();
$include("database.php");//your database connection file

function getmode($mode) {
     switch($mode) {
         case "add":
             //your code for the mode change
         break;
         case "edit":
             //your edit code
         break;
     default:
        //your code that you want processed if the mode is empty.
    }
getmode($_GET['mode']);
}
?>
[/code]

Now you can add as many cases as you want to this. You can also add a funtion like this inside of the case. You would link to it just like before, [b]whatever.php?mode=edit[/b] or [b]mode=add[/b]. I would recomend trying it this way.


Hope this helps,
Tom
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.