PHP code not working how I'd like - php

I am creating a form where user can enter contact number and data is fetched from the database. now a same phone number can belongs to multiple people, this code is just fetching detail of one person at a time. what changes should i do so that it can fetch data for all the people.
<?php
if (preg_match("/^[0-9]+/", $_POST['name'])) {
$name = $_POST['name'];
//connect to the database
$db = mysql_connect("localhost", "root", "") or die ('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb = mysql_select_db("trsv_data");
//-query the database table to find person_id
$sql_search = "SELECT person_id FROM contactnumbers WHERE contact_number = " . $name;
// $sql_search="SELECT person_id FROM Email WHERE Email LIKE '%" . $name . "%' ";
//-run the query against the mysql query function
$result = mysql_query($sql_search);
//-create while loop and loop through result set
while ($row = mysql_fetch_array($result)) {
$person_id = $row['person_id'];
//-display the result of the array
echo "<ul>\n";
//echo "Person Id: " . $person_id . "\n";
echo "Person Id: " . $person_id . "\n";
//http://localhost:8080/Trillium_Emarketing/Trillium/output/person_search.php
echo "</ul>";
//-query the database table to find Person_FirstName
$sql_Person = "SELECT FirstName, company_id FROM person WHERE person_id =" . $person_id;
//-run the query against the mysql query function
$result = mysql_query($sql_Person);
//-create while loop and loop through result set
while ($row = mysql_fetch_array($result)) {
$FirstName = $row['FirstName'];
$company_id = $row['company_id'];
//-display the result of the array
echo "<ul>\n";
echo "<b>First Name:</b> " . $FirstName;
}
//-query the database table to find Person_MiddleName
$sql_Person = "SELECT MiddleName FROM person WHERE person_id =" . $person_id;
//-run the query against the mysql query function
$result = mysql_query($sql_Person);
//-create while loop and loop through result set
while ($row = mysql_fetch_array($result)) {
$MiddleName = $row['MiddleName'];
//-display the result of the array
echo "&nbsp";
echo "<b>Middle Name:</b> " . $MiddleName;
}
//-query the database table to find Person_LastName
$sql_Person = "SELECT LastName FROM person WHERE person_id =" . $person_id;
//-run the query against the mysql query function
$result = mysql_query($sql_Person);
//-create while loop and loop through result set
while ($row = mysql_fetch_array($result)) {
$LastName = $row['LastName'];
//-display the result of the array
echo "&nbsp";
echo "<b>Last Name:</b> " . $LastName;
echo "</ul>";
}
echo "<p>";
//-query the database table to find Emails
$sql_Email = "SELECT Email FROM email WHERE person_id =" . $person_id;
//-run the query against the mysql query function
$result = mysql_query($sql_Email);
//-create while loop and loop through result set
while ($row = mysql_fetch_array($result)) {
$Email = $row['Email'];
//-display the result of the array
echo "<ul>";
echo "<b>Email: </b> " . $Email;
}
//-query the database table to find Email_type
$sql_Email = " SELECT Email_types FROM email_type,email WHERE email_type.email_type_id = email.email_type_id AND person_id =" . $person_id;
//-run the query against the mysql query function
$result = mysql_query($sql_Email);
//-create while loop and loop through result set
while ($row = mysql_fetch_array($result)) {
$Email_type = $row['Email_types'];
//-display the result of the array
echo "&nbsp";
echo "<b>Email Type: </b> " . $Email_type;
}
//-query the database table to find Email_status
$sql_Email = "SELECT email_status FROM email_status,email WHERE email_status.email_status_id = email.email_status_id AND person_id =" . $person_id;
//-run the query against the mysql query function
$result = mysql_query($sql_Email);
//-create while loop and loop through result set
while ($row = mysql_fetch_array($result)) {
$Email_status = $row['email_status'];
//-display the result of the array
echo "&nbsp";
echo "<b>Email Status:</b> " . $Email_status;
echo "</ul>";
}
echo "<p>";
//-query the database table to find contact Number
$sql_contactnumber = "SELECT contact_number FROM contactnumbers WHERE person_id =" . $person_id;
//-run the query against the mysql query function
$result = mysql_query($sql_contactnumber);
//-create while loop and loop through result set
while ($row = mysql_fetch_array($result)) {
$contact = $row['contact_number'];
//-display the result of the array
echo "<ul>";
echo "<b>Contact: </b>" . $contact;
}
//-query the database table to find contact Number Type
$sql_contactnumber = "SELECT contact_number_types FROM contact_number_types,contactnumbers WHERE contact_number_types.contact_num_types_id = contactnumbers.contact_num_type_id AND contactnumbers.person_id = " . $person_id;
//-run the query against the mysql query function
$result = mysql_query($sql_contactnumber);
//-create while loop and loop through result set
while ($row = mysql_fetch_array($result)) {
$contact_type = $row['contact_number_types'];
//-display the result of the array
echo "&nbsp";
echo "<b>Contact type: </b> " . $contact_type . "\n";
echo "</ul> ";
}
echo "<p>";
//-query the database table to find Company
$sql_company = "SELECT company_name FROM company WHERE company_id =" . $company_id;
//-run the query against the mysql query function
$result = mysql_query($sql_company);
//-create while loop and loop through result set
while ($row = mysql_fetch_array($result)) {
$company_name = $row['company_name'];
//-display the result of the array
echo "<ul>";
echo "<b>Company Name: </b>" . $company_name;
}
//-query the database table to find Company Type
$sql_company = "SELECT company_type FROM company_type,company WHERE company_type.company_type_id = company.company_type_id AND company_id =" . $company_id;
//-run the query against the mysql query function
$result = mysql_query($sql_company);
//-create while loop and loop through result set
while ($row = mysql_fetch_array($result)) {
$company_type = $row['company_type'];
//-display the result of the array
echo "&nbsp";
echo "<b>Company Type: </b>" . $company_type;
echo "</ul>";
}
//-query the database table to find Product blast
$sql_product_blast = "SELECT product_name FROM product,product_blast WHERE product.product_id = product_blast.product_id AND product_blast.person_id = " . $person_id;
//-run the query against the mysql query function
$result = mysql_query($sql_product_blast);
//-create while loop and loop through result set
while ($row = mysql_fetch_array($result)) {
$product_name = $row['product_name'];
//-display the result of the array
echo "<ul>\n";
echo "<b>Product Blasted: </b>" . $product_name . "\n";
echo "</ul>";
}
}
}
}
?>

