Hi, whoever you are who is going to solve this

I like to code programs that make life a
little easier. This project is to display accounts in a database I use showing login info,
lastime used and number of times. I also want to be able to click and go to the account.

Everything works except that I can’t relocate to the url.
and the current code:

type or paste code here
<!DOCTYPE><html><head><title>email menu</title></head>
<body><center>
<form name="form" method="post" action="">
<?php
$con=mysqli_connect("localhost","root","","homedb");
//============== check connection
if(mysqli_errno($con))
{echo "Can't Connect to mySQL:".mysqli_connect_error(); exit; }
else
{echo "Connected to mySQL</br>";}
//This creates the drop down box
echo "<select name= 'target'>";
echo '<option value="">'.'--- Select lookup account---'.'</option>';
$query = mysqli_query($con,"SELECT target FROM emailtbl");
$query_display = mysqli_query($con,"SELECT * FROM emailtbl");
while($row=mysqli_fetch_array($query))
{echo "<option value='". $row['target']."'>".$row['target']
.'</option>';}
echo '</select>';
?>
<p><p>
<CENTER>then

<input type="submit" name="submit" value="Submit"/>
</form>
<?php
if(isset($_POST['target']))
{
$name = $_POST['target'];
$fetch="SELECT id, target, called, username, password, purpose, emailused,lastused, count FROM emailtbl WHERE target = '".$name."'";
$result = mysqli_query($con,$fetch);
if(!$result)
{echo "Error:".(mysqli_error($con));}
//display the table
echo '<table border="1">'.'<tr>'.'<td bgcolor="#ccffff align="center">'. 'lookup menu'. '</td>'.'</tr>';
echo '<tr>'.'<td>'.'<table border="1">'.'<tr>'.'
<td bgcolor="#CFB53B align="center">'.'id'.'</td>'.'
<td bgcolor="#CFB53B align="center">'.'target'.'</td>'.'
<td bgcolor="#CFB53B align="center">'.'called'.'</td>'.'
<td bgcolor="#ccffff align="center">'.'username'.'</td>'.'
<td bgcolor="#ccffff align="center">'.'password'.'</td>'.'
<td bgcolor="#ccffff align="center">'.'purpose'. '</td>'.'
<td bgcolor="#ccffff align="center">'.'emailused'.'</td>'.'
<td bgcolor="#CFB53B align="center">'.'lastused'.'</td>'.'
<td bgcolor="#CFB53B align="center">'.'count'. '</td>'.'</tr>';
while($data=mysqli_fetch_row($result))
{echo ("<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td><td>$data[3]</td>
<td>$data[4]</td><td>$data[5]</td><td>$data[6]</td><td>$data[7]</td><td>$data[8]</td></tr>");}
echo '</table>'.'</td>'.'</tr>'.'</table>';
$id="id";
$target="target";
$count="count";
$lastused="lastused";
$sql = "UPDATE emailtbl SET lastused=NOW(), count=$count + 1 WHERE id=$id";
if ($con->query($sql) === TRUE) { echo "Record updated successfully"; }
else { echo "Error updating record: " . $con->error; }
}
?>
<p>
<button onclick="myFunction('<?php echo $target;?>')">go to url</button>
<?php
?>
</body></html>

Are you trying to relocate it on the same host or a different host? What error(s) are you getting on the new url?

Maybe it’s too early in the morning but I cannot find any JavaScript in the code. So no definition of myFunction.

Also you are defining query_display but never using it.

I see you have also posted this question multiple times in the BleepingComputer | Technical Support Forums, such as Hi, whoever you are who is going to solve this. - Programming.

I hoped someone would help. sorry

What line is it that does not do that? Is it the line that is:

<button onclick="myFunction('<?php echo $target;?>')">go to url</button>

Note that above that is:

$target="target";

Right? So the myFunction is getting target, right? Is that what is intended?

If the problem is happening where myFunction is called then you need to show myFunction. Everything else is probably not relevant. Showing very much that is not relevant might cause someone to not even try to help.

If the problem is somewhere else in the code then please be specific.

You haven’t explained what you are trying to do. If you don’t answer questions aimed to get a clearer picture, how to you expect people to help you?

I want to select a target(which is an url) from the menu and then go to that target.

Sorry folks but I can’t see a legitimate site having an interface which displays username and password and going to a url is the intended action. Maybe I am reading too much into this but it looks like a hacker interface dealing with stolen information and clicking the “target” takes you to the login page of the particular user.

It may just be the OPs own site that they use to store their own passwords for other sites - I also use different emails for different sites, so I can see who is selling my email address and make it easier to block emails if I later regret signing up for something. I write mine in a book as it’s pretty unlikely that will get hacked.

I never imagined that asking for coding help would bring out psychiatrists, accountants and saints to help. I have been advised as to logic, accounting, sent to manuals and accused of theft just for requesting syntax in a personal project.

Well why not simply use an <a href tag?

<a href="<?php echo $target;?>"><button>go to url</button></a>

You could also use an onclick event.

<button onclick="window.location.href='<?php echo $target;?>';">go to url</button>

You could also set the action attribute of a form to direct to the “target”.

<form action="<?php echo $target;?>" method="post">
    <button>go to url</button>
</form>

But to write a JS function, i.e. myFunction, which in general is user-side (within html) to perform a redirect which in general is server-side (before output to the browser) is problematic.

Not… quite sure how its problematic when you’ve shown the code to do that exact thing in your second code block… it would hardly be the first time someone around here has wrapped a single line of code in a function for whatever reason.

That said…

I think where you’ve gone wrong in this thread is that you phrased your OP like you have some code that isnt working. You then didnt show that code, and when people have asked…at least 4 times in this thread, you still havent shown them the code. You also haven’t indicated what I believe you actually mean…

What I take you to mean is you havent written the code and you’re asking someone to write it for you. Which is a completely different ask.

Fortunately for you, Drummin has provided the code necessary to execute what you’re asking about.

This is so discouraging. I thought that some knowledgable PHP programmer would say “oh you just do
this” or that can’t de done but I keep getting statements such as "You haven’t explained what you
are trying to do. If you don’t answer questions aimed to get a clearer picture, how to you expect
people to help you?

I’ve posted my objective several times in various ways such as “I want to select a target(which is
a website - field name is “website”) from the menu and then go to that target”. I’ve posted my code
and screenshots and thought since I could see them, you could also. Please don’t accuse me of trying
to get you to write for me, the code is written. I am a retired senior just trying to maintain awareness.
If I’m welcome to use this forum, say so.

<!DOCTYPE><html><head><title>lookup menu</title></head>
<body><center>
<form name="form" method="post" action="">
<?php
$con=mysqli_connect("localhost","root","","homedb");
//============== check connection
if(mysqli_errno($con))
{echo "Can't Connect to mySQL:".mysqli_connect_error(); exit; }
else
{echo "Connected to mySQL</br>";}
//This creates the drop down box
echo "<select name= 'website'>";
echo '<option value="">'.'--- Select lookup account---'.'</option>';
$query = mysqli_query($con,"SELECT website FROM emailtbl");
$query_display = mysqli_query($con,"SELECT * FROM emailtbl");
while($row=mysqli_fetch_array($query))
{echo "<option value='". $row['website']."'>".$row['website']
.'</option>';}
echo '</select>';
?>
<p><p>
<CENTER>then

<input type="submit" name="submit" value="Submit"/>
</form>
<?php
if(isset($_POST['website']))
{
$name = $_POST['website'];
$fetch="SELECT id, website, called, username, password, purpose, emailused,lastused, count FROM emailtbl WHERE target = '".$name."'";
$result = mysqli_query($con,$fetch);
if(!$result)
{echo "Error:".(mysqli_error($con));}
//display the table
echo '<table border="1">'.'<tr>'.'<td bgcolor="#ccffff align="center">'. 'lookup menu'. '</td>'.'</tr>';
echo '<tr>'.'<td>'.'<table border="1">'.'<tr>'.'
<td bgcolor="#CFB53B align="center">'.'id'.'</td>'.'
<td bgcolor="#CFB53B align="center">'.'website'.'</td>'.'
<td bgcolor="#CFB53B align="center">'.'called'.'</td>'.'
<td bgcolor="#ccffff align="center">'.'username'.'</td>'.'
<td bgcolor="#ccffff align="center">'.'password'.'</td>'.'
<td bgcolor="#ccffff align="center">'.'purpose'. '</td>'.'
<td bgcolor="#ccffff align="center">'.'emailused'.'</td>'.'
<td bgcolor="#CFB53B align="center">'.'lastused'.'</td>'.'
<td bgcolor="#CFB53B align="center">'.'count'. '</td>'.'</tr>';
while($data=mysqli_fetch_row($result))
{echo ("<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td><td>$data[3]</td>
<td>$data[4]</td><td>$data[5]</td><td>$data[6]</td><td>$data[7]</td><td>$data[8]</td></tr>");}
echo '</table>'.'</td>'.'</tr>'.'</table>';
$id="id";
$website="website";
$count="count";
$lastused="lastused";
$sql = "UPDATE emailtbl SET lastused=NOW(), count=$count + 1 WHERE id=$id";
if ($con->query($sql) === TRUE) { echo "Record updated successfully"; }
else { echo "Error updating record: " . $con->error; }
}
?>
<p>
<a href="<?php echo $website;?>">go to website</a>
<!-- <a href="<?php echo $website;?>"><button>go to website</button></a>-->
<!--<button onclick="window.location.href='<?php echo $website;?>';">go to website</button>-->
<!--<form action="<?php echo $website;?>" method="post">
    <button>go to website</button></form>-->
<!--<button onclick="myFunction('<?php echo $website;?>')">go to website</button>-->
<?php
?>
</body></html>

Show me. Anywhere. In your code that you have shown us for the last 14 posts. Where you define the function “myFunction”. Then you can tell me your code is written.

Find the words “function myFunction” anywhere in this thread.

You have some of the most knowledgable PHP coders out there answering your thread, but #1: The definition for myFunction is a Javascript function, not a PHP one. #2: Even though you’ve got really knowledgable Javascript people answering the thread too, you have not shown us this mythical function you’ve written, so we can’t answer your question.

You have walked into a garage, told the mechanics your car’s busted, and left the car at home, Sir.

1 Like

Do the fields website and target hold the same value?
You are using the field website to populate your select options, then on POST you are using the selected value to query against the same table using the field target.

The main issue I believe you are having with your code is the location where you are defining $website as you have it AFTER the closing bracket } of the while loop. Any query result value[keys] , i.e. $data[1] is not present outside the while loop. A quick fix would be to define the variable $website within the while loop so the defined variable could be using in your link below. Your code modified.

while($data=mysqli_fetch_row($result))
{echo "<tr><td>$data[0]</td><td>$data[1]</td><td>$data[2]</td><td>$data[3]</td>
<td>$data[4]</td><td>$data[5]</td><td>$data[6]</td><td>$data[7]</td><td>$data[8]</td></tr>";
$website=$data[1];
}

That should “fix” your page.

I understand this is for your own use but it is a good practice to check user input before doing a database query with it. You are querying for the field website for your select and passing this value through POST so you should compare POST against this list of websites.

You would start by defining an array of websites built with your query results. Place at the top of your page before output to the browser. NOTE: I changed your connection type.

$con = new mysqli("localhost","root","","homedb");

//websites
$websites=array();
$query = mysqli_query($con,"SELECT website FROM emailtbl");
while($row=mysqli_fetch_array($query)){
	$websites[] = $row['website'];
} 

You can then use this array to build your <select> options instead of a separate query.

<?php
echo '<select name="website">';
	echo '<option value="">'.'--- Select lookup account---'.'</option>';
	foreach($websites as $website){
		echo '<option value="'. $website.'">'.$website.'</option>';
	}
echo '</select>';
?>

Now back to the point of validating the user input sent by a form. You would simply check if the value is in the $websites array by using in_array() like so.

if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['website']) && in_array($_POST['website'],$websites)){

}

I would place this IF condition just below where the $websites array was built as any POST processing should really be above output to the browser so updated information is shown to the user.

It would be within this IF condition where I do the UPDATE query for count field. There is no need to do an extra query here to get the current count as the query can be written as field=field + 1 or

`count`=`count` + 1

Looking through the forum you will notice people using or talking about prepared statements to make database queries in a more secure way. This was the reason I changed your connection type. Your UPDATE query can then be made like so.


if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['website']) && in_array($_POST['website'],$websites)){

	$sql_update = "UPDATE `emailtbl` SET `lastused`=NOW(), `count`=`count` + 1 WHERE `website`=?";
	$query_update = $con->prepare($sql_update);
	$query_update->bind_param("s", $_POST['website']); 		
	$query_update->execute();
}

