Failed to pass an input variable to my query - php

I am creating a phonebook program, where I can register and create my own contact list. After login, the page will be directed to the tabmain page where I can view, add, edit or delete contact, in a multitab form.
Here's the problem: Whenever I click the save button (the button in function displayNew), it should be getting the first name and surname then, look for the database if there is a match. If not, then add it should them. Now, what happens is, after I click the save button, nothing is passed to the variable.
Here is my code:
<!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" lang="en">
<head>
<title>Tab-View Sample</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<meta name="description" content="" />
<meta name="keywords" content="" />
<link rel="stylesheet" type="text/css" href="css_collect/distribution/tab-view.css" />
</head>
<body>
<?php $id = isset($_GET['id']) ? $_GET['id'] : 1; ?>
<?php
include 'conSql.php';
session_start();
$mySession = $_SESSION['user'];
//displays firsname and lastname of the user
$getNames = getNames($mySession);
$gET = explode("^", $getNames);
echo "Hi, "."<b>". $gET[0] ." ". $gET[1] ."</b>"."!";
echo "</br>";
echo "</br>";
?>
<div class="TabView" id="TabView">
<!-- ***** Tabs ************************************************************ -->
<div class="Tabs" style="width: 452px;">
<a <?=($id == 1) ? 'class="Current"' : 'href="sample.php?id=1"';?>>Contact List</a>
<a <?=($id == 2) ? 'class="Current"' : 'href="sample.php?id=2"';?>>Add contact</a>
<a <?=($id == 3) ? 'class="Current"' : 'href="sample.php?id=3"';?>>Edit / Delete</a>
</div>
<!-- ***** Pages *********************************************************** -->
<div class="Pages" style="width: 450px; height: 300px;">
<div class="Page" style="display: <?=($id == 1) ? 'block' : 'none';?>"><div class="Pad"></div>
list of query
</div>
<div class="Page" style="display: <?=($id == 2) ? 'block' : 'none';?>"><div class="Pad"></div>
<Form Name ='' Method ='GET' action = 'tabmenu.php'>
<?php
displayNew();
if(isset($_GET['btnAdd'])){
$fname = $_GET['txtFname'];
$lname = $_GET['txtLname'];
// $s = "SELECT * ";
// $s .="FROM tbl_contactlist ";
// $s .="WHERE fname = '". $_GET['txtFname'] ."' and lname = '". $_GET['txtLname'] ."'";
echo $s;
}
?>
</div>
<div class="Page" style="display: <?=($id == 3) ? 'block' : 'none';?>"><div class="Pad"></div>
<?php
displayEdit();
?>
</div>
</div>
</div>
<script type="text/javascript" src="css_collect/distribution/tab-view.js"></script>
<script type="text/javascript">
tabview_initialize('TabView');
</script>
<?php
function getNames($mySession){
//get the real name of the user (firstname and lastname)
$s = "SELECT * ";
$s .="FROM tbl_users ";
$s .="WHERE username = '". $mySession ."' ";
$rc = mysql_query($s);
$row = mysql_fetch_assoc($rc) or die();
$details = $row["fname"]."^".$row["lname"];
return $details;
}
function displayNew(){
echo "<table border = '0'>";
echo "<tr>";
echo "<td colspan='2'><b>New Contact</b></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2'>Please fill up the fields.</td>";
echo "</tr>";
echo "<tr>";
echo "<td>First Name: </td>";
echo "<td><input type='text' name='txtFname' id='txtFname'></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Last Name: </td>";
echo "<td><input type='text' name='txtLname' id='txtLname'></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Nick Name: </td>";
echo "<td><input type='text' name='txtNicname' id='txtNicname'></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Contact Number: </td>";
echo "<td><input type='text' name='txtContactNum' id='txtContactNum'></td>";
echo "</tr>";
echo "<tr>";
echo "<td></td>";
echo "<td><input type='submit' name='btnAdd' id='btnAdd' value='SAVE'></td>";
echo "</tr>";
echo "</table>";
}
function displayEdit(){
echo "<table border = '0'>";
echo "<tr>";
echo "<td colspan='2'><b>Edit / Delete Page</b></td>";
echo "</tr>";
echo "<tr>";
echo "<td colspan='2'>You can edit or delete:</td>";
echo "</tr>";
echo "<tr>";
echo "<td>First Name: </td>";
echo "<td><input type='text' name='txtFname' id='txtFname'></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Last Name: </td>";
echo "<td><input type='text' name='txtLname' id='txtLname'></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Nick Name: </td>";
echo "<td><input type='text' name='txtNicname' id='txtNicname'></td>";
echo "</tr>";
echo "<tr>";
echo "<td>Contact Number: </td>";
echo "<td><input type='text' name='txtContactNum' id='txtContactNum'></td>";
echo "</tr>";
echo "<tr>";
echo "<td></td>";
echo "<td><input type='submit' name='btnEdit' id='btnEdit' value='EDIT'>";
echo "<input type='submit' name='btnDelete' id='btnDelete' value='DELETE'></td>";
echo "</tr>";
echo "<tr>";
echo "<td></td>";
echo "<td><input type='submit' name='btnCancel' id='btnCancel' value='Cancel'></td>";
echo "</tr>";
echo "</table>";
}
function testMe(){
echo "<script type='text/javascript'>\n";
echo "alert('T E S T');\n";
echo "</script>";
}
?>
</body>
</html>

