fopen Data save in Database - php

I want to save datas from an fopen link into my database .. i get some help to use explode .. for that i got some code which should work .. but it doesn´t.
i have an error somewhere but i cannot find it (i cut out the database connection part to save space on screen)
stg_interface have the columns "uid, tan, kid, ziel, banner, verdienst, preis, aufendhalt, menge, reload, sponsor, werbeart, status, beschreibung, mailtext, gueltig"
// ***** Database Connection ***** //
// Interfacedaten laden
$sql_interface = mysql_query("SELECT * FROM stg_interface WHERE sponsor = 'allads4you' AND Werbeart = 'forcedbanner' LIMIT 1");
$output_interface = mysql_fetch_assoc($sql_interface);
$id_betreiber = $output_interface['id_betreiber'];
$id_seite = $output_interface['id_seite'];
$schnittstellencode = $output_interface['schnittstellencode'];
$max_reload = $output_interface['max_reload'];
$max_verguetung = $output_interface['max_verguetung'];
$min_verguetung = $output_interface['min_verguetung'];
$min_aufenthalt = $output_interface['min_aufenthalt'];
$restmenge = $output_interface['restmenge'];
$umrechnung = $output_interface['umrechnung'];
$eigenverdienst = $output_interface['eigenverdienst'];
$verguetungsoption = $output_interface['verguetungsoption'];
$eigenverdienst = $output_interface['eigenverdienst'];
$aufenthalt_zusatz = $output_interface['$aufenthalt_zusatz'];
$max_reload = $output_interface['max_reload'];
$restmenge = $output_interface['restmenge'];
$verhalten = $output_interface['verhalten'];
// extra vars for database values
$uid = '0';
$beschreibung = 'Allads4you Kampagne';
$mailtext = '';
$gueltig = '';
// Link öffnen
$fb=#fopen('http://www.allads4you.de/interface/?typ=forcedbanner&id='.$id_betreiber.'&sid='.$id_seite.'&pw='.$schnittstellencode.'&uebrig='.$restmenge.'&reload='.$max_reload.'&verguetung='.$min_verguetung.'','r');
// From here something works wrong and i cannot find the error
// standard vars to calculate
$file1 = "";
$file_fein = "";
$nummer = 0;
$spalten = 5;
$position = 0;
$einbuchen = true;
if($fb){
while($line=fgets($fb,1000)) {
$file_fein.= $line;
}
fclose($fb);
} // part to get data
$ausgabe = explode("|", $file_fein);
$for_zaehler = (count($ausgabe)/$spalten)-1;
for ($zaehler=0;$zaehler <= $for_zaehler;$zaehler++) {
$einbuchen = true;
//Reines einlesen vom Interface ANFANG
$k_id = (int)$ausgabe[$position];
$k_tan = md5('aa4y'.$k_id.'fb');
$k_name = addslashes($ausgabe[$position+1]);
$k_reload = (int)$ausgabe[$position+2]*3600;
$k_menge = (int)$ausgabe[$position+3];
$verguetung_check = $ausgabe[$position+4];
$verguetung_user = round(($ausgabe[$position+4]*$umrechnung) - (($ausgabe[$position+4]*$umrechnung) / 100 * $eigenverdienst),2);
$verguetung_webm = round($ausgabe[$position+4]*$umrechnung,2);
$k_banner_link = "http://www.allads4you.de/codes/forcedbannerview.php?id=".$id_betreiber."&bid=".$ausgabe[$position]."&aid=".$id_seite."";
$k_ziel_link = "http://www.allads4you.de/codes/forcedbannerklick.php?id=".$id_betreiber."&bid=".$ausgabe[$position]."&aid=".$id_seite."";
$k_aufenthalt = $min_aufenthalt;
//Reines einlesen vom Interface ENDE
//Prüfungsroutinen ANFANG only to check if the values are ok to put into database
//Vergütung
if($verguetung_webm >= $max_verguetung){
if($verguetungsoption == '2'){
$verguetung_webm = $max_verguetung;
$verguetung_user = round(($max_verguetung) - (($max_verguetung) / 100 * $eigenverdienst),2);
$einbuchen = true;
}else{
$einbuchen = false;
}
}
//Aufenthalt
if($einbuchen == true){
if($k_aufenthalt > $max_aufenthalt){
$einbuchen = false;
}
if($einbuchen = true){
if($k_aufenthalt < $min_aufenthalt){
$k_aufenthalt = $min_aufenthalt;
}
$k_aufenthalt+= $aufenthalt_zusatz;
}
}
//Reload
if($einbuchen == true){
if($k_reload > ($max_reload*3600)){
$einbuchen = false;
}elseif($k_reload == ''){
$einbuchen = false;
}elseif($k_reload == 0){
//24 Stunden reload falls 0 Stunden Reload
$k_reload = 86400;
}
}
//Menge
if($einbuchen == true){
if($k_menge < $restmenge){
$einbuchen = false;
}
}
//Prüfungsroutinen ENDE
//Vorberteiten für Ausgabe falls vorhanden ANFANG
if($einbuchen == true){
$nummer++;
}
if($verhalten <= '2' && $einbuchen == true){
$anzeige.='<tr bgcolor="#90EE90">
<td align="center">'.$nummer.'</td>
<td align="center">'.$k_id.'</td>
<td align="right">'.number_format($verguetung_webm,2,",",".").'</td>
<td align="right">'.number_format($verguetung_user,2,",",".").'</td>
<td align="center">'.($k_reload/3600).'</td>
<td align="right">'.number_format($k_menge,0,",",".").'</td>
<td align="center">'.$k_aufenthalt.'</td>
<td>'.substr($k_name,0,64).'</td>
</tr>';
}
//Vorbereiten für Ausgabe falls vorhanden ENDE
//Einbinden in die Datenbank falls eingestellt ANFANG
if($verhalten >= '2' && $einbuchen == true){
$vorhanden = db_query ("SELECT * FROM vms_gebuchte_werbung WHERE sponsor = 'allads4you' and `werbeart` = 'forcedbanner' and kid = '".$k_id."' LIMIT 1");
if (mysql_num_rows($vorhanden) > 0){
db_query ("UPDATE vms_gebuchte_werbung SET status = '1', banner = '".$k_banner_link."', ziel = '".$k_ziel_link."', menge = '".$k_menge."', preis = '".$verguetung_webm."', verdienst = '".$verguetung_user."', reload = '".$k_reload."', aufendhalt = '".$k_aufenthalt."' WHERE sponsor = 'allads4you' and `werbeart` = 'forcedbanner' and kid = '".$k_id."' and status = '0' LIMIT 1");
}else{
// part to save data into database
db_query ("INSERT INTO vms_gebuchte_werbung (uid,tan,kid,ziel,banner,preis,verdienst,aufendhalt,menge,reload,sponsor,werbeart, status, beschreibung, mailtext, gueltig) VALUES ('".$uid."', '".$k_tan."','".$k_id."','".$k_ziel_link."','".$k_banner_link."','".$verguetung_webm."','".$verguetung_user."','".$k_aufenthalt."','".$k_menge."','".$k_reload."','allads4you','forcedbanner','".$status."','".$beschreibung."','".$mailtext."','".$gueltig."')");
}
}
//Einbinden in die Datenbank falls eingestellt ENDE
$position=$position+$spalten;
}
db_close();
?>
I read it many times if i forget and ; or something like that but nothing .. can anyone give me some advice how to fix it ?