On your page you are also displaying the selected record so we should grab this data while we are in this validated POST IF condition and like the UPDATE query we’ll use a prepared statement query to handle the POST user input. Also like the $websites array we’ll put the website data into an array to be used anywhere on the page. All in all the code before output to browser now looks like this.

<?php 
$con = new mysqli("localhost","root","","homedb");

//websites
$websites=array();
$websitedata=array();

$query = mysqli_query($con,"SELECT website FROM emailtbl");
while($row=mysqli_fetch_array($query)){
	$websites[] = $row['website'];
} 

if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['website']) && in_array($_POST['website'],$websites)){

	$sql_update = "UPDATE `emailtbl` SET `lastused`=NOW(), `count`=`count` + 1 WHERE `website`=?";
	$query_update = $con->prepare($sql_update);
	$query_update->bind_param("s", $_POST['website']); 		
	$query_update->execute();
							 
	//websitedata
	$sql_websitedata ="SELECT `id`, `website`, `called`, `username`, `password`, `purpose`, `emailused`,`lastused`, `count` FROM `emailtbl` WHERE `target`=?";	
	$query_websitedata = $con->prepare($sql_websitedata);
	$query_websitedata->bind_param("s", $_POST['website']);  
	$query_websitedata->execute();		
	$result_websitedata = $query_websitedata->get_result();		
	while ($row = $result_websitedata->fetch_assoc()){
		$websitedata[] = $row;
	}	
}
?>

