previous/next record button php/mysql woes - php

My question deals with my next/previous buttons. I can get my update/delete buttons to work, but I'm so ready to tear out my hair when dealing with the next/previous buttons. Any help would be spectacular! Here's my code. Also, I'm pretty new to PHP so if this is bad coding, please let me know and point me in the right direction so I can fix my mistakes. Thanks!!!
session_start();
include "connectionfile.php";
if (isset($_POST['fname']) &&
isset($_POST['lname']) &&
isset($_POST['email']) &&
isset($_POST['login']) &&
isset($_POST['password']) &&
isset($_POST['super']) &&
isset($_POST['foldername']))
{
$id = get_post('id');
$fname = get_post('fname');
$lname = get_post('lname');
$email = get_post('email');
$login = get_post('login');
$password = hash('sha256', get_post('password'));
$super = get_post('super');
$foldername = get_post('foldername');
if ($_POST['submit']==0){
$query = mysql_query("SELECT * FROM `Logins` WHERE ID < '".$id."' ORDER BY ID DESC LIMIT 1;");
while($row = mysql_fetch_array($query)){
$id = $row['ID'];
$fname = $row['fname'];
$lname = $row['lname'];
$email = $row['email'];
$login = $row['login'];
$password = $row['password'];
$super = $row['super'];
$foldername = $row['foldername'];
}
}else if ($_POST['submit']==1){
$query = "UPDATE Logins SET fname = '$fname', lname='$lname', email='$email".'#carouselclinical.com'."', login='$login', password='$password', super='$super', foldername='$foldername'";
$query .= "WHERE ID = '$id';";
if (!mysql_query($query, $connect))
echo "INSERT failed: $query<br />" .
mysql_error() . "<br /><br />";
}else if($_POST['submit']==2){
$delete_query = "DELETE FROM Logins WHERE ID = '".$id."';";
mysql_query($delete_query);
$rc = mysql_affected_rows();
echo "Rows Affected " . $rc;
}
if ($_POST['submit']==3){
$query = mysql_query("SELECT * FROM `Logins` WHERE ID= '". $id ."' ORDER BY ID ASC LIMIT 1;");
while($row = mysql_fetch_array($query)){
$id = $row['ID'];
$fname = $row['fname'];
$lname = $row['lname'];
$email = $row['email'];
$login = $row['login'];
$password = $row['password'];
$super = $row['super'];
$foldername = $row['foldername'];
}
}
}
mysql_close($connect);
function get_post($var)
{
return mysql_real_escape_string($_POST[$var]);
}
?>
<form action="" method="post"><pre>
id <input type="text" readonly="readonly" name="id" value="<?php echo "$id"; ?>" />
First Name <input type="text" name="fname" value="<?php echo "$fname"; ?>" />
Last Name <input type="text" name="lname" value="<?php echo "$lname"; ?>" />
Email <input type="text" name="email" value="<?php echo "$email"; ?>" /> There's no need to put #carouselclinical.com.
Login <input type="text" name="login" value="<?php echo "$login"; ?>"/>
Password <input type="text" name="password" value="<?php echo "$password"; ?>"/>
Super? <input type="text" name="super" value="<?php echo "$super"; ?>" />
foldername <input type="text" name="foldername" value="<?php echo "$foldername"; ?>" />
<button name="submit" value="0">Previous</button>
<button name="submit" value="1">UPDATE</button>
<button name="submit" value="2">Delete</button>
<button name="submit" value="3">Next</button>
</pre>
Home <br />
Log out
</form>

