Jump to content

urgent help with passing variable in URL


busby

Recommended Posts

hey

 

ive got a page that lists a users bank accounts...when the user chooses to update an account it goes to a new page where they can edit the details...when it goes to this new page it takes the user's ID and the account ID with it in the URL (http://localhost/oswp/bank/updateaccounts.php?id=34&cid=1)

 

so i know these variables are correctly being transferred.

 

now when the user has made their changes and clicked the update button it should update the database and then re-locate back to the original page listing all accounts...to get back to this page and show the correct accounts for that user i thaught i needed to take the ID from the URL...give it a variable...and use it in the URL back to the original page. so for example the variable and update sql and re-locate code would look like this.

 

$cid = $_GET['cid'];

	//updating the table
	$result=mysql_query("UPDATE accounts SET accno='$accno', pin='$pin', type='$type', name='$balance', balance='$balance', active='$active' WHERE accid=$id");

	header("location:accounts.php?id=$cid");

 

however currently when the user clicks update and it tries to relocate back to the original page its not getting the variable in the URL.

(http://localhost/oswp/bank/accounts.php?id=)

 

and im getting the errors:

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\uni\oswp\assignment\bank\accounts.php on line 26

's bank accounts

 

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\uni\oswp\assignment\bank\accounts.php on line 30

 

basically saying its expected a number up in the URL and its not finding one...but i dont know why...any ideas please its quite urgent.

 

here is the relevant code for this section:

 

Accounts.php:

<a href="adminlogout.php">Logout</a><br />
<?php
$id = $_GET['id'];
if(!isset($_SESSION['myusername'])) {
header("location:adminlogin.php");
}
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = 'bank';

$conn = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);
$result = mysql_query("SELECT * FROM customer WHERE cusid=$id");
$row2 = mysql_fetch_array($result);
echo $row2['name'] . "'s bank accounts" . "<br><br>";
$result2 = mysql_query("SELECT * FROM accounts WHERE cusid=$id");
echo "<form method='post' action='accounts.php?id=$id'>";
while($row = mysql_fetch_array($result2))
{
echo "<input type='hidden' value='$row[accid]' name='accid'>" . $row['accno'] . $row['name'] . $row['type'] . $row['balance'] . $row['active'] . "<a href='updateaccount.php?id=$row[accid]&cid=$id'>Update</a>" . "<br>";
}
echo "</form>";
?>

 

updateaccounts.php:

$id = $_GET['id'];
$cid = $_GET['cid'];
if(!isset($_SESSION['myusername'])) {
header("location:adminlogin.php");
}
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$db = 'bank';

$conn = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);

if(isset($_POST['update']))
{	
$accno=$_POST['accno'];	
$pin=$_POST['pin'];	
$type=$_POST['type'];	
$name=$_POST['name'];	
$balance=$_POST['balance'];	
$active=$_POST['active'];	
// checking empty fields
	//if item field is empty
	if(empty($accno))
	{
		echo "<font color='red'>account number field is empty.</font><br/>";
	}
	elseif(empty($pin))
	{
		echo "<font color='red'>pin field is empty.</font><br/>";
	}
	elseif(empty($type))
	{
		echo "<font color='red'>account type field is empty.</font><br/>";
	}
	elseif(empty($name))
	{
		echo "<font color='red'>account name field is empty.</font><br/>";
	}
	elseif(empty($balance))
	{
		echo "<font color='red'>balance field is empty.</font><br/>";
	}
	elseif(empty($active))
	{
		echo "<font color='red'>active field is empty.</font><br/>";
	}

else
{	
	//updating the table
	$result=mysql_query("UPDATE accounts SET accno='$accno', pin='$pin', type='$type', name='$balance', balance='$balance', active='$active' WHERE accid=$id");
	header("location:accounts.php?id=$cid");
}
}

 

 

Link to comment
Share on other sites

remove the header call and just echo $cid to make sure it has the variable to use in the header.

 

Also, I don't know if it should matter, but I always concat the get var back on to the header url like

 

header("Location: page.php?cid=".$cid)

 

I know the var should be parsed since it's within double quotes, but with headers I've just always done it this way.

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.