I want to send data to database, but if result = 1 status=plusone, result = 2 status=plustwo, etc..
It should work like that..
But no, it work like this: result = 2 status=plusone .
What did i missed? Help me..
I've tried this:
$item = '0';
$result = $item + $points;
and:
$result = $points + 0;
Here is rest (part of) code:
if($result = 1){
$Sql_Query ="INSERT INTO points SET unique_id = '$id', description = '$Description', points = '$points', status= 'plusone'";
if(mysqli_query($con,$Sql_Query)) {
echo 'Succcess!';
}
}elseif($result = 2){
$Sql_Query ="INSERT INTO points SET unique_id = '$id', description = ' $Description', points = '$points', status= 'plustwo'";
if(mysqli_query($con,$Sql_Query)) {
echo 'Succcess!';
}
}
= and == are different; = is for assignment, and == is to compare statement
Related
I had a previous question on how to do this script and have figured out a way but I am not too sure if I should use this method.
The other post is available here.
Can someone point me in the right direction if there is a more faster/efficient way of running this script as my previous way I was doing it was sometimes taking over an hour to finish.
<?php
//Require admin
require_once("inc/admin.php");
require_once ("../includes/routeros_api.class.php");
//SET
$ip = "10.100.1.1";
//Connect to MikroTik API
$API = new RouterosAPI();
$API->debug = $config['api']['debug'];
if (!$API->connect($ip, $config['api']['username'], $config['api']['password'])) {
echo "Could not connect to RouterOS API";
} else {
$API->write('/ip/accounting/snapshot/take',true);
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
$API->write('/ip/accounting/snapshot/print',true);
$READ = $API->read(false);
$ARRAY = $API->parseResponse($READ);
foreach($ARRAY as $ACCOUNTING) {
$ip_src = $ACCOUNTING['src-address'];
$ip_dst = $ACCOUNTING['dst-address'];
$bytes = $ACCOUNTING['bytes'];
//Check if ip in use UPLOAD
$query = "SELECT id, ipv4 FROM services WHERE ipv4='$ip_src' AND deleted !='1'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_array($result);
if(mysqli_num_rows($result) > 0) {
$service_id = $row['id'];
//Update Download Traffic
$check_if_exist_query = "SELECT * FROM traffic_counters WHERE service_id='$service_id' AND date=CURRENT_DATE()";
$check_result = mysqli_query($conn, $check_if_exist_query);
$check_num_rows = mysqli_num_rows($check_result);
if($check_num_rows == 0) {
$add_query = "INSERT INTO traffic_counters (service_id, upload_bytes, date) VALUES ('$service_id', '$bytes', CURRENT_DATE());";
$add_result = mysqli_query($conn, $add_query);
} else {
$update_query = "UPDATE traffic_counters SET
upload_bytes = upload_bytes + $bytes
WHERE service_id='$service_id' AND date=CURRENT_DATE();
";
$update_result = mysqli_query($conn, $update_query);
}
}
//Check if ip in use DOWNLOAD
$query = "SELECT id, ipv4 FROM services WHERE ipv4='$ip_dst' AND deleted !='1'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_array($result);
if(mysqli_num_rows($result) > 0) {
$service_id = $row['id'];
//Update Download Traffic
$check_if_exist_query = "SELECT * FROM traffic_counters WHERE service_id='$service_id' AND date=CURRENT_DATE()";
$check_result = mysqli_query($conn, $check_if_exist_query);
$check_num_rows = mysqli_num_rows($check_result);
if($check_num_rows == 0) {
$add_query = "INSERT INTO traffic_counters (service_id, download_bytes, date) VALUES ('$service_id', '$bytes', CURRENT_DATE());";
$add_result = mysqli_query($conn, $add_query);
} else {
$update_query = "UPDATE traffic_counters SET
download_bytes = download_bytes + $bytes
WHERE service_id='$service_id' AND date=CURRENT_DATE();
";
$update_result = mysqli_query($conn, $update_query);
}
}
}
$API->disconnect();
}
?>
1.) You can start with "simplify" insert queries:
Just have some variable $INSERT_VALUES and do this in loops
$INSERT_VALUES.=", ('$service_id', '$bytes', CURRENT_DATE())";
After Loop, you can do only one Insert (only one query) to database:
$add_query = "INSERT INTO traffic_counters (service_id, upload_bytes, date) VALUES INSERT_VALUES";
2.) if you have LARGE amount of rows, using UPDATE is very bad idea, but at this type of using that, there is no way to solve this. You can Try call on mysql START TRANSACTION; and after all of updates, call COMMIT;
3.) On large database SELECT ... WHERE ... is slower than load all datas to php array and then find your needed row by array_search or by using good method of creating array (For example $DATA[service_id]["colunm"]=$value)
I'm new to PHP, now I face some strange issue about updating the value.
From these codes
for($i=0; $i < $count; $i++){
$sql = ("UPDATE applicant_skill SET App_skill_performance_score = '".$s_score[$i]."', App_skill_knowledge_score = ".$k_score[$i]."' WHERE App_Data_ID ='".$a_id."' && Position_ID = '".$p_id."' && Skill_ID = '".$skill_id[$i]."' ");
$resultt = "";
if ($conn->query($sql) == TRUE) {
$resultt = "FINISH";
} else {
$resultt = "ERROR";
}
}
At first I use INSERT and it works fine. Now I change to UPDATE but it updates nothing (the field is already in database waiting for update).
I don't know where mistake is, please help.
Try
$sscrore = $s_score[$i];
$kscore = $k_score[$i];
$aid = $a_id;
$pid = $p_id;
$skillid = $skill_id[$i];
$sql = ("UPDATE applicant_skill SET App_skill_performance_score = '$sscrore', App_skill_knowledge_score = '$kscore' WHERE App_Data_ID ='$aid' AND Position_ID = '$pid' AND Skill_ID = '$skillid' ");
Basically, you are missing one ' before ".$k_score[$i]."'
use AND instead of &&
This is my table in my database. It should only contain one row.
I have this code, that checks if there is no data , if there is not.. The data is inserted, else it's updated. The problem is, that if i update only Brevet, the value of Baccalaureabt will disappear from my database. Any help?
Code:
if(isset($_POST['submit']))
{ $brevet = $_POST['Brevet'];
$baccalaureatbt = $_POST['Baccalaureatbt'];
$sql1="SELECT Brevet,Baccalaureatbt FROM university";
if ($result=mysqli_query($con,$sql1))
{
$rowcount=mysqli_num_rows($result);
}
if($rowcount==0)
{
$sql="INSERT INTO university(Brevet,Baccalaureatbt) VALUES('$brevet','$baccalaureatbt')";
$result = mysql_query($sql);
}
else
{
$sql2 = "UPDATE university SET Brevet = '$brevet' , Baccalaureatbt = '$baccalaureatbt'";
$result2 = mysql_query($sql2);
}
The issue is that you are setting both values, if however either $baccalaureatbt or $brevet is empty, it will be updated with a empty value, i.e the original value will be deleted.
There are multiple ways to avoid this, but one way to do it is like so:
$sql2 = "UPDATE university SET ";
if(!empty($brevet)){
$sql2 .= "Brevet = '$brevet'";
$first = 1;
}
if(!empty($baccalaureatbt)){
$sql2 .= isset($first) ? ", Baccalaureatbt = '$baccalaureatbt'" : "Baccalaureatbt = '$baccalaureatbt' " ;
}
$result2 = mysql_query($sql2);
To update the values dynamically, use the following code. Just add the values you want in the values array.
if(isset($_POST['submit']))
{
$brevet = $_POST['Brevet'];
$baccalaureatbt = $_POST['Baccalaureatbt'];
$sql1="SELECT Brevet,Baccalaureatbt FROM university";
$result=mysqli_query($con,$sql1);
if ($result)
{
$rowcount=mysqli_num_rows($result);
}
if($rowcount==0)
{
$sql="INSERT INTO university(Brevet,Baccalaureatbt) VALUES('$brevet','$baccalaureatbt')";
$result = mysql_query($sql);
}
else
{
$values = array("brevet","baccalaureatbt");
$sql2 = "UPDATE university ";
$n = 0;
foreach($_POST as $key => $val) {
if(in_array($key, $values)) {
$sql2 += ($n !== 0 ? ", " : 0);
$sql2 += "SET".$key." = ".$val;
$n++;
}
if($n > 0) {
$result2 = mysql_query($sql2);
}
}
}
}
I've made a like button on my site (which uses ExpressionEngine), and it works. However when I put almost identical code on another page, the like button's text is always what it should have been on the last page (the opposite to what it should be, though refreshing the page puts it right, so I can't just flip the values. Here's the code that works:
<?php
$DB1 = $this->EE->load->database('ext_db', TRUE);
$mapID = "{entry_id}";
$ipAddress = $_SERVER['REMOTE_ADDR'];
$thisquery = "SELECT * FROM mapLikes WHERE ipAddress = '$ipAddress' AND mapID = '$mapID'";
$q = $DB1->query($thisquery);
$results = $q->result_array();
foreach ($q->result() as $row)
{
$liked = $row->liked;
}
$buttontext = 'Like';
$buttonimage = "1";
if ($liked == "1") {
$buttontext = 'Unlike';
$buttonimage = "2";
}
if($_POST['like']) {
if ($liked == "1") {
$thisquery = "DELETE FROM mapLikes WHERE mapID = '$mapID' AND ipAddress = '$ipAddress'";
$DB1->query($thisquery);
} else {
$thisquery = "INSERT INTO mapLikes (mapLikeID, mapID, liked, ipAddress) VALUES ('null', '$mapID', '1', '$ipAddress')";
$DB1->query($thisquery);
}
}
?>
And the code that doesn't:
<?php
$DB1 = $this->EE->load->database('ext_db', TRUE);
$mapID = "{segment_3_category_id}";
$ipAddress = $_SERVER['REMOTE_ADDR'];
$thisquery = "SELECT * FROM categoryLikes WHERE ipAddress = '$ipAddress' AND mapID = '$mapID'";
$q = $DB1->query($thisquery);
$results = $q->result_array();
foreach ($q->result() as $row)
{
$liked = $row->liked;
}
$buttontext = 'Like';
$buttonimage = "1";
if ($liked == "1") {
$buttontext = 'Unlike';
$buttonimage = "2";
}
if($_POST['like']) {
if ($liked == "1") {
$thisquery = "DELETE FROM categoryLikes WHERE mapID = '$mapID' AND ipAddress = '$ipAddress'";
$DB1->query($thisquery);
} else {
$thisquery = "INSERT INTO categoryLikes (categoryLikeID, mapID, liked, ipAddress) VALUES ('null', '$mapID', '1', '$ipAddress')";
$DB1->query($thisquery);
}
}
?>
As you can see, the only difference between the two pieces of code are the $mapID and the queries. Yet for some reason one works and the other doesn't. Anyone have any idea what might be going on? I'm thinking this is more a php problem than an ExpressionEngine one.
The real question, I think, is why the first example does work...
You are setting the state of the button based on the current value of $liked, but then if $_POST['like'] is set, you're changing the state of the system and the button is now incorrect. If you switch your code around and test for $_POST['like'] first, then set the value of $like after you act on it, your button will show the correct state.
Figured it out now, and also realised the first bit of code had the same issues as the second. What I did was I made the POST script set the button state, like so:
if($_POST['like']) {
if ($liked == "1") {
$thisquery = "DELETE FROM mapLikes WHERE mapID = '$mapID' AND ipAddress = '$ipAddress'";
$DB1->query($thisquery);
$buttontext = 'Like';
$buttonimage = "1";
} else {
$thisquery = "INSERT INTO mapLikes (mapLikeID, mapID, liked, ipAddress) VALUES ('null', '$mapID', '1', '$ipAddress')";
$DB1->query($thisquery);
$buttontext = 'Unlike';
$buttonimage = "2";
}
}
This query runs when I comment out the code above '}else{'. Have done so many like it and way more complex but I can't see where I'm tripping up.
(The page won't run like a syntax mistake. all vars match table)
Thanks.
Allen
if ($version == 'prem'){
$sql ="SELECT * FROM artistInfo WHERE user_id = '$user_id' AND artist_name = '$artist_name' ";
$res = mysql_query($sql);
$num = mysql_num_rows($res);
if($num>0){
while($row = mysql_fetch_array($res)){
$artist_id = $row['artist_id'];
} else {
mysql_query("INSERT INTO artistInfo (user_id, artist_name) VALUES ('$user_id', '$artist_name')");
$row_num = mysql_insert_id();
$artist_id = $user_id."-".$row_num;
mysql_query("UPDATE artistInfo SET artist_id = '$artist_id' WHERE row_num = '$row_num' ");
}
}
}
You're missing the closing } for the while loop.
missing } for while loop
if($num>0){
while($row = mysql_fetch_array($res)){
$artist_id = $row['artist_id'];
}
}
else {
I think that your last bracket got misplaced. you will also need to move the last } just before else
if ($version == 'prem'){
$sql ="SELECT * FROM artistInfo WHERE user_id = '$user_id' AND artist_name = '$artist_name' ";
$res = mysql_query($sql);
$num = mysql_num_rows($res);
if($num>0){
while($row = mysql_fetch_array($res)){
$artist_id = $row['artist_id'];
}
} else {
mysql_query("INSERT INTO artistInfo (user_id, artist_name) VALUES ('$user_id', '$artist_name')");
$row_num = mysql_insert_id();
$artist_id = $user_id."-".$row_num;
mysql_query("UPDATE artistInfo SET artist_id = '$artist_id' WHERE row_num = '$row_num' ");
}
}