adding php if condition in mysql query - php

i am trying to add an if condition to mysql query to only update a certain field if condition is met, here is my code below, but i keep getting this error
ERROR
{ "error": { "message":SQLSTATE[HY093]: Invalid parameter number:
parameter was not defined } }
CODE
$ok = 1;
$sql = "UPDATE users SET
fn = :first,
ln = :last
";
if($ok == 1){
$sql .= ",phone = :phone";
}
$sql .= "WHERE users.id = :id";

Keep space between your concatenation,
$ok = 1;
$sql = "UPDATE users SET
fn = :first,
ln = :last";
if($ok == 1){
$sql .= ", phone = :phone ";
}
$sql .= " WHERE users.id = :id";

I think you are missing a space
$sql =
"UPDATE users SET
fn = :first,
ln = :last
,phone = :phoneWHERE users.id = :id"

Related

Retrieve data from one table and insert/update in another table in mysql

I want to retrieve data from three tables using some condition and then insert/update in another three tables. This is a very simple process, the script of which is developed in php. But the catch is that the records retrieved from each table is 100k+. PHP script runs only with small number of records and gives time out error for large data. Can anyone please suggest how to solve this issue. All the three tables data needs to be fetched at runtime. Below is my php script which gives timeout error
switch($action){
case 'tequipebudget':
$oldPresta = budPrestaDataTransfer::getOldPresta('tequipebudget');
$budProviderBudget = budPrestaDataTransfer::updatebudProviderBudget($oldPresta, 'budproviderbudget', 3);
break;
case 'tequipebudgetjum':
$oldPrestaJum = budPrestaDataTransfer::getOldPresta('tequipebudgetjum');
$budProviderBudgetJum = budPrestaDataTransfer::updatebudProviderBudget($oldPrestaJum, 'budproviderbudgetjum', 3);
break;
case 'tequipebudgetavhisto':
$oldPrestaAvHisto = budPrestaDataTransfer::getOldPresta('tequipebudgetavhisto');
$budProviderBudgetAvHisto = budPrestaDataTransfer::updatebudProviderBudget($oldPrestaAvHisto, 'budproviderbudgetavhisto', 3);
break;
}
static public function getOldPresta($table) {
$sql = "SELECT Annee, CodeEntite, CodeProjet, MtBudgetAEquipeKE, projet_id";
if($table == 'tequipebudgetavhisto') {
$sql .= " ,avenant_id ";
}
$sql .= " FROM ".$table." WHERE Annee < 2020 ";
$dbObj = budPDO::getInstance();
$prestaList = $dbObj->getAllResults($sql);
return $prestaList;
}
static public function updatebudProviderBudget($prestaList, $table, $autreId) {
foreach($oldPresta as $key=>$val) {
$sql = "SELECT count(*) as cnt FROM ".$table." WHERE Annee = '".$val['Annee']."' AND CodeEntite = '".$val['CodeEntite']."' AND
CodeProjet = '".$val['CodeProjet']."' AND projet_id = '".$val['projet_id']."' AND provider_id = '".$oldPresta['AuterId']."' ";
$dbObj = budPDO::getInstance();
$res = $dbObj->getOneRow($sql);
if($res['cnt'] == 0){ // record does not exists in table
$update = "INSERT INTO ".$table." SET Annee = '".$val['Annee']."', CodeEntite = '".$val['CodeEntite']."',
CodeProjet = '".$val['CodeProjet']."', cost = '".$val['MtBudgetAEquipeKE']."' ,
projet_id = '".$val['projet_id']."', provider_id = '".$autreId."',
addedon_date = '".NOW_CONST."' ";
if($table == 'budproviderbudgetavhisto') {
$update .= " ,avenant_id= '".$val['avenant_id']."' ";
}
}else {
$update = "UPDATE ".$table." SET Annee = '".$val['Annee']."', CodeEntite = '".$val['CodeEntite']."',
CodeProjet = '".$val['CodeProjet']."', cost = '".$val['MtBudgetAEquipeKE']."' ,
projet_id = '".$val['projet_id']."', provider_id = '".$autreId."',
modifiedon_date = '".NOW_CONST."' ";
if($table == 'budproviderbudgetavhisto') {
$update .= " ,avenant_id= '".$val['avenant_id']."' ";
}
$update .= " WHERE Annee = '".$val['Annee']."' AND CodeEntite = '".$val['CodeEntite']."' AND
CodeProjet = '".$val['CodeProjet']."' AND projet_id = '".$val['projet_id']."' AND provider_id = '".$autreId."' ";
if($table == 'budproviderbudgetavhisto') {
$update .= " AND avenant_id= '".$val['avenant_id']."' ";
}
}
//echo "update -- " . $update. "<br><br>";
$sth = $dbObj->pdo->prepare($update);
$exec = $sth->execute();
}
}
You could increase the timeout in your PHP settings.
ini_set('max_execution_time','{number of seconds}');
Then you will probably also have to increase the memory limit.
ini_set('memory_limit', '2GB');
But it would be better to leave large data logic to the database. So if I were you, I would write a stored procedure / function and execute it with PHP only exec

