I am looking to update one of the table. After I update, all the duplicate data is getting inserted again. Especially, the cloneSQL part of the code. I tried using DISTINCT, NOT EXISTS but no luck.
if(DB_num_rows($checkResult) > 0){
$cloneSQL = "UPDATE DISTINCT pricematrixdiscount SET
discount='" . $vals[3] . "'
WHERE debtorno='" . $_POST['cloneTo'] . "',
product_line='" . $vals[1] . "',
salestype='" . $vals[2] . "' ";
}
else {
$cloneSQL = "INSERT into pricematrixdiscount
(debtorno,
product_line,
salestype,
discount) VALUES
('" . $_POST['cloneTo'] . "',
'" . $vals[1] . "',
'" . $vals[2] . "',
'" . $vals[3] . "')";
How can I insert only distinct values on the pricematricdiscount table without the duplicates being inserted?
I already saw other post related to this question, but they did not solve my problem. I want to automatically go to next rows and want to insert that row in the table. I am transfering sample of data from another database. I want to insert large set of rows... Right now I am getting first row repeatedly in all rows. I need to go to to next rows automoatically
Below is my sample code...
while ($row = mysql_fetch_array($query)) {
$sql = "INSERT INTO table_name (info1, info2, info3,...) "
."VALUES (" . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . "),
(" . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . "),
(" . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . "),
(" . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . ")";
}
This sql Query could help you. You can insert the select datas in one Query. See MySQL documentation: http://dev.mysql.com/doc/refman/5.7/en/insert-select.html
INSERT INTO table_name
(info1, info2, info3)
SELECT
source_table.info1,
source_table.info2,
source_table.info3,
FROM
source_table
WHERE
(WHERE CLAUSE)
do a separate insert for each selected rows:
while ($row = mysql_fetch_array($query)) {
$sql = "INSERT INTO table_name (incremented_field, info1, info2, info3) "
."VALUES (NULL, " . $row['info1'] . "," . $row['info2'] . "," . $row['info3'] . ")";
mysql_query($sql);
}
Here is my code:
$result = mysql_query("INSERT INTO clients (client_id, name, surname, tel1,tel2,id_num,address)
VALUES ('" .$updating_id . "','" .$updatedName1 . "','" .$updatedName1 . "', '" .$updatedSurname1
. "', '" . $updatedTel1 . "', '" .$updatedTel2 . ", '" .$updatedId_num1. "', '" .$updatedAddress1.
") ON DUPLICATE KEY UPDATE name='" . $updatedName1 . "', surname='" . $updatedSurname1 . "',
tel1='" . $updatedTel1 . "', tel2='" . $updatedTel2 . "', id_num='" . $updatedId_num1 . "',
address='" .$updatedAddress1 . "'");
if(mysql_query($result))
{ echo $updatedName1," ", $updatedSurname1, " updated successfully ";
}
else {
echo mysql_error();}
}
I am noticing that the first quote on the mysql_query("INSERT INTO...
is closing with the first quote of the VALUES ('" .$updating_id . "'... statement and yet the way I quoted is the one in my examples, I have assessed.
Use if($result) instead of if(mysql_query($result)). thx #Vinie
And you miss two simple quotes in your VALUES statement:
$updatedTel2 . "'
$updatedAddress1."'
And you need to have a look at mysql_real_escape_string(); or at least use PDO :)
I have part of the code below:
while($array = $result->fetch_assoc() ){
$second_query = "INSERT INTO".TBL_USERSDONTPAY."VALUES ($array[\"username\"], $array[\"password\"], '0',$array[\"userid|\"], )";
$second_result = $database->query($second_query);
}
The query doesn't seem to work. Any clues? I think it's a problem with the quotes or something. How can actually pass array elements?
here is my whole code i want to move one row to another table
$q = "SELECT * FROM ".TBL_USERS." WHERE username = '$subuser'";
$result = $database->query($q);
if($result && $result->num_rows == 1){
while($array = $result->fetch_assoc() ){
$second_query = "INSERT INTO" . TBL_USERSDONTPAY . "VALUES ('" . $array['username'] . "', '" . $array['password'] . "', '0', '" . $array['userid'] ."')";
$second_result = $database->query($second_query);
if($second_result){
// it worked!
$q = "DELETE FROM ".TBL_USERS." WHERE username = '$subuser'";
$database->query($q);
}
}
}
You need to clean that query up and remove the final comma.
$second_query = "INSERT INTO " . TBL_USERSDONTPAY . " VALUES ('" . $array['username'] . "', '" . $array['password'] . "', '0', '" . $array['userid'] . "')";
I see several issues with your query code
escaping of the array indexes in your string:
you can either end the string and concatenate the parts together:
$second_query = "INSERT INTO " . TBL_USERSDONTPAY .
" VALUES ('" . $array['username'] . "', '" . $array['password'] . "', '0', '" . $array['userid'] . "')";
or use the {$var} syntax:
$second_query = "INSERT INTO " . TBL_USERSDONTPAY .
" VALUES ('{$array['username']}', '{$array['password']}', '0', '{$array['userid']}')";
missing spaces (see example code above .. you were missing the spaces before and after the table name)
missing field names. your query may work without if you specify all fields in the right order, but will fail misteriously when you alter the table later (e.g. add a field to the table)
$second_query = "INSERT INTO " . TBL_USERSDONTPAY .
" (username, password, foo, user_id)".
" VALUES ('{$array['username']}', '{$array['password']}', '0', '{$array['userid']}')";
please note you should actually insert the correct field names in the second line of my example above. You can find more information on this in the MySQL docs for INSERT
I'm having a huge issue with this database. It connects correctly and with the information from the form's $_POST queries that are being inserted into the table company_info within the correct fields.
Now, I have no idea what I'm doing wrong here, but I keep getting the die error of
"Error querying database".
The database version is: phpMyAdmin 2.6.4-pl3
MySQL: 5.0
Any ideas? I can provide you the rest of the code if needed.
$dbc = mysql_connect('db390590179.db.1and1.com', 'dbo390590179', '*********')
or die('Error connecting to MySQL server.');
mysql_select_db("db390590179", $dbc);
$query = "INSERT INTO company_info (company_name, company_phone, company_contact, company_address, " .
"company_city, company_state, company_zip, " .
"state_living, vehicles, position, " .
"experience, training, hazmat, " .
"require_hazmat, load_nyc, take_truck_home, " .
"have_rider, have_pet, choose_route, " .
"fuel, cash_advance, days_before_home, " .
"log_system, slip_seat, pre_pass, " .
"ez_pass, health_insurance, retirement_plan, " .
"payment_plan, calculate_pay, freight, " .
"loads, home_on_time, idle_time, " .
"equipment_condition, canada)" .
"VALUES ('$company_name', $company_phone', '$company_contact', '$company_address', '$company_city', " .
"'$company_state', '$company_zip', " .
"'$state_living', '$vehicles', '$position', " .
"'$experience', '$training', '$hazmat', " .
"'$require_hazmat', '$load_nyc', '$take_truck_home', " .
"'$have_rider', '$have_pet', '$choose_route', " .
"'$fuel', '$cash_advance', '$days_before_home', " .
"'$log_system', '$slip_seat', '$pre_pass', " .
"'$ez_pass', '$health_insurance', '$retirement_plan', " .
"'$payment_plan', '$calculate_pay', '$freight', " .
"'$loads', '$home_on_time', '$idle_time', " .
"'$equipment_condition', '$canada')";
$result = mysql_query($query, $dbc)
or die('Error querying database.');
mysql_close($dbc);
I think it's because it's missing a quote before the variable $company_phone in your INSERT statement.
just combine the different values within a single quote.
E.g., "'$company_state , $company_zip,' " ."'$state_living , $vehicles, $position, '" ."'$experience, $training, $hazmat, '" ....
this will work perfectly and also include the missing quote in the begining of the *$company_phone* has to be included.
You can remove double quote from each line and combine them. I have removed syntax errors. You can assure that result is getting or not. Try this code.
$dbc = mysql_connect('db390590179.db.1and1.com', 'dbo390590179', '*********')
or die('Error connecting to MySQL server.');
mysql_select_db("db390590179", $dbc);
$query = "INSERT INTO company_info (company_name, company_phone, company_contact, company_address,
company_city, company_state, company_zip,
state_living, vehicles, position,
experience, training, hazmat,
require_hazmat, load_nyc, take_truck_home,
have_rider, have_pet, choose_route,
fuel, cash_advance, days_before_home,
log_system, slip_seat, pre_pass,
ez_pass, health_insurance, retirement_plan,
payment_plan, calculate_pay, freight,
loads, home_on_time, idle_time,
equipment_condition, canada)
VALUES ('$company_name', '$company_phone', '$company_contact', '$company_address', '$company_city',
'$company_state', '$company_zip',
'$state_living', '$vehicles', '$position',
'$experience', '$training', '$hazmat',
'$require_hazmat', '$load_nyc', '$take_truck_home',
'$have_rider', '$have_pet', '$choose_route',
'$fuel', '$cash_advance', '$days_before_home',
'$log_system', '$slip_seat', '$pre_pass',
'$ez_pass', '$health_insurance', '$retirement_plan',
'$payment_plan', '$calculate_pay', '$freight',
'$loads', '$home_on_time', '$idle_time',
'$equipment_condition', '$canada')";
$result = mysql_query($query, $dbc)
or die('Error querying database.');
mysql_close($dbc);
for (int i = 0; i < CheckBoxList1.Items.Count - 1; i++)
{
String str = "";
if (CheckBoxList1.Items[i].Selected)
{
str = CheckBoxList1.Items[i].Text;
con.Open();
string sql =
"Insert into dbtable(Category,BookTitle,Feature,SubCategory)values('" +
DDLCategory.SelectedItem.Text + "','" + TxtBooktitle.Text + "','" +
CheckBoxList1.Items[i].Text + "','" + DDLSubcategory.SelectedItem.Text +
"')";
SqlCommand cmd = new SqlCommand(sql, con);
}
}
Just use DEBUGGER and see how things are working and you should be able to resolve such issues easily.