PHP keeping session logged on after link - php

I have written a page that displays a load of data from MySQL database and all works perfect EXCEPT that when i click the Home link (the title of the page) it logs out and i need to log back in, im probably missing something stupid or not doing something i need, code below
<?php
session_start();
?><title>Vend365 Monitor (Beta test)</title>
<h1><u>Vend 365 online monitor (Beta test)<p></p></u></h3>
<?php
require_once ("V365Connect.php");
//Following if 'update' is clicked
if (isset($_POST['lastseen'])){?><p>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 1px;
text-align: left;
}
</style>
<h1>Vend 'Last Seen' times: Page loaded at <?php echo date("d/m/Y G:i:s"); ?></h1>
<?php
$result1 = mysql_query("SELECT distinct customer FROM vends order by customer") or die(mysql_error());
while($row = mysql_fetch_assoc($result1))
{ echo '<table><th>';
?><font size = "5">Customer - '<?php echo $row[customer];?>'</font><?php
echo '</th>';
$result2 = mysql_query("SELECT * FROM vends where customer ='".$row[customer]."'") or die(mysql_error());
while($row1 = mysql_fetch_assoc($result2))
{
echo '<tr><td>';
?>Machine <b>'<?php echo $row1[machine];?>'</b> last seen online at <b>'<?php echo $row1[lastseen];?>'</b> running version <b>'<?php echo $row1[myversion];?>'</b><?php
$today = date("d/m/Y G:i:s");
$lastseentime = $row1[lastseen];
$diff = $today-$lastseentime;
if ($diff == "1"){
?><b> <font color = "red"> -- Last seen online yesterday</font></b> <?php ;}
if ($diff > "1"){
?> <b><font color = "red"> -- Last seen online BEFORE yesterday</font></b> <?php ;}
echo '</td></tr><p>' ;
}
}?></table>
<?php
}else{
if (isset($_POST['update'])){
if ($_POST['checkbox']=='checked'){
$isalive = 1;
} else {
$isalive = 0;
}
if ($_POST['checkbox1']=='checked'){
$hb800 = 1;
} else {
$hb800 = 0;
}
if ($_POST['checkbox2']=='checked'){
$hb1100 = 1;
} else {
$hb1100 = 0;
}
if ($_POST['checkbox3']=='checked'){
$hb1400 = 1;
} else {
$hb1400 = 0;
}
if ($_POST['checkbox4']=='checked'){
$hb1700 = 1;
} else {
$hb1700 = 0;
}
if ($_POST['checkbox5']=='checked'){
$gcmreboot = 1;
} else {
$gcmreboot = 0;
}
if ($_POST['checkbox6']=='checked'){
$emailreboot = 1;
} else {
$emailreboot = 0;
}
if ($_POST['checkbox7']=='checked'){
$hbgcm = 1;
} else {
$hbgcm = 0;
}
if ($_POST['checkbox8']=='checked'){
$hbemail = 1;
} else {
$hbemail = 0;
}
if ($_POST['checkbox9']=='checked'){
$edigcm = 1;
} else {
$edigcm = 0;
}
if ($_POST['checkbox10']=='checked'){
$ediemail = 1;
} else {
$ediemail = 0;
}
if ($_POST['checkbox11']=='checked'){
$reportgcm = 1;
} else {
$reportgcm = 0;
}
if ($_POST['checkbox12']=='checked'){
$reportemail = 1;
} else {
$reportemail = 0;
}
if ($_POST['checkbox13']=='checked'){
$pingmailgcm = 1;
} else {
$pingmailgcm = 0;
}
if ($_POST['checkbox14']=='checked'){
$pingmailemail = 1;
} else {
$pingmailemail = 0;
}
if ($_POST['checkbox15']=='checked'){
$internetgcm = 1;
} else {
$internetgcm = 0;
}
if ($_POST['checkbox16']=='checked'){
$internetemail= 1;
} else {
$internetemail = 0;
}
if ($_POST['checkbox17']=='checked'){
$sqlgcm = 1;
} else {
$sqlgcm = 0;
}
if ($_POST['checkbox18']=='checked'){
$sqlemail = 1;
} else {
$sqlemail = 0;
}
if ($_POST['checkbox19']=='checked'){
$backupgcm = 1;
} else {
$backupgcm = 0;
}
if ($_POST['checkbox20']=='checked'){
$backupemail = 1;
} else {
$backupemail = 0;
}
$sqlupdate = "update vends set isalive='".$isalive."',customer='".$_POST['customer']."',machine='".$_POST['machine']."',mailserver='".$_POST['smtp']."',emails='".$_POST['emails']."',gcm='".$_POST['gcm']."',hb800='".$hb800."',hb1100='".$hb1100."',hb1400='".$hb1400."',hb1700='".$hb1700."',sqlserver='".$_POST['sqlserver']."',sqlport='".$_POST['sqlport']."',sqlinstance='".$_POST['sqlinstance']."',sqldatabase='".$_POST['sqldatabase']."',sqlname='".$_POST['sqlname']."',sqlpassword='".$_POST['sqlpassword']."',rebootgcm='".$gcmreboot."',rebootemail='".$emailreboot."',hbgcm='".$hbgcm."',hbemail='".$hbemail."',edigcm='".$edigcm."',ediemail='".$ediemail."',reportgcm='".$reportgcm."',reportemail='".$reportemail."',mailpinggcm='".$pingmailgcm."',mailpingemail='".$pingmailemail."',internetgcm='".$internetgcm."',internetemail='".$internetemail."',sqlgcm='".$sqlgcm."',sqlemail='".$sqlemail."',backupgcm='".$backupgcm."',backupemail='".$backupemail."',lastseen='".$lastseen."' where mac='".$_SESSION['mac']."'";
mysql_query($sqlupdate) or die(mysql_error());?><h1>
--->Customer - <font color = blue><?php echo $_SESSION['customer'];?></font>
<br />
--->Machine - <font color = blue><?php echo $_SESSION['machine'];?></font>
<p>
<?php
echo "Request sent";
}
else
{
//First load screen to select customer when correct details are entered
if (!isset($_POST['update'])){
if (!isset($_POST['customer'])){
if (!isset($_POST['machine'])){
if (isset($_POST['submit'])){
$result = mysql_query("SELECT * FROM users where user='".$_POST['user']."' and pass='".$_POST['pass']."'") or die(mysql_error());
$count = mysql_num_rows($result);
if ($count == 1){
?><p><p> <table style="border:1px solid black;"><tr><td><h1>Welcome '<?php echo $_POST['user'];?>'</td></tr></table><?php
$_SESSION['customer'];
$_SESSION['machine'];
$_SESSION['mac'];
if (!isset($_POST['customer'])) {
if (!isset($_POST['machine'])) {
echo "<h1><form action ='' method='post'>";
echo "Please Select Your Customer<br />";
$result1 = mysql_query("SELECT distinct customer FROM vends order by customer") or die(mysql_error());
echo "<select name='customer'>";
while($row = mysql_fetch_assoc($result1))
{
echo "<option value = '".$row[customer]."'>".$row[customer]."</option>";
}
echo "</select>";
echo "<input type='submit' value='Go'>";
echo "</form>";
echo "";
echo "Show all 'Last Seen' times";
?>
<form method='post'>
<input type='submit' value='Show Last Seen Status' name ='lastseen' />
</form>
<?php
}
}}else {
// If wrong details entered
echo "Sorry, wrong username or password, please go back and try again";
}
} else {
// Following is first time load screen
?>
<!DOCTYPE HTML> <html>
<head>
<link rel="stylesheet" type="text/css" href="style-sign.css">
</head><h1>
<title>Vend 365 Monitor</title>
<body id="body-color">
<div id="Sign-In">
<fieldset style="width:30%">
<legend>LOG-IN HERE</legend>
<form method="POST"> User <br><input type="text" style="font-size: 30px;" name="user" size="20"><br> Password <br><input type="password" style="font-size: 30px;" name="pass" size="20"><br>
<input id="button" type="submit" style="font-size: 30px; "name="submit" value="Log-In"> </form> </fieldset>
</div>
</body>
</html>
<?php
}}}
} else {
}
?>
<?php
// Select a vending machine
if (!isset($_POST['submit'])){
if (isset($_POST['customer'])) {
$example = $_POST['customer'];
$_SESSION['customer'] = $example;
$result2 = mysql_query("SELECT * FROM vends where customer='".$example."'") or die(mysql_error());
?><h1>
<font color = black>--->Customer - <font color = blue><?php
echo $_SESSION['customer'];?><p></font></font><?php
echo "<form action ='' method='post'>";
echo "Please Select Your Machine<br />";
echo "<select name='machine'>";
while($row = mysql_fetch_assoc($result2))
{
echo "<option value = '".$row[machine]."'>".$row[machine]."</option>";
}
echo "</select>";
echo "<input type='submit' value='Go'>";
echo "</form>";
}}
// show all customer/machine info
if (isset($_POST['machine'])) {
$example1 = $_POST['machine'];
$_SESSION['machine'] = $example1;?>
<h1><font color = black>--->Customer - <font color = blue><?php
echo $_SESSION['customer'];?><br /></font></font><br /><font color = black>--->Machine - <font color = blue><?php
echo $_SESSION['machine']; ?><p><?php
$result3 = mysql_query("SELECT * FROM vends where customer='".$_SESSION['customer']."' and machine ='".$_SESSION['machine']."'") or die(mysql_error());
while ($rows = mysql_fetch_assoc($result3))
{
$tag1 = $rows['hb800'];
$checkedstatus1 = '';
if($tag1 == '1')
{
$checkedstatus1 = 'checked';
} else {
$checkedstatus1 = 'unchecked';
}?><font color = black><form method='post'>
0800 Heartbeat check - <input type='checkbox' value='checked' name='checkbox1' <?php echo $checkedstatus1; ?> />
<br /><?php
$tag2 = $rows['hb1100'];
$checkedstatus2 = '';
if($tag2 == '1')
{
$checkedstatus2 = 'checked';
} else {
$checkedstatus2 = 'unchecked';
}?>
1100 Heartbeat check- <input type='checkbox' value='checked' name='checkbox2' <?php echo $checkedstatus2; ?> />
<br />
<?php $tag3 = $rows['hb1400'];
$checkedstatus3 = '';
if($tag3 == '1')
{
$checkedstatus3 = 'checked';
} else {
$checkedstatus3 = 'unchecked';
}?>
1400 Heartbeat check - <input type='checkbox' value='checked' name='checkbox3' <?php echo $checkedstatus3; ?> />
<br />
<?php
$tag4 = $rows['hb1700'];
$checkedstatus4 = '';
if($tag4 == '1')
{
$checkedstatus4 = 'checked';
} else {
$checkedstatus4 = 'unchecked';
}?>
1700 Heartbeat check - <input type='checkbox' value='checked' name='checkbox4' <?php echo $checkedstatus4; ?> /><br />
<?php
$tag5 = $rows['rebootgcm'];
$checkedstatus5 = '';
if($tag5 == '1')
{
$checkedstatus5 = 'checked';
} else {
$checkedstatus5 = 'unchecked';
}?>
Send GCM when rebooted - <input type='checkbox' value='checked' name='checkbox5' <?php echo $checkedstatus5; ?> /><br />
<?php
$tag6 = $rows['rebootemail'];
$checkedstatus6 = '';
if($tag6 == '1')
{
$checkedstatus6 = 'checked';
} else {
$checkedstatus6 = 'unchecked';
}?>
Send email when rebooted - <input type='checkbox' value='checked' name='checkbox6' <?php echo $checkedstatus6; ?> /><br />
<?php
$tag7 = $rows['hbgcm'];
$checkedstatus7 = '';
if($tag7 == '1')
{
$checkedstatus7 = 'checked';
} else {
$checkedstatus7 = 'unchecked';
}?>
Heartbeat check GCM - <input type='checkbox' value='checked' name='checkbox7' <?php echo $checkedstatus7; ?> /><br />
<?php
$tag8 = $rows['hbemail'];
$checkedstatus8 = '';
if($tag8 == '1')
{
$checkedstatus8 = 'checked';
} else {
$checkedstatus8 = 'unchecked';
}?>
Heartbeat check Email - <input type='checkbox' value='checked' name='checkbox8' <?php echo $checkedstatus8; ?> /><br />
<?php
$tag9 = $rows['edigcm'];
$checkedstatus9 = '';
if($tag9 == '1')
{
$checkedstatus9 = 'checked';
} else {
$checkedstatus9 = 'unchecked';
}?>
EDI fail check GCM - <input type='checkbox' value='checked' name='checkbox9' <?php echo $checkedstatus9; ?> /><br />
<?php
$tag10 = $rows['ediemail'];
$checkedstatus10 = '';
if($tag10 == '1')
{
$checkedstatus10 = 'checked';
} else {
$checkedstatus10 = 'unchecked';
}?>
EDI fail check Email - <input type='checkbox' value='checked' name='checkbox10' <?php echo $checkedstatus10; ?> /><br />
<?php
$tag11 = $rows['reportgcm'];
$checkedstatus11 = '';
if($tag11 == '1')
{
$checkedstatus11 = 'checked';
} else {
$checkedstatus11 = 'unchecked';
}?>
Report fail GCM - <input type='checkbox' value='checked' name='checkbox11' <?php echo $checkedstatus11; ?> /><br />
<?php
$tag12 = $rows['reportemail'];
$checkedstatus12 = '';
if($tag12 == '1')
{
$checkedstatus12 = 'checked';
} else {
$checkedstatus12 = 'unchecked';
}?>
Report fail Email - <input type='checkbox' value='checked' name='checkbox12' <?php echo $checkedstatus12; ?> /><br />
<?php
$gcm = $rows[gcm];
$tag13 = $rows['mailpinggcm'];
$checkedstatus13 = '';
if($tag13 == '1')
{
$checkedstatus13 = 'checked';
} else {
$checkedstatus13 = 'unchecked';
}?>
Ping email server GCM - <input type='checkbox' value='checked' name='checkbox13' <?php echo $checkedstatus13; ?> /><br />
<?php
$tag14 = $rows['mailpingemail'];
$checkedstatus14 = '';
if($tag14 == '1')
{
$checkedstatus14 = 'checked';
} else {
$checkedstatus14 = 'unchecked';
}?>
Ping email server Email - <input type='checkbox' value='checked' name='checkbox14' <?php echo $checkedstatus14; ?> /><br />
<?php
$tag15 = $rows['internetgcm'];
$checkedstatus15 = '';
if($tag15 == '1')
{
$checkedstatus15 = 'checked';
} else {
$checkedstatus15 = 'unchecked';
}?>
Ping internet fail GCM - <input type='checkbox' value='checked' name='checkbox15' <?php echo $checkedstatus15; ?> /><br />
<?php
$tag16 = $rows['internetemail'];
$checkedstatus16 = '';
if($tag16 == '1')
{
$checkedstatus16 = 'checked';
} else {
$checkedstatus16 = 'unchecked';
}?>
Ping internet fail email - <input type='checkbox' value='checked' name='checkbox16' <?php echo $checkedstatus16; ?> /><br />
<?php
$tag17 = $rows['sqlgcm'];
$checkedstatus17 = '';
if($tag17 == '1')
{
$checkedstatus17 = 'checked';
} else {
$checkedstatus17 = 'unchecked';
}?>
Failed SQL ping GCM - <input type='checkbox' value='checked' name='checkbox17' <?php echo $checkedstatus17; ?> /><br />
<?php
$tag18 = $rows['sqlemail'];
$checkedstatus18 = '';
if($tag18 == '1')
{
$checkedstatus18 = 'checked';
} else {
$checkedstatus18 = 'unchecked';
}?>
Failed SQL ping email - <input type='checkbox' value='checked' name='checkbox18' <?php echo $checkedstatus18; ?> /><br />
<?php
$tag19 = $rows['backupgcm'];
$checkedstatus19 = '';
if($tag19 == '1')
{
$checkedstatus19 = 'checked';
} else {
$checkedstatus19 = 'unchecked';
}?>
Backup fail GCM - <input type='checkbox' value='checked' name='checkbox19' <?php echo $checkedstatus19; ?> /><br />
<?php
$tag20 = $rows['backupemail'];
$checkedstatus20 = '';
if($tag20 == '1')
{
$checkedstatus20 = 'checked';
} else {
$checkedstatus20 = 'unchecked';
}?>
Backp fail email - <input type='checkbox' value='checked' name='checkbox20' <?php echo $checkedstatus20; ?> /><br />
<u><b><p>Details</b></u><p>
<?php $_SESSION['mac'] = $rows[mac]; ?>
Mac address - '<?php echo $_SESSION['mac']; ?>'<br />
Customer name - '<?php echo $rows[customer]; ?>'<br />
Machine name - '<?php echo $rows[machine]; ?>'<br />
Current version - '<?php echo $rows[myversion]; ?>'<br />
GCM app code - <input type='text' value='<?php echo $gcm; ?>' size=150 rows=4 name='gcm' />......<br />
SMTP server - '<?php echo $rows[mailserver]; ?>'<br />
Email addresses (seperate by commas) - '<?php echo $rows[emails]; ?>'<br /><p>
<u><b>SQL Credentials</b></u><p>
SQL server - '<?php echo $rows[sqlserver]; ?>'<br />
SQL Port - '<?php echo $rows[sqlport]; ?>'<br />
SQL Instance - '<?php echo $rows[sqlinstance] ?>'<br />
SQL Database - '<?php echo $rows[sqldatabase]; ?>'<br />
SQL User name - '<?php echo $rows[sqlname]; ?>'<br />
SQL Password - '<?php echo $rows[sqlpassword]; ?>' <br /><p>
<p>
<u><b>Alive status</u></b><p>
Last seen online - '<?php echo $rows[lastseen]; ?>'<br />
<?php
$tag = $rows['isalive'];
$checkedstatus = '';
if($tag == '1')
{
$checkedstatus = 'checked';
} else {
$checkedstatus = 'unchecked';
}?>
Request 'IsAlive' status - <input type='checkbox' value='checked' name='checkbox' <?php echo $checkedstatus; ?> /> - This will send a GCM and Email if alive.<p>
<input type='submit' value='Update account details' name ='update' /><p>
</form><p>
<?php
}}}}
?>

