Please Help With Adding / Removing Content

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ORiGIN
Forum Newbie
Posts: 20
Joined: Sat Nov 11, 2006 11:17 pm

Please Help With Adding / Removing Content

Post by ORiGIN »

Hello, I am lost. I have been trying to make a page where it loads links from mysql or a file. I have links on the left and I want the administrator to be able to change, add, and or remove them. I have an index page, where you just see everything, then you login and see the admin page. This is where I would like to edit everything. I really need some help. How would I:
  • 1. Load The Links
  • 2. Have An "Edit Button To The Right Of The Link (On Admin Page Only))
  • 3. Add Links
  • 4. Remove Links
I made a table in mysql "studentwork", it has 2 fields, "text" and "link". I wanted the text for the link to be stored in "text" and the url to be stored in "link". I tried modifying a news script to do what I want but completely screwed it up.

Could somebody please help me out? Thanks.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

What code do you have so far?
ORiGIN
Forum Newbie
Posts: 20
Joined: Sat Nov 11, 2006 11:17 pm

Post by ORiGIN »

I was trying to use some bits of code from a bunch of tuts from pixel2life. com - http://www.pixel2life.com/tutorials/php ... s_scripts/ but I no longer have any of that code I put together.
ORiGIN
Forum Newbie
Posts: 20
Joined: Sat Nov 11, 2006 11:17 pm

Post by ORiGIN »

I did however keep the adding to the database page, maybe this will kind of show you what I want.

Code: Select all

 <? 
//initilize PHP

if($_POST['submit']) //If submit is hit
{
   //then connect as user
   //change user and password to your mySQL name and password
   mysql_connect("DOMAIN","LOGIN","PASSWORD"); 
	
   //select which database you want to edit
   mysql_select_db("DATABASENAME"); 
	
   //convert all the posts to variables:
   $name = $_POST['name'];
   $url = $_POST['url'];
   
   //Insert the values into the correct database with the right fields
   //mysql table = studentwork
   //table columns = id, name, url
   //post variables = $name, $url
   $result=MYSQL_QUERY("INSERT INTO studentwork (id,name,url)".
      "VALUES ('NULL', '$name', '$url')"); 

    //confirm
   echo "Student Page Added"; 
}
else
{
// close php so we can put in our code
?>
<form method="post" action="addwork.php">
<TABLE>
<TR>
   <TD>Name:</TD>
   <TD><INPUT NAME='name' TYPE='TEXT' id="name" size=60></TD>
</TR>
<TR>
   <TD>URL:</TD>
   <TD><INPUT NAME='url' TYPE='TEXT' id="url" VALUE='' size=60></TD>
</TR><br>
<TR>
   <TD></TD><br>
   <TD><INPUT TYPE="submit" name="submit" value="submit"></TD> 
</TR>
</TABLE>
</form>

<?
} //close the else statement
?>
That works just fine, I just want would like to know how to read that and put it into a table column to the right of the page. Kind of like this:

Regular Page:

Link 1
Link 2
Link 3
so on

Admin Page:

Link 1 (edit)
Link 2 (edit)
Link 3 (edit)
so on
ORiGIN
Forum Newbie
Posts: 20
Joined: Sat Nov 11, 2006 11:17 pm

Post by ORiGIN »

I have found something to display them, but I guess I will forget about putting the edit link next to them all, I can do it but there is no room. Her eis the code I found:

Code: Select all



include "config.php";

// connect to the mysql server
$link = mysql_connect($server, $db_user, $db_pass)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
mysql_select_db($database)
or die ("Could not select database because ".mysql_error());

// read data from database
$result = mysql_query("select * from $table order by id desc limit $rows", $link)
or die ("Could not read data because ".mysql_error());

// print the data in a table
if (mysql_num_rows($result)) {
print "<table cellpadding=0 cellspacing=0 border=0 width=\"100%\">\n";
while ($qry = mysql_fetch_array($result)) {
print "<tr><td>&nbsp;&nbsp;<a href=\"" . "$qry[url]\" target=\"_blank\">$qry[name]</a>";
//print $qry[comment];
print "</td></tr>\n";
}
print "</table>\n";
}

