Here is the return of AJAX, which display what value I am trying to store into database:
Here is what is really stored into database (58.00 is what I stored):
Here is the structure of table:
Here is the PHP:
$req = $bdd->prepare("INSERT INTO salon_histo(reference, designation, colour, size, type, price, qty, payment, date) VALUES(:reference, :designation, :colour, :size, :type, :price, :qty, :payment, NOW())");
$req->execute(array(
'reference' => $_POST['reference'],
'designation' => $_POST['designation'],
'colour' => $_POST['colour'],
'size' => $_POST['size'],
'type' => $_POST['type'],
'price' => floatval($price[0]),
'qty' => $_POST['soldQty'],
'payment' => $_POST['payment']
));
$req->closeCursor();
echo json_encode($price[0]);
How can MySQL store a data with 2 decimals at 0 when I am trying to store 58,33? I tried in PHP to use floatval and indeed the number becomes 58.
Price appears to be an array:
'price' => floatval($price[0]),
Possibly the decimal part has key 1?
'price' => (float) ($price[0] . '.' . $price[1]),
If that doesn't work, please var_dump($price) and paste me the result.
The other thing to check is wether PHP is formatting those numbers with a comma or not.
Related
I am using PDO for writing to sqlite database.
// $dbo is an instance of PDO
$query = "INSERT INTO items (id, name, rank) VALUES (:id, :name, :rank)";
$statement = $dbo->prepare($query);
$values = array('id' => 1, 'name' => 'Some Name', 'rank' => '');
$statement->execute($values);
$values = array('id' => 1, 'name' => 'Some Name', 'rank' => NULL);
$statement->execute($values);
After executing any of these, I expect "rank" to be an empty string or even a NULL is acceptable.
But what I get in the database is 'null', yes a string 'null' value, not the real NULL.
Could not find any solution after several attempts.
Try sending NULL directly in this line
$values = array('id' => 1, 'name' => 'Some Name', 'rank' => NULL);
I have this code:
$possible_pics = array(
'red-pfp' => 'teem-pfp-red.svg',
'pink-pfp' => 'teem-pfp-pink.svg',
'blue-pfp' => 'teem-pfp-blue.svg',
'green-pfp' => 'teem-pfp-green.svg',
'purple-pfp' => 'teem-pfp-purple.svg',
'yellow-pfp' => 'teem-pfp-yellow.svg',
'orange-pfp' => 'teem-pfp-orange.svg',
);
shuffle($possible_pics);
echo reset($possible_pics);
The result of it, I want to insert it into a database this way:
$sentence = $connection->prepare("
INSERT INTO users (id, user, pass, email, profile_pic) VALUES (:id, :user, :password, :email, :profile_pic)
");
$sentence->execute(array(
':id' => $user_id,
':user' => $user,
':password' => $password,
':email' => $email,
':profile_pic' => $possible_pics
));
At this point I have already connected to the database, I'm just doing the SQL code.
As you can see, I am inserting the values into the database through an array, and in the part of :profile_pic, I am saying that I want to insert the result of the first code I added to my question, where I am shuffling the array and it only brings us 1 value. The problem here is that when I run this, it shows this:
Notice: Array to string conversion on line 73
Why is this happening and how can I make so it inserts the value of the randomized array? Where I do that, it works perfectly, and it returns effectively only 1 value. I can't do an implode() because it marks, unexpected implode().
In resume, how can I insert into a database the returned value of the randomized array I showed at the beginning of my question?
Just pick the output of reset function to a variable and use it in your insert statement.
shuffle($possible_pics);
$shuffledPic = reset($possible_pics);
...
$sentence->execute(array(
':id' => $user_id,
':user' => $user,
':password' => $password,
':email' => $email,
':profile_pic' => $shuffledPic
));
i have this array in an example, how can i get the same result from query from database?, i need to replace the values which the values of the database.
$data = array(
array(
'qty' => 1,
'Price' => 1.00,
'total' => 1.00
),
array(
'qty' => 2,
'Price' => 1.00,
'total' => 2.00
),
array(
'qty' => 3,
'Price' => 1.00,
'total' => 3.00
)
);
then in the example use the nusoap lib
foreach($data as $concept) {
$par['Concepts'][] = new soapval('Concept', 'Concept', $concept);
}
so i need to call the query:
$query_data_cot = mysql_query("SELECT * FROM data WHERE id='1'");
while($data_quote=mysql_fetch_array($query_data_cot)){
$conceptosDatos[]["qty"]=$data_quote['qty'];
$conceptosDatos[]["Price"]=$data_quote['price'];
$conceptosDatos[]["total"]=$data_quote['total'];
}
but when i do these i got an error
Error: Array ( [faultcode] => soap:Server [faultstring] => Server was unable to process request. --->
thank you
Everytime you use $conceptosDatos[]... the [] will create a new subarray. So your result will be something like this
array(
array('qty' => ..),
array('Price' => ..),
array('total' => ...),
array('qty' => ..),
array('Price' => ..),
array('total' => ...),
...
)
Instead you need to create a new subarray only for a whole set, so use something like this
$query_data_cot = mysql_query("SELECT qty, price, total FROM data WHERE id='1'");
while($data_quote=mysql_fetch_assoc($query_data_cot)){
$conceptosDatos[] = $data_quote;
}
Of course you could also do it like this
$query_data_cot = mysql_query("SELECT qty, price, total FROM data WHERE id='1'");
while($data_quote=mysql_fetch_assoc($query_data_cot)){
$conceptosDatos[] = array(
'qty' => $data_quote['qty'],
'Price' => $data_quote['price'],
'total' => $data_quote['total'],
);
}
But why write every field if you're going to copy the whole array anyway?
If you need to use different names (as in your example price and Price), you can either change your db schema or use aliases in your query, giving you this code:
$query_data_cot = mysql_query("SELECT qty, price AS Price, total FROM data WHERE id='1'");
while($data_quote=mysql_fetch_assoc($query_data_cot)){
$conceptosDatos[] = $data_quote;
}
This has the advantage that if you wish to modify your code in the future, you'd only need to modify the query (and could probably encapsulate this logic inside a function) - so less work for future you.
By the way, did you see the big red box in the manual on all mysql_* methods's sites? It's deprecated and PDO as well as MySQLi are way better alternatives. This helps decide what to use.
Try this , while($data_quote=mysql_fetch_array($query_data_cot , MYSQL_ASSOC )) , as you are retrieving the $data as an associative array .
Each assignment line in your loop is creating a new element of the $conceptDatos array, not filling in a different element of the same element. So your array looks like:
array(
array('qty' => 1),
array('Price' => 1.0),
array('total' => 1.0),
array('qty' => 2),
array('Price' => 1.0),
array('total' => 1.0),
...
)
Your loop shoud be:
while($data_quote=mysql_fetch_array($query_data_cot)){
$conceptDatos[] = array(
'qty' => $data_quote['qty'],
'Price' => $data_quote['price'],
'total' => $data_quote['total']
);
}
I've recently starting using PDO in a rebuild of a client's taxi booking system.
I have a script called create_booking.php, which initially inserts the booking details into a bookings table in the MySQL database. After inserting the customers details it retrieves the lastinsertID to get the booking ref. It then creates a job in the jobs table and references the booking reference to relate the job/booking.
The first insert is working fine, but the second insert isn't . Any ideas?
if (isset($_POST['customer_title'])) {
include('../assets/db_connection.php');
$create_booking = $db->prepare("INSERT INTO bookings(customer_name, billing_address, contact_tel, contact_mob, contact_email, party_pax, party_cases, booking_notes, price, booking_agent, booking_date, booking_status, authorised)
VALUES(:customer_name, :billing_address, :contact_tel, :contact_mob, :contact_email, :party_pax, :party_cases, :booking_notes, :price, :booking_agent, :booking_date, :booking_status, :authorised );");
$create_booking->execute(array(
":customer_name" => $customer_title . ' ' . $customer_first_name . ' ' . $customer_last_name,
":billing_address" => $billing_address,
":contact_tel" => $customer_tel,
":contact_mob" => $customer_mobile,
":contact_email" => $customer_email,
":party_pax" => $passengers,
":party_cases" => $cases,
":booking_notes" => $booking_notes,
":price" => $price,
":booking_agent" => $booking_agent,
":booking_date" => $booking_date,
":booking_status" => $booking_status,
":authorised" => $authorised
));
$booking_ref = $db->lastInsertId('booking_ref'); // Takes Booking Ref generated in $create_booking
$create_job = $db->prepare("INSERT INTO jobs(booking_ref, pickup_date, pickup_time, pickup_address, destination_address, return, scheduled)
(:booking_ref, :pickup_date, :pickup_time, :pickup_address, :destination_address, :return, :scheduled )");
$create_job->execute(array(
":booking_ref" => $booking_ref,
":pickup_date" => $pickup_date,
":pickup_time" => $pickup_time,
":pickup_address" => $pickup_address,
":destination_address" => $pickup_destination,
":return" => "N",
":scheduled" => "N"
));
}
Your second SQL query is missing VALUES.
INSERT INTO() ... VALUES()
$create_job = $db->prepare("INSERT INTO jobs(booking_ref, pickup_date, pickup_time, pickup_address, destination_address, return, scheduled)
VALUES (:booking_ref, :pickup_date, :pickup_time, :pickup_address, :destination_address, :return, :scheduled )");
I have an annoying problem with mdb2 while trying to insert empty values in an integer field.
$query = $conn->prepare("INSERT INTO enquiries
(type, idvenue, cname, fname, lname, phone, email, date, guests, budget, comments)
VALUES (:party, :venue, :cname, :fname, :lname, :phone, :email, :ddate, :guests, :budget, :options)",
array('integer', 'integer', 'text', 'text', 'text', 'text', 'text', 'datetime', 'integer', 'integer', 'text'),
MDB2_PREPARE_MANIP);
$result = $query->execute($_POST);
my $_POST array is:
[party] => 2
[venue] =>
[anyve] => checked
[cname] =>
[fname] => Javi
[lname] => Prieto
[phone] => 078087028492
[email] => ravenfewejp#gmfweail.com
[ddate] => 10/10/2011
[guests] => 10
[budget] => 15
[options] => My party!
in the moment that :venue is not empty, it works like a charm, even if :cname (text) is still empty, but when :venue is empty like this I get an undefined error. So I guess is happening only with integer fields, what am I missing?
try this before executing statement:
$_POST['venue'] = (int) $_POST['venue'];
I realised there's no problem with passing null values to integer fields in this kind of queries. The problem was I was sending one more element in $_POST when venue was empty, so the number of elements in $_POST was different than the number of placeholders.
Thanks for the help!