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..
Related
I am using the opencart system and I've added a specific form for our company. Basically it keeps up with our orders as we input into the system. I'm trying to add to multiple records to a table, but it only will work for the first one.
$this->db->query("INSERT INTO " . DB_PREFIX . "customer_orders SET customer = '" . $data['customer'] . "', email = '" . $data['email'] . "', telephone = '" . $data['telephone'] . "', purchase_order = '" . $data['purchase_order'] . "', date_received = '" . $date . "', date_due = '" . $data['date_due'] . "', insurance = '" . $data['insurance'] . "'");
$customer_order = $this->db->getLastId();
foreach ($data['description'] as $description) {
$this->db->query("INSERT INTO " . DB_PREFIX . "customer_order_description SET customer_order_id = '" . $customer_order . "', description = '" . $description . "'");
}
This item would have 2 descriptions, so it should place each description in the order description table, but it only does the first one.
$queryb = "SELECT product, imei, country, warranty, config from PRODUCT WHERE product_slno = '$mnserialno' ";
$resultb = mysql_query($queryb, $gndbconn) ;
if(mysql_num_rows($resultb) > 0)
{
$queryc = "UPDATE PRODUCT SET product='$desc', product_slno='$mnserialno',imei='$imei',country='$country',warranty='$warranty',config='$config' WHERE product_slno = '.$mnserialno.' ";
$resutc = mysql_query($queryc, $gndbconn) ;
}
else{
$querya = "INSERT INTO PRODUCT SET product='$desc', product_slno='$mnserialno',imei='$imei',country='$country',warranty='$warranty',config='$config'";
$resulta = mysql_query($querya, $gndbconn) ;
}
I want to check the serial number if that serial number already exist in database so records get update, otherwise it get insert into the database.
but the code inserting the records only, no updation, what is the fault i am not getting,
how to prevent the duplicate entry?
INSERT INTO PRODUCT SET
(`product`, `product_slno`, `imei`, `country`, `warranty`, `config`)
VALUES
('" . $desc . "', '" . $mnserialno . "', '" . $imei . "', '" . $country . "', '" . $warranty . "', '" . $config . "')
ON DUPLICATE KEY UPDATE
product='" . $desc . "',
product_slno='" . $mnserialno . "',
imei='" . $imei . "',
country='" . $country . "',
warranty='" . $warranty . "',
config='" . $config . "'";
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.
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
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.