SoeiroMass SoeiroMass - 6 months ago 11
SQL Question

php - How can I see a line of table, before search a field

I have been making a website that I can have access a database, and I'm new in php.

That table is a
cliente table and I want in php, to search for a 'numSerieProduto_cliente', and when find something show all lines.

Ex: Search for 1234, and show this: exemple

The page that I create and I want implemented that type of search:

<?php

require_once("./include/membersite_config.php");

if(!$fgmembersite->CheckLogin())
{
$fgmembersite->RedirectToURL("login.php");
exit;
}
if ($_POST["Password"] == 'mypw'){
echo "<a href='register.php'> Registar. </a><br></li>";
echo "<a href='confirmreg.php'> Confirmar a Registar. </a><br></li>";
echo "<a href='access-controlled.php'> Area de Trabalhador. </a><br></li>";
}else{
header('Location:access-controlledlog.php');
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
<head>
<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
<title> Area de Admenistrador </title>
<link rel="STYLESHEET" type="text/css" href="style/fg_membersite.css">
</head>
<body>
<div id='fg_membersite_content'>
<p>
Logged in as: <?= $fgmembersite->UserFullName() ?>
</p>
<p>
<a href='login-home.php'>Home</a>
</p>
</div>
</body>
</html>


How can I make this happen ?

Mysql code for produto table:

CREATE TABLE IF NOT EXISTS `produto` (
`id_produto` int(16) NOT NULL AUTO_INCREMENT,
`numSerie_produto` int(128) NOT NULL,
`nomeDoProduto_produto` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id_produto`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1 ;


Mysql code for cliente table:

CREATE TABLE IF NOT EXISTS `cliente` (
`id_cliente` int(16) NOT NULL AUTO_INCREMENT,
`numServico_cliente` int(32) NOT NULL,
`nome_cliente` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
`morada_cliente` varchar(256) COLLATE utf8_unicode_ci NOT NULL,
`telefone_cliente` int(10) NOT NULL,
`produto_cliente` varchar(128) COLLATE utf8_unicode_ci NOT NULL,
`numSerieProduto_cliente` int(128) NOT NULL,
PRIMARY KEY (`id_cliente`,`numServico_cliente`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=3 ;

Answer

First, you will need to establish a connection via php to your database like this

<?php
$connection = new mysqli("DB_SERVER", "DB_USER", "DB_PASSWORD", "DATABASE_NAME");
?>

you can then build the database select statements using this connection like this

function getInfo(){
    global $connection; // This is only needed if you use require_once. see below
    $stmt = $connection->prepare("SELECT * FROM cliente");
    $stmt->execute();
    $stmt->close();
}

You can then assign that output to a variable like this

$entries = getinfo();

and then since it will be an array of information you can iterate over the information like this

foreach($entries as $DBentries){
   echo $DBentries['id_cliente'];
   echo $DBentries['numServico_cliente'];
   // et cetera.... for each column...
}

I would also like to suggest that you place your database information and functions in separate files and then include them with require_once. It makes it easier to access the connections and functions and is more secure.

To build a table of this data you could do

<table>
<th>id_cliente</th>
<?php
foreach ($entries as $DBentries) {
echo "<tr>";
echo "<td>".$DBentries['id_cliente']."</td>";
echo "</tr>";
}
?>
</table>

Also, it is best practice to only select the columns that you need in a select statement and not use the '*' wildcard unless you actually need every column. You can select multiple tables like this"

SELECT column_name1,column_name2,column_name3 FROM TABLENAME;