help w/mysli

1Tsme1941

Member
Hi guys, the below code creates my dropdown.
I select the record and submit and the selected record is displayed.
------------------------------------------------------------------
<!DOCTYPE><html><head><title>email menu</title></head>
<body><center>
<form name="form" method="post" action="">
<?php
// error_reporting(0);
error_reporting(E_ALL ^ E_NOTICE);
include 'homedb-connect.php';

//This creates the drop down box
echo "<select name= 'target'>";
echo '<option value="">'.'--- Select email account ---'.'</option>';
$query = mysqli_query($con,"SELECT target FROM lookuptbl");
$query_display = mysqli_query($con,"SELECT * FROM lookuptbl");
while($row=mysqli_fetch_array($query))
{echo "<option value='". $row['target']."'>".$row['target']
.'</option>';}
echo '</select>';
?>
<input type="submit" name="submit" value="Submit"/><!-- update "lastused" using selected "target"-->
</form></body></html>

<!DOCTYPE><html><head><title>email menu</title></head>
<body><center>

<?php
// error_reporting(0);
error_reporting(E_ALL ^ E_NOTICE);
include 'homedb-connect.php';
if(isset($_POST['target']))
{
$name = $_POST['target'];
$fetch="SELECT target, purpose, user, password, email, visits, date,
saved FROM lookuptbl 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">'. 'Email menu'. '</td>'.'</tr>';
echo '<tr>'.'<td>'.'<table border="1">'.'<tr>'.'
<td bgcolor="#CFB53B>'.'target'.'</td>'.'
<td bgcolor="#ccffff>'.'purpose'. '</td>'.'
<td bgcolor="#ccffff>'.'user'.'</td>'.'
<td bgcolor="#ccffff>'.'password'.'</td>'.'
<td bgcolor="#ccffff>'.'email'.'</td>'.'
<td bgcolor="#CFB53B>'.'visits'. '</td>'.'
<td bgcolor="#CFB53B>'.'date'.'</td>'.'
<td bgcolor="#CFB53B>'. 'saved' .'</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></tr>");}
echo '</table>'.'</td>'.'</tr>'.'</table>';
}
?>
</body></html>
=========================================

but the headers
should be: target purpose user password email visits date saved
but are: purpose password visits saved
target,user,email,and date are skipped (every other heading is skipped)?
-----------------------------------------------
a screenshot is included to illustrate
--------------------------------------------
What's left after is that I want include a button to link to the target.
suggestions?
 

Attachments

  • dropdown.png
    dropdown.png
    211.3 KB · Views: 3

Cromewell

Administrator
Staff member
Why are you writing some strings with a bunch of concatenations? i.e.
Code:
'<table border="1">'.'<tr>'.'<td bgcolor="#ccffff align="center">'. 'Email menu'. '</td>'.'</tr>';
Instead of
Code:
'<table border="1"><tr><td bgcolor="#ccffff align="center">Email menu</td></tr>';
It's not going to change the output, but it makes it easier for me to read.

For your missing columns, you are missing a closing " in your html.
Code:
<td bgcolor="#ccffff align="center">
                   ^
Actually, you are missing the closing quote on all your bgcolor settings
 

1Tsme1941

Member
thanks so much, the concatenations were suggested by another forum and puzzled me. Please, how to incorporate a button and "onclick" to link to "target"?
 

1Tsme1941

Member
thanks so much, the concatenations were suggested by another forum and puzzled me. Please, how to incorporate a button and "onclick" to link to "target"?
here is what I tried. of course no go:

<!DOCTYPE><html><head><title>lookup menu</title></head>
<body><center>
<script language="Javascript" type="text/javascript">
function gotolink() {
var destination= self.location;

for(var i = 0; i<document.formname.radiobutton.length; i++){
if(document.formname.radiobutton.checked) {
destination=document.formname.radiobutton.value }
}
window.location = destination;
}
</script>

<form name="form" method="post" action="">
<?php
// error_reporting(0);
error_reporting(E_ALL ^ E_NOTICE);
include 'homedb-connect.php';

//This creates the drop down box
echo "<select name= 'target'>";
echo '<option value="">'.'--- Select account ---'.'</option>';
$query = mysqli_query($con,"SELECT target FROM lookuptbl");
$query_display = mysqli_query($con,"SELECT * FROM lookuptbl");
while($row=mysqli_fetch_array($query))
{echo "<option value='". $row['target']."'>".$row['target']
.'</option>';}
echo '</select>';
?>
<input type="submit" name="submit" value="Submit"/><!-- update "lastused" using selected "target"-->
</form><center>

