Data inserting blank into database - php

I am inserting data into the addresses of the customers in magento, I am able to add all the fields except the street field, would anyone know why?
The data is not entered, does not show any error, the other fields are all text or select, so they will normally, this field has only one label, I get the database label, I give a var_dump and it returns me the addresses, but does not add, I think I'm wrong on setStreet1, setStreet2, setStreet3, setStreet4.
Would I have to enter these values ​​in any different way? How do I set this data in the database? Would I have set them differently? I put the data in the bank normally by its name, first_name = Firstname, and here I am trying to do the same thing, but in street it is different is like [street] [0], street. Other data are like [first_name], [last_name]. Because he opened another [] would I have to add it in another way?
The way I pull this information is this way
'rua'=>$usuario_loaded->getStreet1(), 'numero'=>$usuario_loaded->getStreet2(), 'complemento'=>$usuario_loaded->getStreet3(), 'bairro'=>$usuario_loaded->getStreet4()
$i = 1;
$link = mysqli_connect('localhost','root','','x');
$link->set_charset("utf8");
$query = "SELECT * from endereços";
$select = mysqli_query($link, $query);
foreach ($select as $key => $selects) {
$link = mysqli_connect('localhost','root','','x');
$link->set_charset("utf8");
$query = "SELECT * from endereços";
$select = mysqli_query($link, $query);
while($row = mysqli_fetch_array($select)){
$teste = array(
$endereço_id = $row['endereço_id'],
$nome = $row['nome'],
$assinatura = $row['assinatura'],
$sobrenome = $row['sobrenome'],
$rua = $row['rua'],
$numero = $row['numero'],
$complemento = $row['complemento'],
$bairro = $row['bairro'],
$cidade = $row['cidade'],
$país = $row['país'],
$estado = $row['estado'],
$cep = $row['cep'],
$telefone = $row['telefone'],
);
var_dump($teste);
$customer = Mage::getModel("customer/address");
$customer ->setId($endereço_id)
->setFirstname($nome)
->setMiddlename($assinatura)
->setLastname($sobrenome)
->setStreet1($rua)
->setStreet2($numero)
->setStreet3($complemento)
->setStreet4($bairro)
->setCity($cidade)
->setCountryId($país)
->setRegionId($estado)
->setPostcode($cep)
->setTelephone($telefone)
try{
$customer->save();
}
catch (Exception $e) {
Zend_Debug::dump($e->getMessage());
}
}
}
Image

I did, I simply had to add the data with an array. Changing only this line
-> setStreet (array ($rua, $numero, $complemento, $bairro))

Related

I cannot populate an array with sql query data to export it as json object in php

I am new with php, I try to call the following function in order to populate an array of previously inserted data in mysql, but I get null.
function GetBusiness($con, $email)
{
$query = "SELECT * from Business WHERE B_EMAIL ='".$email."'";
$res = mysqli_query($con,$query);
$result_arr = array();
while($row = mysqli_fetch_array($res))
{
$result_arr[] = $row;
}
return $result_arr;
}
and then I try to construct my json object as..
$result_business = array();
$result_business = GetBusiness($con, $email);
$json['result'] = "Success";
$json['message'] = "Successfully registered the Business";
$json["uid"] = $result_business["id"];
$json["business"]["name"] = $result_business["name"];
$json["business"]["email"] = $result_business["email"];
but for even if the data are inserted successfully the part of $result_business is null, why I get null? I my $query typed wrong?
thank you
The problem is, your function GetBusiness returns a multi-dimensionnal array, you can use this one instead :
function GetBusiness($con, $email)
{
$query = "SELECT * from Business WHERE B_EMAIL ='".$email."'";
$res = mysqli_query($con,$query);
return mysqli_fetch_array($res);
}
Also, you must use the MySQL columns that you selected to access the data of the rowset. Something like
$json["uid"] = $result_business["B_ID"];
$json["business"]["name"] = $result_business["B_NAME"];

select value into variable using input from form

