I want to get the clicked number
and load the data from the database according to this number. The column that the numbers are stored is named as id.
I have this code in order to display the numbers(id)...
$sql = "SELECT id FROM work WHERE username='$username' order by id asc limit 10;";
$result = mysql_query($sql);
if ($result != 0) {
$num_results = mysql_num_rows($result);
for ($i=0;$i<$num_results;$i++) {
$row = mysql_fetch_array($result);
$id = $row['id'];
echo '' .$id. '';
}
}
And then I want to load the data from this number in a form which the code for the form is...
function kotoula() {
$username = $_SESSION["username"];
if($query = mysql_query("SELECT job_title,company,website,start_date,end_date,start_year,end_year,work_history FROM work WHERE id>'$id' AND username='$username' order by id asc limit 10") or die(mysql_error()))
{
if(mysql_num_rows($query)>=1){
while($row = mysql_fetch_array($query)) {
$job_title = $row['job_title'];
$company = $row['company'];
$website = $row['website'];
$start_date = $row['start_date'];
$end_date = $row['end_date'];
$start_year = $row['start_year'];
$end_year = $row['end_year'];
$work_history = $row['work_history'];
}
}
}
}
Just put it in the URL using GET. Something like:
http://yoururl.com/user.php?id=12345
Then, user.php will receive that value on $_GET array. Example:
$_GET['id'];
Related
I am trying to capture a value that is calculated on a PHP page called "classes_day.php" at the same time as I pass a value per GET, "? Day = YYYY-mm-dd" to it. How do I do this with JS or JQuery?
<?php
// aulas_dia.php
include '../config.php';
$exped_duration = 14*60;
if (isset($_GET['data'])) {
$data = $_GET['data'];
$query = "SELECT * FROM `task` WHERE `dia` LIKE ".$data."";
$result = mysqli_query($link,$query);
$soma = 0;
while ($row = mysqli_fetch_assoc($result)) {
$soma = $soma+$row['duration'];
}
$aulas_free = floor(($exped_duration-$soma)/50);
echo $aulas_free;
}
?>
I already tried using an iframe and contentwindow, but iframe gets the value and the contentwindow is empty (weird isn't it?).
Following Barmar's tip, I'm using $ .get, but I don't know why this loop is not working, can anyone help me?
for (i = 0; i < num_days; i++) {
x = (first_day+i)%7;
y = (first_day+i-x)/7;
h_dia(String(y)+String(x),i+1);
data_c = ano+"-"+mes+"-"+String(i+1);
$.get("aulas_dia.php?data="+data_c, function(data){
console.log(String(y)+String(x)+" - "+data_c+" - "+data);
set_aulas_fun(String(y)+String(x),data);
});
}
Use $.get() to send an AJAX request.
$.get("classes_day.php?data=YYYY-MM-DD", function(response) {
console.log(response);
});
BTW, you can add up all the durations in the SQL query instead of using a PHP loop. And you should use a prepared statement to prevent SQL injection.
<?php
include '../config.php';
$exped_duration = 14*60;
if (isset($_GET['data'])) {
$data = $_GET['data'];
$query = "SELECT SUM(duration) AS total FROM `task` WHERE `dia` LIKE ?";
$stmt = $link->prepare($query);
$stmt->bind_param("s", $data);
$stmt->execute();
$result = $stmt->get_result();
$row = $result->fetch_assoc();
$soma = $row['total'];
$aulas_free = floor(($exped_duration-$soma)/50);
echo $aulas_free;
}
While getting data from the database it is alerting 0000:00:00 while in the database it is inserting write date
<?php
$query = "select * from Reply t1 inner join users t2 on t1.UserId = t2.UserId where comment = '$commentid'";
$run1 = mysqli_query($mysqli,$query);
$numberRows = mysqli_num_rows($run1);
while($row1 = mysqli_fetch_array($run1))
{
$Reply = $row1['Reply'];
$UserId = $row1['UserId'];
$UserName = $row1['UserName'];
$date1 = $row['Date'];
echo "<script>alert('$date1')</script>";
$ageDate1 = time_elapsed_string($date1);
echo "<script>alert('$ageDate1')</script>";
?>
I have two problems.
First one:
I have a two tables in my database (gmembers and groups) and I want to check if a user quit from the group and there are no memebrs remaining in it delete that group. Firstly I collected every groups that has no members in an array called junk. Then I used a for loop to access every item of the junk array and delete from the database. But since I want to delete from two tables at the same time I had to use JOIN. I don't know why but this only delete from the gmembers table and nothing happens with the groups table.
I tried to tear it into two parts and delete once from the gmembers and then from the groups in another sql but I got the same result as I got with the JOIN one.
Code:
<?php
if(isset($_POST["action"]) && $_POST['action'] == "quit_group"){
// Empty check
if($gS == "" || $uS == ""){
exit();
}
// Make sure already member
$sql = "SELECT id FROM gmembers WHERE gname=? AND mname=? AND approved=? LIMIT 1";
$stmt = $conn->prepare($sql);
$stmt->bind_param("sss",$gS,$uS,$one);
$stmt->execute();
$stmt->store_result();
$stmt->fetch();
$numrows = $stmt->num_rows;
if($numrows < 1){
exit();
}
// Remove from the database
$sql = "DELETE FROM gmembers WHERE mname=? AND gname=? LIMIT 1";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss",$uS,$gS);
$stmt->execute();
$stmt->close();
// If the group is empty remove from the database
$junk = array();
$sql = "SELECT * FROM gmembers WHERE approved=? AND admin=?";
$stmt = $conn->prepare($sql);
$stmt->bind_result($zero,$zero);
$stmt->execute();
$result = $stmt->get_result();
while($row = $result->fetch_assoc()){
array_push($junk, $row["gname"]);
}
$stmt->close();
for($i=0; $i<count($junk); $i++){
// Delete from gmembers
$groupa = $junk[$i];
$sql = "DELETE * FROM gmembers gm JOIN groups gr ON gm.gname = gr.name WHERE gm.gname=? AND gr.name=?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ss",$groupa,$groupa);
$stmt->execute();
$stmt->close();
}
echo "was_removed";
exit();
}
?>
Second One:
I'm also having problems with ajax.responseText. When a group has been created and there are no erros I want to header the user to that groups that he/she has just created. So when everything is fine I echo the name of the group ($name) and the was created title. Then with javascript I check for these and I header them to the group page. But at this point my code fails. It only echos group_created|example group in an alert box and window.location = "group.php?g="+sid; does not work.
PHP Code:
<?php
// Create new group
if(isset($_POST["action"]) && $_POST['action'] == "new_group"){
// GATHER THE POSTED DATA INTO LOCAL VARIABLES
$name = preg_replace('#[^a-z 0-9]#i', '', $_POST['name']);
$inv = preg_replace('#[^0-9.]#', '', $_POST['inv']);
// DUPLICATE DATA CHECKS FOR USERNAME AND EMAIL
if ($inv == "1"){
$inv = "0";
}
if ($inv == "2"){
$inv = "1";
}
$sql = "SELECT id FROM groups WHERE name=? LIMIT 1";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s",$name);
$stmt->execute();
$stmt->store_result();
$stmt->fetch();
$n_check = $stmt->num_rows;
// FORM DATA ERROR HANDLING
if($name == "" || $inv == ""){
echo "The form submission is missing values.";
exit();
} else if ($n_check > 0){
echo "The group name you entered is alreay taken";
exit();
} else if (strlen($name) < 3 || strlen($name) > 50) {
echo "Group name must be between 3 and 50 characters";
exit();
} else if (is_numeric($name[0])) {
echo 'Group name cannot begin with a number';
exit();
} else {
$stmt->close();
// END FORM DATA ERROR HANDLING
// Begin Insertion of data into the database
// Add group to database
$gicon = "group_icon.png";
$sql = "INSERT INTO groups (name, creation, logo, invrule, creator)
VALUES(?,NOW(),?,?,?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssss",$name,$gicon,$inv,$uS);
$stmt->execute();
$stmt->close();
// Add to group member to database
$sql = "INSERT INTO gmembers (gname, mname, approved, admin)
VALUES(?,?,?,?)";
$stmt = $conn->prepare($sql);
$stmt->bind_param("ssss",$name,$uS,$one,$one);
$stmt->execute();
$stmt->close();
if (!file_exists("../groups")) {
mkdir("../groups", 0755);
}
// Create directory(folder) to hold each user's files(pics, MP3s, etc.)
if (!file_exists("../groups/$name")) {
mkdir("../groups/$name", 0755);
}
$gLogo = '../images/group_icon.png';
$gLogo2 = "../groups/$name/group_icon.png";
if (!copy($gLogo, $gLogo2)) {
echo "failed to create logo.";
}
echo "group_created|$name";
exit();
}
exit();
}
?>
The error handling, inserting, folder/file creating etc. works perfect except this line: echo "group_created|$name";
JS Code:
function createGroup(){
var name = _("gname").value;
var inv = _("invite").value;
if(name == "" || inv == ""){
alert("Fill in all fields");
return false;
}else{
status.innerHTML = 'please wait...';
var ajax = ajaxObj("POST", "php_parsers/group_parser.php");
ajax.onreadystatechange = function(){
if(ajaxReturn(ajax) == true){
var datArray = ajax.responseText.split("|");
if(datArray[0] == "group_created"){
var sid = datArray[1];
window.location = "group.php?g="+sid;
}else{
alert(ajax.responseText);
}
}
}
ajax.send("action=new_group&name="+name+"&inv="+inv);
}
}
Here I check for what ajax gives back and I split into two part the echo "group_created|$name"; but the window.location function does not work.
For the Second One
Use:
window.Location.assign("group.php?g="+sid);
Or:
window.Location.replace("group.php?g="+sid);
I'm new at php. I get the last id from database. For each id I want the state and the link. I'll check if state == 1, then get the content of the link (there's JavaScript variable that I need that is in the content of link). I'll send that variable with location.href.
Then I get that variable with $_GET in the second page. I want to store that var into database, then come back to first page and get the second link from the database and again do the same works.
How can I send the $j into second page for saving the x_cor and y_cor, and how to increase the $j when it comes to first page again?
<html>
<head>
<title>firstpage</title>
</head>
<body>
<?php
include_once ('simple_html_dom.php');
include_once ('split_unicode_function.php');
// getting the last id from db
$connection = #mysql_connect("localhost", "root", "kkuser");
$select_db = mysql_select_db("kk", $connection);
$sql = "SELECT id FROM netbarg ORDER BY id DESC LIMIT 1";
$result = mysql_query($sql, $connection);
$rows = mysql_fetch_array($result);
$last_id = $rows['id'];
// getting id and link for each column
for ($j = 1; $j <= 2; $j++)
{
$select_db = mysql_select_db("kk", $connection);
$id = "SELECT state FROM `table` WHERE id='$j' ";
$result = mysql_query($id, $connection);
$row = mysql_fetch_array($result);
echo $state = $row[0] . '<br />';
// getting link
$link = "SELECT link FROM `table` WHERE id='$j' ";
$result = mysql_query($link, $connection);
$rows = mysql_fetch_array($result);
$link = $rows[0];
// (state is just 1 or 2)check if the state is 2 or not...
if ($state == 1)
{
$f = file_get_contents($link);
echo "<div>$f</div>";
}
}
?>
<script>
$("body").hide();
location.href ='secondpage.php?val='+point0+;
</script>
</body>
</html>
second page
<html>
<head>
<title>second page</title>
</head>
<body>
<?php
if (isset($_GET['val']))
{
$hat = $_GET['val'];
echo $hat;
$coords = trim($hat, '()');
// echo $coords.'<br />';
$a = array();
$a = explode(",", $coords);
var_dump($a);
echo $long = $a[0];
echo '<br />';
if ($long == "undefined") $lat = "undefined";
else echo $lat = $a[1];
if ($_SERVER['REQUEST_METHOD'] == "GET")
{
$connection = #mysql_connect("localhost", "root", "kkuser");
$select_db = mysql_select_db("kk", $connection);
$update = "UPDATE `table` SET `x_cor`='$long',`y_cor`='$lat' , `state`='2' WHERE `id`='$j' ";
$insert_todb = mysql_query($update, $connection);
if ($insert_todb) echo "coordinates has been updated", '<br />';
}
}
?>
<script>
location.href ='firstpage.php';
</script>
</body>
</html>
if get the last insert id use mysqli_insert_id($con);
it return last insert id from database using this geted id you can
I made the following script but I want that each script should echo the json_encode array only when I call the function. When I tried defining the function, and then calling it, it displayed nothing. It is working if the scripts are not made in the functions. How do I make different functions and then call different functions according to my usage?
<?php
ini_set('display_errors', '0');
error_reporting(0);
require_once("include/db.php");
date_default_timezone_set('Asia/Kolkata');
$regno ='14ASDFJ234';
$password = '0';
$name = 'EASPORTS';
$priority = 0;
//fetch priority
$query = "SELECT priority FROM users WHERE regno='{$regno}' AND pass='{$password}' LIMIT 1";
$res = mysql_query($query, $conn) or die(mysql_error());
$found = mysql_fetch_array($res);
if($found)
{
$priority=$found['priority'];
}
//echo $priority;
echo 'news feed : <br> '
$sql = "SELECT * FROM newsfeed";
$result = mysql_query($sql,$conn) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$details[] = array(
'name' => $row['name'],
'feed' => $row['feed']
);
}
echo json_encode($details);
// announcement details...
echo "<br> Announcement details: <br>";
$sql1 = "SELECT * FROM announcements WHERE name = '$name'";
$result1 = mysql_query($sql1,$conn) or die(mysql_error());
while ($row1 = mysql_fetch_array($result1)) {
$details1[] = array(
'name' => $row1['name'],
'pname' => $row1['pname'],
'date' => $row1['date'],
'time' => $row1['time'],
'status' => $row1['status']
);
}
echo json_encode($details1);
//events script...
?>
You should do like this
public function somefunction()
{
$query = "SELECT priority FROM users WHERE regno='{$regno}' AND pass='{$password}' LIMIT 1";
$res = mysql_query($query, $conn) or die(mysql_error());
$found = mysql_fetch_array($res);
if($found)
{
$priority=$found['priority'];
}
//echo $priority;
echo 'news feed : <br> '
$sql="SELECT * FROM newsfeed";
$result=mysql_query($sql,$conn) or die(mysql_error());
while ($row=mysql_fetch_array($result)) {
$details[] = array(
'name' => $row['name'],
'feed' => $row['feed']
);
}
echo json_encode($details);
}
now when you call somefunction() you will get json encoded array as result