I execute some PHP code .
After this, I want to send a value over post to another PHP site, which I want use it via $_POST. I don't need sessions or cookies or anyway.
else if ($tag == 'in_accepted') {
$coni=mysqli_connect("localhost","LOGIN","PW","movement");
$tablename = $_POST['tablename'];
$jsonString = $_POST['json'];
$jArray = json_decode($jsonString, true);
$comment = $_POST['comment'];
$result2 = mysqli_query($coni, "ALTER TABLE `$tablename` COMMENT = '$comment'");
$response["comment_succes"] = 1;
foreach($jArray as $key => $value){
$ean = $value["ean"];
//$trimmedean = substr($ean, 0, 3) .".". substr($ean, 3, 4) .".". substr($ean, 7, 4);
//$result = mysqli_query($coni,"INSERT INTO `$tablename` (unique_id, ean, accepted) VALUES ('$uuid', '$trimmedean', '0')"); // or die(mysql_error());
$result = mysqli_query($coni, "UPDATE `$tablename` SET accepted='1' where ean='$ean'");
}
if ($result > 0) {
$response["success"] = 1;
!!!!SEND $tablename to URL.php
echo json_encode($response);
}
else {
$response["error"] = 1;
echo json_encode($response);
}
Use CURL to post the data to the specified url.
Make a dynamic form and submit it with the js script/jquery automatically.
Related
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
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
$username= $_SESSION['username'];
require "connection.php";
// GET THE DATA FROM POST IF IT EXISTS
$data = isset($_POST['data']) ? $_POST['data'] : false;
// IF IT IS A VALID ARRAY THEN PROCESS IT
if (is_array($data)) {
// LOOP THOUGH EACH SUBMITTED RECORD
foreach($data as $id => $rec) {
// START AN UPDATE STRING
$updatestr = '';
// ADD FIELD / VALUES TO UPDATE STRING
foreach($rec as $fieldname => $value) {
if($fieldname == 'id'){
continue;
}
else{
$updatestr .= "`{$fieldname}` = '{$value}',";
}
}
// REMOVE THE TRAILING ,
trim($updatestr, ',');
$updatestr = rtrim($updatestr, ',');
// CREATE THE UPDATE QUERY USING THE ID OBTAINED FROM
// THE KEY OF THIS data ELEMENT
$query = "UPDATE `call` SET {$updatestr} WHERE id= '$id'";
// SEND IT TO THE DB
$result= mysqli_query($conn, $query);
}
echo "working";
}
else {
echo "not working";
}
?>
I have this code and it works perfectly, however I want to add
mysqli_real_escape_string But how can I do that to each variable since I don't know there exact information? I want it before it gets added to the query incase special characters were added
I also realized that my id never changes, it always stays one, whats the problem with that?
Granted I do not have access to PHP right now I believe this should work and get you on prepared statements.
<?php
foreach($data as $id => $rec) {
// START AN UPDATE STRING
$update_fields = array();
$bind_params_types = ''
$bind_params_values = array();
// ADD FIELD / VALUES TO UPDATE STRING
foreach($rec as $fieldname => $value) {
if($fieldname == 'id'){
continue;
}
else{
$update_fields[] = '{$fieldname} = ?';
$bind_params_types .= 's';
$bind_params_values[] = $value;
}
}
$update_fields = implode(',', $update_fields);
$bind_params_values = implode(',', $value);
// CREATE THE UPDATE QUERY USING THE ID OBTAINED FROM
// THE KEY OF THIS data ELEMENT
$query = "UPDATE `call` SET {$update_fields} WHERE id= '$id'";
if($stmt = mysqli_prepare($conn, $query)){
$stmt->bind_param($bind_params_types,$bind_params_values);
$stmt->execute();
} else {
echo "failed";
}
}
}
I've done addon in your code before updation i've esacpe the string
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
session_start();
$username= $_SESSION['username'];
require "connection.php";
// GET THE DATA FROM POST IF IT EXISTS
$data = isset($_POST['data']) ? $_POST['data'] : false;
// IF IT IS A VALID ARRAY THEN PROCESS IT
if (is_array($data)) {
// LOOP THOUGH EACH SUBMITTED RECORD
foreach($data as $id => $rec) {
// START AN UPDATE STRING
$updatestr = '';
// ADD FIELD / VALUES TO UPDATE STRING
foreach($rec as $fieldname => $value) {
if($fieldname == 'id'){
continue;
}
else{
$updatestr .= "`{$fieldname}` = '{$value}',";
}
}
// REMOVE THE TRAILING ,
trim($updatestr, ',');
$updatestr = rtrim($updatestr, ',');
$updatestr = mysqli_real_escape_string($conn, $updatestr);
// CREATE THE UPDATE QUERY USING THE ID OBTAINED FROM
// THE KEY OF THIS data ELEMENT
$query = "UPDATE `call` SET {$updatestr} WHERE id= '$id'";
// SEND IT TO THE DB
$result= mysqli_query($conn, $query);
}
echo "working";
}
else {
echo "not working";
}
?>
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 have beem retrieving the data from mysqli db by php.
$query = "SELECT id, parentId, firstName, middleName, lastName, mobileNumber, photo FROM person WHERE orgId='".$orgId."' ";
$sql = mysqli_query($con, $query);
$data=array();
if($rows_count!=0){
while ($rows_fetch = mysqli_fetch_assoc($sql))
{
var_dump($rows_fetch);
$info = $rows_fetch;
array_push($data, $info);
$isSuccessful = true;
$code = 200;
}
}else{
$isSuccessful = true;
$code = 202;
}
$response = array("resp"=>$data, "code"=>$code, "isSuccessful"=>$isSuccessful);
If blob field is empty, I am getting response. If blob is there, I am getting empty response
while ($rows_fetch = mysqli_fetch_assoc($sql))
{
//var_dump($rows_fetch);
$info = $rows_fetch;
$info['photo'] = base64_encode($rows_fetch['photo']);
array_push($data, $info);
$isSuccessful = true;
$code = 200;
}
I have changing blob to base64 code.
I'm trying to create a twitter style following system for my site. When ever the script below runs is adds an "Array" inside the database instead of just the user id of the person you're trying to follow. Dunno how to fix it.
<?
session_start();
# Connect to the mysql database
include_once "library/connect_to_mysql.php";
if(isSet($_POST['mem'])){
#filter everything but numbers for security
$mem1 = preg_replace('#[^0-9]#i', '', $_POST['mem']);
$mem2 = $mem1;
#Decode the Session IDX variable and extract the user's ID from it
$decryptedID = base64_decode($_SESSION['idx']);
$id_array = explode("p3h9xfn8sq03hs2234", $decryptedID);
$my_id = $id_array[1];
$sql = mysql_query("SELECT following_array FROM Members WHERE id='$my_id' LIMIT 1");
while($row = mysql_fetch_array($sql)) {
$following = $row["following_array"];
}
$followArry1 = explode(',', $following);
if (in_array($mem1, $followArry1)) {
exit();
}
if ($followArry1 != "") {
$followArry2 = "$followArry1,$mem2";
} else {
$followArry2 = "$mem2";
}
$UpdateArray = mysql_query("UPDATE Members SET following_array ='$followArry2' WHERE id='$my_id'") or die (mysql_error());
exit();
}else{
exit();
}
?>
Any help appreciated
/////// Updated Code ////////////////////
$followArry1 = explode(",", $following);
if (in_array($mem1, $followArry1)) { exit(); }
if ($followArry1 != "") {
$followArry1 = implode(',', $followArry1 + array($mem1));
} else {
$followArry1 = $mem1;
}
$UpdateArray = mysql_query("UPDATE myMembers SET following_array ='$followArry1' WHERE id='$my_id'") or die (mysql_error());
$followArry1 is an array, which converted to string shows "Array".
See proof here (live demo: http://ideone.com/tMndH):
$followArry1 = array('a', 'b', 'c'); // just an array
$result = "$followArry1"; // it is now string containing "Array"
Try the following (example here: http://ideone.com/h5ilz):
$followArry1 = array('a', 'b', 'c');
$mem = 'd';
$followArry2 = implode(',', $followArry1 + array($mem));
I also think that the problem is with adding $mem variable to $followyArry1 array.
Try this code...
if ($followArry1 != "") {
$followArry1[] = $mem1;
$followArry1 = implode(',', $followArry1);
} else {
$followArry1 = $mem1;
}