Passing values of a variables upon clicking some links

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

User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Yes, there should be something after "=". If there isn't, that is where the problem is. Something you might want to try in your second page is adding a check for empty and is_numeric to make sure you validate. Also, for testing, throw a die instead of a header:

Code: Select all

if ( !isset($_GET['id']) || empty($_GET['id']) || !is_numeric($_GET['id']) )
{
    //header('Location: article_list.php');
    die("The search identifier " . $_GET['id'] . " had errors.");
}
But definitely make sure that there is a different value for "id" in each of the outputted links from page 1.
User avatar
khaki_monster
Forum Commoner
Posts: 73
Joined: Tue Oct 11, 2005 12:36 am
Location: Philippines
Contact:

Post by khaki_monster »

the second page("view_article.php") now give this error message: "The search identifier had errors." it wont even give the content of the 'id'. is their any configuration bout passing some values through links?

wow... i feel so embarrass that i can even solved this kind of problem. how much more if im given bigger project than this. :?

im kind'a desperate right now :cry: could anybody trace my coding regarding what goes wrong...

here are the code:

- MySQL

Code: Select all

`news_dtl` (
  `idx` int(8) unsigned NOT NULL auto_increment,
  `section` int(1) NOT NULL default '0',
  `author` varchar(35) default NULL,
  `title` varchar(150) NOT NULL default '',
  `lead` text NOT NULL,
  `body` text NOT NULL,
  `comments` varchar(255) default NULL,
  PRIMARY KEY  (`idx`)
)

`news_mas` (
  `id` int(8) unsigned NOT NULL auto_increment,
  `date` varchar(8) NOT NULL default '',
  `location` varchar(3) NOT NULL default '0',
  `section` int(1) NOT NULL default '0',
  PRIMARY KEY  (`id`)
)

Code: Select all

-article_list.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<link href="../../default.css" rel="stylesheet" type="text/css">
<link href="../../balita.css" rel="stylesheet" type="text/css">
</head>
<?php 
require("../../config.php"); 
conn_db($host, $user, $pass, $dbse);
?>
<body>
<div align="center">
<table width="396" border="1" cellpadding="0" cellspacing="0" bordercolor="#666666" class="tble">  
<?php
	$query = 'select news_mas.location, news_mas.section, news_dtl.title from news_mas, news_dtl
			  where news_mas.id = news_dtl.idx and news_mas.section = news_dtl.section order by news_mas.location ASC';
	$result = mysql_query($query);
	
	while($ctr = mysql_fetch_array($result)){ //Begin While
    $xsec = $ctr['section'];
    switch ($xsec){ // Begin Switch
        case 1;
        $sec = 'Balita';
        break;

        case 2;
        $sec = 'Opinyon';
        break;

        case 3;
        $sec = 'Kalingawan';
        break;

        case 4;
        $sec = 'Sports';
        break;
    } // End Switch

    echo '<tr><td height="20"><span class="article_title">'.$ctr['location'].' - '.$sec.': '.'<a href="view_article.php?id='.$ctr['id'].'">'.$ctr['title'].'</a></span></td>';
    echo '<td width="40" height="20"><div align="center"><span class="maintxt">Edit</span></div></td></tr>';
}// End While 
?>
</table>
</div>
</body>
</html>

Code: Select all

-view_article.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Untitled Document</title>
<link href="../../default.css" rel="stylesheet" type="text/css">
<link href="../../balita.css" rel="stylesheet" type="text/css">
</head>
<?php
require ("../../config.php");
conn_db($host, $user, $pass, $dbse);
?>
<body>
<div align="center">
  <table width="590" border="0" cellpadding="3" cellspacing="0">
  <?php
  
  if ( !isset($_GET['id']) || empty($_GET['id']) || !is_numeric($_GET['id']) )
  {
    die("The search identifier " . $_GET['id'] . " had errors.");
	} 
  
  $id = $_GET['id'];
  
  $query = "select title, author, body from news_dtl
		    where idx = '$id'";
  $result = mysql_query($query);
  $row = mysql_fetch_array($result);
  echo'
    <tr>
      <td width="594" height="79"><div id="divalign">
          <div align="left" class="dtitle">'.$row['title'].'</div>
          <p class="byline">'.$row['author'].'</p>
          <br>
          <p>'.nl2br($row['body']).'</p>
        </div></td>
    </tr>';
	?>
  </table>