The problem is that, you are always use $row = mysql_fetch_array($result) so you are always rewrite the $result and $row. So when you last call this, that will give you tha last row in your last loop, and in your main loop will terminated.
try this:
$sql_search = "SELECT person_id FROM contactnumbers WHERE contact_number = " . mysqli_real_escape_string($name);
$id_result = mysqli_query($link, $sql_search);
while ($id_row = mysqli_fetch_array($id_result)) {
//....
}
1) Avoid sql injections
2) Do not use mysql functions. Use mysqli or PDO functions instead of mysql_* functions.

Related

php and mysql fill empty values as empty <td>'s

I'm trying to compare products and I'm already finished. I just have a problem that my product features are not under the right product names because I need to fill the gaps between with empty <td></td>.
Here is my code from the function that fills the values.
function datatable($id)
{
$conn = connection();
$productPost = $_POST["product"];
$sqlSpecTitle = "Select title as title from product where uid = '$id'";
$resultTitle = mysqli_query($conn, sqlSpecTitle) or die("database error:" . mysqli_error($conn));
foreach ($productPost as $product)
{
$sqlSpecValue = "Select productname, title, value from text join product on uid = uid join feature on uid = uid where productname = '$product" and uid = '$id';
$resultValue = mysqli_query($conn, $sqlSpecValue or die("database error:" . mysqli_error($conn));
if(mysqli_num_row($resultValue) > 0
{
while($row = mysqli_fetch_assoc($resultTitle))
{
echo "<td>" . $row['title'] . "<td>";
}
while ($row = mysqli_fetch_assoc($resultValue))
{
if($row['value'] == null)
{
echo "<td>" . "empty" . "<td>";
}
else
{
echo "<td> . $row['value'] . "</td>";
}
}
}
}
}
The productnames are getting filled in another function that is as much the same.
function headerTable()
{
$conn = connection();
$productPost = $_POST["product"];
foreach ($productPost as $product) {
$sqlSpecValue = "SELECT productname, title, value from text
join product on uid = uid
join feature on uid = uid
where productname = '$product';
$resultValue = mysqli_query($conn, $sqlSpecValue) or die("database error:" . mysqli_error($conn));
$row = mysqli_fetch_assoc($resultValue);
echo "<td id='product'>" . $row['productname'] . "</td>";
}
}
You make a "join" over between your tables, so you only get data if you have something in "text" table.
Just switch to "right join" and it should work.

Select Data by Id PHP

I have trouble to select a set of specific data using ID from the database. For example, employee one has a unique id of e000000001, when I click the view button in the index will lead to employee detail page which shows the detail of that particular employee instead of all the employees' detail. Thank you.
//from index.php page
<?php
require_once 'db/dbEmpList.php';
$sqlStr = "SELECT * FROM employees;";
$result = $connection->query($sqlStr);
if ($result->num_rows > 0) {
echo "<table class='table table-sm'><thread><tr><th>Full Name</th><th>Employee ID</th><th>Position</th><th>View Employee's Details</th></tr>";
while ($row = $result->fetch_assoc()) {
echo "<tr><td>"
. $row["empName"]. "</td><td>"
. $row["empID"]. "</td><td>"
. $row["position"]. "</td>"
. "<td> <a href='employeedetail.php?id={$row["empID"]}'>View</a>"
. "</td></tr>";
}
}
// from employee page
require_once 'db/dbEmpDetail.php';
$sql = "SELECT * FROM employees where empID = '{$row["empID"]}' ";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>' .'<td>' .$row["empName"].'</td>'.'<td>'. $row["position"].'</td>' .'<td>'.$row["empNRIC"].'</td>' .'<td>'.$row["empID"].'</td>' .'<td>'.$row["empEmail"].'</td>' .'<td>'.$row["empPwd"].'</td>' . "</tr>";
}
} else {
echo "0 results";
}
mysqli_close($connection);
?>
// FROM EMPLOYEE PAGE
The way you retrieve URL query string is wrong. You should be using $_GET to get the query string from URL. In your case it should be $_GET['id']. See the code below:
require_once 'db/dbEmpDetail.php';
$employeeid = trim(mysqli_real_escape_string($_GET['id']));
$sql = "SELECT * FROM employees where empID = '".$employeeid."' ";
$result = mysqli_query($connection, $sql);
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>' .'<td>' .$row["empName"].'</td>'.'<td>'. $row["position"].'</td>' .'<td>'.$row["empNRIC"].'</td>' .'<td>'.$row["empID"].'</td>' .'<td>'.$row["empEmail"].'</td>' .'<td>'.$row["empPwd"].'</td>' . "</tr>";
}
}
else {
echo "0 results";
}
mysqli_close($connection);
?>