QUERY FAILED.. error in your SQL syntax;.. check MariaDB for the right syntax to use near ''customer_pass' = '899b573719facc368f32770ea0b68e32'

I'm trying to create a sign up form, it was working fine until I tried to add md5 to the password field set, I'm not sure why the Query failed. Any help would be much appreciated.
function sign_up(){
if(isset($_POST['register'])){
$c_email = escape_string($_POST['c_email']);
$c_name_first = escape_string($_POST['c_name_first']);
$c_name_last = escape_string($_POST['c_name_last']);
$c_pass = escape_string($_POST['c_pass']);
$c_image = escape_string($_FILES['c_image']['name']);
$c_image_tmp = escape_string($_FILES['c_image']['tmp_name']);
$c_address = escape_string($_POST['c_address']);
$c_address_details = escape_string($_POST['c_address_details']);
$c_city = escape_string($_POST['c_city']);
$c_state = escape_string($_POST['c_state']);
$c_zip = escape_string($_POST['c_zip']);
$c_contact = escape_string($_POST['c_phone']);
move_uploaded_file($c_image_tmp, "customer/customer_images/$c_image");
$query = query("SELECT customer_id FROM customers WHERE customer_email = '{$c_email}'");
confirm($query);
if(mysqli_num_rows($query) > 0){
set_message("This email or username is taken");
}else {
$insert_c = query("INSERT INTO customers (customer_firstname,customer_lastname,customer_address,c_addr_details,customer_email,customer_pass,customer_state,customer_city,customer_zip,customer_phone,customer_image) VALUES ('$c_name_first','$c_name_last','$c_address','$c_address_details','$c_email','$c_pass','$c_state','$c_city','$c_zip','$c_contact','$c_image')");
confirm($insert_c);
}
$query = "UPDATE user SET 'customer_pass' = '".md5(md5(last_id()).$c_pass)."' WHERE 'customer_id' = '".last_id()."'";
$send_update_query = query($query);
confirm($send_update_query);
set_message_success("Sign up successful!");
}
}
Try
$query = 'UPDATE user SET customer_pass = '.md5(md5(last_id()).$c_pass).' WHERE customer_id = '.last_id();
Check you string when you use " or '

How to write a string variable inside mysql query?