</div>
</body>
</html>
thanx Everah, it seems your the only 1 who'z very much interested with this subject...
hope to get more help from you :)

cheerz!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Your problem is with your query on your first page. You never select "id", so your reference to it is showing null. Change your query from this...

Code: Select all

$query = 'select news_mas.location, news_mas.section, news_dtl.title from news_mas, news_dtl
              where news_mas.id = news_dtl.idx and news_mas.section = news_dtl.section order by news_mas.location ASC';
To this...

Code: Select all

$query = 'SELECT news_mas.id, news_mas.location, news_mas.section, news_dtl.title 
        FROM news_mas, news_dtl
        WHERE news_mas.id = news_dtl.idx 
        AND news_mas.section = news_dtl.section 
        ORDER BY news_mas.location ASC';
And run the script again. See if it works now.
User avatar
khaki_monster
Forum Commoner
Posts: 73
Joined: Tue Oct 11, 2005 12:36 am
Location: Philippines
Contact:

Passing values of a variables upon clicking URL [SOLVED]

Post by khaki_monster »

wow... its actually working! tanx a lot Everah! i really appreciate your help.
i hope you'll continue to contibute for those who are helpless...

my ignorance in PHP tel'z me that i should read more... :D

theirs a lot of work ahead for me with this project...

hope you'll still stand by there to help me again :)

THANKS VERY MUCH!

cheerz!
Last edited by khaki_monster on Fri Jan 06, 2006 7:07 am, edited 1 time in total.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

No problem. Most of us have been there before. It is usually the simple ones that get us the most stuck. Hang in there. I'm sure soon you will be in here answering questions yourself.
User avatar
khaki_monster
Forum Commoner
Posts: 73
Joined: Tue Oct 11, 2005 12:36 am
Location: Philippines
Contact:

Post by khaki_monster »