It looks like you don't ever close the form tag in the HTML, that might cause the page to not do anything when you click submit

Related

Changing font colour according to the data output

I have a data table to show on the table one row is called "address". There are only two types of address can be output "dhaka" and "yaka" . I want to show the font colour as red when the output is "dhaka" and green when the output is "yaka".
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dashboard</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.js"></script>
<style type="text/css">
.wrapper{
width: 650px;
margin: 0 auto;
}
.page-header h2{
margin-top: 0;
}
table tr td:last-child a{
margin-right: 15px;
}
</style>
<script type="text/javascript">
$(document).ready(function(){
$('[data-toggle="tooltip"]').tooltip();
});
</script>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<?php
require_once "config.php";
$sql = "SELECT * FROM student_record";
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
echo "<table class='table table-bordered table-striped table-hover '>";
echo "<thead>";
echo "<tr>";
echo "<th>#</th>";
echo "<th>Name</th>";
echo "<th>Address</th>";
echo "<th>Marks</th>";
echo "<th>Action</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['address'] . "</td>";
echo "<td>" . $row['marks'] . "</td>";
echo "<td>";
echo "<a href='read.php?id=". $row['id'] ."' title='View
Record' data-toggle='tooltip'><span class='glyphicon glyphicon-eye-open'></span></a>";
echo "<a href='update.php?id=". $row['id'] ."' title='Update
Record' data-toggle='tooltip'><span class='glyphicon glyphicon-pencil'></span></a>";
echo "<a href='delete.php?id=". $row['id'] ."' title='Delete
Record' data-toggle='tooltip'><span class='glyphicon glyphicon-trash'></span></a>";
echo "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
mysqli_free_result($result);
} else{
echo "<p class='lead'><em>No records were found.</em></p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($conn);
}
mysqli_close($conn);
?>
<form method="post" action="search1.php?go" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form>
</div>
</div>
</div>
</div>
</body>
</html>
You need to actually use if elseif statements:-
<?php
if ($row['address'] == 'dhaka') {
$color = 'RED';
} elseif ($row['address'] == 'yaka') {
$color = 'GREEN';
}
echo "<td><span style=\"color: $color\">" . $row['address'] . "</span></td>";
Please try to include this code inside while loop
if($row['address'] =='dhaka'){$color='red';}
else{$color='green';}
And update the address line with
echo "<td style='color:'.$color.'>" . $row['address'] . "</td>";

I want to know how to get the selected data on another webpage using PHP