You can then have a clean html section free of database queries.

<!DOCTYPE>
<html>
	<head>
	<title>lookup menu</title>
	</head>
<body>
<center>
<form name="form" method="post" action="">
<?php
echo '<select name="website">';
	echo '<option value="">'.'--- Select lookup account---'.'</option>';
	foreach($websites as $website){
		echo '<option value="'. $website.'">'.$website.'</option>';
	}
echo '</select>';
?>

<p>then
<input type="submit" name="submit" value="Submit"/></p>
</form>
<?php
if(!empty($websitedata)){
	//display the table
	echo '<table border="1">
		<tr>
			<td style="background-color:#ccffff; text-align:center">lookup menu</td>
		</tr>
		<tr>
			<td>
				<table border="1">
					<tr>
						<td style="background-color:#CFB53B; text-align:center">id</td>
						<td style="background-color:#CFB53B; text-align:center">website</td>
						<td style="background-color:#CFB53B; text-align:center">called</td>
						<td style="background-color:#ccffff; text-align:center">username</td>
						<td style="background-color:#ccffff; text-align:center">password</td>
						<td style="background-color:#ccffff; text-align:center">purpose</td>
						<td style="background-color:#ccffff; text-align:center">emailused</td>
						<td style="background-color:#CFB53B; text-align:center">lastused</td>
						<td style="background-color:#CFB53B; text-align:center">count</td>
					</tr>';
				foreach($websitedata as $data){
					echo '<tr>
						<td>'.$data['id'].'</td>
						<td>'.$data['website'].'</td>
						<td>'.$data['called'].'</td>
						<td>'.$data['username'].'</td>
						<td>'.$data['password'].'</td>
						<td>'.$data['purpose'].'</td>
						<td>'.$data['emailused'].'</td>
						<td>'.$data['lastused'].'</td>
						<td>'.$data['count'].'</td>
					</tr>';
				}
				echo '</table>
			</td>
		</tr>
	</table>';
	
	echo '<p><a href="'.$website.'">go to website</a></p>';
}
?>