Try adding an else right above mysql_close($connect);. My guess is that on the initial page load you are not posting any values, so no action is taken. This will create a default ID if none is defined in your top if.
else{
$query = mysql_query("SELECT * FROM `Logins` ORDER BY ID ASC LIMIT 1;");
while($row = mysql_fetch_array($query)){
$id = $row['ID'];
$fname = $row['fname'];
$lname = $row['lname'];
$email = $row['email'];
$login = $row['login'];
$password = $row['password'];
$super = $row['super'];
$foldername = $row['foldername'];
}
Also, on your if ($_POST['submit']==3), you need to change the = to > in your $query so you can get the next record. Currently you would be selecting the same ID, not the next higher.
$query = mysql_query("SELECT * FROM `Logins` WHERE ID > '". $id ."' ORDER BY ID ASC LIMIT 1;");
Finally, when doing Previous/Next you also need to take into consideration how you will deal with Previous when you are on the first ID, and Next when you are on the last id, as you will return an empty result set from MySQL.

Related

I'm Unable to set value of textbox using php variable

I'm trying to set value of html input type text textboxes to empty when user clicks Search button and empID is not matched, but its giving error:
mysqli_num_rows() expects parameter 1 to be mysqli_result
Here is code:
<html>
<body>
<form action="" method="post">
<h2>Employee Form</h2>
<input type="text" name="empID">
<input type="submit" name="searchRec" value="Search" />
<hr>
Employee ID: <input type="text" name="empIDC" value="<?php echo htmlentities($employeeID); ?>">
<br><br>
Name: <input type="text" name="name" value="<?php echo htmlentities($Name); ?>">
<br><br>
Address: <input type="text" name="address" value="<?php echo htmlentities($Address); ?>">
<br><br>
</form>
<?php
if( isset( $_REQUEST['searchRec'] ))
{
$employeeID = ($_POST["empID"]);
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "bc140_DB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT empID, Name, Address, Dateofbirth, Salary, Timein from Employee where empID == $employeeID";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result > 0)){ while($row = mysqli_fetch_array($result, MYSQL_ASSOC)) { $employeeID = $row['empID']; $Name = $row['Name']; $Address = $row['Address']; $Dateofbirth = $row['Dateofbirth']; $Salary = $row['Salary']; $timestamp = $row['timeIn']; } }else{ $employeeID = ""; $Name = ""; $Address = ""; $Dateofbirth = ""; $Salary = ""; $timestamp = ""; }
}
?>
</body>
</html>
1st : Change your code order otherwise you will get undefined error . your trying the embed the variable with html before creating the variable .
2nd : should be use single = not == empID = $employeeID
3rd : your mixing mysql with mysqli here mysql_fetch_array($result, MYSQL_ASSOC)
Change to
mysqli_fetch_array($result,MYSQLI_ASSOC);
4th: And also use isset() to confirm that variable exists or not if exists echo it otherwise echo the empty string .
5th: change your if like this if(mysqli_num_rows($result)>0){ }
file.php
<?php
if( isset( $_REQUEST['searchRec'] ))
{
......
$employeeID = $row['empID'];
$Name = $row['Name'];
$Address = $row['Address'];
$Dateofbirth = $row['Dateofbirth'];
$Salary = $row['Salary'];
$timestamp = $row['timeIn'];
......
}
?>
<html>
<body>
.....
Employee ID: <input type="text" name="empIDC" value="<?php if(isset($employeeID)){ echo htmlentities($employeeID); } else { echo ""; } ?>">
.....
</body>
</html>
you have forgotten ';' value="<?php echo htmlentities($employeeID); ?>"

How do I run multiple SQL Queries using "if(isset($_POST['Submit'])){"

