Jump to content


Photo

Help with database editing script


  • Please log in to reply
1 reply to this topic

#1 Nothadoth

Nothadoth
  • Members
  • PipPipPip
  • Advanced Member
  • 119 posts

Posted 17 August 2006 - 11:31 AM

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!

$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>
";
}

}


#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 17 August 2006 - 12:49 PM

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.

  if ($_POST['name'] == "" OR $_POST['url'] == "") {

give this a try

if ((!$name) || (!$url)) {

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.

<?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']);
}
?>

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, whatever.php?mode=edit or mode=add. I would recomend trying it this way.


Hope this helps,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users