How to Escaping Single Quotes With mysqli_real_escape_string? - mysql

My code is like this :
My array of city (echo '<pre>';print_r($city);echo '</pre>';die();) :
Array
(
[0] => Array
(
[CityCode] => 14879
[CityName] => Soldeu
)
[1] => Array
(
[CityCode] => 14881
[CityName] => Ari'nsal
)
[2] => Array
(
[CityCode] => 14882
[CityName] => El Tarter
)
[3] => Array
(
[CityCode] => 14883
[CityName] => Grau Roig
)
[4] => Array
(
[CityCode] => 175198
[CityName] => Llorts
)
)
In city code : 14881, city name : Ari'nsal
It's single quote in string.
I try code like this :
$date = date('Y-m-d H:i:s');
$sql = "INSERT INTO hotel_search_city (nation_code, city_code, city_name, created_at, updated_at) values ";
$valuesArr = array();
foreach($city as $row){
$nation_code = $value->nation_code;
$city_code = $row['CityCode'];
$city_name = mysqli_real_escape_string($row['CityName']);
$created_at = $date;
$updated_at = $date;
$valuesArr[] = "('$nation_code', '$city_code', '$city_name', '$created_at', '$updated_at')";
}
$sql .= implode(',', $valuesArr);
$query = $sql;
$this->db->query($query);
There exist error like this : Message: mysqli_real_escape_string() expects exactly 2 parameters, 1 given....
Any solution to solve my problem?
Thank you very much

$date = date('Y-m-d H:i:s');
$sql = "INSERT INTO hotel_search_city (nation_code, city_code, city_name, created_at, updated_at) values (?, ?, ?, ?, ?)";
foreach ($city as $row) {
$nation_code = $value->nation_code;
$city_code = $row['CityCode'];
$city_name = $row['CityName'];
$created_at = $date;
$updated_at = $date;
$fields = array($nation_code, $city_code, $city_name, $created_at, $updated_at);
$this->db->query($query, $fields);
}
or
$date = date('Y-m-d H:i:s');
foreach ($city as $row) {
$fields = array(
'nation_code' => $value->nation_code,
'city_code' => $row['CityCode'],
'city_name' => $row['CityName'],
'created_at' = $date,
'updated_at' = $date
);
$this->db->insert('hotel_search_city', $fields);
}

Related

Need to iterate through array to capture all rows - currently only saving the number in the [0] or first record

