Jump to content

Using $Id Inside An Echo For A Url


drew4663

Recommended Posts

I have a textbox that you can enter a provider name and it searches for the name and displays the results. The code I use is....

 

mysql_connect ("mydomain", "username","password") or die (mysql_error());
mysql_select_db ("dbname");
$term = $_POST['term'];

$sql = mysql_query("select * from providers where provider like '%$term%'");
$ID = $_GET['providerID'];
while ($row = mysql_fetch_array($sql)){
echo '<br/> ProviderID: '.$row['providerID'];
echo '<br/> Provider: '.$row['provider'];
echo '<br/> Service: '.$row['service'];
echo '<br/> Phone: '.$row['phone'];
echo '<br/> Tier: '.$row['tier'];
echo '<a href="update.php?providerID=<?php echo $ID; ?>">Update</a>';

 

This is what it will give me on the page.

 

ProviderID: 1

Provider: Comcast

Service: Cable

Phone: 866-511-6489

Tier: National Business Tech Support

Update <----- link

 

When I click update the URL says.....

 

http://wwww.mydomain...php?providerID=<?php echo $ID; ?>

 

here is the code for the update.php file

 

<?php
$ID=$_GET['providerID'];
mysql_connect ("mydomain", "username","password") or die (mysql_error());
mysql_select_db ("dbname");

$sql = "select * from `providers` where ID = $ID";
$query = mysql_query($sql);

while ($row = mysql_fetch_array($query)){

$providerID = $row['providerID'];
$provider = $row['provider'];
$service = $row['service'];
$phone = $row['phone'];
$tier = $row['tier'];

//we will echo these into the proper fields

}
mysql_free_result($query);
?>

<html>
<head>
<title>Edit User Info</title>
</head>

<body>

<form action="updateinfo.php" method="post">

<p>provider id:<br/>
<input type="text" value="<?php echo $providerID;?>" name="providerID" disabled/>

<br/>

Provider:<br/>
<input type="text" value="<?php echo $provider;?>" name="provider"/>

<br/>

Service:<br/>
<input type="text" value="<?php echo $service;?>" name="service"/>

<br/>

Phone:<br/>
<input type="text" value="<?php echo $phone;?>" name="phone"/>
<br>
Tier:<br/>
<input type="text" value="<?php echo $tier;?>" name="tier"/>
</br>
<br>
<br>
<input type="submit" value="submit changes"/>
</p>
</form>
</body>
</html>

 

 

All I am trying to do is when I create a search to be able to click on update and it allow me to update that particular record via the ID. Any clues or pointers?

Link to comment
Share on other sites

Thank you for your assistance. I tried....

 

echo "<a href='update.php?providerID=$ID'>Update</a>";

 

and it resulted in

 

http://mydomain.com/update.php?providerID=

 

I am also getting these errors.....

 

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /xxx/xxxx/x/xxxxxx/xxxx/xxxxx/update.php on line 10

 

Warning: mysql_free_result() expects parameter 1 to be resource, boolean given in /xxx/xxxx/x/xxxxxx/xxxx/xxxxx/update.php on line 21

Link to comment
Share on other sites

FYI - When I manually put in a number

 

here...

$sql = "select * from `providers` where ID = 3";

 

It works great. So, I am guessing that I am having an issue with maybe the way the records are pulled in from the search.

 

This is my html file for my search.....

 

search.html

<html>
<head>
 <title>Search the Database</title>
<link href="search_providers.css" rel="stylesheet" type="text/css">
</head>

<body>


<div id="wrapper">
<form action="search.php" method="post">
 <div id="providername">
 <p>Search:
<input type="text" name="term" />
 </p>
 </div>


 <div id="providersubmit">
 <p>
 <input type="submit" name="submit" value="Submit" /></p></div>
</form>
</div>

</body>
</html>

 

 

Here is the php file for the result of the search.

 

search.php

mysql_connect ("mydomain", "username","password") or die (mysql_error());
mysql_select_db ("dbname");
$term = $_POST['term'];

$sql = mysql_query("select * from providers where provider like '%$term%'");
$ID = $_GET['providerID'];
while ($row = mysql_fetch_array($sql)){
echo '<br/> ProviderID: '.$row['providerID'];
echo '<br/> Provider: '.$row['provider'];
echo '<br/> Service: '.$row['service'];
echo '<br/> Phone: '.$row['phone'];
echo '<br/> Tier: '.$row['tier'];
echo "<a href='update.php?providerID=$ID'>Update</a>";


 

So here is what the search will get me. Sometimes it will pull one record or ten depends on what you enter into the search, but I am wanting whatever records are pulled to have that update link reference that particular id and post it in the url and load it in the html form. I'm not sure if I am making myself clear or not so bare with me.

 

Example:

 

ProviderID: 7

Provider: AT&T - Ameritech, SW Bell, SBC, Bell South

Service: DSL, T1

Phone: 888-434-6186

Tier: AT&T Advanced Technical Support

Update <----link should read when mouse-hover "http://www.mydomain.com/update.php(and then the ID # 7)

 

ProviderID: 8

Provider: AT&T - SBC Global

Service: DSL

Phone: 1-877-722-3755

Tier: Tier 1 - Tech Support

Update <----link should read when mouse-hover "http://www.mydomain.com/update.php(and then the ID # 8)

 

ProviderID: 9

Provider: AT&T Bell South

Service: DSL

Phone: 888-321-2375

Tier: Tier 1

Update <----link should read when mouse-hover "http://www.mydomain.com/update.php(and then the ID # 9)

 

ProviderID: 10

Provider: AT&T

Service: T1

Phone: 888-613-6330 opt 2, 1, 1, 1

Tier: Tier 1

Update <----link should read when mouse-hover "http://www.mydomain.com/update.php(and then the ID # 10)

 

After I click the update link I want the html form to load that ID and other info so that it can be edited.

Edited by drew4663
Link to comment
Share on other sites

Yes. That is correct.

 

This

echo "<br/><a href='update.php?providerID=$row[providerID]'>Update</a>";

 

Produces

http://mydomain.com/update.php?providerID=1

 

Now I have the update.php code of......

<?php
mysql_connect ("localhost", "username","password") or die (mysql_error());
mysql_select_db ("dbname");
$sql = "select * from `providers` where providerID = $providerID";
$query = mysql_query($sql);

while ($row = mysql_fetch_array($query)){

$providerID = $row['providerID'];
$provider = $row['provider'];
$service = $row['service'];
$phone = $row['phone'];
$tier = $row['tier'];

//we will echo these into the proper fields

}
mysql_free_result($query);
?>

<html>
<head>
<title>Edit User Info</title>
</head>

<body>

<form action="updateinfo.php" method="post">

<p>provider id:<br/>
<input type="text" value="<?php echo htmlentities($_GET['providerID'],ENT_QUOTES); ?>" name="providerID" disabled/>

<br/>

Provider:<br/>
<input type="text" value="<?php echo $provider;?>" name="provider"/>

<br/>

Service:<br/>
<input type="text" value="<?php echo $service;?>" name="service"/>

<br/>

Phone:<br/>
<input type="text" value="<?php echo $phone;?>" name="phone"/>
<br>
Tier:<br/>
<input type="text" value="<?php echo $tier;?>" name="tier"/>
</br>
<br>
<br>
<input type="submit" value="submit changes"/>
</p>
</form>
</body>
</html>

 

Doing it this way does put the "providerID" in the correct field but none of the other fields populate and I still get the boolean errors because of this line not being correct.

$sql = "select * from `providers` where providerID = $providerID";

 

Any suggestions of what I should be putting there? Should I remove the echo htmlentities($_GET['providerID'],ENT_QUOTES); and place it after WHERE?

Edited by drew4663
Link to comment
Share on other sites

I finally got it to work......thanks everyone for pointing me in the correct direction.

 

$url="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$pathFragments = explode('=', $url);
$end = end($pathFragments);
$sql = mysql_query("select * from providers where providerID like $end");
while ($row = mysql_fetch_array($sql)){
   $providerID = $row['providerID'];
   $provider = $row['provider'];
   $service = $row['service'];
   $phone = $row['phone'];
$tier = $row['tier'];

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.