PHP array implode keys and values to function

I'm not too familiar with PHP arrays, I have the following code that generates query to output the results needed.
$allstore = $_POST['store'];
function createSelect($allstore)
{
if (empty($allstore))
return "";
$querySelect = "";
$queryJoin = "";
$baseTable = "";
foreach ($allstore as $store => $value) {
if (!$querySelect) {
$baseTable = $store;
$querySelect = "SELECT " . $store . ".item_no, " . $store . ".actual_price, " . $store . ".selling_price, " . $store . ".qty as " . $store;
} else {
$querySelect .= ", " . $store . ".qty as " . $store;
$queryJoin .= "
INNER JOIN " . $store . " ON " . $baseTable . ".item_no = " . $store . ".item_no";
}
}
$querySelect .= " FROM " . $baseTable;
$query = $querySelect . $queryJoin;
return $query;
}
//Stores to be shown
$allstore = ['s_M9' =>0 , 's_M10' =>1];
$query = (createSelect($allstore));
$result = mysql_query($query);
//rest of code...
As you can see above, at the very top there is $allstore = $_POST['store']; Which collects values based from previous form POST method that has checkbox with the name=store[] .
Now According to the function shown, if I create my own keys and values like this
$allstore = ['s_M9' =>0 , 's_M10' =>1];
the output shows exactly what i'm looking for. But the problem goes on how to let $allstore implode those stores s_M9, s_M10 based on what the user has selected on the previous page ( checkbox )? I mean, the user can select either one of the stores or Both stores . How can I implode the checked results between those brackets without inserting them manually?
Thank You
Edit :
<?php
echo "<form action='somewhere.php' method='POST'>";
$query = "SELECT * from stores_list ORDER BY short Asc";
$result = mysql_query($query);
if(mysql_num_rows($result)>0){
$num = mysql_num_rows($result);
for($i=0;$i<$num;$i++){
$row = mysql_fetch_assoc($result);
echo "<input type=checkbox name=store[] value={$row['short']} style='width:20px; height:20px;'>{$row['short']}";
}
}
else{
//No Stores Available
echo "No Stores Found !";
}
echo "</td><input type='submit' value='Search'/></form>";
$allstore = [];
if (!empty($_POST['store'])) {
foreach ($_POST['store'] as $value) {
$allstore[$value] = 1; // or 0, it doesn't matter because your function adds all the keys
}
}
$query = (createSelect($allstore));
$result = mysql_query($query);
And of course you have to take care of your createSelect function to avoid SQL Injections, please read here