Related

record inserts, but doesn't update mysql

I'm having an issue trying to update my database. I'm able to insert if it doesn't exist, but it doesn't update the record if exists. I think is not finding the current values of the table. Any ideas?
<?php
function add_log($input_output, $l_sales, $l_enroll, $l_offers)
{
global $database;
$date = date('m-d-Y');
$l_sales = safety_filter($l_sales);
$l_enroll = safety_filter($l_enroll);
$l_offers = safety_filter($l_offers);
$cusersup = get_the_current_user('u_manager');
$cuseropm = get_the_current_user('u_opsmanager');
$cuserid = get_the_current_user('id');
$cuser = get_the_current_user('user_name');
if($input_output == 'input')
{
$query_call = mysql_query("SELECT 1 FROM $database->log WHERE l_date='$date' AND l_user_name='$cuser'");
if(mysql_num_rows($query_call) > 0)
{
while($list_calls = mysql_fetch_assoc($query_call))
{
$old_calls = $list_calls['l_calls'];
$old_sales = $list_calls['l_sales'];
$old_enroll = $list_calls['l_enroll'];
$old_offers = $list_calls['l_offers'];
}
$update = mysql_query("UPDATE $database->log SET
l_call=[$old_calls] + [1],
l_sales=[$old_sales] + [$l_sales],
l_enroll=[$old_enroll] + [$l_enroll],
l_offers=[$old_offers] + [$l_offers]
WHERE l_date='$date' AND l
_user_name='$cuser'");
if(mysql_affected_rows() > 0){return true; }
else { if($update == true){ return true; } else { return false; } }
}
else
{
mysql_query("INSERT INTO $database->log (l_date, l_user_name, l_calls, l_sales, l_enroll, l_offers) VALUES ('$date', '$cuser', '1', '$l_sales', '$l_enroll', '$l_offers')");
if(mysql_affected_rows() > 0){return true; }
else{ return false; }
}
}
}
?>
I think the problem is that you are selecting 1 ("SELECT 1 FROM..."), so you are never getting any of the values from the table (just the number "1"). This tells you something is there, but doesn't actually return any values.
Try selecting all fields ("SELECT * FROM..."), or at least list the fields you need to do the update.

depending on condition show error message in php

i have code like this
<?php
require('../config.php');
require_once($CFG->dirroot . '/user/editlib.php');
$errorMessage = '';
$successMessage = '';
if(isset($_SESSION['successMessage']))
{
$successMessage = $_SESSION['successMessage'];
unset($_SESSION['successMessage']);
}
if (isset($_POST['register'])) {
if(!preg_match("/^(?=.*[0-9])(?=.*[a-z])(\S+)$/i", $_POST['password']))
{
$errorMessage="don't allow spaces";
}
$errors = array();
$data = array();
$chk_sql = "SELECT * FROM {user} u where username = ?";
if (!empty($chk_sql) ) {
$errorMessage='Username already taken';
}
if(!$chk_username = $DB->get_record_sql($chk_sql, array($_POST['username'])) )
{
$secret = $_POST['secret'];
$access_code_sql = "SELECT * FROM {accesscode} WHERE random_no= ? and status=1";
if($chk_secret = $DB->get_record_sql($access_code_sql, array($secret)) )
{
if ( $chk_secret->used >= $chk_secret->number ) {
$errorMessage = "your access code limit completed..";
}
else
{
$cadminid = $chk_secret->cadmin_id;
$clientid = $chk_secret->clientid;
$DB->execute("UPDATE {accesscode} SET used = used+1 WHERE random_no = '$secret'");
$insert_record = new stdClass();
$insert_record->firstname = $_POST['firstname'];
$insert_record->lastname = $_POST['lastname'];
$insert_record->username = $_POST['username'];
$insert_record->secret = $secret;
$insert_record->password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$insert_record->timecreated = time();
$insert_record->maildigest = $cadminid;
$insert_record->maildisplay = $clientid;
$insert_record->idnumber = 1;
$insert_record->mnethostid = 1;
$insert_record->confirmed = 1;
$insert_record->email = $_POST['email'];
if($result = $DB->insert_record('user', $insert_record))
{
$_SESSION['successMessage'] = "record created successfully";
header('Location: register.php');
}
else
$errorMessage = "error! can you please try again";
}
}
else
$errorMessage = "your access code is wrong..";
}
}
?>
so i want to write condition like another if condition
if ( $chk_secret->status='0' ) {
$errorMessage = "your access code deactivated..";
}
if not they can register
i tried..but i didn't get idea where i have to add that if..
before i have condition like if number>used it will show some error message like your accesscode limit completed
can anyone help me..
thanks in advance..
= is for value assignment
== is compare two operands
so you need to change
if ( $chk_secret->status='0' ) {
to
if ( $chk_secret->status=='0' ) {
UPDATE:
your query SELECT * FROM {accesscode} WHERE random_no= ? and status=1
which means it going to return only status == 1
you can check with number of rows returned is ZERO then through status zero error message.
Or else
Get rows only based on random_no exists and then check status key

PHP API returning wrong response for android app

I am creating a API for android developer in PHP in which he want to delete some values from database and want to show a message after that.
Now the problem is this data is deleting successfully but this API always shows else part message after complete the process. If I remove the else part its return the null which crash the android app. So I just want to give a proper json message to the android developer
Here is the code which I am trying
if($clear_my_property == "yes" && $clear_my_requirement == "yes" && $all_of_these == "yes" && $user_name_id == $user_name_id1)
{
$tables_count = array("property_for_sale","property_for_rent","cpo_post_requirements");
foreach($tables_count as $table_count)
{
$user_count = mysql_query("select * from $table_count where user_name = '$user_name'");
$total_user_count = mysql_num_rows($user_count);
if($total_user_count > 0)
{
$tables_data = array("property_for_sale","property_for_rent","cpo_post_requirements");
foreach($tables_data as $table_data)
{
$user_sql = mysql_query("delete from $table_data where user_name='$user_name'");
if($user_sql)
{
$response['success'] = 1;
$response['user']['error_msg'] = 'Clear Successfully All History!';
}
}
}
else
{
$response['success'] = 0;
$response['user']['error_msg'] = 'Record Not Found!';
}
}
}
I know there is something wrong with this logic. But I need expert advise where my logic is wrong and what I have to do make it success
Problem with your original code, is that you are setting success/failure inside the loop. One of the four table may/may not contain the username. And if the last table don't have that, then as per your logic you are getting "record not found" even if previous iteration of the loop deleted data from the tables where username exists.
<?php
$conn = mysqli_connect(.....);
if($clear_my_property == "yes" && $clear_my_requirement == "yes" && $all_of_these == "yes" && $user_name_id == $user_name_id1) {
$tables_count = array("property_for_sale","property_for_rent","cpo_post_requirements");
$userHistoryDeleted = 0;
foreach($tables_count as $table_count) {
//if history is found, then it will be deleted otherwise not
mysql_query("delete from $table_count where user_name = '$user_name'");
if(mysqli_affected_rows($conn)) {
$userHistoryDeleted = 1;
}
}
$msg = 'Record Not Found!';
if($userHistoryDeleted) {
$msg = 'Clear Successfully All History!';
}
$response['success'] = $userHistoryDeleted;
$response['user']['error_msg'] = $msg;
}
Change your code :
if($total_user_count > 0)
{
$tables_data = array("property_for_sale","property_for_rent","cpo_post_requirements");
foreach($tables_data as $table_data)
{
$user_sql = mysql_query("delete from $table_data where user_name='$user_name'");
if($user_sql)
{
$response['success'] = 1;
$response['user']['error_msg'] = 'Clear Successfully All History!';
}
}
}
else
{
$response['success'] = 0;
$response['user']['error_msg'] = 'Record Not Found!';
}
to this one
if($total_user_count > 0)
{
$tables_data = array("property_for_sale","property_for_rent","cpo_post_requirements");
foreach($tables_data as $table_data)
{
$user_sql = mysql_query("delete from $table_data where user_name='$user_name'");
}
$response['success'] = 1;
$response['user']['error_msg'] = 'Clear Successfully All History!';
}

Parse error: parse error in php

I have this php code that accesses the tables users and portfolio
However I keep running into Parse error: parse error in /Users/...../Sites/..../sell.php on line 28
If someone could help me out, it'd be of great help.
<?
// require common code
require_once("includes/common.php");
//check for errors
$error = false;
if (isset($_POST["submit"]))
{
if(empty($_POST["symbol"]))
{
$error = true;
$message = "Enter a name";
}
else if(empty($_POST["shares"]))
{
$error = true;
$message = "Enter the shares";
}
// check if user has the stocks and the shares
$id = $_SESSION["id"];
$symbol = $_POST["symbol"];
$sharesQuery = "SELECT shares FROM portfolio WHERE id = $id AND symbol = '$symbol' ";
else if(($shares = mysql_query($sharesQuery)) == false)
{
$error = true;
$message = "Don't have the stock";
}
// else, if everything checks out, delete it and increment the cash
else
{
$deleteQuery = "DELETE from portfolio WHERE id = $id AND symbol = '$symbol'";
mysql_query($deleteQuery);
$incrementQuery = "UPDATE users SET cash = cash + ($shares * lookup($symbol)->price) WHERE id = $id ";
mysql_query($incrementQuery);
//set the variables into session and then redicrect to sell2
$_SESSION["symbol"] = $_POST["symbol"];
$_SESSION["shares"] = $_POST["shares"];
redirect("sell2.php");
}
}
?>
From where is this else coming in? Add a }:
} else if(($shares = mysql_query($sharesQuery)) == false)
Your final code will be:
<?
// require common code
require_once("includes/common.php");
//check for errors
$error = false;
if (isset($_POST["submit"])) {
if (empty($_POST["symbol"])) {
$error = true;
$message = "Enter a name";
}
else if (empty($_POST["shares"])) {
$error = true;
$message = "Enter the shares";
}
// check if user has the stocks and the shares
$id = $_SESSION["id"];
$symbol = $_POST["symbol"];
$sharesQuery = "SELECT shares FROM portfolio WHERE id = $id AND symbol = '$symbol' ";
} else if (($shares = mysql_query($sharesQuery)) == false) {
$error = true;
$message = "Don't have the stock";
}
// else, if everything checks out, delete it and increment the cash
else {
$deleteQuery = "DELETE from portfolio WHERE id = $id AND symbol = '$symbol'";
mysql_query($deleteQuery);
$incrementQuery = "UPDATE users SET cash = cash + ($shares * lookup($symbol)->price) WHERE id = $id ";
mysql_query($incrementQuery);
//set the variables into session and then redicrect to sell2
$_SESSION["symbol"] = $_POST["symbol"];
$_SESSION["shares"] = $_POST["shares"];
redirect("sell2.php");
}
?>
On line 28:
else if(($shares = mysql_query($sharesQuery)) == false)
You need a bracket to close the current if statement before doing an else if:
<?
// require common code
require_once("includes/common.php");
//check for errors
$error = false;
// check if user has the stocks and the shares
$id = $_SESSION["id"];
$symbol = $_POST["symbol"];
$sharesQuery = "SELECT shares FROM portfolio WHERE id = $id AND symbol = '$symbol' ";
if (isset($_POST["submit"])){
if(empty($_POST["symbol"])){
$error = true;
$message = "Enter a name";
}else if(empty($_POST["shares"])){
$error = true;
$message = "Enter the shares";
}else if(($shares = mysql_query($sharesQuery)) == false){
$error = true;
$message = "Don't have the stock";
}else{
$deleteQuery = "DELETE from portfolio WHERE id = $id AND symbol = '$symbol'";
mysql_query($deleteQuery);
$incrementQuery = "UPDATE users SET cash = cash + ($shares * lookup($symbol)->price) WHERE id = $id ";
mysql_query($incrementQuery);
//set the variables into session and then redicrect to sell2
$_SESSION["symbol"] = $_POST["symbol"];
$_SESSION["shares"] = $_POST["shares"];
redirect("sell2.php");
}
}
?>
It was silly mistake on my part, the declarations for id and symbol should have been at the top of the condition, right after isset,
Thanks for the speedy answers

Insert on duplicate key update returns value of 0

I am creating a web page where you can edit local listing sites for clients. I am using the INSERT ... ON DUPLICATE KEY UPDATE syntax in mysql and php. Below is the code:
edit-listings-exec.php
<?
//Array to store validation errors
$errmsg_arr = array();
//Validation error flag
$errflag = false;
$member_id = $_SESSION['MEMBER_ID'];
$userresult = mysqli_query($link, "SELECT username
FROM members
WHERE member_id = '$member_id'");
$row = mysqli_fetch_array($userresult);
$username = $row['username'];
//run a for loop to check for duplicates
for ($i=1; $i < 10; $i++) {
$selectbox = $_POST['listing'.$i];
for ($j=1; $j < 10; $j++) {
if ($j != $i) {
if ($_POST['listing'.$j] === $selectbox && $_POST['listing'.$j] != "") {
$errmsg_arr[] = "Duplicate listing source '$selectbox'";
$errflag = true;
break;
}
}
}
}
//if there are input validations redirect back to main page
if($errflag) {
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
session_write_close();
header("location: edit-listings.php");
exit();
}
//run loop to define post variables
for ($k=1; $k < 10; $k++) {
${'listing'.$k} = mysqli_real_escape_string($link, $_POST['listing'.$k]);
${'listing'.$k.'_site'} = mysqli_real_escape_string($link, $_POST['listing'.$k.'_site']);
}
//insert listings into local_listings table for the user
$qry = "INSERT INTO local_listings (
member_id,
username,
listing1,
listing1_site,
listing2,
listing2_site,
listing3,
listing3_site,
listing4,
listing4_site,
listing5,
listing5_site,
listing6,
listing6_site,
listing7,
listing7_site,
listing8,
listing8_site,
listing9,
listing9_site)
VALUES (
'$member_id',
'$username',
'$listing1',
'$listing1_site',
'$listing2',
'$listing2_site',
'$listing3',
'$listing3_site',
'$listing4',
'$listing4_site',
'$listing5',
'$listing5_site',
'$listing6',
'$listing6_site',
'$listing7',
'$listing7_site',
'$listing8',
'$listing8_site',
'$listing9',
'$listing9_site')
ON DUPLICATE KEY UPDATE
listing1 = '$listing1',
listing1_site = '$listing1_site',
listing2 = '$listing2',
listing2_site = '$listing2_site',
listing3 = '$listing3',
listing3_site = '$listing3_site',
listing4 = '$listing4',
listing4_site = '$listing4_site',
listing5 = '$listing5',
listing5_site = '$listing5_site',
listing6 = '$listing6',
listing6_site = '$listing6_site',
listing7 = '$listing7',
listing7_site = '$listing7_site',
listing8 = '$listing8',
listing8_site = '$listing8_site',
listing9 = '$listing9',
listing9_site = '$listing9_site'";
$result = mysqli_query($link, $qry);
if (mysqli_affected_rows($result) < 1) {
$errmsg_arr[] = "Unable to insert listings for $username";
$_SESSION['ERRMSG_ARR'] = $errmsg_arr;
header ("location: edit-listings.php");
}
elseif (mysqli_affected_rows($result) === 1) {
$_SESSION['RESULT'] = "New listings for '$username' inserted successfully";
header ("location: edit-listings-success.php");
}
elseif (mysqli_affected_rows($result) === 2) {
$_SESSION['RESULT'] = "Listings for '$username' have been updated";
header ("location: edit-listings-success.php");
}
?>
The data is successfully inserting into the local_listings table but mysqli_affected_rows always returns a value of 0. It should be returning a value of 1 if it inserts a new row or 2 if it simply updates. Any ideas why this is happening?
Try this:
if (mysqli_affected_rows($link) < 1) {
... your code
}
take a look here

Categories