• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 396
  • Last Modified:

radiobutton

I have alist of articles taken out of the db. At the end of each line, I've put a radiobutton, because you can only change 1 article at the time. I'm I on the right track?

<?php
if (isset($_POST["check"])) {
      $res=mysql_query("Select * FROM articles WHERE id in (" . $_POST["check"] . ")") or die(mysql_error());
}
?>

<form method='post' name='form1' action='changearticles.php'>
<table border="1" width="100%" cellpadding="0" cellspacing="0">
<tr>
      <td width="20%"><b>&nbsp;ID</b></td>
      <td width="60%"><b>&nbsp;Title</b></td>
      <td width="10%"><b>&nbsp;Category&nbsp;</b></td>
      <td width="10%"><center><b>Change</b></center></td>
</tr>
<?php
$res=mysql_query("SELECT id, title, category FROM articles Order by id") or die(mysql_error());
while($r = mysql_fetch_array($res)) {
      echo '<tr><td>&nbsp;'.$r['id'].'</td><td>&nbsp;'.$r['title'].'</td><td><center>'.$r['category'].'</center></td><td><center><input name="check" type="radio" value="'.$r['id'].'" /></center></td></tr>';  
}
?>
</table>
<br />
<input type=submit name=cmdSubmit value='Change' />
</form>
0
jvuz
Asked:
jvuz
  • 12
  • 6
  • 4
2 Solutions
 
Aamir SaeedCommented:
i think you should use a hyperlink for it as it is standard as you might see it in phpmyadmin [EDIT]

0
 
glcumminsCommented:
I agree that a link is the better choice, because it is easier to use. The user only needs to click once, rather than twice (once on the radio button, and once on the submit button).

A radio button would be a good choice if:
   1. You are limited on line space, and
   2. You provide multiple options on your submit buttons (Eg, change, delete, move, copy)
0
 
jvuzAuthor Commented:
So I should do it this way. Ask, through a query, every links, then, by clicking on a link, go to a page where I get the ID and get all data into a textbox, so I can change it?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
glcumminsCommented:
Yes. Your link would look like this:

<a href="changearticles.php?id=<?php $r['id']; ?>">Edit article</a>

Then in your 'changearticles.php', just check for the existance of the article ID:

if (isset($_REQUEST['id']))
{
   $id = $_REQUEST['id'];
   // Retrieve the article and display the textbox
}
0
 
Aamir SaeedCommented:
i think you 'll have to echo the id e.g.

<a href="changearticles.php?id=<?php echo $r['id']; ?>">Edit article</a>
0
 
jvuzAuthor Commented:
In the page where I get the list of all the articles and I put them in an url,

<?php
                                     $res=mysql_query("SELECT id, title FROM articles Order by id") or die(mysql_error());
                                    while($r = mysql_fetch_array($res)) {
                        echo '<a href=pages/changingarticle.php?id='.$r['ID'].'>' . $res['ID']. '&nbsp;' . $r['title'] . '</a>'.'<br />';
                        } while($r = mysql_fetch_array($res));
                               ?>

I get:
Notice: Undefined index: ID
0
 
jvuzAuthor Commented:
Sorry, already solved that.
0
 
Aamir SaeedCommented:
i think your field name is id and here you are using ID
0
 
glcumminsCommented:
Try a lower-case 'id', since that matches what you are pulling from the database.
0
 
jvuzAuthor Commented:
So now I need to select... and put this in a textbox?
0
 
Aamir SaeedCommented:
yes
0
 
glcumminsCommented:
Yes, so that the user can edit the article.
0
 
Aamir SaeedCommented:
1; make select query
2; take vaues and display in the textbox
3; keep these input boxes into form
4; submit that form and get values e.g.
   $_POST['posted_data'];
5; update query
0
 
jvuzAuthor Commented:
I don't think this is in the good direction.

<?php
                                   $res=mysql_query("SELECT id, title, article, category, author WHERE id=".$_GET['id']) or die(mysql_error());
                                   $r = mysql_fetch_array($res);
                                   $article = nl2br($r['article']);
                                   echo '<table><tr>Title:<td></td><td><input type="text" name="title" size="90" maxlength="120"  />'.$r['title'].'</td></tr><tr>Title:<td></td><td>'.$r['title'].'</td></tr>;
                                   
                       ?>
0
 
Aamir SaeedCommented:
this is wrong as you are not entering the table name

$res=mysql_query("SELECT id, title, article, category, author WHERE id=".$_GET['id']) or die(mysql_error());

as i dont see
   'from'
anywhere

the syntax of query is : select field1, field2 from table
0
 