value of dropdown list from mysql

I'm trying to compose an estimate formula, and I stucked with value of dropdown list populated by MySQL.
The idea of this formula is when a user select a service from dropdown list and put the quantity in textfield the program will compute the price for the service.
The value of the prize is selected from MySQL table.
$query="SELECT $con_tent FROM services WHERE $id;
$con_tent= 'price'. '*'. $qunatity
But I don't know how to get the value from dropdwon list.
Probably with Ajax but still don't know how.
I solved this by modyfing code from http://www.9lessons.info/2010/08/dynamic-dependent-select-box-using.html
<?php
require_once 'login.php';
$db_server = mysql_connect($db_hostname, $db_user, $db_password);
mysql_select_db($db_database) or die("unable to select database:" . mysql_error());
echo "<form action=licz.php method='post'>";
echo " <label for=\"select\"><select name=\"\" value=\"Select\" size=\"1\">";
$query = "SELECT * FROM uslugi ORDER BY id ASC";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
global $ff;
$ajdi = $row['id'];
$nazwa = $row['nazwa'];
$options.= "<option value=\"$ajdi\" name=\"oko\">" . $nazwa . $ajdi;
}
echo "<option>";
echo $options;
echo "</option></select>";
echo " <input type=\"submit\" name=\"Submit\" value=\"Submit\">";
echo "</form>";
function wybor() {
global $id;
global $con_tent;
$var = 'price' . '*';
$quantity = 3;
//quantity will by from textfield but now it constant
$id_value = 1;
// here i need to make it dynamic
$id = "id={$id_value}";
$con_tent = $var . $quantity;
}
echo wybor();
$query = "SELECT $con_tent FROM services WHERE $id";
//query
if (!$query) Die("Unable to query: " . mysql_error());
$result = mysql_query($query);
if (!$result) Die("Unable to query: " . mysql_error());
$rows = mysql_num_rows($result);
for ($a = 0; $a < $rows; ++$a) {
$row = mysql_fetch_row($result);
echo $row[0] . " ";
echo $row[1] . " ";
echo $row[2] . " ";
echo $row[3] . "$br";
}
?>
You should apply ajax call to get value for database when there is a change in select box through calling a function on onchange event of javascript.
Read More for jquery AJAX
http://www.sitepoint.com/ajax-jquery/
http://www.tutorialspoint.com/jquery/jquery-ajax.htm

PHP - Not echoing data from a MySQL database, but no errors?

So I have this PHP code:
Note: I do use mysqli_connect() further up.
$result = mysqli_query($con,"SELECT * FROM `smf_messages` WHERE `id_board` = 18");
if(!$result) {
echo "<center><p>Couldn't fetch news posts. Error code 2.</p></center>";
mysqli_close($con);
} else {
$posts = array();
$topicbdy = array();
while($row = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
$posts[$row['id_topic']] = $row['id_topic'];
$topicbdy[$row['id_msg']] = $row['id_msg'];
}
$display = max($posts);
$display2 = min($topicbdy);
$qry = "SELECT * FROM `smf_messages` WHERE `id_board` = 18 AND `id_topic` = " . $display . " AND `id_msg` = " . $display2;
$result2 = mysqli_query($con,$qry);
//echo $qry;
if(!$result2) {
echo "<center><p>Couldn't fetch news posts. Error code 3.</p></center>";
} else {
while($show = mysqli_fetch_array($result,MYSQLI_ASSOC))
{
echo "<center><h1>" . $show['subject'] . "</h1></center><br /><br />";
echo "<center>" . $show['body'] . "</center><br />";
}
}
mysqli_free_result($result);
mysqli_free_result($result2);
mysqli_close($con);
It's supposed to get the latest topic out of the database for my SMF-based forum from the news board, by getting the highest topic id, but the lowest post id. It seems to be doing the query just fine, as I don't get any errors, but it doesn't show the subject or body. What should I do?
Your $result variable is wrong for second query fetch. For your second query
while($show = mysqli_fetch_array($result,MYSQLI_ASSOC))
Should be
while($show = mysqli_fetch_array($result2,MYSQLI_ASSOC))
^

Categories