I could use some help with storing an array. The process defined below is working and saving a record! It saves the record defined in the [0] column. If a zero is there, it saves record one, if a 1 is there it saves record two etc.
Now I need to automate this so it will do that for EACH RECORD as it iterates through. Perhaps a second foreach statement to tie them together? Or a for loop with an integer variable? I have tried many combos and it is still not working.
//0 = feeobject row[]: to keyfield=>val
//Array ([0] => 0)
foreach($AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0] as $key=>$data ) {
$PaymentID = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['PaymentID'];
$ClientID = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['ClientID'];
$ClientName = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['ClientName'];
$Account_ID = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['Account_ID'];
$State = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['State'];
$PaymentDate = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['PaymentDate'];
$Amount = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['Amount'];
$CheckID = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['CheckID'];
$CreditorName = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['CreditorName'];
$DRC_ClientID = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['DRC_ClientID'];
$DRC_TransactionID = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'][0]['DRC_TransactionID'];
$sql = "INSERT INTO tblAgencyFees (PaymentID, ClientID, ClientName, Account_ID, State, PaymentDate, Amount, CheckID, CreditorName, DRC_ClientID, DRC_TransactionID) VALUES ('".$PaymentID."', '".$ClientID."', '".$ClientName."', '".$Account_ID."', '".$State."', '".$PaymentDate."', '".$Amount."', '".$CheckID."', '".$CreditorName."', '".$DRC_ClientID."', '".$DRC_TransactionID."')";
mysqli_query($connection, $sql);
}
//}
//}
echo $sql;
$res = mysqli_query($connection, $sql);
If( !$res ) exit( mysqli_error($connection) );
The array:
Array (
[AgencyFeesGetInfoResult] => Array (
[FeeObject] => Array (
[0] => Array (
[PaymentID] => 816
[ClientID] => 1141
[ClientName] => Ortega, Daniel
[Account_ID] => 2222100000010717
[State] => OK
[PaymentDate] => 2019-07-31T00:00:00
[Amount] => 8.0000
[CheckID] => 10
[CreditorName] => 0
[DRC_ClientID] => 1195
[DRC_TransactionID] =>
)
[1] => Array (
[PaymentID] => 817
[ClientID] => 1141
[ClientName] => Ortega, Daniel
[Account_ID] => 2222100000010717
[State] => OK
[PaymentDate] => 2019-07-31T00:00:00
[Amount] => 92.0000
[CheckID] => 11
[CreditorName] => MF
[DRC_ClientID] => 1195
[DRC_TransactionID] =>
)
[2] => Array (
[PaymentID] => 847
[ClientID] => 1141
[ClientName] => Ortega, Daniel
[Account_ID] => 2222100000010717
[State] => OK
[PaymentDate] => 2019-08-14T13:21:49.23
[Amount] => 195.0000
[CheckID] => 13
[CreditorName] => MF
[DRC_ClientID] => 1195
[DRC_TransactionID] =>
)
)
)
)
You can do a simple loop over your data and do multiple inserts.
$arrFeeObjects = $AgencyFeesBasicArray['AgencyFeesGetInfoResult']['FeeObject'];
if(count($arrFeeObjects) > 0)
{
foreach($arrFeeObjects as $intKey => $data)
{
$sql = "INSERT INTO tblAgencyFees (PaymentID, ClientID, ClientName, Account_ID, State, PaymentDate, Amount, CheckID, CreditorName, DRC_ClientID, DRC_TransactionID)
VALUES ('". $data['PaymentID'] ."', '". $data['ClientID'] ."', '". $data['ClientName'] ."', '". $data['Account_ID'] ."', '". $data['State'] ."', '". $data['PaymentDate'] ."', '". $data['Amount'] ."', '". $data['CheckID'] ."', '". $data['CreditorName'] ."', '". $data['DRC_ClientID'] ."', '". $data['DRC_TransactionID'] ."')";
mysqli_query($connection, $sql);
}
}

how to update table in silex (PHP micro-framework)?

I have used $sql to insert data into table using below code in model
$sql = "INSERT into $database";
$sql .= ( ;
foreach($data as $key => $value)
{
$sql .= "$key, ";
}
$sql = substr($sql, 0);
$sql .= ) VALUES ( ;
foreach($data as $key => $value)
{
$sql .= "'$value', ";
}
$sql = substr($sql, 0);
$sql .=);
my problem is how to update sametable using silex
according to silex documentation you should use doctrine ORM provider:
$app->register(new Silex\Provider\DoctrineServiceProvider(), array(
'db.options' => array(
'driver' => 'pdo_sqlite',
'path' => __DIR__.'/app.db',
),
));
$app['db']->insert('tablename', array(
'key1' => 'val1',
'key2' => 'val2',
'key3' => 'val3'));

insert data on wordpress database with a foreach

On Wordpress, i want to insert data in my database on a table i created for purpose, i'm using the global $wpdb and insert.
I've got many more data to insert, is that possible to insert a foreach or any loop inside my array?
Thank you in advance,
jean-Charles
global $wpdb;
$name="JC";
$email="jcdarocha#gmail.com";
$website="www.jcdarocha.co.uk";
$country="UK";
$message="hello World";
global $wpdb;
$wpdb->insert( 'my_table',
array(
'$name' => '$name',
'$email' => $email,
'$website' => $website,
'$country' => $country,
'$message' => $message
),
array( '%s', '%d' ) );
Use $wpdb->query and concat all data into a string
$insert = "INSERT INTO my_table (name, email, website, country, message) VALUES ";
foreach( $datas as $data ) {
$insert .= sprintf("('%s','%s','%s','%s','%s'),", $data['name'], $data['email'], $data['website'] , $data['country'], $data['message'] )
}
$wpdb->query( rtrim($insert, ','));
global $wpdb;
$name="JC";
$email="jcdarocha#gmail.com";
$website="www.jcdarocha.co.uk";
$country="UK";
$message="hello World";
global $wpdb;
$wpdb->insert( 'my_table',
array(
'name' => $name,
'email' => $email,
'website' => $website,
'country' => $country,
'message' => $message
) );