jvuzAuthor Commented:
Tahnx, how stupid of me. But I'm wondering how I can put the data that I pulled out of the db into a textbox.
0
 
jvuzAuthor Commented:
I think is better

<?php
                                   $res=mysql_query("SELECT id, title, article, category, author FROM articles WHERE id=".$_GET['id']) or die(mysql_error());
                                   $r = mysql_fetch_array($res);
                                   $article = nl2br($r['article']);
                                   echo '<table><tr>Title:<td></td><td><textarea cols="5" rows="5" name="tekst">'.$r['title'].'</textarea></td></tr><tr>Title:<td></td><td>'.$r['title'].'</td></tr>;
                                   
                       ?>
0
 
jvuzAuthor Commented:
I think I'm getting there:

<?php
                                   $res=mysql_query("SELECT id, title, article, category, author FROM articles WHERE id=".$_GET['id']) or die(mysql_error());
                                   $r = mysql_fetch_array($res);
                                   $article = nl2br($r['article']);
                                   echo '<table><tr><td valign="top">Title:</td><td><textarea cols="70" rows="1" name="tekst">'.$r['title'].'</textarea></td></tr><tr><td valign="top">Article:</td><td><textarea cols="70" rows="10" name="tekst">'.$r['article'].'</textarea></td></tr></table>';
                                   
                       ?>

The only thing is, I don't get the box for the title smaller (in height)
0
 
jvuzAuthor Commented:
I'm this far for now.

<?php
                                   $res=mysql_query("SELECT id, title, article, category, author FROM articles WHERE id=".$_GET['id']) or die(mysql_error());
                                   $r = mysql_fetch_array($res);
                                   $article = nl2br($r['article']);
                                   /*echo '<table><tr><td valign="top">Title:</td><td><textarea cols="70" rows="1" name="tekst">'.$r['title'].'</textarea></td></tr><tr><td valign="top">Article:</td><td><textarea cols="70" rows="10" name="tekst">'.$r['article'].'</textarea></td></tr></table>';*/
                                   echo '<form method="post" action="' . $_SERVER['PHP_SELF'] . '" style="margin-left:1px;">';
                                   echo '<table>';
                            echo '<tr><td valign="top">Title</td><td><input type="text" name="title" value="'.$r['title'].'"style="width:535px;" /></td></tr>';
                            echo '<tr><td valign="top">Article:</td><td><textarea id="article"  name="event" rows="10" style="width:535px;">'.$r['article'].'</textarea></td></tr>';
                            echo '<tr><td></td><td><input type="submit" value="Change" /></td></tr></table>';
                            echo '</form>';
                       ?>

I think I need to add a way to send this to the db
0
 
jvuzAuthor Commented:
If I do this, it shows nothing on screen

   <?php
                                   $res=mysql_query("SELECT id, title, article, category, author FROM articles WHERE id=".$_GET['id']) or die(mysql_error());
                                   $r = mysql_fetch_array($res);
                                   $article = nl2br($r['article']);
                                   if(isset($_POST['submit'])) {
                                   echo '<form method="post" action="changearticles.php" style="margin-left:1px;">';
                                   echo '<table>';
                            echo '<tr><td valign="top">Title</td><td><input type="text" name="title" value="'.$r['title'].'"style="width:535px;" /></td></tr>';
                            echo '<tr><td valign="top">Article:</td><td><textarea id="article"  name="event" rows="10" style="width:535px;">'.$r['article'].'</textarea></td></tr>';
                            echo '<tr><td></td><td><input type="submit" value="Change" /></td></tr></table>';
                            echo '</form>';
                            mysql_query("UPDATE articles SET title='" . $_POST['title'] . "', article='" . $_POST['article'] . "' WHERE id='" . $id . "'");
                        }
                       ?>
0
 
jvuzAuthor Commented:
That's solved, but now it doesn't change anything. I can change something in the article for instance and when I click Change, nothing's changed.

<?php
                                   $res=mysql_query("SELECT id, title, article, category, author FROM articles WHERE id=".$_GET['id']) or die(mysql_error());
                                   $r = mysql_fetch_array($res);
                                   $article = nl2br($r['article']);
                                   if(!isset($_POST['submit'])) {
                                   echo '<form method="post" action="changearticles.php" style="margin-left:1px;">';
                                   echo "<input type=\"hidden\" name=\"berichtID\" value=\"".$r['id']."\" />";
                                   echo '<table>';
                            echo '<tr><td valign="top">Title</td><td><input type="text" name="title" value="'.$r['title'].'"style="width:535px;" /></td></tr>';
                            echo '<tr><td valign="top">Article:</td><td><textarea id="article"  name="article" rows="10" style="width:535px;">'.$r['article'].'</textarea></td></tr>';
                            echo '<tr><td></td><td><input type="submit" value="Change" /></td></tr></table>';
                            echo '</form>';
                        } else {
                            mysql_query("UPDATE articles SET title='" . $_POST['title'] . "', article='" . $_POST['article'] . "' WHERE id='" . $_POST['berichtID'] . "'")or die(mysql_error());
                        }
                       ?>
