user2147051
user2147051

Reputation: 3

Displaying MYSQL data in a HTML table AFTER a search

I want to thank everyone here for the help I have recieved so far. My next question is a bit more complicated.

So I have a database set up on my server, and I have a form on my website where I am submitting data to my MYSQL database.

After I submit the data, I am having trouble searching for it, displaying possible results, and then making those results HYPERLINKED so that the user can find out more about they are looking for.

My "common.php" script is set up like this:

<?php

$username = "XXX";
$password = "XXX";
$hostname = "XXX"; 
$database = "XXX";

mysql_connect($hostname, $username, $password, $database) or die

("Unable to connect to MySQL");

echo "Connected to MySQL<br>";

?>��

My "insertdata.php" script is set up like this:

<?php

require("common.php");

// connect with form

$name=$_POST['firstname'];
$lastname=$_POST['lastname'];
$city=$_POST['city'];
$state=$_POST['state'];
$zip=$_POST['zip'];
$phone=$_POST['phone'];
$email=$_POST['email'];
$various=$_POST['various'];
$other=$_POST['other'];


//  insert data into mysql

$query="INSERT INTO datatable
(
firstname, 
lastname, 
city, 
state, 
zip, 
phone,
email,
various, 
other, 
)
VALUES
(
'$firstname', 
'$lastname', 
'$city', 
'$state', 
'$zip', 
'$phone',
'$email', 
'$various', 
'$other', 
)";

$result=mysql_query($query);

// if successfull displays message "Data was successfully inserted into the database". 

if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}

else {
echo "ERROR... data was not successfully insert into the database";
}

mysql_close();

?>��

From there, I want to make the inserted data searchable.

My problem is, when the search is completed, I want to only display the First Name and Last Name in two separate columns.

From there, I want a link displayed in a third separate column with a link in each row that says "View Record Details."

Finally, when "View Record Details" in clicked, it brings me to the correct record, formatted again in an HTML table.

The closest I have come to a solution is:

<?php
require("common.php");
$query="SELECT * FROM datatable";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$firstname=mysql_result($result,$i,"firstame");
$lastname=mysql_result($result,$i,"lastname");
$i++;}
?>

As an additional question, when I use PDO, does that change my HTML?

Upvotes: 0

Views: 1873

Answers (1)

ethrbunny
ethrbunny

Reputation: 10469

Switch to PDO. Your code will look something like this:

$conn = new PDO('mysql:host=db_host;dbname=test', $user, $pass);  
$sql = 'SELECT * FROM datatable';   
foreach ($conn->query($sql) as $row) {  
    print $row['firstname'] . "\t";  
    print $row['lastname'] . "\n";  

}  

EDIT: To link back for details add this line after the 2nd print:

print "<a href='somephp.php?idx=" . $row[ 'idx' ] . "'>link here</a>";

You'll need another php file called 'somephp.php':

$conn = new PDO('mysql:host=db_host;dbname=test', $user, $pass);  
$idx = $_REQUEST[ 'idx' ];

$sql = 'SELECT * FROM datatable where idx = ?';   
$stmt = $conn->prepare( $sql );
$stmt->bindParam( 1, $idx );
$stmt->execute();
$row = $stmt->fetch();

// now print all the values...
print $row['firstname'] . "\t";  
print $row['lastname'] . "\t";  
print $row['address'] . "\t";  

and so on...

NOTE: This depends on each record having a unique key 'idx'. I don't see this in your values above so you'll have to find a way to incorporate it if you want to use this code.

ALSO: You ask - does this change the HTML and does this handle table formatting - No to both. You do all the HTML formatting via the print statements. All PHP does it output lines to the browser.

Upvotes: 1

Related Questions