I want to implement this query :
if(x=1){
$update = "close = '$date'";
}
else {
$update = "open = '$date'";
}
$query = "Update table1 set $update where id=100";
mysql_query($query);
but I got an error, the Mysql can't execute the query ?
<?php
if($x==1){
$update = "close = '".$date."'";
}
else {
$update = "open = '".$date."'";
}
$query = "update table1 set $update where id=100";
mysql_query($query);
?>
use this
<?php
if(x==1){
$update = "close = '$date'";
}
else {
$update = "open = '$date'";
}
$query = "Update table1 set '".$update."' where id=100";
mysql_query($query);
?>
try to use this code........
<?php
if($x==1){ // use $ for variables
$update = "close = '".$date."' "; //always concatenate variables
}
else {
$update = "open = '".$date."' ";
}
$query = "Update table1 set '".$update."' where id=100";
mysql_query($query, $connection); // don`t forget to add mysql connection
?>
Put your query in the quotes. Try following :
if(x==1){
$update = "close='".$date."'";
}
else {
$update = "open = '".$date."'";
}
$query = "Update table1 set ".$update." where id=100";
mysql_query($query) or die(mysql_error());
Put mysql_error() to check what error are you getting from mysql
Replace
$query = Update table1 set $update where id=100;
to
$query = "Update table1 set ".$update." where id=100";

Function not updating database

I recently was fiddling with my code and ran into an issue and i cannot figure out where it is. I added the "Update site_sync SET test = test+1" code last, which is firing perfectly fine. but everything else seems to be catching somewhere and it's not throwing errors anywhere on my site.
function skynetInfect($db)
{
$sql = "SELECT skyNet FROM site_sync;";
$sql .= "UPDATE site_sync SET test = test+1;";
$sql .= "SELECT count(*) FROM starinformation WHERE starOwner = -1;";
$que = $db->prepare($sql);
try { $que->execute();
$que->nextRowset();
while($row = $que->fetch(PDO::FETCH_BOTH))
{
if($row[0] == 'on')
{
$que->nextRowset();
$row2 = $que->fetch(PDO::FETCH_BOTH);
$x = $row2[0];
echo $x;
$sql = "UPDATE starinformation SET starOwner = -1 WHERE starOwner <> -1 ORDER BY rand() LIMIT {$x};";
$que = $db->prepare($sql);
$que->bindParam('id', $rand);
try{ $que->execute();}catch(PDOException $e) { echo $e->getMessage();}
}
}
}catch(PDOExceptions $e) { echo $e->getMessage();}
}
You are overwriting $que inside the inner if conditional. That happens here:
$sql = "UPDATE starinformation SET starOwner = -1 WHERE starOwner <> -1 ORDER BY rand() LIMIT {$x};";
$que = $db->prepare($sql); // use a variable name other than $que here
Also, is there any reason you are actually using PDO::FETCH_BOTH?
$sql = "SELECT skyNet FROM site_sync;";
$sql .= "UPDATE site_sync SET test = test+1;";
$sql .= "SELECT count(*) FROM starinformation WHERE starOwner = -1;";
$que = $db->prepare($sql);
I don't think you can execute multiple queries using PDO
$que = $db->prepare($sql);
I don't see any parameter in your Query so why the prepare?

Run few sql query with php concatenation operator

I've a html form where 3 field exist which process by PHP.
1) Password 2) Upload Logo and 3) Tag Line field.
So user can upload either password or logo or tag line or all field or 2 field. So that I run following query. But it's not Edit the DB. Is that ok or any suggestions of your ?
$update = mysql_query("UPDATE e_users SET email = '$email'");
if(!empty($pass_post))
{
$update .= "pass = '$pass', salt = '$random_salt'";
}
if(!empty($logo_text))
{
$update .= "logo_text = '$logo_text'";
}
if(!empty($file))
{
$update .= "logo = '$student_pic'";
}
$update .= "WHERE uname = '$ses_user'";
Thank You.
You left out the commas when you appended additional fields to the query, and you should have a space before WHERE (not strictly necessary in this case, since all your assignments end with a quote, but it's a good idea). And you were trying to concatenate to a mysql result resource, not the SQL string.
$update = "UPDATE e_users SET email = '$email'";
if(!empty($pass_post))
{
$update .= ", pass = '$pass', salt = '$random_salt'";
}
if(!empty($logo_text))
{
$update .= ", logo_text = '$logo_text'";
}
if(!empty($file))
{
$update .= ", logo = '$student_pic'";
}
$update .= " WHERE uname = '$ses_user'";
$result = mysql_query($update);

Categories