</center>
</body>
</html>

Note the variable $website for link is defined the display loop “like” I mentioned in the beginning of my last post on this version.

<?php 
$con = new mysqli("localhost","root","","homedb");

//websites
$websites=array();
$websitedata=array();

$query = mysqli_query($con,"SELECT website FROM emailtbl");
while($row=mysqli_fetch_array($query)){
	$websites[] = $row['website'];
} 

if($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['website']) && in_array($_POST['website'],$websites)){

	$sql_update = "UPDATE `emailtbl` SET `lastused`=NOW(), `count`=`count` + 1 WHERE `website`=?";
	$query_update = $con->prepare($sql_update);
	$query_update->bind_param("s", $_POST['website']); 		
	$query_update->execute();
							 
	//websitedata
	$sql_websitedata ="SELECT `id`, `website`, `called`, `username`, `password`, `purpose`, `emailused`,`lastused`, `count` FROM `emailtbl` WHERE `target`=?";	
	$query_websitedata = $con->prepare($sql_websitedata);
	$query_websitedata->bind_param("s", $_POST['website']);  
	$query_websitedata->execute();		
	$result_websitedata = $query_websitedata->get_result();		
	while ($row = $result_websitedata->fetch_assoc()){
		$websitedata[] = $row;
	}	
}
?>
<!DOCTYPE>
<html>
	<head>
	<title>lookup menu</title>
	</head>
