Solved

PHP import help

Posted on 2012-04-03
2
215 Views
Last Modified: 2012-04-03
Hi,

I am very new to PHP and MySQL however I do not think this request is too difficult.

I am trying to build a php script that will allow my market managers to enter their company profit factors for the year. The form asks what country they work in and what their profit factors are. It then imports this data into a MYSQL database.

Some sample data looks like this

Market Profit Factor
UK              50
BE               200
DE              80

This works fine, however I would like to know how I can tell my script to just overwrite existing records. As If I enter a profit factor twice then I get two rows of data i.e -

Market Profit Factor
UK              50
UK              50

Is there anyway I can just overwrite the data?

I have attached my php

<?php

$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="bonuscalc"; // Database name 
$tbl_name="market_values"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form 
$market=$_POST['market'];
$profit_factor=$_POST['profit_factor'];


// Insert data into mysql 
$sql="INSERT INTO $tbl_name(market, profit_factor)VALUES('$market', '$profit_factor')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}

else {
echo "ERROR";
}

// close connection 
mysql_close();
?>

Open in new window

0
Comment
Question by:Dan560
2 Comments
 
LVL 9

Accepted Solution

by:
wasiftoor earned 500 total points
ID: 37801664
I have noticed that you are using an INSERT statement. This will always insert a new row into the Database. Consider using an UPDATE statement instead. I am attaching sample code. Please make sure that all of your markets already exist in the database. (UK,BE, DE). If you have not data yet for a particular market, just put in an entry with a zero value to start off with.

// Update data in mysql. Replace the existing insert statement.
$sql="UPDATE $tbl_name SET profit_factor = '$profit_factor' WHERE market = '$market'";
$result=mysql_query($sql);

Hopefully this will work. Good Luck!
0
 
LVL 2

Author Comment

by:Dan560
ID: 37801687
Perfect, it works well. Thank you!
0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question