<?php
// error_reporting(0);
error_reporting(E_ALL ^ E_NOTICE);
include 'homedb-connect.php';
if(isset($_POST['target']))
{
$name = $_POST['target'];
$fetch="SELECT target, purpose, user, password, email, visits, date,
saved FROM lookuptbl 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>
<tr><td><table border="1">

<th bgcolor="#CFB53B"><input name="radiobutton" type="radio"
onClick="gotolink()">target</th>

<th bgcolor="#ccffff">purpose</th>
<th bgcolor="#ccffff">user</th>
<th bgcolor="#ccffff">password</th>
<th bgcolor="#ccffff">email</th>
<th bgcolor="#CFB53B">visits</th>
<th bgcolor="#CFB53B">date</th>
<th bgcolor="#CFB53B">saved</th></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></tr>");}
echo '</table>'.'</td>'.'</tr>'.'</table>';
}
?>
</body></html>
 

Cromewell

Administrator
Staff member
Something like this would work.
Code:
echo "<button onclick='window.location.href=\"$data[0]\"';Click Here</button>";
Throwing a UI framework in as you progress further would probably be better/easier. But right now it looks like you are working on the basics so no need to add more complexity than necessary. They can be a lot to absorb at first, but make many things much easier.
 

Cromewell

Administrator
Staff member
It needs to go inside your loop. Then in whichever cell you want the button in.
Asuming you want it where data[0] is already you would just do this:
Code:
echo ("<tr><td><button onclick=\"window.location.href='$data[0]'\">$data[0]</button></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></tr>");}
echo '</table>'.'</td>'.'</tr>'.'</table>';
Unless you just want a link, then use an <a> tag instead of button.

PHP will let you do a lot of source formatting to make your life easier editing this too. Even something as small as your loop at the end
Code:
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>
  </tr>";
}
 

1Tsme1941

Member
Here's the code. do I have the loop
opening and closing { }s right? See screenshot
shouldn't the button be
in the bottom line with
the "www...........?
the button does nothing there
----------------------------------------
<!DOCTYPE><html><head><title>lookup menu</title></head>
<body><center>
<script language="Javascript" type="text/javascript">
function gotolink() {
var destination= self.location;

for(var i = 0; i<document.formname.radiobutton.length; i++){
if(document.formname.radiobutton.checked) {
destination=document.formname.radiobutton.value }
}
window.location = destination;
}
</script>

<form name="form" method="post" action="">
<?php
// error_reporting(0);
error_reporting(E_ALL ^ E_NOTICE);
include 'homedb-connect.php';

//This creates the drop down box
echo "<select name= 'target'>";
echo '<option value="">'.'--- Select account ---'.'</option>';
$query = mysqli_query($con,"SELECT target FROM lookuptbl");
$query_display = mysqli_query($con,"SELECT * FROM lookuptbl");
while($row=mysqli_fetch_array($query))
{echo "<option value='". $row['target']."'>".$row['target']
.'</option>';}
echo '</select>';
?>
<input type="submit" name="submit" value="Submit"/><!-- update "lastused" using selected "target"-->
</form><center>

<?php
// error_reporting(0);
error_reporting(E_ALL ^ E_NOTICE);
include 'homedb-connect.php';
if(isset($_POST['target']))
{
$name = $_POST['target'];
$fetch="SELECT target, purpose, user, password, email, visits, date,
saved FROM lookuptbl 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>
<tr><td><table border="1">

<th bgcolor="#CFB53B"><input name="radiobutton" type="radio"
onClick="gotolink()">target</th>

<th bgcolor="#ccffff">purpose</th>
<th bgcolor="#ccffff">user</th>
<th bgcolor="#ccffff">password</th>
<th bgcolor="#ccffff">email</th>
<th bgcolor="#CFB53B">visits</th>
<th bgcolor="#CFB53B">date</th>
<th bgcolor="#CFB53B">saved</th></tr>';
while($data=mysqli_fetch_row($result))
{
echo ("<tr><td><button onclick=\"window.location.href='$data[0]'\">$data[0]</button></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></tr>");}
echo '</table>'.'</td>'.'</tr>'.'</table>';
}
?>
</body></html>
 