Hi i am having an issue selecting a value form my table into a variable in the PHP so that I can calculate the cost of something
here is the code I have so far I want to be able to select a "cost" value from the table C_price where the values of I_type and a_type match
E.g. the table structure looks like this
ID=1,A_type=line,I_type=Head,cost=5
if on the form i enter line and head
i need to be able to get the value 5 in to a venerable i can use in calculations and insert into another table AKA i need to get cost into a variable somehow
the following was my try and i need help im new at all this so please help
$E_C;
$T_cost = "1";
$date = date("d.m.y");
$name = $_POST["from"];
$email = $_POST["email"];
$ref = $_POST["link"];
$i_type = $_POST["i_type"];
$a_type = $_POST["a_type"];
$extra = $_POST["extra"];
$des = $_POST["description"];
$BG = $_POST["BG"];
$bg_type = $_POST["BGtype"];
$msg = $_POST["message"];
$auto_reply = ("thanks for the email we will get back to you as soon as we can about the cost and how you can pay");
$msg = wordwrap($msg, 70);
$host = "localhost";// hostname
$USER = "root";// username
$PASS = "Password";// password
$DBNAME = "andrea";// databace name
$tbl_name = "c_price";// table name
$con = mysqli_connect("localhost", $USER, $PASS, $DBNAME)or die("mySQL server connection failed");
$all = "SELECT cost FROM C_price WHERE a_type=$a_type,i_type=$i_type";
$result = mysqli_query($con,$all) or die("Error getting total storse");
while($row = mysqli_fetch_array($result))
{
echo $row['cost'];
}
if ($a_type = 'waist' && $extra='Y')
{
$E_C = $cost * .3;
}
elseif ($a_type = 'knee' && $extra='Y')
{
$E_C = $cost * .35;
}
elseif ($a_type ='full' && $extra='Y')
{
$E_C = $cost * .4;
}
else
{
$E_C = 0;
}
$T_cost = $cost + $E_C;
if ($BG = 'y')
{
$T_cost = $T_cost + 10;
}
You can't use mysqli and mysql at a same time.. Mysqli is a class... So first change that things...
while($row = mysqli_fetch_array($result))
{
echo $row['cost'];
}
$news1 = mysqli_result($result, 0); // 0 is the index of the field, not the row
echo $news1;
echo $cost;`
Query should be like this...
$all = "SELECT cost FROM C_price WHERE a_type='$a_type'and i_type='$i_type'";
You cant mix mysql and mysqli
change this line In the while loop and add for error mysqli_error
$news1 = mysql_result($result, 0);
$news1 = mysqli_result($result) or die(mysqli_error());
and your query is wrong as well and A_type is not same as A_type and same goes for I_type as well
$all = "SELECT cost FROM C_price WHERE a_type=$a_type,i_type=$i_type";
//Change it to
$all = "SELECT cost FROM C_price WHERE A_type='$a_type'and I_type='$i_type'";
//and A_type is not same as a_type and same goes for I_type as well

Why is the query returning only one set of data?

The problem I have is when I echo or print the following variables, the data I receive is that of the last business listed in my table only.
At present no matter the listing I click I get the same set of data for the last business returned.
As you can see in the below code I am passing the business_name from the clicked listing to be used in my query to find the relevant business profile information.
$business_name = mysql_real_escape_string($_GET['business_name']);
$query = "SELECT
business_id,
category,
years_recommended,
profile_size,
business_name,
established,
employees,
service,
strengths,
ideal_for,
reassurance
FROM
business_data
WHERE
business_name = '$business_name'
AND
profile_size = 'A'
OR
profile_size = 'B'
OR
profile_size = 'C'
OR
profile_size = 'D'
OR
profile_size = 'E'";
$result = mysql_query($query, $dbc)
or die (mysql_error($dbc));
while($row = mysql_fetch_array($result)) {
$business_id = $row["business_id"];
$profile_size = $row["profile_size"];
$category = $row["category"];
$years = $row["years_recommended"];
$established = $row["established"];
$employees = $row["employees"];
$service = $row["service"];
$strengths = $row["strengths"];
$ideal_for = $row["ideal_for"];
$reassurance = $row["reassurance"];
}
echo...
If you need more information please let me know.
Is there anything wrong with my code?
Many thanks in advance.
Your echo call is outside the fetch loop, so you'll only see the last result even though the others were returned.
while($row = mysql_fetch_array($result)) {
$business_id = $row["business_id"];
$profile_size = $row["profile_size"];
$category = $row["category"];
$years = $row["years_recommended"];
$established = $row["established"];
$employees = $row["employees"];
$service = $row["service"];
$strengths = $row["strengths"];
$ideal_for = $row["ideal_for"];
$reassurance = $row["reassurance"];
// Echo **inside** the loop
echo...
}
If you wish, you can store all the results in a large array, which can then be used anywhere subsequently in your script, as many times as needed:
// Array for all results
$results = array();
while($row = mysql_fetch_array($result)) {
// Append each row fetched onto the big array
$results[] = $row;
}
// Now use it as needed:
foreach ($results as $r) {
echo $r['profile_size'];
print_r($r);
}
your echo should be inside the loop

Data insert problem within foreach loop

The problem is that only some of the XML data is being Inserted into the my mysql database. 10 results are supposed to be entered into the database but it varies between 2 and 8 results. I have no idea why it is doing this and I have tried adding a sleep function to slow the script down, but the data that is inserted into the data base is never as much as when I echo it out on screen. Any help would be much appreciated..
function post_to_db($xml,$cat_id){
if ($xml->Items->Request->IsValid == 'True'){
$xml = $xml->Items->Item;
foreach($xml as $item){
$asin = (string)$item->ASIN;
$title = (string)$item->ItemAttributes->Title;
$content = (string)
$item->EditorialReviews->EditorialReview->Content;
$sku = (string)$item->ItemAttributes->SKU;
$brand = (string)$item->ItemAttributes->Brand;
$feature = (string)$item->ItemAttributes->Feature;
$model_no = (string)$item->ItemAttributes->Model;
$review = (string)$item->ItemLinks->ItemLink[5]->URL;
$check = "SELECT * FROM `products` WHERE `asin` = '$asin'";
$checked = mysql_query($check);
$numrows = mysql_num_rows($checked);
if ($numrows == 0){
$query = "INSERT INTO `products`".
"(`cat_id`,`asin`,`sku`,`brand`,".
"`model_no`,`title`,`content`,`feature`) ".
"VALUES".
"('$cat_id','$asin','$sku','$brand',".
"'$model_no','$title',".
"'$content','$feature')";
$result = mysql_query($query);
$post_id = mysql_insert_id();
$review_page[] = array($post_id=>$review);
}
}
}
return $review_page;
}
My guess would be some of your variables from XML are creating an invalid query (do they contain quotes?)
Instead of this for each variable:
$asin = (string)$item->ASIN;
Do this instead:
$asin = mysql_real_escape_string((string)$item->ASIN);
If the problem still persists, change your mysql_query line to this for debugging:
$result = mysql_query($query) or die(mysql_error());

Need Help With Implementing Simple Stuff with PHP and MYSQL

Here is my code -
<?php
$u = $_SESSION['username'];
while($fetchy = mysqli_fetch_array($allusers))
{
mysqli_select_db($connect,"button");
$select = "select * from button where sessionusername='$u' AND response = 'approve'";
$query = mysqli_query($connect,$select) or die('Oops, Could not connect');
$result= mysqli_fetch_array($query);
$email = mysqli_real_escape_string($connect,trim($result['onuser']));
echo $email;
if($email){
mysqli_select_db($connect,"users");
$select_name = "select name, icon from profile where email = '$email'";
$query_2 = mysqli_query($connect,$select_name) or die('Oops, Could not connect. Sorry.');
$results= mysqli_fetch_array($query_2);
$name = mysqli_real_escape_string($connect,trim($results['name']));
$icon = mysqli_real_escape_string($connect,trim($results['icon']));
echo $name;
}
}
NOw, there are two reponses in db. So, two names are getting echoed, but they both are SAME. Why so? Eg
DB - NAMEs - Apple and Orange.
Displayed - Apple Apple.
Database example -
SESSIONUSERNAME OnUSer
s#s.com apple
s#s.com orange
EDITED
Using #endophage's method -
AppleOrange and AppleOrange.
As your loop stands now, $u will always be the same, so $select will always have the same value, and so will $email, and so will $select_name, so it is no surprise that the same record keeps coming back.
Edit
If the $select_name query returns multiple results, then you need to loop through the results with a while loop like the other queries.
Try this, you had your while loop in the wrong place:
<?php
$u = $_SESSION['username'];
mysqli_select_db($connect,"button");
$select = "select * from button where sessionusername='$u' AND response = 'approve'";
$query = mysqli_query($connect,$select) or die('Oops, Could not connect');
while($result = mysqli_fetch_array($query))
{
$email = mysqli_real_escape_string($connect,trim($result['onuser']));
echo $email;
if($email){
mysqli_select_db($connect,"users");
$select_name = "select name, icon from profile where email = '$email'";
$query_2 = mysqli_query($connect,$select_name) or die('Oops, Could not connect. Sorry.');
$results= mysqli_fetch_array($query_2);
$name = mysqli_real_escape_string($connect,trim($results['name']));
$icon = mysqli_real_escape_string($connect,trim($results['icon']));
echo $name;
}
}

Categories