just whe i thought that im done with this problem. so here i am again. :(

i dont understand why my script doesn't work('update'). from my 'ariticle_list.php' which enable a single query upon
clicking some links on the list('and this is working fine') or should go to update page('edtfrm.php') when a user clicks "Edit" links on the list.

under 'edtfrm.php' is 'edtscript.php - this is the script that will be process after 'edtfrm.php' to make some changes(update) but its not working.

questions:

1. is the way i pass the value of variable('<form method="post" action="edtscript.php?id=<?= $row['id']; ?>">') is wrong?

or

2. is my update statement is incorrect?

bellow is the given code from the said PHP files(assuming all connections are fine):

Code: Select all

-edtfrm.php

<HTML>
<HEAD>
<link href="../../frmStyle.css" rel="stylesheet" type="text/css">
<link href="../../balita.css" rel="stylesheet" type="text/css">
<link href="../../default.css" rel="stylesheet" type="text/css">
</HEAD>
<BODY>
<?php
require("../../config.php");
conn_db($host, $user, $pass, $dbse);

if ( !isset($_GET['id']) || empty($_GET['id']) || !is_numeric($_GET['id']) )
{
die("The search identifier " . $_GET['id'] . " had errors.");
}

$id = $_GET['id'];

  $query = "select news_mas.id, news_mas.location, news_mas.section, news_dtl.title, news_dtl.author, news_dtl.lead, 
  			news_dtl.body, news_dtl.comments from news_mas, news_dtl where news_mas.id = '$id' and news_dtl.idx = '$id'";
  $result = mysql_query($query);
  $row = mysql_fetch_array($result);
  
  $loc = $row['location'];
  $sec = $row['section'];
?>
<div align="center">
  <form method="post" action="edtscript.php?id=<?= $row['id']; ?>">
    <table width="525" border="0" cellpadding="3" cellspacing="0" class="frmtable">
      <tr>
        <td height="25" colspan="3" nowrap></td>
      </tr>
      <tr>
        <td width="77" height="26" align="right" nowrap><img src="../../images/location.jpg" width="77" height="20"></td>
        <td width="460" valign="top" ><select name="<?= $loc; ?>" id="location">
		    <option value="XXX">Super Balita</option>
            <? if($loc == "CDO"){ ?>
			<option value="CDO" selected>Cag. de Oro</option> <? } else { ?>
			<option value="CDO">Cag. de Oro</option> <? } ?>
			
			<? if($loc == "CBU") { ?> 
            <option value="CBU" selected>Cebu</option> <? } else {?>
            <option value="CBU">Cebu</option> <? } ?>
            
			<? if($loc == "DVO") {?>
			<option value="DVO" selected>Davao</option> <? } else {?>
			<option value="DVO">Davao</option> <? } ?>
			
			<? if($loc == "GSN") { ?>
            <option value="GSN" selected>Gen. Santos</option> <? } else {?>
			<option value="GSN">Gen. Santos</option> <? } ?>
          </select>
	    </td>
        <td width="1"></td>
      </tr>
      <tr>
        <td height="26" nowrap><img src="../../images/section.jpg"></td>
        <td valign="top" ><select name="<?= $sec ?>" id="section">
            <option value="0">- - - - - - - - - - - - - - - -</option>
			<? if($sec == 1) {?>
            <option value="1" selected>Balita</option> <? } else {?>
			<option value="1">Balita</option> <? } ?>
			
			<? if($sec == 2) {?>
            <option value="2" selected>Opinyon</option> <? } else {?>
			<option value="2">Opinyon</option> <? } ?>
			
			<? if($sec == 3) {?>
            <option value="3" selected>Kalingawan</option> <? } else {?>
			<option value="3">Kalingawan</option> <? } ?>
            
			<? if($sec == 4) {?>
            <option value="4" selected>Sports</option> <? } else {?>
			<option value="4">Sports</option> <? } ?>
          </select></td>
        <td></td>
      </tr>
      <tr>
        <td height="26" nowrap><img src="../../images/title.jpg"></td>
        <td ><input name="title" type="text" class="frmarea" id="title" value="<?= $row['title']; ?>"></td>
        <td></td>
      </tr>
      <tr>
        <td height="26" nowrap><img src="../../images/author.jpg"></td>
        <td ><input name="author" type="text" class="frmarea" id="author" value="<?= $row['author']; ?>"></td>
        <td></td>
      </tr>
      <tr>
        <td height="26" nowrap><img src="../../images/lead.jpg"></td>
        <td rowspan="2" valign="top" ><textarea name="lead" cols="65" rows="3" class="frmarea" id="lead"><?= $row['lead']; ?></textarea></td>
        <td rowspan="2"></td>
      </tr>
      <tr>
        <td height="22" nowrap></td>
      </tr>
      <tr>
        <td height="27" nowrap><img src="../../images/image.jpg"></td>
        <td><input name="image" type="file" class="frmarea" id="image"></td>
        <td></td>
      </tr>
      <tr>
        <td height="26" nowrap><img src="../../images/caption.jpg" border="0"></td>
        <td rowspan="2" valign="top" ><textarea name="caption" rows="3" class="frmarea" id="caption"></textarea></td>
        <td rowspan="2"></td>
      </tr>
      <tr>
        <td height="26" nowrap></td>
      </tr>
      <tr>
        <td height="26" nowrap  ><img src="../../images/body.jpg" border="0"></td>
        <td rowspan="2" valign="top" ><textarea name="body" cols="65" rows="15" class="frmarea" id="body"><?= $row['body']; ?></textarea></td>
        <td rowspan="2"></td>
      </tr>
      <tr>
        <td height="182" nowrap></td>
      </tr>
      <tr>
        <td height="61" valign="top" nowrap ><img src="../../images/comments.jpg"></td>
        <td valign="top"><textarea name="comments" rows="4" class="frmarea" id="comments"><?= $row['comments']; ?></textarea></td>
        <td></td>
      </tr>
      <tr>
        <td height="37"></td>
        <td ><table width="100%" border="0" cellpadding="3" cellspacing="0">
            <tr>
              <td width="69" height="31" ><input name="cmdEdt" type="submit" class="btun" id="cmdEdt" value="Update"></td>
              <td width="69" ><input name="cmdCancel" type="submit" class="btun" id="cmdCancel" value="Cancel"></td>
              <td width="130">&nbsp;</td>
              <td width="75" valign="top"><input name="cmdRet" type="submit" class="btun" id="cmdRet" value="Return"></td>
              <td width="69" ><input name="cmdReset" type="reset" class="btun" id="cmdReset" value="Reset"></td>
            </tr>
          </table></td>
        <td></td>
      </tr>
    </table>
  </form>
</div>
</BODY>
</HTML>
--------------------------------------

Code: Select all

-edtscript.php

<?php
require("../../config.php");
conn_db($host, $user, $pass, $dbse);

if ( !isset($_GET['id']) || empty($_GET['id']) || !is_numeric($_GET['id']) )
{
die("The search identifier " . $_GET['id'] . " had errors.");
}

$id = $_GET['id'];

if($_POST['cmdEdt'])
{
	$save = 0;
	$location = trim($_POST['location']);
	$section = trim($_POST['section']);
	$title = trim($_POST['title']);
	$author = trim($_POST['author']);
	$lead = trim($_POST['lead']);
	$body = trim($_POST['body']);
	$comments = trim($_POST['comments']);
	$date = date('m.d.y');
	
	$query = "update news_mas, news_dtl 
			  set news_mas.date = '$date', news_mas.location = '$location', news_mas.section = '$section',
			  	  news_dtl.section = news_mas.section, news_dtl.author = '$author', news_dtl.title = '$title'
				  news_dtl.lead = '$lead', news_dtl.body = '$body', news_dtl.comments = '$comments'
			  where news_mas.id = news_dtl.idx";
	mysql_query($query);
	$save = mysql_affected_rows();
	if($save > 0)
	{
		header("Location: ../preview/article_list.php");
	}else{	
		die("No affected row");
	}
}
?>
rsmarsha
Forum Contributor
Posts: 242
Joined: Tue Feb 08, 2005 4:06 am
Location: Leeds, England

Post by rsmarsha »

Your update query doesn't seem to specify a row to update.

Code: Select all

$query = "update news_mas, news_dtl 
              set news_mas.date = '$date', news_mas.location = '$location', news_mas.section = '$section', 
                    news_dtl.section = news_mas.section, news_dtl.author = '$author', news_dtl.title = '$title' 
                  news_dtl.lead = '$lead', news_dtl.body = '$body', news_dtl.comments = '$comments' 
              where news_mas.id = news_dtl.idx";
Try changing to :

Code: Select all

$query = "update news_mas, news_dtl 
              set news_mas.date = '$date', news_mas.location = '$location', news_mas.section = '$section', 
                    news_dtl.section = news_mas.section, news_dtl.author = '$author', news_dtl.title = '$title' 
                  news_dtl.lead = '$lead', news_dtl.body = '$body', news_dtl.comments = '$comments' 
              where news_mas.id = news_dtl.idx AND news_mas.id='$id'";
The section added was :

Code: Select all

AND news_mas.id='$id'
As if i've got this right, $id is a variable passed from the URL to identify which row needs updating.
User avatar
khaki_monster
Forum Commoner
Posts: 73
Joined: Tue Oct 11, 2005 12:36 am
Location: Philippines
Contact:

Post by khaki_monster »

:( its still not working, i already try your suggestion. my validation still's return "No affected row" means there were no data change.

tanx for the help.

hope to here some more feedbacks :)

cheerz!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

khaki_monster, try adding error checking to your update query. You might also want to run two different UPDATE queries (one for each table) just to make sure that each table is being updated appropriately.

This is your current code in editscript.php:

Code: Select all

$query = "update news_mas, news_dtl
              set news_mas.date = '$date', news_mas.location = '$location', news_mas.section = '$section',
                    news_dtl.section = news_mas.section, news_dtl.author = '$author', news_dtl.title = '$title'
                  news_dtl.lead = '$lead', news_dtl.body = '$body', news_dtl.comments = '$comments'
              where news_mas.id = news_dtl.idx";
    mysql_query($query);
    $save = mysql_affected_rows();
    if($save > 0)
    {
        header("Location: ../preview/article_list.php");
    }else{    
        die("No affected row");
    }
Try doing this with it...

Code: Select all

$query = "UPDATE news_mas, news_dtl SET 
        news_mas.date = '$date', 
        news_mas.location = '$location', 
        news_mas.section = '$section', 
        news_dtl.section = '$section', # changed this from news_mas.section to '$section'
        news_dtl.author = '$author', 
        news_dtl.title = '$title', # Added a comma here (syntax issue)
        news_dtl.lead = '$lead', 
        news_dtl.body = '$body', 
        news_dtl.comments = '$comments' #Added a space after this line (Syntax issue)
        WHERE news_mas.id = news_dtl.idx";
    if ( !$result = mysql_query($query) )
    {
        die("There was an error with the update query: " . mysql_error());
    }

    $save = mysql_affected_rows($result);
    if($save > 0)
    {
        header("Location: ../preview/article_list.php");
    } 
    else 
    {    
        die("No affected row");
    }
User avatar
khaki_monster
Forum Commoner
Posts: 73
Joined: Tue Oct 11, 2005 12:36 am
Location: Philippines
Contact:

Post by khaki_monster »

ey, how are you? thanx again, i was actually expectiong your help :). so, i guess my 'Update' statementz causes the trouble. also i added some statement with your corrections.