SELECT and list children and parent - alternative [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
SELECT and list children and parent
I received this answer from xception:
https://stackoverflow.com/a/12770593/445820
Unfortunately, GROUP_CONCAT isn't the solution for me and the other question is too extended, therefore I am re-asking this question.
I need an alternative to that query with subqueries. Since I'm not good enough with queries, this task is beyond my abilities too.
Help please.
Here is the code:
// Prepare query
$columns = "c.rule_id, c.rule_title, GROUP_CONCAT(r.rule_description ORDER BY r.rule_position ASC SEPARATOR '~sep~') AS rule_desc, ";
$columns .= "GROUP_CONCAT(r.parse_bbcode ORDER BY r.rule_position ASC SEPARATOR '~sep~') AS bbcode, ";
$columns .= "GROUP_CONCAT(r.parse_links ORDER BY r.rule_position ASC SEPARATOR '~sep~') AS links, ";
$columns .= "GROUP_CONCAT(r.parse_smilies ORDER BY r.rule_position ASC SEPARATOR '~sep~') AS smilies";
$sql_array = array(
'SELECT' => $columns,
'FROM' => array(RULES_TABLE => 'c'),
'LEFT_JOIN' => array(
array(
'FROM' => array(RULES_TABLE => 'r'),
'ON' => 'r.parent_id = c.rule_id',
),
),
'WHERE' => 'c.parent_id = 0 AND r.public = 1',
'GROUP_BY' => 'c.rule_id',
'ORDER_BY' => 'c.cat_position',
);
$sql = $db->sql_build_query('SELECT', $sql_array);
$result = $db->sql_query($sql);
$cat_count = 1;
$alpha_count = 'abcdefghijklmnopqrstuvwxyz';
while ($row = $db->sql_fetchrow($result))
{
$template->assign_block_vars('rules', array(
'RULE_CATEGORY' => $row['rule_title'],
'ROW_COUNT' => $cat_count,
));
$rules_ary = explode('~sep~', $row['rule_desc']);
$parse_bbcode = explode('~sep~', $row['bbcode']);
$parse_links = explode('~sep~', $row['links']);
$parse_smilies = explode('~sep~', $row['smilies']);
$counter = 0;
foreach ($rules_ary as $key => $rule)
{
$uid = $bitfield = $options = '';
generate_text_for_storage($rule, $uid, $bitfield, $options, $parse_bbcode[$key], $parse_links[$key], $parse_smilies[$key]);
$template->assign_block_vars('rules.rule', array(
'RULE_DESC' => generate_text_for_display($rule, $uid, $bitfield, $options),
'ALPHA_COUNT' => $alpha_count{$counter},
));
$counter++;
}
$cat_count++;
}
$db->sql_freeresult($result);
Some of the functions might be unknown to you. FYI this is phpBB related code.
Sorry for not adding the code in the first time.
// Prepare query
$columns = "c.rule_id, c.rule_title, r.rule_description AS rule_desc, ";
$columns .= "r.parse_bbcode AS bbcode, ";
$columns .= "r.parse_links AS links, ";
$columns .= "r.parse_smilies AS smilies";
$sql_array = array(
'SELECT' => $columns,
'FROM' => array(RULES_TABLE => 'c'),
'LEFT_JOIN' => array(
array(
'FROM' => array(RULES_TABLE => 'r'),
'ON' => 'r.parent_id = c.rule_id',
),
),
'WHERE' => 'c.parent_id = 0 AND r.public = 1',
'ORDER_BY' => 'c.cat_position',
);
$sql = $db->sql_build_query('SELECT', $sql_array);
$result = $db->sql_query($sql);
$cat_count = 0;
$alpha_count = 'abcdefghijklmnopqrstuvwxyz';
$prev_rule_id = 0;
$r_rule_titles = array();
$rule_id = null;
while ($row = $db->sql_fetchrow($result))
{
if( $rule_id != $row['rule_id'] ) {
$rule_id = $row['rule_id'];
$cat_count++;
$counter = 0;
$template->assign_block_vars('rules', array(
'RULE_CATEGORY' => $row['rule_title'],
'ROW_COUNT' => $cat_count,
));
}
$uid = $bitfield = $options = '';
generate_text_for_storage($row['rule_desc'], $uid, $bitfield, $options, $row['bbcode'], $row['links'], $row['smilies']);
$template->assign_block_vars('rules.rule', array(
'RULE_DESC' => generate_text_for_display($row['rule_desc'], $uid, $bitfield, $options),
'ALPHA_COUNT' => $alpha_count{$counter},
));
$counter++;
}
$db->sql_freeresult($result);

FB:registration - array doesnt save any data

I did the Facebook Registration Plugin like in the official Facebook tutorial and with this code there it shows the total Array on screen:
if ($_REQUEST) {
echo '<p>signed_request contents:</p>';
$response = parse_signed_request($_REQUEST['signed_request'],
FACEBOOK_SECRET);
echo '<pre>';
print_r($response);
echo '</pre>';
} else {
echo '$_REQUEST is empty';
}
this brings me the following array:
signed_request contents:
Array
(
[algorithm] => HMAC-SHA256
[expires] => 1324xxxx400
[issued_at] => 132446xxx80
[oauth_token] => AAADRjT73VhwBALl6Gb3EVarvyGU7xxxxxxxxxxxxxxxxxxxSAUuoZAGlydkX2pH3
[registration] => Array
(
[name] => Philipp Mail
[email] => p.mail#xxxxde
[location] => Array
(
[name] => Munich, Germany
[id] => 1.1604xxxxxx286E+14
)
[birthday] => xx/xx/19x7
)
[registration_metadata] => Array
(
[fields] => [{'name':'name'}, {'name':'email'}, {'name':'location'}, {'name':'birthday'}]
)
[user] => Array
(
[country] => de
[locale] => de_DE
)
[user_id] => 10xxxxxxx5426
)
Now I changed it for storing several data to mysql:
if ($_REQUEST) {
echo '<p>signed_request contents:</p>';
$response = parse_signed_request($_REQUEST['signed_request'],
FACEBOOK_SECRET);
$name_arr = explode(' ',$name,2);
$vname = $name_arr[0];
$zname = isset($name_arr[1])?$name_arr[1]:'';
$email = $response["registration"]["email"];
$ort = $response["registration"]["location"]["name"];
$anrede = $response["registration"]["gender"];
$geburtstag = $response["registration"]["birthday"];
// Connecting to database
mysql_connect($dbhost, $dbuser, $dbpass) or die("MySQL Error: " . mysql_error());
mysql_select_db($dbname) or die("MySQL Error: " . mysql_error());
// Inserting into users table
$result = mysql_query("INSERT INTO REKRU_mem (mem_id, vname, zname, ort, email, userpass, chili, regdatum, geburtstag, fbuid)
VALUES
(NULL, '$vname', '$zname', '$ort', '$email', MD5('".$gesamtpass."'),'$chili', '0000-00-00 00:00:00', '$geburtstag', '$user_fbid')");
if($result){
// GOT RESULTS
}
else
{
// Error in storing
}
}
else
{
echo '$_REQUEST is empty';
}
When I look in mysql after a registration there is a new line but it saves only the actual registration time. Can anyone of U see what is my mistake?
do not use this code in a plublic Area! You can make a SQL-Injection (could also be the problem why you cannot insert).
Try this instead of the mysql_query:
$vname = mysql_real_escape_string($vname);
$zname = mysql_real_escape_string($zname);
$ort = mysql_real_escape_string($ort);
$email = mysql_real_escape_string($email);
$gesamtpass = mysql_real_escape_string($gesamtpass);
$chili = mysql_real_escape_string($chili);
$geburtstag = mysql_real_escape_string($geburtstag);
$user_fbid = mysql_real_escape_string($user_fbid);
$result = mysql_query("INSERT INTO REKRU_mem (mem_id, vname, zname, ort, email, userpass, chili, regdatum, geburtstag, fbuid)
VALUES (NULL, '".$vname."', '".$zname."', '".$ort."', '".$email."', MD5('".$gesamtpass."'),'".$chili."', '0000-00-00 00:00:00', '".$geburtstag."', '".$user_fbid."')");