Attachments

  • dropdown.png
    dropdown.png
    158.3 KB · Views: 5

Cromewell

Administrator
Staff member
Your { } look ok.

Thinking more, you likely need http:// in front of the url target. As it is, I would expect it to try and navigate to http://localhost/home/crud-link.php/www.codelobster.com which is clearly not what you are intending

You could in also abuse an html form to get the linking to work. This method doesn't rely on javascript, but javascript being disabled isn't really a thing you need to worry about anymore.
echo '<form action="http://' . $data[0] . '"><button type="submit">Go to ' . $data[0] . '</button></form> ';

What's going on with the radio buttons? Just trying another method to link?
 

1Tsme1941

Member
ok Cromewell, clicking the link ets "object not found". I certainly don't want to name the link(target) each time.
please explain "echo '<form action="http://' . $data[0] . '"><button type="submit">Go to ' . $data[0] . '</button></form> '; "
I really prefer a link
 

1Tsme1941

Member
ok Cromewell, now I get " Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\home\crud-link.php on line 63". I certainly don't want to name the link(target) each time.
please explain "echo '<form action="http://' . $data[0] . '"><button type="submit">Go to ' . $data[0] . '</button></form> '; "
I really prefer a link. my code again:
------------------------------------------------------------------------------------------
<!DOCTYPE><html><head><title>lookup menu</title></head>
<body><center>
<script language="Javascript" type="text/javascript">
function gotolink() {
var destination= self.location;

for(var i = 0; i<document.formname.radiobutton.length; i++){
if(document.formname.radiobutton.checked) {
destination=document.formname.radiobutton.value }
}
window.location = destination;
}
</script>

<form name="form" method="post" action="">
<?php
// error_reporting(0);
error_reporting(E_ALL ^ E_NOTICE);
include 'homedb-connect.php';

//This creates the drop down box
echo "<select name= 'target'>";
echo '<option value="">'.'--- Select account ---'.'</option>';
$query = mysqli_query($con,"SELECT target FROM lookuptbl");
$query_display = mysqli_query($con,"SELECT * FROM lookuptbl");
while($row=mysqli_fetch_array($query))
{echo "<option value='". $row['target']."'>".$row['target']
.'</option>';}
echo '</select>';
?>
<input type="submit" name="submit" value="Submit"/><!-- update "lastused" using selected "target"-->
<center>