Trying to make a CRUD, everything works except my Update function. I feel like the problem is in the second sql query. When I click on submit it just refreshes and the change is gone. Can anyone show me how to find what I need to change/show me what to change?
<head>
<title>Update</title>
</head>
<body>
</form>
<?php
require_once('dbconnect.php');
$id = $_GET['id'];
$sql = "SELECT * FROM dealers where ID=$id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<form action="" method="post">';
echo "Company: <input type=\"text\" name=\"CName\" value=\"".$row['CName']."\"></input>";
echo "<br>";
echo "Contact: <input type=\"text\" name=\"Contact\" value=\"".$row['Contact']."\"></input>";
echo "<br>";
echo "City: <input type=\"text\" name=\"City\" value=\"".$row['City']."\"></input>";
echo "<br>";
echo "<input type=\"Submit\" = \"Submit\" type = \"Submit\" id = \"Submit\" value = \"Submit\">";
echo "</form>";
}
echo "</table>";
} else {
echo "0 results";
}
if(isset($_POST['Submit'])){
$sql = "UPDATE dealers SET CName='$CName', Contact='$Contact', City='$City' where ID=$id";
$result = $conn->query($sql);
}
$conn->close();
?>
Instead of building a form inside PHP, just break with ending PHP tag inside your while loop and write your HTML in a clean way then start PHP again. So you don't make any mistake.
Also you've to submit your $id from your form too.
Try this
<?php
require_once('dbconnect.php');
$id = $_GET['id'];
$sql = "SELECT * FROM dealers where ID=$id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
?>
<form action="" method="post">
<input type="hidden" name="id" value="<?= $id ?>" />
Company: <input type="text" name="CName" value="<?= $row['CName'] ?>" />
<br>
Contact: <input type="text" name="Contact" value="<?= $row['Contact'] ?>" />
<br>
City: <input type="text" name="City" value="<?= $row['City'] ?>" />
<br>
<input type="Submit" name="Submit" id="Submit" value="Submit" />
</form>
<?php
} // end while loop
echo "</table>";
}
else {
echo "0 results";
}
Note: You are passing undefined variables into your update query. As you are submitting your form you must have to define those variables before you use them.
if (isset($_POST['Submit'])) {
$CName = $_POST['CName'];
$Contact = $_POST['Contact'];
$City = $_POST['City'];
$id = $_POST['id'];
$sql = "UPDATE dealers SET CName='$CName', Contact='$Contact', City='$City' where ID=$id";
$result = $conn->query($sql);
}
$conn->close();
that loop? ID primary key or not?
maybe u need create more key in table dealer like as_id
<input type="hidden" name="idform" value="$as_id">
in statment
if($_POST){
$idf = $_POST['idform'];
if(!empty($idf)){
$sql = "UPDATE dealers SET CName='$CName', Contact='$Contact', City='$City' where as_id=$idf";
$result = $conn->query($sql);
}
$conn->close();
}

Unable to disable checkbox