I have use loop to show limited data and each loop i have button to select it.
I have a syntax error in my view.php i dont know what to put in that query
How do i select a row in index.php to view more details in view.php. I tried giving it a name in the echo $rows; loop but i got syntax error too.
This is my code in index.php
<?php
$sqlQuery = mysql_query("SELECT fname FROM info");
while ($rows = mysql_fetch_array($sqlQuery)){
echo $rows['fname'];
echo "<input type='submit' value='Show more info' name='submit' class='btn btn-info'>";
}
?>
-----------------------end code for index------------------------------
this is my code in view
<?php
$sqlQuery = mysql_query("SELECT * FROM info WHERE fname=$_POST['fname']");
echo "<table border='1' width='50%'>";
echo "<tr>";
echo "<td>First Name</td>";
echo "<td>last Name</td>";
echo "<td>Age Name</td>";
echo "</tr>";
while ($rows = mysql_fetch_array($sqlQuery)){
echo "<tr>";
echo "<td>";
echo $rows['fname'];
echo "</td>";
echo "<td>";
echo $rows['lname'];
echo "</td>";
echo "<td>";
echo $rows['age'];
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
-------------------------------end code for view--------------------
<form method="POST" action="view.php">
<?php
$sqlQuery = mysql_query("SELECT fname FROM info");
while ($rows = mysql_fetch_array($sqlQuery)){
echo "<input type='text' value='".$rows['fname']."' name='fname'>";
echo "<input type='submit' value='Show more info' name='submit' class='btn btn-info'>";
}
?>
</form>
Try this in view.php
echo $_POST['fname'];
$query = "SELECT * FROM info WHERE fname LIKE ".$_POST['fname']."";
echo $query;
check what output are you getting

how to avoid duplicate values to print in the html table

Order.php:
<?php
require_once('conn.php');
session_start();
$itemId=$_SESSION['itemId'];
$tnumber=$_SESSION['tnumber'];
$custno=$_SESSION['custno'];
$sql="select itemId,subtitle,price,quantity from cart where tnumber='$tnumber'" ;
$sql2="select subtitle from cart where itemId='$itemId'";
$res2=mysqli_query($dbhandle,$sql2);
$row1= mysqli_num_rows($res2);
$res=mysqli_query($dbhandle,$sql);
$total=0;
?>
<html>
<head>
<title>Home</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" type='text/css' href='cartbox.css'/>
</head>
<body>
<h1></h1>
<script src="home.js"></script>
<div id="shopping-cart"> </div>
<?php
echo "<table id='t1' border='1'>
<th>Subtitle</th>
<th>Quantity</th>
<th>Price</th>
<th>Amount</th>
</tr>";
if (row1 > 0) {
while ($row=mysqli_fetch_array($res)) {
$amount=$row['price']*$row['quantity'];
echo "<form id='addform{$row['itemId']}' method='post' action='cartdelete.php'> ";
echo "<tr>";
echo "<td>" .$row['subtitle'] ."</td>";
echo "<td>" .$row['quantity'] ."</td>";
echo "<td>" .$row['price'] ."</td>";
echo "<td>" . $amount . "</td>";
echo "<td><input type='submit' value='x' name='submit'></td>";
echo "<td><input type='text' name='itemId' value= '{$row['itemId']}'></td>";
echo"</form>";
echo "</tr>";
$total = $total+ $amount;
}
}
echo "</table>";
?>
<?php echo $total ?>
<input type="button" name="continue" value="Continue Order" style="position: absolute;top:200px;" onclick="location.href='customerdicecream.php'">
</body>
</html>
I'm trying to display the data in an HTML table (with primary key). I need the subtitle to be entered into the HTML table, only once, but with the if condition I'm not able to achieve it. How can I do that?
You may use DISTINCT / GROUP BY in your query for prevention of the redundant value.
ex.
$sql="select itemId,subtitle,price,quantity from cart where tnumber='$tnumber'" ;
$sql2="select DISTINCT subtitle from cart where itemId='$itemId'";
Reference Site
Define a variable $prevSubTitle before the while ($row=mysqli_fetch_array($res)) { condition.
And when you read value for it from $row['subtitle'], check if it matches with previously read value. If 'false' display new value, else fill td with a space as <td> </td>
#prevSubTitle = '';
while ($row=mysqli_fetch_array($res)) {
$amount=$row['price']*$row['quantity'];
echo "<form id='addform{$row['itemId']}' method='post' action='cartdelete.php'> ";
echo "<tr>";
$subtitle = $row['subtitle'];
if( ! ( $subtitle == $prevSubTitle ) ) {
$prevSubTitle = $subtitle;
}
else {
$subtitle = ' ';
}
echo "<td>" . $subtitle ."</td>"; // empty when matched with previous row title
echo "<td>" .$row['quantity'] ."</td>";
echo "<td>" .$row['price'] ."</td>";

Object not Found 404 Error [duplicate]

This question already has answers here:
Object Not Found error for simple php script
(4 answers)
Closed 9 years ago.
This code returns me a 404 Object Not Found error that I could not solve by modifying the code. And I don't know whether it's a code problem or a localhost problem. I need this to be running for my Final Year Project. Please help. Thanks a lot guys.
<?php
session_start();
require "dbc.php";
echo "<center>";
echo "<img src='web_header.jpg'/>";
echo "</center>";
if($_SESSION['admin_username'])
{
echo "Hello : ".$_SESSION['admin_username'];
echo "<p><a href ='admin_panel.php'>Admin Main</a> || <a href='manage_patients.php'>Manage Patients</a> || <a href ='manage_doctor.php'>Manage Doctors</a> || <a href ='mail_form.php'>Send Email</a> || <a href='logout.php'>Logout</a> ";
echo "<title>Manage Patients</title>";
echo "<h2>Manage Patients</h2>";
$query = mysql_query("SELECT * FROM users");
$numrows = mysql_num_rows($query);
if ($numrows != 0)
{
echo "<table width='1285' height='102' border='1'>";
echo "<tr><th>ID</th><th>Name</th><th>IC Number</th><th>Address</th><th>Mobile Number</th><th>E-mail Address</th><th>Doctor ID</th></tr>";
while ($rows = mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td width='57' height='33'>";
echo "<center>";
echo $rows['id'];
echo "</center>";
echo "</td>";
echo "<td width='173'>";
echo $rows['name'];
echo "</td>";
echo "<td width='113'>";
echo "<center>";
echo $rows['icnum'];
echo "</center>";
echo "</td>";
echo "<td width='622'>";
echo $rows['address'];
echo "</td>";
echo "<td width='110'>";
echo "<center>";
echo $rows['mobile'];
echo "</center>";
echo "</td>";
echo "<td width='170'>";
echo "<center>";
echo $rows['email'];
echo "</center>";
echo "</td>";
echo "<td width='90'>";
echo "<center>";
echo $rows['docID'];
echo "</center>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
echo"<br>";
echo"</br>";
echo "<form action = 'manage_patients.php method=POST'>";
echo "<p>Assign Doctor ID : ";
echo "<input type='text' name='assign_id' value='Insert Doctor ID Here'>";
echo " for Patient ID : ";
echo "<input type='text' name='id' value='Insert Patient ID Here'></p>";
echo "<input type= 'submit' name='submit' value='Assign Now'>";
echo "</form>";
if (isset($_POST['submit']))
{
require "dbc.php";
$name = mysql_real_escape_string($_POST['assign_id']);
$id = mysql_real_escape_string($_POST['id']);
$query = mysql_query("UPDATE users SET docID='$assign_id' WHERE id='$id'");
echo "Doctor ID Successfully Assigned for Patient ID : $name ";
echo "<p>Refresh page to view changes.</p>";
}
}
}
else
{
header("location:index.html");
}
?>
change this
<form action = 'manage_patients.php method=POST'>
to
<form action = 'manage_patients.php' method='POST'>
If you are getting a 404 Error, it means that the page could not be found. So it is most likely not a problem with your code (that would give you a 500 Server Error). Instead you most likely have used the wrong URL.
<form action = 'manage_patients.php method=POST'>
I find this line of yours a bit suspiscious

How to display date in next page?

I really need some help about how to pass returndate value in next page(save.php). the value that i cant' pass was under this "(input type='hidden' name='retDate[$i]' value='$retDate')".
I'm using calendar datepicker at this website(http://www.triconsole.com/php/calendar_datepicker.php). Appreciate if someone can help me about this and do refer below for my coding.
under "result.php"
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"></br>
<h1>RESULT </h1>
<p><b>Escalation Date : </b>
<?php echo $_POST["date1"] ?> until <?php echo $_POST["date2"] ?>
</p>
<?php
......
//Select database
$selected = mssql_select_db($myDB, $link)
or die("Couldn't open database $myDB");
//declare the SQL statement that will query the database
$query = "SELECT.....";
//execute the SQL query and return records
if ($result = mssql_query($query, $link)){
echo "<form name='form1' method='post' action='save.php'>";
echo "<table border='1'>
<tr>
<th>batch_exception_id</th>
<th>batch_id</th>
<th>process_date_time</th>
<th>Return Date</th>
</tr>";
$i=0;
while ($row = mssql_fetch_assoc($result)) {
$rDate = $row['ReturnDate'];
$beID = $row['batch_exception_id'];
$proc_dt = $row['process_date_time'];
echo "<tr>";
echo "<td>" . $beID . "<input type='hidden' name='beID[$i]' value='$beID'/></td>";
echo "<td>" . $row['batch_id'] . "</td>";
echo "<td>" . $proc_dt . "<input type='hidden' name='procDT[$i]' value='$proc_dt'/></td>";
if($rDate == ""){
echo "<td>";
$f_name="retDate[".$i."]";
$myCalendar = new tc_calendar($f_name, true, false);
$myCalendar->setIcon("calendar/images/iconCalendar.gif");
$myCalendar->setDate(date('d'), date('m'), date('Y'));
$myCalendar->setPath("calendar/");
$myCalendar->setYearInterval(2000, 2020);
$myCalendar->dateAllow('2000-01-01', '2020-01-01');
$myCalendar->setDateFormat('j F Y');
$myCalendar->setAlignment('left', 'bottom');
//$myCalendar->setSpecificDate(array("", "0", "0"), 0, 'year');
//$myCalendar->setSpecificDate(array("0", "0"), 0, 'month');
//$myCalendar->setSpecificDate(array("0"), 0, '');
$myCalendar->writeScript();
echo "<input type='hidden' name='retDate[$i]' value='$retDate'/>";
//echo "<input type='hidden' name='retDate[$i]' value='".$myCalendar->getDate()."'/>";
$i++;
echo "</td>";
} else {
echo "<td>" . $rDate . "</td>";
}
echo "</tr>";
}
echo "</table><br/>";
echo "<input type='button' value='<<' onclick='history.back(-1)'/>";
echo "<input type='hidden' name='total_rec' value='$i'/>";
echo "<input type='submit' value='Save'/>";
echo "<input type='button' value='Print' onclick='window.print()'/>";
echo"</form>";
}
//close the connection
mssql_close($link);
?><br/>
</body>
under "save.php"
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0"></br>
<?php
//$ReturnDate = $_POST["rDate"];
$arrbeID = $_POST["beID"];
$tot_rec = $_POST["total_rec"];
$arrprocDT = $_POST["procDT"];
$arrretDate = $_POST["retDate"];
for ($i=0; $i<$tot_rec;$i++) {
echo "Batch Esc. ID: ".$arrbeID[$i]."
| Proc. DateTime: ".$arrprocDT[$i]."
| Ret. Date: ".$arrretDate[$i]."
<br>";
}
?><br/>
</body>
I think you need this line:
echo "<input type='hidden' name='retDate[$i]' value='$retDate'/>";
to be:
echo "<input type='hidden' name='retDate[$i]' value='$rDate'/>";
because $rDate is where you have actually stored the return date that you fetched from the database:
$rDate = $row['ReturnDate'];
solution:-
Put below sript under head in Result.php
<link rel="stylesheet" title="Style CSS" href="cwcalendar.css" type="text/css" media="all" />
<script type="text/javascript" src="calendar.js"></script>
Add below code under body in Result.php
$date="date[".$i."]";
echo "<input type='text' name='date[$i]' id='$date' value=' ' onclick=\"fPopCalendar('".$date."')\">";
script reference:
http://codetale.com/2009/06/21/javascript-calendar-widget-108/

Categories