php: session not working while running live on server - php

I am working on a project of PHP. I have a strange error. My PHP project is working well on local server (WAMP server). But after hosting it on live server, it is not working.
Problem: Session variable created on Login.php page is not passing value on Report.php page. The code is as below:
Login.php
<?php
$con=mysql_connect("mysql51****************","username","password")or die(mysql_error());
$select_db=mysql_select_db("database_name",$con);
$error="";
if(isset($_POST['submit']))
{
$userid=mysql_real_escape_string($_POST['username']);
$password=mysql_real_escape_string($_POST['password']);
$sql="SELECT * FROM user_details WHERE user_name='{$userid}' AND password='{$password}' ";
$result = mysql_query($sql);
if(mysql_num_rows($result) <= 0)
{
$error="Invalid UserId or Password.";
}
else
{
//session_set_cookie_params(60*60*60, '/', '.abcxyz.com');
ini_set('session.cookie_domain','.abcxyz.com');
session_start();
//$userid=mysql_real_escape_string($_POST['abc']);
//echo($userid);
$_SESSION['user']=$userid;
$_SESSION['userid']="true";
//echo $_SESSION['userid'];
$error="Successfully Login";
//header("Location: admin.php?page=report");
/*echo '<script type="text/javascript">alert("header is not working.' . $_SESSION['user'] . '");</script>';
*/ ?>
<script type="text/javascript">
/*alert("Please select a Source And Destination Country"); */
window.location.href='admin.php?page=report';
</script>
<?php
}
}
?>
<br />
<h1 style="text-align:center">Login</h1>
<form class="login" action="login.php" method="post" name="form1" id="form1">
<p>Username:</p>
<input class="login-input" type="text" name="username" value=""/>
<p> Password : </p>
<input class="login-input" type="password" name="password" value=""/>
<p>
<p style="color:#F00; font-size:12px; font-weight:100"><?php echo htmlentities($error); ?> </p>
<input class="login-submit" type="submit" name="submit" value="Login"/>
</form>
Report.php
<?php
include("connections/Connections.php");
/*$con=mysql_connect("localhost","admin","")or die(mysql_error());
$select_db=mysql_select_db("test",$con);*/
/*session_start();
if(session_is_registered('test'))
{
echo"registered";
}
else
{
header("Location: login.php");
}*/
//$user=$_SESSION['userid'];
$user=$_SESSION['userid'];
echo '<script type="text/javascript">alert("header is not working.' .$user . '");</script>';
if($user=="true")
{
//echo " Logining Successfully.";
}
else
{
?>
<script type="text/javascript">
//alert("Please select a Source And Destination Country");
window.location.href='login.php';
</script>
<?php
//header("Location: login.php");
}
?>
<?php
/*session_start();
$user=$_SESSION['userid'];
echo($user);
if($user=="Admin")
{
echo " Logining Successfully.";*/
$sql="SELECT * FROM register ";
if(isset($_POST['btnfilter']))
{
$search_term=mysql_real_escape_string($_POST['search_text']);
$answer = $_POST['filter'];
if ($answer == "ID") {
$sql .="WHERE id= '{$search_term}' ";
}
elseif ($answer == "Name") {
$sql .="WHERE fullname Like '%{$search_term}%'";
}
elseif ($answer == "DOB") {
$sql .="WHERE dob Like '%{$search_term}%' ";
}
elseif ($answer == "Occupation") {
$sql .="WHERE occupation Like '%{$search_term}%' ";
}
else
{
echo("Pealse Enter a valid value");
}
}
elseif(isset($_POST['btnrmfilter']))
{
$sql="SELECT * FROM register";
}
$query=mysql_query($sql) or die(mysql_errno());
//}
/*else
{
header('Location: /login.php');
}*/
?>
<style type="text/css">
table
{
font-size:12px;border-bottom:1px solid #ccc;
border-left:1px solid #ccc}
td
{
padding:5px 3px;
border-top:1px solid #ccc;
border-right:1px solid #ccc}
</style>
<div class="content">
<h1>Data Reading From database.</h1>
<form id="search_form" method="post" action="">
<div class="radio">
<input type="radio" name="filter" value="ID" />ID
<input type="radio" name="filter" value="Name" />Name
<input type="radio" name="filter" value="DOB"/>DOB
<input type="radio" name="filter" value="Occupation"/>Occupation<br />
</div>
<div class="input" >
<input type="text" name="search_text" value=""/>
<input type="submit" value="FilterData" name="btnfilter" />
<input type="submit" value="RemoveFilter" name="btnrmfilter" />
</div>
</form>
<table border="0" cellpadding="0" cellspacing="0">
<colgroup>
<col width="2%" style="color:#f60" valign="middle" align="center" >
<col width="12%" >
<col width="8%" >
<col width="5%" align="center" >
<col width="10%" >
</colgroup>
<tr style="background:#eee; height:30px;">
<td>ID</td>
<td>Name</td>
<td>DOB</td>
<td>Nationality</td>
<td>Mobile No</td>
<td>Phone No</td>
<td>Email</td>
<td>Education</td>
<td>Occupation</td>
<td>Comment</td>
<td >Noofexp</td>
</tr>
<?php while($row = mysql_fetch_array($query)) { ?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['fullname']; ?></td>
<td><?php echo $row['dob']; ?></td>
<td><?php echo $row['nationality']; ?></td>
<td><?php echo $row['mobno']; ?></td>
<td><?php echo $row['phno']; ?></td>
<td><?php echo $row['email']; ?></td>
<td><?php echo $row['education']; ?></td>
<td><?php echo $row['occupation']; ?></td>
<td><?php echo $row['comment']; ?></td>
<td><?php echo $row['noofexp']; ?></td>
</tr>
<?php } ?>
</table>
<h2> </h2>
<h3>Thanks for view.</h3>
<!-- end .content --></div>
<div class="footer">
<p>Footer</p>
<!-- end .footer --></div>
<!-- end .container --></div>
</body>
</html>
Edit:
As the answers suggested, i edited as below:
Login.php:
<?php
session_start();
$con=mysql_connect("mysql51*************","username","password")or die(mysql_error());...
...
Report.php:
<?php
session_start();
include("connections/Connections.php");...
...
But still i am not getting the session variable value in Report.php.

session_start(); should be placed on top of the both pages.

have you checked your phpinfo() ?
make shure you have
Session Support - enabled

You must add session_start(); in your report.php page

If it runs on your WAMP successfully, It is very possible that the problem is not from your code but from your hosting company.
Something like this happened to me before.
Just call you hosting company and tell them to enable session on their server for you and they will it for you.
and always start session first before any script just as you did above.
session_start();
Enjoy

Go try adding this on your code:
if(!$con)
{
die('Could not connect:'. mysql_error());
}
and see what the error is.

very easy solve to this problem copy this code
error_reporting(0);
session_start();
Note: give space from left side of session_start(); if you want understand about this watch this video

Related

Using isset ($_POST[array[0]) not working

I need a little help with my code. If I substitute the second isset statement with the input name "submit2" everything works fine. However, if I replace the name with a string variable from an array, it doesn't work. I reviewed the ISSET manual and saw that I could use an array. There has to be something I did not take into consideration with my statement.
// First isset
if (isset($_POST['submit1']))
{
$firstname = trim($_POST['firstname']);
$lastname = trim($_POST['lastname']);
$sqlPlayersearch = "SELECT id, first_name, last_name, user_city, user_state, user_zip FROM wp_players WHERE first_name = '$firstname' AND last_name = '$lastname'";
$resultSearch = mysqli_query($link, $sqlPlayersearch);
}
$sqlPlyrSrch2 = "SELECT id FROM wp_players WHERE first_name = '$firstname' AND last_name = '$lastname'";
$PlyrSrch2 = mysqli_query($link, $sqlPlyrSrch2);
$plyr = array();
while ($rowplyr1 = mysqli_fetch_array($PlyrSrch2)) {
$plyr[] = $rowplyr1[0];
}
echo $plyr[0]; // "3"
var_dump($plyr[0]); // string(1) "3"
// Second isset
if (isset($_POST['$plyr[0]'])){
$plyrfound = "Yes, It is Working";
}
?>
<html>
<style type="text/css">
.tgd1 {border-collapse:collapse;border-spacing:0;border-color:#aabcfe;}
.tgd1 td{font-family:Arial, sans-serif;font-size:14px;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aabcfe;color:#669;background-color:#e8edff;}
.tgd1 th{font-family:Arial, sans-serif;font-size:14px;font-weight:normal;padding:10px 5px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:#aabcfe;color:#039;background-color:#b9c9fe;}
.tgd1 .tg-hmp3{background-color:#D2E4FC;text-align:left;vertical-align:top}
.tgd1 .tg-baqh{text-align:center;vertical-align:top;font-weight: 900}
.tgd1 .tg-mb3i{background-color:#D2E4FC;text-align:right;vertical-align:top}
.tgd1 .tg-lqy6{text-align:right;vertical-align:top}
.tgd1 .tg-0lax{text-align:left;vertical-align:top}
form {
display: block;
}
form {
margin-bottom: 20px;
}
</style>
<body>
<form method='POST'>
<label for="fname">First Name</label>
<input style="width: 200px;" type="text" id="fname" name="firstname" pattern="[A-Za-z]*">
<label for="lname">Last Name</label>
<input style="width: 200px;" type="text" id="lname" name="lastname" pattern="[A-Za-z]*">
<input type="submit" name="submit1" value="Find Player">
<table class="tgd1">
<tr>
<th class="tg-baqh" colspan="4">Players</th>
</tr>
<tr>
<td class="tg-baqh" style="text-align:center" width="5%">Player</td>
<td class="tg-baqh" style="text-align:center" width="8%">City</td>
<td class="tg-baqh" style="text-align:center" width="10%">State</td>
<td class="tg-baqh" style="text-align:center" width="10%">Select Player</td>
</tr>
<?php
while($row2 = mysqli_fetch_array($resultSearch))
{ $plyr_id = $row2[0];
var_dump($plyr_id);?>
<tr>
<td class="tg-0lax" style="text-align:center"><?php echo $row2[1]; ?> <?php echo $row2[2]; ?></td>
<td class="tg-lqy6" style="text-align:center"><?php echo $row2[3]; ?></td>
<td class="tg-lqy6" style="text-align:center"><?php echo $row2[4]; ?></td>
<td style="text-align:center"><input type="submit" value="Select" name="<?php echo $plyr_id;?>"></td>
</tr>
</form>
<?php
}
?>
</table>
<h1>Eric <?php echo $plyrfound; ?></h1>
</body>
</html>
When I inspect the code in a browser this is what I see:
I have also placed some echo statements and var_dumps to see where I might be making a mistake:
I am expecting to see "Eric Yes, It is working". However, it does not seem to be finding the second isset as a true statement when I hit the "select" button.
Thank you in advance for any assistance.

Access the content of page if session is activated else activate session first

please help me solve this problem. I want to access content of a page only when Session is activated, else redirect user to activate session first. But when I redirect user to session page, it is stuck and cannot go back to content page. I am new here so please help me out from this problem.
<?php
session_start();
if(!isset($_SESSION['username'])){
echo "cookie is not activated" ;
header('Location: http://localhost/CC/Loginsession.php');
die;
}
else {
?>
<!doctype html>
<html>
<head>
<title>Update in PHP</title>
</head>
<body>
<?php
$servername="localhost";
$username="root";
$password="";
$conn=mysql_connect($servername,$username,$password);
if(!$conn ) {
die('Could not connect: ' . mysql_error());
}
$sq1 = 'select * from biodata';
mysql_select_db('firstdb');
$display=mysql_query($sq1,$conn);
if(!$display ) {
die('Could not get data: ' . mysql_error());
exit;
}
if (mysql_num_rows($display) == 0) {
echo "No rows found, nothing to print so am exiting";
exit;
}
?>
<table border="2" style= "background-color: #84ed86; color: #761a9b; margin: 0 auto;" >
<thead>
<tr>
<th>ID</th>
<th>Fname</th>
<th>Lname</th>
<th>Email</th>
<th>Phone</th>
<th>Message</th>
<th>Update</th>
</tr>
</thead>
<tbody>
<?php
while( $row = mysql_fetch_assoc( $display ) ){
echo
"<form method= 'post' />
<tr>
<td ><input name='UID' value='{$row['ID']}' readonly/></td>
<td ><input name='upfname' value='{$row['fname']}' /></td>
<td ><input name='uplname' value='{$row['lname']}' /></td>
<td ><input name='upemail' value='{$row['email']}' /></td>
<td ><input name='upphone' value='{$row['phone']}' /></td>
<td ><input name='upmessage' value='{$row['message']}' /></td>
<td><input type='Submit' name='update' value='Update' id='".$row["ID"]."' </td>
</tr>
</form>";
}
?>
</tbody>
</table>
<?php
if(isset($_REQUEST['update']))
{
$id = $_REQUEST['UID'];
$upfn = $_REQUEST['upfname'];
$upln = $_REQUEST['uplname'];
$upem = $_REQUEST['upemail'];
$upph = $_REQUEST['upphone'];
$upms = $_REQUEST['upmessage'];
$up="UPDATE biodata
SET
fname='$upfn',
lname='$upln',
email='$upem',
phone='$upph',
message='$upms'
WHERE ID = $id";
$updbb=mysql_query($up,$conn);
if($updbb){
header('Location: http://localhost/Prac/updateinsamepage.php');
}
}
}
?>
</body>
</html>>
and My session Login form code is here
<?php
session_start();
if(isset($_SESSION['username'])){
echo "Already registered as $_SESSION[username]" ;
}
else if($_SERVER['REQUEST_METHOD'] == 'POST'){
$uname=htmlentities($_POST['username']);
$pass=htmlentities($_POST['password']);
if(!empty($uname) && !empty($pass)) {
$_SESSION['username']=$uname;
echo "Thanks<br />" . "UserName: $uname " . "Password: $pass";
}
else{
echo "Please fill out the both fields";
}
}
else {
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Loginsession</title>
</head>
<body>
<form method="post">
Username:<input type="text" id="username" name="username" /> <br /><br />
Password:<input type="password" id="password" name="password"/><br /><br />
<input type="hidden" name="hiddenvalue" value="<?php http://localhost/CC/Loginsession.php?username=overwritten ?>"/>
<input type="Submit" value="Login" name="Submit" id="submit" />
</form>
<?php }?>
<?php
session_unset();
session_destroy();
?>
</body>
</html>
You can try this script in place of header
echo '<script>window.location="localhost/CC/Loginsession.php"</script>';

How to fill texboxes from database when an onClick event happens

I want my php file to fill textboxes with information from the database according to the selected value of a select box tag. And every time the selection changes the textboxes should be re-filled accordingly.
The problem is that when I call a javascript method which contains php code the page is reloaded and some initial values collected by $_POST[] when the page was loaded for the first time are lost.
I want to find a way such that those initial values collected by $_post[] are conserved even after the page re-executes the php. How can I solve this problem.
<script type="text/javascript">
function displaymessagespatient()
{
<?php
function docmsg()
{
$title = $_POST["title"];
$conn=mysqli_connect("localhost","root","","askthedoctor");
$sql1="select patient_text from messages where title='".$title."';";
$sql2="select doctor_text from messages where title='".$title."';";
$result1=mysqli_query($conn,$sql1);
$result2=mysqli_query($conn,$sql2);
$row1=mysqli_fetch_array($result1);
$row2=mysqli_fetch_array($result2);
return $row1[0];
}
function patmsg()
{
$title = $_POST["title"];
$conn=mysqli_connect("localhost","root","","askthedoctor");
$sql1="select patient_text from messages where title='".$title."';";
$sql2="select doctor_text from messages where title='".$title."';";
$result1=mysqli_query($conn,$sql1);
$result2=mysqli_query($conn,$sql2);
$row1=mysqli_fetch_array($result1);
$row2=mysqli_fetch_array($result2);
return $row2[0];
}
?>
document.getElementById("answer").innerHTML=docmsg();
document.getElementById("question").innerHTML=patmsg();
}
</script>
</head>
<body>
<?php
if( isset($_POST['username']))
{
$username=$_POST["username"];
$password=$_POST["password"];
$password=md5($password);
$conn=mysqli_connect("localhost","root","","askthedoctor");
$sql="select password from login where username='".$username."';";
$result=mysqli_query($conn,$sql);
$row=mysqli_fetch_array($result);
if($row[0]!=$password)
{
echo "The username or password that you entered are incorrect!";
echo "<br/>";
echo "Go back to login";
die();
}
$sql1="select userprivileges from login where username='".$username."';";
$sql2="select image_path from registration1 where id=(select id from login where username='".$username."');";
$result1=mysqli_query($conn,$sql1);
$result2=mysqli_query($conn,$sql2);
$row1=mysqli_fetch_array($result1);
$row2=mysqli_fetch_array($result2);
$imagepath=$row2[0];
}
if($row1[0]==2)
{
?>
<form id="patient" method="POST" enctype="multipart/form-data" action="login.php" >
<div id="header" class="class_header">
Sign Out
</div>
<div id="body" >
<br/>
<table class="table">
<tr>
<td><font color="white" ><h1 color="white" style="font-size:200%;" align="center">Welcome <?php echo $username;?></h1></font></td>
<td> <div id="box"><image height="65px" width="65px" src="<?php echo $imagepath; ?>"></div></td>
<tr>
</table>
</div>
<div id="separator" ></div>
<div id="separator" ></div>
<div id="bodyy" style="height: 60%; width: 60%;" class="div">
<br/><br/><br/>
<fieldset id="registration">
<table class="table">
<tr>
<th>Messages</th>
<?php
$sql="select title from messages where paitient_id=(select id from login where username='".$username."');";
$result=mysqli_query($conn,$sql);
while($row=mysqli_fetch_array($result))
{
?>
<th>Problem Description</th>
<th>Doctor's Answer</th>
<th></th>
</tr>
<tr>
<td><select name="title">
<?php echo "<option value=\"mesazhi1\">".$row[0]."</option>";}?>
</select>
</td>
<td><textarea rows="4" col="50" id ="question" readonly> </textarea></td>
<td><textarea rows="4" col="50" id ="answer" readonly> </textarea></td>
</tr>
<tr>
<td></td>
</tr>
<tr>
<td><input type="submit" name="openmessage" value="Display Selected Message" onClick="displaymessagespatient()"></td>
<td><input type="button" name="btnSubmit" value="Ask a new question" ></td>
</tr>
</table>
</fieldset>
</div>
</div>
</form>
<?php } ?>
</body>

Error when inserting data into table in sql using php form

i'm creating asset database system (just an offline system, not connected to internet) that will show all assets list. on the list, i can click on any asset to view the details. also i'm manage to update the details or delete the asset. but when it goes to asset record parts, it give an error on inserting record to asset using a form.
here is my record add form. and i'm also wanna make machine id visible under MACHINE ID field in the form, but i did't know yet how to put the data there.
for inserting record, its will capture machine id on rekod_add.php ( record add) url address from asset table to passing into rekod_tab table.
here is my record add page ( rekod_add.php )
<?php
//Start session
session_start();
//Check whether the session variable SESS_MEMBER_ID is present or not
if(!isset($_SESSION['username']) || (trim($_SESSION['password']) == '')) {
header("location: login.php");
exit();
}
?>
<html>
<head>
<title>EXA_mySQL</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<style type="text/css">
body,td,th {
font-family: Tahoma, Geneva, sans-serif;
}
</style>
</head>
<body>
<script type="text/javascript">function checkinput() {
var id_mesin = document.getElementById('id_mesin').value;
if(!id_mesin.match(/\S/)) {
alert ('Please enter Machine ID');
return false;
} else {
return true;
}
}
</script>
<?php
$con=mysqli_connect("localhost","root","admin","exa");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$id_mesin = $_POST['id_mesin'];
$query = "SELECT * FROM asset WHERE id_mesin ='".$id_mesin."'";
$result = mysqli_query($con,$query);
$rows = mysqli_fetch_array($result);
?>
<table width="733" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form_insert" method="post" action="rekod_add_ac.php">
<table width="709" border="0" align="center">
<tr>
<th width="23" scope="col">MACHINE ID</th>
<th colspan="2" scope="col">DATE</th>
<th width="68" scope="col">TIME</th>
<th width="175" scope="col">RECEIVE CALL BY</th>
<th width="97" scope="col">CURRENT METER</th>
<th width="90" scope="col">LAST METER</th>
<th width="136" scope="col">J.SHEET NO</th>
</tr>
<tr>
<td> </td>
<td colspan="2"><input name="tarikh_rekod" type="text" id="tarikh_rekod" size="15" /></td>
<td><input name="time" type="text" id="time" size="10" maxlength="9" /></td>
<td><input type="text" name="call_by" id="call_by" /></td>
<td><input name="meter_semasa" type="text" id="meter_semasa" size="15" /></td>
<td><input name="meter_last" type="text" id="meter_last" size="15" /></td>
<td><input name="rujukan" type="text" id="rujukan" size="10" /></td>
</tr>
<tr>
<td> </td>
<th width="81">PROBLEM</th>
<th width="5">:</th>
<td colspan="3"><textarea name="masalah" id="masalah" cols="55" rows="5"></textarea></td>
<th colspan="2" rowspan="2"><p>REMARK</p>
<p>
<textarea name="remark" cols="30" rows="6" id="remark"></textarea>
</p></th>
</tr>
<tr>
<td> </td>
<th>SOLUTION</th>
<th>:</th>
<td colspan="3"><textarea name="solution" id="solution" cols="55" rows="5"></textarea></td>
</tr>
<tr>
<td colspan="8" align="right"><?php echo "<input type='hidden' value='" . $rows['id_mesin'] . "' name='id_mesin'>"; echo "<input type='submit' value='Add Record'>";?></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
<?php
mysqli_close($con);
?>
</body>
</html>
here is my rekod_add_ac.php
<?php
session_start();
if(!isset($_SESSION['username']) || (trim($_SESSION['password']) == '')) {
header("location: login.php");
exit();
}
?>
<html>
<head>
<title>EXA_mySQL</title>
<script type="text/javascript">
<!--
function CloseWindow() {
window.close();
window.opener.location.reload();
}
//-->
</script>
</head>
<body>
<?php
error_reporting(E_ALL);
ini_set('display_errors','on');
$con=mysqli_connect("localhost","root","admin","exa");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
print_r($_POST);
$id_mesin=$_POST['id_mesin'];
$tarikh_rekod=$_POST['tarikh_rekod'];
$time=$_POST['time'];
$call_by=$_POST['call_by'];
$meter_semasa=$_POST['meter_semasa'];
$meter_last=$_POST['meter_last'];
$rujukan=$_POST['rujukan'];
$masalah=$_POST['masalah'];
$solution=$_POST['solution'];
$remark=$_POST['remark'];
$rekod_in="INSERT INTO rekod_tab ( id_mesin, tarikh_rekod, time, call_by, meter_semasa, meter_last, rujukan, masalah, solution, remark) VALUES ( $'id_mesin', $'tarikh_rekod', $'time', $'call_by', $'meter_semasa', $'meter_last', $'rujukan', $'masalah', $'solution', $'remark')";
$result=mysqli_query($con, $rekod_in);
if($result){
echo "Successful";
echo "<BR>";
echo "<th><form>";
echo "<input type='button' onClick='CloseWindow()' value='Back to Exa_mySQL' align='middle'>";
echo "</form></th>";
}
else {
echo "Data error, please recheck before submit.";
echo "<BR>";
echo "Click back to add record.";
echo "<BR>";
echo "<form action='rekod_add.php?id=$id_mesin' method='post'>";
echo "<td><input type='hidden' value='$id_mesin' name='id_mesin'>";
echo "<input type='submit' value='Back'></td>";
echo "</form>";
echo "<th><form>";
}
mysqli_close($con);
?>
</body>
</html>
after user done inserting record details, the form will add the record on rekod_tab table including machine id ( id_mesin ) which automatically capture from url like i said before.
but the result is error. when inserting detail manual in sql, its work. can anyone help me?
here my error result.
sorry for my bad english.
Try INSERT query like this
$rekod_in="INSERT INTO rekod_tab
( id_mesin, tarikh_rekod, time, call_by, meter_semasa, meter_last,
rujukan, masalah, solution, remark)
VALUES ( '$id_mesin', '$tarikh_rekod', '$time', '$call_by', '$meter_semasa',
'$meter_last', '$rujukan', '$masalah', '$solution', '$remark')";

Search function for virtuemart

please could you advice me if there is some mistake in process of creating this page?
I would like to do search component for virtuemart and I use one php file, where I have written whole code. I want to add there new conditions to $compare2 variable using array variable send2[] and by adding new select options. Please do you have any suggestions? I am not an expert. Thanks
<?php
$con=mysqli_connect("","","","");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if (isset($_POST['Submit'])) {
$compare1 = $_POST['send1'];
?>
?>
<table border="1" cellspacing="2" cellpadding="2">
<?php
for ($i=0; $i<sizeof($compare1);$i++) {
$result = mysqli_query($con,"SELECT * FROM auto_virtuemart_products WHERE '$compare1[$i]' LIKE product_sku");
?>
<?php
while($row = mysqli_fetch_array($result))
{
?>
<tr>
<td>
<?php echo $row['product_sku'];?>
</td>
<td>
<?php echo $row['product_weight'];?>
</td>
</tr>
<?php
}
}
?>
</table>
<?php
echo "<input type='button' value='spat' onclick=location.href=''>";
}
else
{
?>
<table border="1" cellspacing="2" cellpadding="2">
<?php
$result = mysqli_query($con,"SELECT * FROM auto_virtuemart_products");
while($row = mysqli_fetch_array($result)) {
?>
<select>
<option><?php echo $row['product_sku']; ?></option>
</select>
<form method="post">
<input type="checkbox" name="send2" value="<?php echo $row['product_sku'] ?>" />
<?php
}
?>
</table>
<input type="Submit" name="Submit2" value="Vyber"/></form>
<table border="1" cellspacing="2" cellpadding="2">
<?php
if (isset($_POST['Submit2'])) {
$compare2 = $_POST['send2'];
$result = mysqli_query($con,"SELECT * FROM auto_virtuemart_products WHERE '$compare2' LIKE product_sku");
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $row['product_sku']; ?></font>
</td>
<td>
<font face="Arial, Helvetica, sans-serif"><?php echo $row['product_weight']; ?></font>
</td>
<td>
<form method="post">
<input type="checkbox" name="send1[]" value="<?php echo $row['product_sku'] ?>" />
</td>
</tr>
<?php
}
}
else
{
echo "Empty";
}
mysqli_close($con);
?>
</table>
<input type="Submit" name="Submit" value="Submit"/></form>
<?php
}
?>

Categories