help w/mysli

Cromewell

Administrator
Staff member
I think I get it now. I was having trouble following the flow of what you had written so I was focusing on the code at the end where you were having trouble.

First, I would change
Code:
if(isset($_GET['target']))
{
$target = $_GET['target'];
To
Code:
if(isset($_REQUEST['target']))
{
$target = $_REQUEST['target'];
The reason being then it doesn't matter if you post the data or send it as part of the url.

Then at the end
Code:
$url= "http://localhost/home/crud-link.php?target=". $data[0];
$link= '<a href="'.$url.'">'. $data[0]. '</a>';
Your url should just be "http://$target" that way you actually link to there rather than back through your own page.
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
I think the url you've crafted now is a misunderstanding of what this from earlier.
 

1Tsme1941

Member
I really thouht "finally" when I saw the link at the target(url) but clicking on it didn't take me to the site. see screenshot
after the select/submit and after click.
 
Last edited by a moderator:

1Tsme1941

Member
I give up, the last time I ran I clicked to select the record, got the dropdown, selected the one I wanted, clicked submit and the record was displayed with the target a link, Now with this same code(I think) I don't even get the dropdown.
-----------------------------------------------------------------------------------------------------------------------------------------
<!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>
 

Cromewell

Administrator
Staff member
Your $_GET should probably be $_REQUEST again, but that won't stop the page from loading to at least show the select. Are there any errors in your php.log? Syntax looks OK, but I may have missed something.

You set $query_display on line 16 but never use it.
 

1Tsme1941

Member
I'm feeling more and more obtuse.
I had that line ($query_display = mysqli_query($con,"SELECT * FROM lookuptbl");) commented out.
I went to the server logs and results below:

Apache/Php(php_error_log) the system cannot find the path specified

the browser php shows nothing
the code:

<!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");
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($_REUEST['target']))
{
$target = $_REUEST['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>
 

Cromewell

Administrator
Staff member
Apache/Php(php_error_log) the system cannot find the path specified
Might be this file.. but php should tell you a line number for an error.
include 'homedb-connect.php';
Or that error might be no favicon or other meaningless error.
The url isn't pointing where I would expect you to want it anymore either.
$url = "<a href='$data[0]'>$data[0] (or whatever you want your link text to be)</a>";
if(isset($_REUEST['target']))
Typo in $_REQUEST
 

1Tsme1941

Member
if, no when we resole this I'm going to endorse this forum on my website.If I am following you correctly with the code below, I now don't get the dropdown,
----------------------------------------------------------------
<!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");
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($_REUEST['target']))
{
$target = $_REUEST['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://$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
if(isset($_REUEST['target']))
Still a typo here :)
$url= "http://$target=". $data[0];
You don't need $target=$data[0], that will craft a url like http://www.computerforum.com=www.computerforum.com, just "http://$target" will do.
If I am following you correctly with the code below, I now don't get the dropdown,
I assume that means PHP is taking issue to something in your code. If you right click the blank page and pick view source do you get partial html?

I don't see anything syntax wise that is wrong, and besides that, my linter is under the impression that the file is fine. Unless it's hiding in homedb-connect.php, and I don't think you've touched that, I can't see it.

The php.log entry would be immensely helpful here. Failing that, you might be able to add echos and see which is the last one you get for an idea of where the problem may be.
 

1Tsme1941

Member
I thouht I had given the code for "homedb-connect.php". I'm getting at execution "Connected successfully".
see code below:

<?php
$dbhost = 'localhost:3306';
$dbuser = 'root';
$dbpass = '';
$dbname = 'homedb';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);

if(! $conn ) {
die('Could not connect: ' . mysqli_error());
}
echo 'Connected successfully<br>';
?>
----------------------------------------
I got the below with the view page source;
<!DOCTYPE><html><head>
<title>lookup menu</title>
</head>
<body><center><b>
<form name="form" method="post" action="">


Connected successfully<br> <select name= 'target'><option value="">--- Select account ---</option><br />
<b>Warning</b>: mysqli_query() expects parameter 1 to be mysqli, null given in <b>C:\xampp\htdocs\home\crud-link.php</b> on line <b>17</b><br />
<br />
<b>Warning</b>: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in <b>C:\xampp\htdocs\home\crud-link.php</b> on line <b>18</b><br />
</select><input type="submit" name="submit" value="Submit"/>
</form><center>


Connected successfully<br> </body></html>
---------------------------------------------------------------------------
current code below:

<!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);
require '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");
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($_REUEST['target']))
{
$target = $_REUEST['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> lastused </td>
<td> Saved </td>
</tr>';

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

// ==========================================================
$url= "http://$target";
$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
<b>Warning</b>: mysqli_query() expects parameter 1 to be mysqli, null given in <b>C:\xampp\htdocs\home\crud-link.php</b> on line <b>17</b><br />
....
$conn = mysqli_connect($dbhost, $dbuser, $dbpass,$dbname);
....
$query = mysqli_query($con,"SELECT target FROM lookuptbl");
Ah, it's just another typo :) You are connecting and storing the connection in $conn, but then using $con when you try to query.
 

1Tsme1941

Member
I click to select and no dropdown. I viewed page source and got the message indicated in the code.
notice the connection
-----------------------------------------------------------------------------------------------------------------------------------------
<!DOCTYPE><html><head>
<title>lookup menu</title>
</head>
<body><center><b>
<form name="form" method="post" action="">

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "homedb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

//This creates the drop down box
echo "<select name= 'target'>";
echo '<option value="">'.'--- Select account ---'.'</option>';

$query = mysqli_query($conn,"SELECT target FROM lookuptbl");

// ------------------------------------------------------------
while($row=mysqli_fetch_array($query)) // mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean
// -----------------------------------------------------------

{echo "<option value='". $row['target']."'>".$row['target']
.'</option>';}
echo '</select>';
?>
<input type="submit" name="submit" value="Submit"/>
</form><center>

<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "homedb";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
// ==============================================
if(isset($_REUEST['target']))
{
$target = $_REUEST['target'];
// ===============================================
$fetch="SELECT target, purpose, user, password, email, visits, date, saved
FROM lookuptbl WHERE target = '". mysqli_real_escape_string ( $conn , $target ) . "'";
// ===============================================================================

$result = mysqli_query($conn,$fetch);
if(!$result)
{echo "Error:".(mysqli_error($conn));}

//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> lastused </td>
<td> Saved </td>
</tr>';

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

// ==========================================================
$url= "http://$target";
$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
// ------------------------------------------------------------
while($row=mysqli_fetch_array($query)) // mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean
// -----------------------------------------------------------
This is saying something went wrong with your query.
try
echo mysqli_error($conn); before you try to fetch the rows.

You should really check that your result isn't false as well.
Code:
$query = mysqli_query($conn,"SELECT target FROM lookuptbl");

if ($query === false) {
  echo "Something went wrong<br />";
  echo mysqli_error($conn);
}
else {
  while($row=mysqli_fetch_array($query))
For a home project something like the above is fine, but for anything that is public/customer facing you don't want to echo out error messages directly like this.
 

1Tsme1941

Member
I hear y
<!DOCTYPE><html><head>
<title>lookup menu</title>
</head>
<body><center><b>
<form name="form" method="post" action="">

<?php
$servername = "localhost"; $username = "root"; $password = ""; $dbname = "homedb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); }

//This creates the drop down box
echo "<select name= 'target'>";
echo '<option value="">'.'--- Select account ---'.'</option>';
$query = mysqli_query($conn,"SELECT target FROM lookuptbl");
if ($query === false)
{ echo "Something went wrong<br />"; echo mysqli_error($conn); }
else
{ echo mysqli_error($conn); }

//-----------------------------------------------------
while($row=mysqli_fetch_array($query)) //mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean on line 22
//--------------------------------------------------

{echo "<option value='". $row['target']."'>".$row['target'] .'</option>';}
echo '</select>';
?>
<input type="submit" name="submit" value="Submit"/>
</form><center>

<?php
$servername = "localhost"; $username = "root"; $password = ""; $dbname = "homedb";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error)
{ die("Connection failed: " . $conn->connect_error); }
// ==============================================
if(isset($_REUEST['target']))
{
$target = $_REUEST['target'];
// ===============================================
$fetch="SELECT target, purpose, user, password, email, visits, date, saved
FROM lookuptbl WHERE target = '". mysqli_real_escape_string ( $conn , $target ) . "'";
// ===============================================================================

$result = mysqli_query($conn,$fetch);
if(!$result)
{echo "Error:".(mysqli_error($conn));}

//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> lastused </td>
<td> Saved </td>
</tr>';

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

// ==========================================================
$url= "http://$target";
$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>
ou re. your caution.
Current code including error message:
---------------------------------------------
 

Cromewell

Administrator
Staff member
else
{ echo mysqli_error($conn); }
You want to fetch results in the else branch, not echo the error, as in the else there is no error. The error would only exist if the query failed, and the error condition is handled in the if block.

I would also expect some output on your generated page indicating the error, the error (//mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean on line 22) indicated that the query failed for some reason. You should see the text "Something went wrong" followed by the error from the database.
 

1Tsme1941

Member
I'm just discouraged that we've tried so many things and seem to backing up. Once we had the dropdown and the record showing as a link, now I don't even get the dropdown. I've tried all your suggestions, tried everything I could , spent months studying manuals and trying what other experts said would work and computerforum is the only source to come close. And I do appreciate your eforts.
 

Cromewell

Administrator
Staff member
IMO there are 2 key factors in that.
  1. I'm not sitting beside you so that we see the same thing
  2. Terminology mismatches - We say something and it means different things to each of us
On to issues I can see
  • You need $_REQUEST, not $_REUEST
  • You are (re)connecting to your db more than you need to, every <?php ?> block on your page shares variables/namespace
    • Also, when reconnecting in different <?php blocks you've mixed OO style and procedural style
    • This may actually work, but it's confusing
  • I may be wrong, but you seem to be rewriting the entire file for each iteration I would only focus on one section at time and get each working iteritively

I understand your frustration, I just about uninstalled MySQL and switched to postgres when trying to run your code. I was trying to figure out why it was telling my I was trying to log in as root when I definitely wasn't, then I saw the second and third connects. To that end, if you really need it....
Code:
<?php
$dbhost = 'localhost';
$dbuser = 'myuser';
$dbpass = 'mypassword';
$dbname = 'dbname';
$conn   = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

if (!$conn) {
   die('Could not connect: ' . mysqli_connect_error());
}
?>
<!DOCTYPE><html><head>
<title>lookup menu</title>
</head>
<body><center><b>
<form name="form" method="post" action="">

<?php
//This creates the drop down box
echo "<select name= 'target'>";
echo '<option value="">' . '--- Select account ---' . '</option>';
$query = mysqli_query($conn, "SELECT target FROM lookuptbl");
if ($query === false) {
   echo "Something went wrong<br />";
   echo mysqli_error($conn);
} else {
   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
// ==============================================
if (isset($_REQUEST['target'])) {
   $target = $_REQUEST['target'];
   // ===============================================
   $fetch  = "SELECT target, purpose, user, password, email, visits, date, saved
FROM lookuptbl WHERE target = '" . mysqli_real_escape_string($conn, $target) . "'";
   // ===============================================================================
  
   $result = mysqli_query($conn, $fetch);
   if (!$result) {
       echo "Error:" . (mysqli_error($conn));
   }
  
   //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> lastused </td>
<td> Saved </td>
</tr>';
  
   while ($data = mysqli_fetch_row($result)) {
      
       // ==========================================================
       $url  = "http://$target";
       $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>
upload_2020-3-13_21-32-40.png
 
Top