mysql_close();
Config.php just has the DB info in it.

How would I make a page that can edit all of them, or maybe just one at a time? After this is solved my work will be complete :D

This is what I am working on, as you can see I have most of the stuff finished. I just need to change some colors and finish the editing thing and I am finished.

EDIT

I have found a code to edit, but I get an error on it:

Code: Select all

<? 
//connect to mysql
//change user and password to your mySQL name and password
mysql_connect("DOMAIN","LOGIN","PASSWORD"); 
	
//select which database you want to edit
mysql_select_db("DBNAME"); 

//If cmd has not been initialized
if(!isset($cmd)) 
{
   //display all the news
   $result = mysql_query("select * from studentwork order by id"); 
   
   //run the while loop that grabs all the news scripts
   while($r=mysql_fetch_array($result)) 
   { 
      //grab the title and the ID of the news
      $title=$r["name"];//take out the title
      $id=$r["id"];//take out the id
     
	 //make the title a link
      echo "<a href='edit.php?cmd=edit&id=$id'>$title - Edit</a>";
      echo "<br>";
    }
}
?>
<?
if($_GET["cmd"]=="edit" || $_POST["cmd"]=="edit")
{
   if (!isset($_POST["submit"]))
   {
      $id = $_GET["id"];
      $sql = "SELECT * FROM news WHERE id=$id";
      $result = mysql_query($sql);        
      $myrow = mysql_fetch_array($result);
      ?>
	  
      <form action="edit.php" method="post">
      <input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
   
      Name:
      <INPUT TYPE="TEXT" NAME="title" VALUE="<?php echo $myrow["name"] ?>" SIZE=30>
      <br>
      URL:
      <input name="message" type="text" value="<? echo $myrow["url"] ?>" size="30">
      <br>
      <input type="hidden" name="cmd" value="edit">
   
      <input type="submit" name="submit" value="submit">
   
      </form>
   
<? } ?>
<?
   if ($_POST["$submit"])
   {
      $title = $_POST["name"];
	  $message = $_POST["url"];	  
	  $sql = "UPDATE studentwork SET name='$title',url='$message' WHERE id=$id";
      //replace news with your table name above
      $result = mysql_query($sql);
      echo "Student Work Updated!";
	}
}
?>
ERROR:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/www/jfenske.awardspace.com/school/3/editwork.php on line 115
Could someone please help me out?

Thanks a lot.
Ruud Hermans
Forum Newbie
Posts: 11
Joined: Tue Nov 28, 2006 8:33 pm

Post by Ruud Hermans »

I'm not a PHP / MySQL guru but if I'm right this error appears when the named table can't be found in your database (at least as far as I am aware that is one of the reasons).

1. Did you edit the script so it could connect to your database?
2. Did you checked if there are no spelling mistakes in the table names?

*If I'm wrong please correct me I'm still trying to figure this all out myself to.
ORiGIN
Forum Newbie
Posts: 20
Joined: Sat Nov 11, 2006 11:17 pm

Thanks A Lot!

Post by ORiGIN »

Ruud Hermans wrote:I'm not a PHP / MySQL guru but if I'm right this error appears when the named table can't be found in your database (at least as far as I am aware that is one of the reasons).

1. Did you edit the script so it could connect to your database?
2. Did you checked if there are no spelling mistakes in the table names?

*If I'm wrong please correct me I'm still trying to figure this all out myself to.
I did have one mistake. I misspelled a table name.

Thanks a lot for all of the help :D
Ruud Hermans
Forum Newbie
Posts: 11
Joined: Tue Nov 28, 2006 8:33 pm

Re: Thanks A Lot!

Post by Ruud Hermans »

ORiGIN wrote: I did have one mistake. I misspelled a table name.

Thanks a lot for all of the help :D
No problem helping others out can only make yourself smarter.
Post Reply