My problem here is that my checkbox will only be disabled when all the checkboxs status is 0. But here, i only want the selected checkbox be disabled, but when the status of checkboxs is not all set to 0, it will not be disabled.
Here is my code:
<?php
$username = $_SESSION['username'];
$query = "SELECT * FROM box WHERE status = 1";
$result = #mysqli_query($con, $query);
$num_rows = #mysqli_num_rows($result);
$disable = '';
if (!$num_rows) {
$disable = 'disabled="disabled"';
}
?>
<form method = "post" action = "">
<input type='checkbox' name="boxs[]" id="1.1" value ="1.1" <?php echo $disable ?>/>
<label for="1.1" class="background1"></label> <br/>
<input type='checkbox' name="boxs[]" id="1.2" value ="1.2"<?php echo $disable ?>/>
<label for="1.2" class="background2"></label>
<br/>
<input type='checkbox' name="boxs[]" id="1.3" value ="1.3"<?php echo $disable ?>/>
<label for="1.3" class="background2"></label>
<input type="submit" name="Next" id="Next" value="next" />
</form>
<?php
if(isset($_POST['Next'])) {
foreach($_POST['boxs'] as $f) {
$sql = "UPDATE box SET status = '0' WHERE boxid = '$f'";
mysqli_query($con,$sql) or die(mysqli_error($con));
$result = "INSERT INTO booked(username, boxid) VALUES('$username', '$f')";
mysqli_query($con,$result) or die(mysqli_error($con));
}
}
?>
So, what is wrong with my code?
<?php $username = $_SESSION['username'];?>
<form method = "post" action = "">
<?php
$username = $_SESSION['username'];
$query = "SELECT * FROM box WHERE boxid=1.1 AND status = 1";
$result = #mysqli_query($con, $query);
$num_rows = #mysqli_num_rows($result);
$disable = '';
if (!$num_rows){
$disable = 'disabled="disabled"';
}
?>
<input type='checkbox' name="boxs[]" id="1.1" value ="1.1" <?php echo $disable ?>/>
<label for="1.1" class="background1"></label> <br/>
<?php
$query = "SELECT * FROM box WHERE boxid=1.2 AND status = 1";
$result = #mysqli_query($con, $query);
$num_rows = #mysqli_num_rows($result);
$disable = '';
if (!$num_rows){
$disable = 'disabled="disabled"';
}
?>
<input type='checkbox' name="boxs[]" id="1.2" value ="1.2"<?php echo $disable ?>/>
<label for="1.2" class="background2"></label>
<br/>
<?php
$query = "SELECT * FROM box WHERE boxid=1.3 AND status = 1";
$result = #mysqli_query($con, $query);
$num_rows = #mysqli_num_rows($result);
$disable = '';
if (!$num_rows){
$disable = 'disabled="disabled"';
}
?>
<input type='checkbox' name="boxs[]" id="1.3" value ="1.3"<?php echo $disable ?>/>
<label for="1.3" class="background2"></label>
<input type="submit" name="Next" id="Next" value="next" />
</form>
<?php
if(isset($_POST['Next']))
{
foreach($_POST['boxs'] as $f){
$sql = "UPDATE box SET status = '0' WHERE boxid = '$f'";
mysqli_query($con,$sql) or die(mysqli_error($con));
$result = "INSERT INTO booked(username, boxid) VALUES('$username', '$f')";
mysqli_query($con,$result) or die(mysqli_error($con));
}
}
?>
I don't know the your exact requirement but; Your solutions may be
<?php $username = $_SESSION['username']; ?>
<form method = "post" action = "">
<?php
$username = $_SESSION['username'];
$query = "SELECT * FROM box";
$result = #mysqli_query($con, $query);
$i=1;
while ($raw = $result->fetch_assoc()) {
if ($raw['status'] == 1) {
$disable = 'disabled="disabled"';
} else {
$disable = '';
}
echo "<input type='checkbox' name='boxs[]' id='" . $raw['boxid'] . "' value ='" . $raw['boxid'] . "' $disable/>";
echo " <label for='" . $raw['boxid'] . "' class='background$i'></label> <br/>";
$i++;
}
?>
</form>

PHP cannot insert variable into database

Problem is in the INSERT INTO statement. I'm trying to insert $today, $id, $stat_sum, $klientas. Everything is inserting except the $id. After I press the submit button, everything is inserting, but where there should be the $id, it shows NULL. I tried to echo $id , it showing normally as it should be.
$today = date('Y-m-d G:i:s');
if( isset($_GET['pas']) )
{
$id = $_GET['pas'];
$res= mysql_query("SELECT MAX(statymo_suma) FROM statymai WHERE aukciono_id = '$id'");
$row= mysql_fetch_array($res);
echo $id;
}
if( isset($_POST['stat_sum']) )
{
echo '<input type="hidden" name="id" value="<?php echo $row[2]; ?>">';
$stat_sum = $_POST['stat_sum'];
$id = $_POST['id'];
$res1= mysql_query("SELECT kliento_id FROM klientai WHERE vartotojo_vardas = '$user_check'");
$row1 = mysql_fetch_assoc($res1);
$klientas = $row1['kliento_id'];
$sql = "INSERT INTO statymai (statymo_laikas,aukciono_id,statymo_suma,kliento_id) VALUES ('$today','$id','$stat_sum','$klientas')";
$res =mysql_query($sql)
or die("Negaliu redaguoti".mysql_error());
//echo "<meta http-equiv='refresh' content='0;url=redaguoti.php'>";
}
?>
<form action="pasiulymas.php" method="POST">
Statymo suma: <input type="text" name="stat_sum" value="<?php echo $row[0]; ?>"><br />
<input type="hidden" name="id" value="<?php echo $row[2]; ?>">
<input type="submit" value=" =Redaguoti "/>
</form>
var_dump($sql) result:
string(123) "INSERT INTO statymai (statymo_laikas,aukciono_id,statymo_suma,kliento_id) VALUES ('2015-12-20 15:04:50','','123456','KL01')"