your correction:

Code: Select all

$query = "UPDATE news_mas, news_dtl SET
        news_mas.date = '$date',
        news_mas.location = '$location',
        news_mas.section = '$section',
        news_dtl.section = '$section', # changed this from news_mas.section to '$section'
        news_dtl.author = '$author',
        news_dtl.title = '$title', # Added a comma here (syntax issue)
        news_dtl.lead = '$lead',
        news_dtl.body = '$body',
        news_dtl.comments = '$comments' #Added a space after this line (Syntax issue)
        WHERE news_mas.id = news_dtl.idx";
mind:

Code: Select all

$query = "Update news_mas, news_dtl Set 
					 news_mas.date = '$date', 
					 news_mas.location = '$location', 
					 news_mas.section = '$section',
			  	  	 news_dtl.section = '$section', 
					 news_dtl.author = '$author', 
					 news_dtl.title = '$title',
				  	 news_dtl.lead = '$lead', 
					 news_dtl.body = '$body', 
					 news_dtl.comments = '$comments' Where news_mas.id = news_dtl.idx 
                                         And news_mas.id = '$id'"; # <- Added Change
coz it actually update('overwrite') all the record on my database without And news_mas.id = '$id'"; :D

although the updating is working, i recieved this error traping.

if display_error On
-Warning: mysql_affected_rows(): supplied argument is not a valid MySQL-Link resource in C:\edtscript.php on line 62
-No affected row


if display_error Off
-No affected row

this should return to

Code: Select all

header("Location: ../preview/article_list.php");
but it wasn't doing it. why?

cheerz!
Post Reply