0
 
jvuzAuthor Commented:
OK, that one's solved.

<?php
ob_start();
?>
<?php
// include/require hier evt nog andere zaken
require ("../scripts/connect.php");
// we willen op deze pagina gebruik maken van beveiliging mbv sessies,
// dus includen (requiren) we session.php
require("../scripts/session.php");
$id = htmlentities($_GET["id"], ENT_QUOTES);
   // Fetch the article category
   $rsc = mysql_query("SELECT category FROM articles WHERE id=".$_GET['id']) or die(mysql_error());
   $row = mysql_fetch_array($rsc);
      $cat = $row['category'];

  if (($cat == 2) && ((int)$_SESSION['slevel'] < 2)) {
      header("Location:accessdenied.php");
      exit(); //stop the script.
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>KB RBINS</title>
<link rel="stylesheet" type="text/css" href="../main.css" />
<link rel="shortcut icon" href="images/favicon.ico" type="image/x-icon" />
</head>
<body>
   <!-- Begin Wrapper -->
   <div id="wrapper">
         <!-- Begin Header -->
         <div id="header">
                  <h1>Knowledgebase System RBINS</h1>      
             </div>
             <!-- End Header -->
         <!-- Begin Faux Columns -->
             <div id="faux">            
                   <!-- Begin Left Column -->
                   <div id="leftcolumn">      
                     <?php
                            include("../scripts/checkuser.php");
                     ?>      
                   </div>
                   <!-- End Left Column -->
                   <!-- Begin Right Column -->
                   <div id="rightcolumn">      
                     <center><h2>Change an article</h2></center><br />  
                     <?php
                                    if( isset( $_GET['id'] ) && is_numeric( $_GET['id'] ) ){
                                        $res = mysql_query( "SELECT title, article, category, author FROM articles  WHERE  id = " .$_GET['id'] ) or die('QUERY_SELECT ERROR:  <hr />' . mysql_error());
                                  $results = mysql_fetch_assoc( $res );
                                  if( $_SERVER['REQUEST_METHOD'] == 'POST' )
                                  {
                                // check if fields are empty

                                // no empty fields, so update:
                                $update = mysql_query( "UPDATE articles SET title     = '" . mysql_real_escape_string( $_POST['title']). "', article = '". mysql_real_escape_string( $_POST['article'] ) . "'WHERE  id = "  . $_GET['id']) or die('QUERY_UPDATE ERROR:  <hr />'. mysql_error());
                                echo "Changes have been made!";
                               }
                                  else
                                  {
                                // form without PHP
                                    ?>
                                   <!-- HIER FORMULIER -->
                                   <form method="post" action="" style="margin-left:1px;">
                        <table>
                              <tr>
                                    <td><input type="hidden" name="berichtID" value="<?php echo $results['id']; ?> " /></td>
                              </tr>
                              <tr>
                                    <td valign="top">Title</td><td><input type="text" name="title" value="<?php echo $results['title']; ?>"style="width:535px;" /></td>
                              </tr>
                            <tr>
                                  <td valign="top">Article:</td><td><textarea id="article"  name="article" rows="10" style="width:535px;"><?php echo $results['article']; ?></textarea></td>
                            </tr>
                            <tr>
                                  <td></td>
                                  <td><input type="submit" value="Change" /></td>
                            </tr>
                         </table>
                         </form>
                                    <?php
                                       }}
                                    else
                                    {
                                  echo "ONGELDIGE URL INVOER";
                                    }
                                    ?>          
                  <br />
                     <div class="clear"></div>                    
                   </div>
                   <!-- End Right Column -->                    
                     <div class="clear"></div>                    
         </div>        
         <!-- End Faux Columns -->
         <!-- Begin Footer -->
         <div id="footer">                  
               <h4>Jo Vanattenhoven&copy;2007</h4>            
         </div>
             <!-- End Footer -->            
   </div>
   <!-- End Wrapper -->
</body>
</html>
<?php
ob_end_flush();
?>

Now I just need to check if there are empty fields and see if the title doesn't exist already.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 12
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now