Php Mysqli not updating data in database - php

Here is my code, it works and no errors pop up and the correct data for the variables are there.
When it's all done it shows Done for the last echo.
However, when I go into heidisql to view the database table, nothing has changed, even when I run the query in heidisql, still same results.
// Make connection to database
$connection = mysqli_connect($host,$user,$pass,$dbnm);
// Make query
$myQuery = "
UPDATE Ekhaya_Inventory SET
ekhaya_inventory_stock_item = '" . $stockItemPost . "',
ekhaya_inventory_stock_left = '" . $stockLeftPost . "',
ekhaya_inventory_stock_out = '" . $stockOutPost . "',
ekhaya_inventory_stock_minimum = '" . $stockMinimumPost . "',
ekhaya_inventory_stock_price_per_item = '" . $stockPricePIPost . "',
ekhaya_inventory_value_of_stock_left = '" . $stockValueOfStockLeftPost . "'
WHERE
ekhaya_inventory_stock_code = '" . $stockCodePost . "'
AND
ekhaya_inventory_stock_code = '" . $stockLocationPost . "'
";
mysqli_query($connection,$myQuery)or die("Error: ".mysqli_error($connection));
mysqli_close($connection)or die("Error: ".mysqli_error($connection));
echo "<br>Done";

WHERE
ekhaya_inventory_stock_code = '" . $stockCodePost . "'
AND
ekhaya_inventory_stock_code = '" . $stockLocationPost . "'
it is wrong because one field can`t contain two different values in the same time

Related

Opencart Undefined in model file?

Hey Guys I am trying to pass data to my model, but for some reason I keep getting an "undefined customitem_id" in my model file. I am testing to see if it will even send to the model so:
the code is as follows. My controller file from customer.php file
$data['customitem_id']= 19;
if(isset($this->request->post['customitem_id'])) {
$this->request->post['customitem_id'];
}
My code from:
public function editCustomer($customer_id, $data) {
if (!isset($data['custom_field'])) {
$data['custom_field'] = array();
}
$this->db->query("UPDATE " . DB_PREFIX . "customer SET customer_group_id = '" . (int)$data['customer_group_id'] . "', sales_representative = '" . $this->db->escape($data['username']) . "', firstname = '" . $this->db->escape($data['firstname']) . "', lastname = '" . $this->db->escape($data['lastname']) . "', email = '" . $this->db->escape($data['email']) . "', telephone = '" . $this->db->escape($data['telephone']) . "', fax = '" . $this->db->escape($data['fax']) . "', custom_field = '" . $this->db->escape(isset($data['custom_field']) ? serialize($data['custom_field']) : '') . "', newsletter = '" . (int)$data['newsletter'] . "', status = '" . (int)$data['status'] . "', approved = '" . (int)$data['approved'] . "', safe = '" . (int)$data['safe'] . "' WHERE customer_id = '" . (int)$customer_id . "'");
$this->db->query("UPDATE " . DB_PREFIX . "custom_item SET customer_id = '" . (int)$customer_id . "' WHERE customitem_id = '" . (int)$customitem_id . "'");
it keeps giving me an undefined variable in the model file. How would I go about making sure it sends the data?
Thanks for your help.
Looks like you're passing an array $data to your editCustomer($customer_id, $data) right?
Try changing this from:
$this->db->query("UPDATE " . DB_PREFIX . "custom_item SET customer_id = '" . (int)$customer_id . "' WHERE customitem_id = '" . (int)$customitem_id . "'")
to
$this->db->query("UPDATE " . DB_PREFIX . "custom_item SET customer_id = '" . (int)$customer_id . "' WHERE customitem_id = '" . (int)$data['customitem_id'] . "'")
note that I changed (int)$customitem_id to (int)$data['customitem_id']
You're using variable $customitem_id in your model but in your controller it is $data['customitem_id']. You likely just need to change $customitem_id to $data['customitem_id'] in your model.

jquery jtable inserting/updating record with apostrophe

I have jtable working perfectly fine with adding/editing/removing records however I cant insert/update a record that contains an apostrophe ! Someone please help!
Below is a snapshot fo my code
Blockquote //Updating a record (updateAction)
$result = mysql_query("UPDATE teg_priority SET CustomerName = '" . $_POST["CustomerName"] . "', Service_Manager = '" . $_POST["Service_Manager"]. "', NGM = '" . $_POST["NGM"] . "', Tag = '" . $_POST["Tag"] . "', CBS = '" . $_POST["CBS"]. "' WHERE CIDN = " . $_POST["CIDN"] . ";");
//Return result to jTable
$jTableResult = array();
$jTableResult['Result'] = "OK";
print json_encode($jTableResult);
}
Blockquote
You can use addslashes on the variables before putting them in the query.
$result = mysql_query("UPDATE teg_priority SET CustomerName = '" . addslashes($_POST["CustomerName"]) . "', Service_Manager = '" . addslashes($_POST["Service_Manager"]). "', NGM = '" . addslashes($_POST["NGM"]) . "', Tag = '" . addslashes($_POST["Tag"]) . "', CBS = '" . addslashes($_POST["CBS"]). "' WHERE CIDN = " .addslashes( $_POST["CIDN"]) . ";");

php sql not working

The query isn't updating, not sure what I've done wrong.
$q_result = mysql_query("UPDATE
users
SET
recovery_answer = '".$_POST['answer']."',
recovery_question = '".$_POST['question']."',
date_question_set = '" . mysql_real_escape_string($register_date) . "'
WHERE
username='" . mysql_real_escape_string($_SESSION['username']) . "'");
Can you echo "UPDATE
users
SET
recovery_answer = '".$_POST['answer']."',
recovery_question = '".$_POST['question']."',
date_question_set = '" . mysql_real_escape_string($register_date) . "'
WHERE
username='" . mysql_real_escape_string($_SESSION['username']) . "'" and post?

php script on duplicate update function

am doing an online admission system as my project.
Am having issues with the on duplicate query..
this is my php script.
$save="INSERT INTO student_reg (Reg_number,username,password,surname,fname,mname,sex,address,city,state,country,
Email,Prog_code,dept_code)
VALUES('".$_SESSION['random']."','".$_SESSION['uname']."','".$_SESSION['password']."','".$_SESSION['sname']."',
'".$_SESSION['fname']."','".$_SESSION['mname']."','".$_SESSION['sex']."',
'".$_SESSION['address']."','".$_SESSION['city']."','".$_SESSION['state']."','".$_SESSION['country']."',
'".$_SESSION['email']."','".$_SESSION['progcode']."','".$_SESSION['deptcode']."')
ON DUPLICATE KEY UPDATE
username= '" . $_POST['username'] . "',password = '" . $_POST['password'] . "',
surname='" . $_POST['surname'] . "',fname = '" . $_POST['fname'] . "',mname = '" . $_POST['mname'] . "',
sex = '" . $_POST['sex'] . "',address= '" . $_POST['address'] . "',
city= '" . $_POST['city'] . "',state= '" . $_POST['state'] . "',country = '" . $_POST['country'] . "',
Email = '" . $_POST['email'] . "',Prog_code = '" . $_SESSION['progcode'] . "',dept_code = '".$_SESSION['deptcode']."'
";
$result=mysql_query($save)or die(mysql_error());
if($save==TRUE){
echo"save";
}
else{
echo "not saved";
}
The s_session(random) is used to automatically assign
Reg_numbers to students after their form has been saved and the value is stored in a unique field in d database called reg_number.
When I execute the query,the insert option works perfectly
but the update doesn't work rather,it kips inserting a new record over and over again into the database once the submit button is click..

mysql_query returns true when executing joined UPDATE query

I am creating a script in PHP that updates my users, user_personal and user_extra table in MySQL using joins. When i execute the query using mysql_query it returns true, but the tables are not updated.
I've read online that mysql_query return false if it fails or a MySQL resource if it succeeds, and also read that this is a known bug by PHP.
This is the query in question.
UPDATE users
INNER JOIN user_personal
ON users.id = user_personal.id
INNER JOIN user_extra
ON users.id = user_extra.id
SET
users.email = '" . $user->email . "',
user_extra.life_condition = '" . $user->life_condition . "',
user_extra.keycode = '" . $user->keycode . "',
user_extra.contact_person_phone = '" . $user->contact_person_phone . "',
user_extra.doctor = '" . $user->doctor . "',
user_extra.doctor_phone = '" . $user->doctor_phone . "',
user_extra.doctorpost_phone = '" . $user->doctorpost_phone . "',
user_extra.condition = '" . $user->condition . "',
user_extra.allergy = '" . $user->allergy . "',
user_extra.goal = '" . $user->goal . "',
user_extra.attention_officer = '" . $user->attention_officer . "',
user_extra.primary_respon = '" . $user->primary_respon . "',
user_personal.name = '" . $user->name . "',
user_personal.last_name = '" . $user->last_name . "',
user_personal.insertion = '" . $user->insertion . "',
user_personal.birthdate = '" . $user->birthdate . "',
user_personal.sex = '" . $user->sex . "',
user_personal.street_name = '" . $user->street_name . "',
user_personal.house_number = '" . $user->house_number . "',
user_personal.postal_code = '" . $user->postal_code . "',
user_personal.city = '" . $user->city . "',
user_personal.country = '" . $user->country . "',
user_personal.tel_nr = '" . $user->tel_nr . "',
user_personal.contact_person = '" . $user->contact_person . "'
WHERE user_personal.name = '" . $user->name . "'
When I executed a smaller test version of the query using one INNER JOIN the function returned a resource, same results when i do the query without joins.
My server uses PHP 5.3.3 and Apache version 2.2.15 with MySQL server 5.1.69.
Can you explain how this happens? And is there a solution for this problem?
I've read the documentation on PHP.net and learned when using INSERT, UPDATE, DELETE, DROP the method returns true or false
When using SELECT, SHOW, DESCRIBE, EXPLAIN you get an resource when the query is successful and false when the query failed.
Thanks for the comments guys.

Categories