Liking system states that variable isn't defined, even though it is

I have the following code (below) to that loads the likes for a specific status (for dynamic use), it works if a user is liking their own post. But if a user is liking someone elses, it epically fails.
My code:
<?php
mysql_connect ('localhost', 'funding9_joeb', 'Brailsf0rdJ0e');
mysql_select_db ('mingle');
include('../includes/ts.php');
$_COOKIE['wds'];
$ssid = $_COOKIE['wds'];
$lu = "SELECT * FROM mingle_sessions WHERE sid = '$ssid' LIMIT 1";
$luq = mysql_query($lu) or die (mysql_query());
while($uidr = mysql_fetch_assoc($luq)) {
$uid = $uidr['uid'];
}
$sql = "SELECT * FROM users WHERE uid = '$uid' LIMIT 1";
$s = htmlentities(strip_tags(stripslashes($_GET['sid'])));
$result = mysql_query($sql) or print ("Can't select entry from table mingle_usr.<br />" . $sql . "<br />" . mysql_error());
while($row = mysql_fetch_array($result)) {
$fname = stripslashes($row['fname']);
$sname = stripslashes($row['sname']);
$dp = $row['dp'];
}
//--------------------------------------------------------------------------
// 2) Query database for data
//--------------------------------------------------------------------------
$sql = "SELECT * FROM mingle_likes WHERE byuid = '$uid' AND onuid = '$s' AND type = 'status'";
$sr = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($sr) >= 1) {
$re = ("SELECT id as id, status as status, sid as sid, UNIX_TIMESTAMP(timestamp) as timestamp FROM mingle_status WHERE uid = '$uid' AND sid = '$s' ORDER BY timestamp DESC LIMIT 1"); //query
$result = mysql_query($re) or die (mysql_error());
while($st = mysql_fetch_assoc($result)) {
$status = nl2br($st['status']);
$sid = $st['sid'];
$td = $st['timestamp'];
$id = $st['id'];
}
$like = mysql_fetch_array(mysql_query("SELECT COUNT(*) as num FROM mingle_likes WHERE onuid = '$sid' AND type = 'status' AND byuid != '$uid'"));
$likes = ceil($like['num']);
$haslike = mysql_fetch_array(mysql_query("SELECT COUNT(*) as nu FROM mingle_likes WHERE onuid = '$sid' AND type = 'status' AND byuid = '$uid'"));
$hasliked = ceil($haslike['nu']);
?>
<script type="text/javascript" src="js/ld_s.js"></script>
<form action='ld.php' method='post' id='ls' style='display:inline; border:0px; margin: 0 0 0 0; padding: 0 0 0 0;'>
<input type="hidden" class="do_<?php echo $sid; ?>" name="onuid" value="<?php echo $sid; ?>" />
<input type="hidden" class="db_<?php echo $sid; ?>" name="byuid" value="<?php echo $uid; ?>" />
<input type="hidden" class="dp_<?php echo $sid; ?>" name="uid" value="<?php echo $uid; ?>" />
<input type="hidden" class="dt_<?php echo $sid; ?>" name="type" value="status" />
<?php
if($hasliked == 1) {
?>
<label>You <?php if($likes >= 1) { echo "and " . $likes . " other people like this"; } else{ echo "liked this |"; }?></label><input type="submit" id="like" class="<?php echo $sid; ?>" name="submit" value="Unlike" />
<?php
}
else {
?>
<label><?php if($likes >= 1) { echo $likes . " people like this |"; }?></label><input type="submit" id="like" class="<?php echo $sid; ?>" name="submit" value="Like" />
<?php
}
?>
</form>
<?php echo time_since($td); ?>
<?php
}
?>
The errors are as follows:
( ! ) Notice: Undefined variable: sid in C:\wamp\www\mingle\ajax\loadslikes.php on line 47
and then the same again for every occurrence of $sid.
I haven't got a clue why at all, like I say, it works perfectly for the user liking his/her own post, just not others.
Any ideas? :-/

Categories