<body>
<center>
<form name="form" method="post" action="">
<?php
echo '<select name="website">';
	echo '<option value="">'.'--- Select lookup account---'.'</option>';
	foreach($websites as $site){
		echo '<option value="'. $site.'">'.$site.'</option>';
	}
echo '</select>';
?>

<p>then
<input type="submit" name="submit" value="Submit"/></p>
</form>
<?php
if(!empty($websitedata)){
	//display the table
	echo '<table border="1">
		<tr>
			<td style="background-color:#ccffff; text-align:center">lookup menu</td>
		</tr>
		<tr>
			<td>
				<table border="1">
					<tr>
						<td style="background-color:#CFB53B; text-align:center">id</td>
						<td style="background-color:#CFB53B; text-align:center">website</td>
						<td style="background-color:#CFB53B; text-align:center">called</td>
						<td style="background-color:#ccffff; text-align:center">username</td>
						<td style="background-color:#ccffff; text-align:center">password</td>
						<td style="background-color:#ccffff; text-align:center">purpose</td>
						<td style="background-color:#ccffff; text-align:center">emailused</td>
						<td style="background-color:#CFB53B; text-align:center">lastused</td>
						<td style="background-color:#CFB53B; text-align:center">count</td>
					</tr>';
				foreach($websitedata as $data){
					echo '<tr>
						<td>'.$data['id'].'</td>
						<td>'.$data['website'].'</td>
						<td>'.$data['called'].'</td>
						<td>'.$data['username'].'</td>
						<td>'.$data['password'].'</td>
						<td>'.$data['purpose'].'</td>
						<td>'.$data['emailused'].'</td>
						<td>'.$data['lastused'].'</td>
						<td>'.$data['count'].'</td>
					</tr>';
					$website = $data['website'];
				}
				echo '</table>
			</td>
		</tr>
	</table>';
	
	echo '<p><a href="'.$website.'">go to website</a></p>';
}
?>

</center>
</body>
</html>