<?php
// error_reporting(0);
error_reporting(E_ALL ^ E_NOTICE);
include 'homedb-connect.php';
if(isset($_POST['target']))
{
$name = $_POST['target'];
$fetch="SELECT target, purpose, user, password, email, visits, date,
saved FROM lookuptbl 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>
<tr><td><table border="1">

<th bgcolor="#CFB53B"><input name="radiobutton" type="radio" onClick="gotolink()">target</th>

<th bgcolor="#ccffff">purpose</th>
<th bgcolor="#ccffff">user</th>
<th bgcolor="#ccffff">password</th>
<th bgcolor="#ccffff">email</th>
<th bgcolor="#CFB53B">visits</th>
<th bgcolor="#CFB53B">date</th>
<th bgcolor="#CFB53B">saved</th></tr>';
while($data=mysqli_fetch_row($result))
{
echo '<form action="http://' . $data[0] . '"><button type="submit">Go to ' . $data[0] . '</button></form> ';
<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></tr>");}
echo '</table>'.'</td>'.'</tr>'.'</table>';
}
?>
</body></html>
 
Last edited:

Cromewell

Administrator
Staff member
The forms action is where you get sent when you click the submit button. By setting it to the site you want to go to, the form sends its data, which in this case, is nothing to the location specified in the form's action attribute. It is a way to make a button a link.

I think we've got some terminology mismatches. If you want a link, as in, the blue underlined text you want an "a" tag. The structure of it is <a href="http://whereyouwanttogo.com">The Blue Underlined Text</a>. If you want a button then you need to fiddle with what we are doing now. Either the JS onclick, or a form.
I certainly don't want to name the link(target) each time.
Then you just need to change the appropriate part. Change the parts where target is getting inserted and see what the end result is.

Your parse error is from this line:
Code:
<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></tr>");}
The echo I gave you was a complete string, however it did not go into a td like the rest of what you have so far. Your code at line 63 is acting as if the string were continued from the previous line. If you are using an editor that does syntax highlighting you should see it start to look funny here.

That echo <form.... bit would have to be placed such that it goes inside your first <td>. What you have is close, but even fixing the minor syntax error, there are parts of the html missing that will break your output.

As a general rule I'm not likely to give you a complete working version. I don't think it's a good way to learn. You just end up with stackoverflow copy-pasta without really understanding what it does.
 

1Tsme1941

Member
I'm not looking for someone to write my code just knowledgeable explanation.
I left programming years go and do this to keep my mind from turning to soup.
I code to do things helpful to me or as challenge. I only ask for help after much
research and trial/error; much as result of forums.
The below code is intended to keep track of passwords,etc. and link to my bank or ebay.
I'm enjoying developing a CRUD type system to keep all current and still link to the
accounts. I hope this clears up my objective.
This code displays the target as a link but clicking only takes me back to the select/submit.
=================================================================
<!DOCTYPE><html><head>
<title>lookup menu</title>
<script language="Javascript" type="text/javascript">
function gotolink() {
var destination= self.location;

for(var i = 0; i<document.formname.radiobutton.length; i++){
if(document.formname.radiobutton.checked) {
destination=document.formname.radiobutton.value }
}
window.location = destination;
}
</script>
</head>
<body><center>
<form name="form" method="post" action="">
<?php
// error_reporting(0);
error_reporting(E_ALL ^ E_NOTICE);
include 'homedb-connect.php';

//This creates the drop down box
echo "<select name= 'target'>";
echo '<option value="">'.'--- Select account ---'.'</option>';
$query = mysqli_query($con,"SELECT target FROM lookuptbl");
$query_display = mysqli_query($con,"SELECT * FROM lookuptbl");
while($row=mysqli_fetch_array($query))
{echo "<option value='". $row['target']."'>".$row['target']
.'</option>';}
echo '</select>';
?>
<input type="submit" name="submit" value="Submit"/>
</form><center>

<?php
// error_reporting(0);
error_reporting(E_ALL ^ E_NOTICE);
include 'homedb-connect.php';
if(isset($_POST['target']))
{
$name = $_POST['target'];
$fetch="SELECT target, purpose, user, password, email, visits, date,
saved FROM lookuptbl 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>
<tr><td>
<table border="1">
<tr>
<td> Target </td>
<td> Purpose </td>
<td> User </td>
<td> Password </td>
<td> Email </td>
<td> Visits </td>
<td> Date </td>
<td> Saved </td>
</tr>';

while($data=mysqli_fetch_row($result))
{

$url= "http://localhost/home/crude-link.php?target=". $data[0];
$link= '<a href="'.$url.'">'. $data[0]. '</a>';

echo ("<tr><td> $link </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></tr>");
}
echo '</table>
</td></tr></table>';
}
?>
</body></html>
------------------------------------------
 

Cromewell

Administrator
Staff member
I'm not looking for someone to write my code just knowledgeable explanation.
I left programming years go and do this to keep my mind from turning to soup.
I code to do things helpful to me or as challenge. I only ask for help after much
research and trial/error; much as result of forums.
I didn't mean to imply you were, only trying to clarify why I don't give a full working piece you can jam in.
$url= "http://localhost/home/crude-link.php?target=". $data[0];
$link= '<a href="'.$url.'">'. $data[0]. '</a>';
What's happening here is you are crafting a url that points to http://localhost/home/crude-link.php?target=www.somewhere.com then sticking it into the target for your anchor tag.

if(isset($_POST['target']))
{
$name = $_POST['target'];
$fetch="SELECT target, purpose, user, password, email, visits, date,
saved FROM lookuptbl WHERE target = '".$name."'";
$result = mysqli_query($con,$fetch);
This bit of code is handling the wrong type of parameter. Things appended to the url after ? go into $_GET, and $_REQUEST. $_REQUEST covers POST, GET and cookie values, so it is probably better to just use that instead. $_POST is for values sent via the post method, which encodes them into the body of the request rather than sending them in the url.
 

Cromewell

Administrator
Staff member
I'm not sure I fully understand what you expect to happen vs what is actually happening.
Code:
$url= "http://localhost/home/crude-link.php?target=". $data[0];
$link= '<a href="'.$url.'">'. $data[0]. '</a>';
Assuming $data[0] = www.somewhere.com, for this block of code, I would expect that you get this in your html:
<a href="http://localhost/home/crude-link.php?target=www.somewhere.com">www.somewhere.com</a>
If that is not the case, then there is an issue.

Code wise, the only logic issue I see is the above mentioned $_POST instead of $_GET/$_REQUEST in the below block. The relevant manual entry is https://www.php.net/manual/en/reserved.variables.request.php
if(isset($_POST['target']))
{
$name = $_POST['target'];

Code:
$fetch="SELECT target, purpose, user, password, email, visits, date,
saved FROM lookuptbl WHERE target = '".$name."'";
For a home project it is likely a non-issue but for anything external you need to be careful with jamming variables from a user into your queries.
This query is vulnerable to injection, you should be binding the values in rather than string concatenating them (https://www.php.net/manual/en/mysqli-stmt.execute.php) or getting an escaped string (https://www.php.net/manual/en/mysqli.real-escape-string.php). That way it stops someone from entering http://localhost/home/crude-link.php?target=b';drop all tables;commit; or something else goofy in their address bar and destroying your database.
Code:
$fetch="SELECT target, purpose, user, password, email, visits, date,
saved FROM lookuptbl WHERE target = '". mysqli_real_escape_string ( $con , $target ) . "'";
 

1Tsme1941

Member
I just want to click on the link produced when I click "submit" and arrive at the target.
----------------------------
if(isset($_GET['target']))
{
$name = $_GET['target'];
------------------------
doesn't work if that's what you're suggesting
and neither does:
------------------------------------------
$fetch="SELECT target, purpose, user, password, email, visits, date, saved
FROM lookuptbl WHERE target = '". mysqli_real_escape_string ( $con , $target ) . "'";
 

Cromewell

Administrator
Staff member
if(isset($_GET['target']))
{
$name = $_GET['target'];
------------------------
doesn't work if that's what you're suggesting
and neither does:
------------------------------------------
$fetch="SELECT target, purpose, user, password, email, visits, date, saved
FROM lookuptbl WHERE target = '". mysqli_real_escape_string ( $con , $target ) . "'";
That is what I am saying you need, but why isn't it working? What value does target have? Does it match what you think it should be? How about the data in your db, have you confirmed it?

edit..Ah I see it. You are putting $_GET['target'] into $name

$fetch="SELECT target, purpose, user, password, email, visits, date, saved
FROM lookuptbl WHERE target = '". mysqli_real_escape_string ( $con , $name ) . "'";
 

1Tsme1941

Member
All the code looks logical (except the $url & $link) & clean..to me lol but now I don't
get the link; "select & submit" goes back to "select & submit". The targets start w/ www. and the data has been confirmed












t
-----------------------------------------------------------------
<!DOCTYPE><html><head>
<title>lookup menu</title>
</head>
<body><center><b>
<form name="form" method="post" action="">

<?php
// error_reporting(0);
error_reporting(E_ALL ^ E_NOTICE);
include 'homedb-connect.php';

//This creates the drop down box
echo "<select name= 'target'>";
echo '<option value="">'.'--- Select account ---'.'</option>';
$query = mysqli_query($con,"SELECT target FROM lookuptbl");
$query_display = mysqli_query($con,"SELECT * FROM lookuptbl");
while($row=mysqli_fetch_array($query))
{echo "<option value='". $row['target']."'>".$row['target']
.'</option>';}
echo '</select>';
?>
<input type="submit" name="submit" value="Submit"/>
</form><center>

<?php
// error_reporting(0);
error_reporting(E_ALL ^ E_NOTICE);
include 'homedb-connect.php';

// ==============================================
if(isset($_GET['target']))
{
$target = $_GET['target'];
// ===============================================
$fetch="SELECT target, purpose, user, password, email, visits, date, saved
FROM lookuptbl WHERE target = '". mysqli_real_escape_string ( $con , $target ) . "'";
// ===============================================================================

$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>
<tr><td>
<table border="1">
<tr bgcolor="#ccffff">
<td> Target </td>
<td> Purpose </td>
<td> User </td>
<td> Password </td>
<td> Email </td>
<td> Visits </td>
<td> Date </td>
<td> Saved </td>
</tr>';

while($data=mysqli_fetch_row($result))
{

// ==========================================================
$url= "http://localhost/home/crud-link.php?target=". $data[0];
$link= '<a href="'.$url.'">'. $data[0]. '</a>';
// ===========================================================

echo ("<tr><td> $link </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></tr>");
}
echo '</table>
</td></tr></table>';
}
?>
</body></html>
 
Top