Related

PHP Checkbox not filling array

I'm trying to create a modify users form, which has two checkboxes on for if a user is an Admin or if they are Active.
I've got it to read the database and check the check boxes accordingly but when I try to get the values from the userform to update, the database my PHP only returns one value.
<?php
session_start();
include 'scripts/dbconnection.php';
include 'scripts/logoncheck.php';
include 'scripts/uservariables.php';
////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////Get operator number and name///////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
$sql = "SELECT * from users";
$result = $mysqli->query($sql);
$displaytable = "<table>";
$displaytable = $displaytable."<thead><tr><th colspan='5'>Users!</th></tr>";
$displaytable = $displaytable."<tr><th>Number</th><th>Name</th><th>Admin</th><th>Active</th><th>Password</th></tr></thead><tbody>";
$user = array();
while($row = $result->fetch_assoc())
{
$displaytable = $displaytable."<tr>";
$displaytable = $displaytable."<td>".$row['opnum']."</td>";
$displaytable = $displaytable."<td><input type='text' name='username[]' value='".$row['opname']."' required></td>";
$admin = $row['admin'];
if($admin == "Yes")
{
$displaytable = $displaytable."<td><input type='checkbox' name='admin[]' value='Yes' checked='checked'/></td>";
}
else
{
$displaytable = $displaytable."<td><input type='checkbox' name='admin[]' value='No'/></td>";
}
$active = $row['active'];
if($active == "Yes")
{
$displaytable = $displaytable."<td><input type='checkbox' name='active[]' value='Yes' checked='checked'/></td>";
}
else
{
$displaytable = $displaytable."<td><input type='checkbox' name='active[]' value='No' /></td>";
}
$displaytable = $displaytable."<td><input type='checkbox' name='reset_password[]' /></td>";
$displaytable = $displaytable."</tr>";
}
$displaytable = $displaytable."</tbody></table>";
$num_rows = mysqli_num_rows($result);
if(isset($_POST['submit']))
{
$username = $_POST['username'];
$useradmin = $_POST['admin'];
$useractive = $_POST['active'];
$resetpassword = $_POST['reset_password'];
print_r($_POST['admin'][0]);
print_r($_POST['admin'][1]);
print_r($_POST['admin'][2]);
for ( $i = 0; $i < $num_rows; $i++ )
{
echo "<br> User:".$username[$i]."<br>";
if(isset($useradmin[$i]))
{
echo $username[$i]." is Admin <br>";
}
elseif(!isset($useradmin[$i]))
{
echo $username[$i]." is not Admin <br>";
}
if(isset($useractive[$i]))
{
echo $username[$i]." is Active <br>";
}
elseif(!isset($useractive[$i]))
{
echo $username[$i]." is not Active <br>";
}
if(isset($resetpassword[$i]))
{
echo $username[$i]." reset password <br>";
}
elseif(!isset($resetpassword[$i]))
{
echo $username[$i]." not reset password <br>";
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<LINK href="home.css" rel="stylesheet" type="text/css">
<title>Add Results</title>
</head>
<body>
<div class="login-card">
<?php include 'scripts/consts/menu.php';?>
<h1>Add Results!</h1>
<br>
<div class="results1">
<form action="#" method="POST">
<?php echo $displaytable;?>
<input name="submit" value="Save" type="submit" class="login login-submit">
</form>
</div>
</div>
</body>
</html>
The values that are being printed from the array useractive currently is just:
Array ( [0] => Yes )
And, what I ideally want is:
Array ( [0] => Yes, [1] => No, [2] => Yes)
In your code, if admin value is No in database and you want to change the value to Yes by checking the checkbox and update, you will get No as a value from the checkbox
Try replacing the checkbox code in the while loop with this:
$admin_checked = "";
if($admin == "Yes")
{
$admin_checked = "checked='checked'";
}
$displaytable = $displaytable."<td><input type='checkbox' name='admin[]' value='Yes' $admin_checked /></td>";
$active = $row['active'];
$active_checked = "";
if($active == "Yes")
{
$active_checked = "checked='checked'";
}
$displaytable = $displaytable."<td><input type='checkbox' name='active[]' value='Yes' $active_checked /></td>";
This way checkboxes will be checked if value is Yes and you only need to specify the checkbox input code once
And get the value from the checkboxes with this:
if(!($useradmin = $_POST['admin'])) //$_POST['admin'] is either "Yes" if checked or null if not checked
{
$useradmin = "No";
}
if(!($useractive = $_POST['active']))
{
$useractive = "No";
}
Strangely enough, you will only get the value of the checkboxes if they have been checked. It looks stupid, but that's how it works.

telephone directory update issue

I was programming something and I faced the problem. I was wondering where is the problem, because my code stop working after mysql_query("UPDATE ...") part. Here is the code, I hope someone will help me.
<?php include("/includes/template/header.php"); ?>
<section>
<form action="" method="post">
<input type="post" name="ime" placeholder="Ime"><br>
<input type="post" name="prezime" placeholder="Prezime"><br>
<input type="post" name="broj" placeholder="Broj telefona"><br>
<input type="post" name="adresa" placeholder="Adresa"><br>
<input type="submit" name="submit" value="Trazi">
</form>
<?php
include("includes/config.php");
if(isset($_POST['submit']))
{
if(!empty($_POST['ime']) && !empty($_POST['prezime']))
{
$trazeno_ime = $_POST['ime'];
$trazeno_prezime = $_POST['prezime'];
$query = "SELECT id, broj_telefona, adresa FROM korisnici WHERE ime = '$trazeno_ime' AND prezime='$trazeno_prezime'";
if ($query_run = mysql_query($query))
{
if(mysql_num_rows($query_run)!=NULL)
{
$query_row = mysql_fetch_assoc($query_run);
$id = $query_row["id"];
$brojtel = $query_row["broj_telefona"];
$adresa = $query_row["adresa"];
echo "<form action=\"\" method=\"post\">";
echo "<input type=\"post\" name=\"ime1\" value=\"$trazeno_ime\"><br></br>";
echo "<input type=\"post\" name=\"prezime1\" value=\"$trazeno_prezime\"><br></br>";
echo "<input type=\"post\" name=\"broj1\" value=\"$brojtel\"><br></br>";
echo "<input type=\"post\" name=\"adresa1\" value=\"$adresa\"><br></br>";
echo "<input type=\"submit\" name=\"submit1\" value=\"Promijeni\">";
echo "</form>";
if(isset($_POST['submit1']))
{
if(!empty($_POST['ime1']) && !empty($_POST['prezime1']) && !empty($_POST['broj1']) && !empty($_POST['broj1']) && !empty($_POST['adresa']))
{
$novoime = $_POST['ime1'];
$novoprezime = $_POST['prezime1'];
$novobroj = $_POST['broj1'];
$novoadresa = $_POST['adresa1'];
mysql_query("UPDATE korisnici SET ime='$novoime', prezime='$novoprezime', broj_telefona='$novobroj', adresa='$novoadresa' WHERE id=$id") or die(mysql_error());
echo "UspjeĆĄno promijenjeni podaci";
}
else
{
echo "Morate unijeti sva polja";
}
}
else
{
echo "Glupost";
}
}
else
{
echo "Nema korisnika u bazi";
}
}
else
{
echo "Hahu";
}
}
else
{
echo "Unesi podatke";
}
}
?>
</section>
</div>
</body>
You need to add quotes to your id statement see below.
"UPDATE korisnici SET ime='$novoime', prezime='$novoprezime', broj_telefona='$novobroj', adresa='$novoadresa' WHERE id='$id'"

Radio button checked not working in php

This is my code :
<td>
<?php
if ($adPropertyPayment == "Direct") {
$checked = "checked = 'checked'";
} else {
$checked = "";
}
if ($adPropertyPayment == "CPC") {
$checked = "checked = 'checked'";
} else {
$checked = "";
}
if ($adPropertyPayment == "CPM") {
$checked = "checked = 'checked'";
} else {
$checked = "";
}
?>
<input type="radio" id="radioPaymentDirect" name="payment" value="Direct" <?php echo $checked ?> onclick="showAmount('Direct');" />Direct
<input type="radio" id="radioPaymentCPC" name="payment" value="CPC" <?php echo $checked ?> onclick="showAmount('CPC');" />CPC
<input type="radio" id="radioPaymentCPM" name="payment" value="CPM" <?php echo $checked ?> onclick="showAmount('CPM');" />CPM
</td>
Checked is not working. I am getting the value of $aspropertypayment in POST.
Use this
<?php
$adPropertyPayment = $_POST['payment'];
if ($adPropertyPayment == "Direct") {
$checkedDir = "checked = 'checked'";
} else {
$checkedDir = "";
}
if ($adPropertyPayment == "CPC") {
$checkedCpc = "checked = 'checked'";
} else {
$checkedCpc = "";
}
if ($adPropertyPayment == "CPM") {
$checkedCpm = "checked = 'checked'";
} else {
$checkedCpm = "";
}
?>
<input type="radio" id="radioPaymentDirect" name="payment" value="Direct" <?php echo $checkedDir;?> onclick="showAmount('Direct');" />Direct
<input type="radio" id="radioPaymentCPC" name="payment" value="CPC" <?php echo $checkedCpc;?> onclick="showAmount('CPC');" />CPC
<input type="radio" id="radioPaymentCPM" name="payment" value="CPM" <?php echo $checkedCpm;?> onclick="showAmount('CPM');" />CPM
Try writting only $checked = 'checked'; in your if statement.

Best Solution for this array

I am using checkboxes to query the database and I am struggling with this one, I am new to MySQL and PHP so sorry if this is simple!
Here is my code that I have...
<input type="checkbox" name="season2005" value="2005" <?php if(isset($_POST['season2005'])) echo "checked='checked'"; ?> > 2005-06
<input type="checkbox" name="season2006" value="2006" <?php if(isset($_POST['season2006'])) echo "checked='checked'"; ?> > 2006-07
<input type="checkbox" name="season2007" value="2007" <?php if(isset($_POST['season2007'])) echo "checked='checked'"; ?> > 2007-08
<input type="checkbox" name="season2008" value="2008" <?php if(isset($_POST['season2008'])) echo "checked='checked'"; ?> > 2008-09
<input type="checkbox" name="season2009" value="2009" <?php if(isset($_POST['season2009'])) echo "checked='checked'"; ?> > 2009-10
<input type="checkbox" name="season2010" value="2010" <?php if(isset($_POST['season2010'])) echo "checked='checked'"; ?> > 2010-11
<input type="checkbox" name="season2011" value="2011" <?php if(isset($_POST['season2011'])) echo "checked='checked'"; ?> > 2011-12
<input type="checkbox" name="season2012" value="2012" <?php if(isset($_POST['season2012'])) echo "checked='checked'"; ?> > 2012-13
<input type="checkbox" name="season2013" value="2013" <?php if(isset($_POST['season2013'])) echo "checked='checked'"; ?> > 2013-14
if (#$_POST['season2005'] == ""){ $season2005 = "0000"; } else { $season2005 = "2005"; }
if (#$_POST['season2006'] == ""){ $season2006 = "0000"; } else { $season2006 = "2006"; }
if (#$_POST['season2007'] == ""){ $season2007 = "0000"; } else { $season2007 = "2007"; }
if (#$_POST['season2008'] == ""){ $season2008 = "0000"; } else { $season2008 = "2008"; }
if (#$_POST['season2009'] == ""){ $season2009 = "0000"; } else { $season2009 = "2009"; }
if (#$_POST['season2010'] == ""){ $season2010 = "0000"; } else { $season2010 = "2010"; }
if (#$_POST['season2011'] == ""){ $season2011 = "0000"; } else { $season2011 = "2011"; }
if (#$_POST['season2012'] == ""){ $season2012 = "0000"; } else { $season2012 = "2012"; }
if (#$_POST['season2013'] == ""){ $season2013 = "0000"; } else { $season2013 = "2013"; }
$seasons = array($season2005,$season2006,$season2007,$season2008,$season2009,$season2010,$season2011,$season2012,$season2013);
$seasonpick = implode(",",$seasons);;
$matcharrays = array("AND season in ($seasonpick)");
At the moment all of the data is being queried to the database, so if nothing is selected them then part of query from this is "AND season in (0000,0000,0000,0000) etc
How would I go about only getting those selected into the array and if none are selected then the array would be blank.
Hope you understand what I mean!
Here is a working form with some checkboxes that will allow you to test and get the sql you intended.
<?php
$dateArr=array();
if(isset($_POST['season']))
{
$dateArr=array_unique($_POST['season']);
$dateSearch=implode(",", $dateArr);
$sql=".... and season in (".$dateSearch.")";
echo $sql;
}
?>
<html>
<form action="?" method="post">
<?php
for($i=0;$i<10;$i++)
{
echo "<input type=\"checkbox\" name=\"season[]\" value=\"".($i+2005)."\"> ".($i+2005);
}
?>
<input type="submit">
</form>
Output when 2009, 2010 and 2011 selected:
.... and season in (2009,2010,2011)
Okay, so how it works:
Checkboxes are best used when they all have the same name ending in a []. This makes it a nice array on it's own.
If post data is set, we then quickly throw an array unique over it (good habit for the most part in these types of queries) so that there are no duplicate values.
Then simply implode it into a string and pop it into the SQL query.
Edit: Added functionality to re-check checkboxes when submitted.
<?php
$dateArr=array();
if(isset($_POST['season']))
{
$dateArr=array_unique($_POST['season']);
$dateSearch=implode(",", $dateArr);
$sql=".... and season in (".$dateSearch.")";
echo $sql;
}
?>
<html>
<form action="?" method="post">
<?php
for($i=0;$i<10;$i++)
{
$chk="";
if(!empty($_POST['season']))
{
if(in_array($i+2005, $_POST['season']))
{
$chk=" checked=\"checked\" ";
}
}
echo "<input type=\"checkbox\" name=\"season[]\" ".$chk." value=\"".($i+2005)."\"> ".($i+2005);
}
?>
<input type="submit">
</form>
Edit 2: Just add quotes in the right places :)
<?php
$dateArr=array();
if(isset($_POST['season']))
{
$dateArr=array_unique($_POST['season']);
$dateSearch=implode("', '", $dateArr);
$sql=".... and season in ('".$dateSearch."')";
echo $sql;
}
?>
<html>
<form action="?" method="post">
<?php
for($i=0;$i<10;$i++)
{
$chk="";
if(!empty($_POST['season']))
{
if(in_array(($i+2005)."i", $_POST['season']))
{
$chk=" checked=\"checked\" ";
}
}
echo "<input type=\"checkbox\" name=\"season[]\" ".$chk." value=\"".(($i+2005)."i")."\"> ".($i+2005)."i";
}
?>
<input type="submit">
</form>
Edit 3: I feel like this is starting to really answer much more than one question :)
You can simply check the textbox to make sure it isn't empty and then append to a SQL string:
$sql="";
if(!empty($_POST['text1']))
{
$sql.=" and ftgf>= ".$_POST['text1']." ";
}
Having said that, I would strongly suggest that you NEVER allow the user to enter in parts of the actual SQL you will run - unless it is a closed/secure environment, which means NOT an ope website.
Insert the below code
$seasons = array($season2005,$season2006,$season2007,$season2008,$season2009,$season2010,$season2011,$season2012,$season2013);
//start
$seasons2 = array();
foreach ($seasons as $season)
{
if($season!=="0000")
{
array_push($seasons2,$season);
}
}
$seasonpick = implode(",",$seasons2);
//end

php session vars

I'm working on news archive page for my website, search over archive is done with start date, end date and news category as search parameters. Form values are stored in $_SESSION var, and then they are passed around as an array for pagination and other purposes.
My question would be how to prevent displaying search results on main archive search page if user for some reason goes again to it to make a new search.
here's the code
<?php
session_start();
if (isset($_POST['submit'])) {
//get data from the form
$archFld_1 = $_POST['archiveFld1'];
$archFld_2 = $_POST['archiveFld2'];
$archFld_3 = $_POST['archiveFld3'];
//just some check on fields
if (strlen($archFld_1) > 10) { $archFld_1 = ""; }
if (strlen($archFld_2) > 10) { $archFld_2 = ""; }
//save them as a array and store to session var
$_archValues = array($archFld_3, $archFld_1, $archFld_2);
$_SESSION['storeValues'] = $_archValues;
}
if (isset($_SESSION['storeValues'])) {
//check params for search
//set cat for query
if ($_SESSION['storeValues'][0] > 0) { $valCat = "AND newsCat=". $_SESSION['storeValues'][0] ." "; } else { $valCat = ""; }
//set date for query
if(($_SESSION['storeValues'][1] != "" ) && ($_SESSION['storeValues'][2] == "")) {
$DateStart = $_SESSION['storeValues'][1];
$valDate = " AND STR_TO_DATE(newsDate, '%d-%m-%Y') >= STR_TO_DATE('$DateStart', '%d-%m-%Y') ";
}
if(($_SESSION['storeValues'][2] != "") && ($_SESSION['storeValues'][1]=="")) {
$DateEnd = $_SESSION['storeValues'][2];
$valDate = " AND STR_TO_DATE(newsDate, '%d-%m-%Y') <= STR_TO_DATE('$DateEnd', '%d-%m-%Y') ";
}
if(($_SESSION['storeValues'][1]!="") && ($_SESSION['storeValues'][2] != "")) {
$DateStart = $_SESSION['storeValues'][1];
$DateEnd = $_SESSION['storeValues'][2];
$valDate = " AND STR_TO_DATE(newsDate, '%d-%m-%Y') BETWEEN STR_TO_DATE('$DateStart', '%d-%m-%Y') AND STR_TO_DATE('$DateEnd', '%d-%m-%Y') ";
}
//query string and stire it to session
$archQuery_string = $valCat.$valDate;
$_SESSION['storeQuery'] = $archQuery_string;
}
//pagination start
$page = $_GET['id'];
$perPage = 10;
$result = wbQuery("SELECT * FROM wb_news WHERE newsLang=1 ". $_SESSION["storeQuery"] ."ORDER BY newsId DESC");
$totalPages = mysql_num_rows($result);
if(!$page)
$page = 1;
$start = ($page - 1)*$perPage;
?>
<div id="sps_middle">
<div class="sps_cnt">
<div id="sps_middle_ly1">
<div class="sps_cnt_small">
<div class="sps_page_title"><h3><?php echo $wb_lng['txtArchiveTitle']; ?></h3></div>
<div class="sps_pages_cnt" style="padding-top: 10px; float: left; margin-bottom: 15px;">
<div class="sps_middle_col01">
<div style="float: left;">
<p>
<?php echo $wb_lng['txtArchiveInfo']; ?>
</p>
<form action="<?php $PHP_SELF; ?>" method="post" name="archiveForm" class="archiveForm">
<ul>
<li>
<input name="archiveFld1" type="text" id="archiveFld1" value="<?php echo $wb_lng['txtArhivaFld_01']; ?>" />
<input name="archiveFld2" type="text" id="archiveFld2" value="<?php echo $wb_lng['txtArhivaFld_02']; ?>" />
<select name="archiveFld3">
<option value="0"><?php echo $wb_lng['txtArhivaFld_07']; ?></option>
<option value="0" ><?php echo $wb_lng['txtArhivaFld_06']; ?></option>
<option value="1"><?php echo $wb_lng['txtArhivaFld_03']; ?></option>
<option value="2"><?php echo $wb_lng['txtArhivaFld_04']; ?></option>
<option value="3"><?php echo $wb_lng['txtArhivaFld_05']; ?></option>
</select>
</li>
<li style="float: right;">
<input name="reset" type="reset" class="sps_archiveform_btn" value="<?php echo $wb_lng['txtArchiveFormReset']; ?>"/>
<input name="submit" type="submit" class="sps_archiveform_btn" value="<?php echo $wb_lng['txtArchiveFormSend']; ?>"/>
</li>
</ul>
</form>
</div>
<hr />
<?php
if (#HERE GOES SOME CODE TO PERFORM THE CHECK!!!#) {
//perform db query
$result = wbQuery("SELECT * FROM wb_news WHERE newsLang=1 ". $_SESSION['storeQuery'] ."ORDER BY newsId DESC LIMIT $start, $perPage");
//count rows
$totalnews = mysql_num_rows($result);
$count = 1;
if($totalnews == 0) {
//no results, say to the user
echo "\t\t\t<div class=\"cil_news_text_big\">\n\t\t\t\t".$wb_lng['txtArchiveNoEntries']."\n\t\t\t</div>\n";
} else {
//we have results, yeeeeeeeeey
while($ROWnews = mysql_fetch_object($result)){
//set link extensions by the news cat
switch ($ROWnews->newsCat) {
case 1:
$newsCat_link = "news";
break;
case 2:
$newsCat_link = "statements";
break;
case 3:
$newsCat_link = "events";
break;
}
//text summary
if (strlen($ROWnews->newsShort) > 0 ) {$newsShortTxt = strip_tags($ROWnews->newsShort);
if ($lang_id==2) { $newsShortTxt = wbTranslit($newsShortTxt); }
} else {
$newsShortTxt = strip_tags($ROWnews->newsFull);
if ($lang_id==2) { $newsShortTxt = wbTranslit($newsShortTxt); }
}
$newsShortTxt = wbShorTxt($newsShortTxt, 210, "... <a title=\"".$wb_lng['txtShowMore']."\" href=\"http://".$_SERVER['HTTP_HOST']."/".$lang_link."/".$newsCat_link."/".$ROWnews->newsId."/full/\">".$wb_lng['txtShowMore']."...</a>");
//show news
echo "\t\t<div class=\"sps_news_list\">\n";
echo "\t\t<div class=\"sps_news_l\">\n";
echo "\t\t\t<img alt=\"\" src=\"http://".$_SERVER['HTTP_HOST']."/content/images/news/_thumb/".$ROWnews->newsImageThumb."\" />\n";
echo "\t\t</div>";
echo "\t\t<div class=\"sps_news_r\">\n";
//transliterate title
if ($lang_id==2) { $newsTitle = wbTranslit($ROWnews->newsTitle); } else { $newsTitle = $ROWnews->newsTitle; }
echo "\t\t\t<div class=\"sps_news_title\">\n\t\t\t\t<a title=\"".$newsTitle."\" href=\"http://".$_SERVER['HTTP_HOST']."/".$lang_link."/".$newsCat_link."/".$ROWnews->newsId."/full/\">".$newsTitle."</a>\n\t\t\t</div>\n";
echo "\t\t\t<div class=\"sps_news_date\">\n\t\t\t\t".$ROWnews->newsDate."\n\t\t\t</div>\n";
echo "\t\t\t<div class=\"sps_news_text_sh\">\n\t\t\t\t".$newsShortTxt."\n\t\t\t</div>\n";
echo "\t\t</div>";
echo "\t\t</div>";
//show <hr /> based on $count
if($totalnews != $count) { echo "\t\t\t<hr />\n"; }
$count++;
}
}
//pagination check
if($totalPages>$perPage) {
?>
<hr />
<div class="sps_pagginate">
<?PHP wbPageTurnFront($PHP_SELF."/".$lang_link."/archive/", $totalPages, $page, $perPage); ?>
</div>
<?php
}
}
?>
Any ideas?
Tnx :)
If user goes to make it a new search then you can clear the session at that time.
unset($_SESSION['storeValues']);

Categories