Thursday 15 July 2010

php - how do I update a row if it exists, or create a new one if it doesnt exist in mysql -



php - how do I update a row if it exists, or create a new one if it doesnt exist in mysql -

orgi trying query correct. want insert record database upon form submission if record not exist. if record exists, want updated in database.

what happening: upon form submit, new record inserted database every time. if duplicate.

update: added column called "u_id" holds unique info each contact in database. so, made unique key column.

if($_post['submit']){ $con=mysqli_connect("localhost","username","password","database_name"); // check connection if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } $contact = ($_post['contact']); $u = ($_post['uid']); $org = mysql_real_escape_string($_post['organization']); $namefirst = mysql_real_escape_string($_post['firstname']); $namelast = mysql_real_escape_string($_post['lastname']); $emailaddy = mysql_real_escape_string($_post['email']); $phonenum = mysql_real_escape_string($_post['phone']); $appquestion = mysql_real_escape_string($_post['appquestion']); $banner = mysql_real_escape_string($_post['banner']); $bulletin = mysql_real_escape_string($_post['bulletin']); $giveaway = mysql_real_escape_string($_post['giveaway']); $app = mysql_real_escape_string($_post['app']); $tshirt = mysql_real_escape_string($_post['tshirt']); $tshirtp = mysql_real_escape_string($_post['tshirtp']); $print = mysql_real_escape_string($_post['print']); $party = mysql_real_escape_string($_post['party']); $orgnotes = mysql_real_escape_string($_post['notes']); $sql="insert database_name (contact_id, u_id, first_name, last_name, email_address, phone_number, org, appquestion, banner, bulletin, giveaway, app, tshirt, promised_tee, print, party, org_notes) values ('$contact', '$u', '$namefirst','$namelast','$emailaddy','$phonenum','$org','$appquestion','$banner','$bulletin','$giveaway','$app','$tshirt','$tshirtp','$print','$party','$orgnotes') on duplicate key update first_name = '$namefirst', last_name = '$namelast', email_address = '$emailaddy', phone_number = '$phonenum', org = '$org', appquestion = '$appquestion', banner = '$banner', bulletin = '$bulletin', giveaway = '$giveaway', app = '$app', tshirt = '$tshirt', promised_tee = '$tshirtp', print = '$print', party = '$party', org_notes = '$orgnotes'" ; if (!mysqli_query($con,$sql)) { die('error: ' . mysqli_error($con)); } echo "1 record added"; mysqli_close($con); }

from have read, need utilize on duplicate key update replace old info new info in database upon form submission. while insert part of code working, portion on duplicate key update not working.

why might portion of code not working? there improve way insert else update information?

i have tried replace (instead of insert , on duplicate key update), didn't work either. here column construction in mysql database:

+-------------+-------------+------+-----+-----------+-------------------+ field | type | null | key | default | +-------------+-------------+------+-----+-----------+-------------------+ contact_id | int(1) | no | pri | null | auto_increment u_id | char(32) | no | uni | null | title | varchar(80) | no | | null | first_name | varchar(100)| no | | null | last_name | varchar(100)| no | | null | job_title | varchar(255)| no | | null | address_1 | varchar(255)| no | | null | address_2 | varchar(255)| no | | null | org_city | varchar(100)| no | | null | org_state | varchar(100)| no | | null | zip_code | varchar(8) | no | | null | country | varchar(100)| no | | null | phone_number | varchar(15) | no | | null | email_address | varchar(100)| no | | null | org | varchar(150)| no | | null | appquestion | tinyint(1) | no | | null | banner | tinyint(1) | no | | null | bulletin | tinyint(1) | no | | null | giveaway | tinyint(1) | no | | null | app | tinyint(1) | no | | null | tshirt | tinyint(1) | no | | null | promised_tee | tinyint(1) | no | | null | print | tinyint(1) | no | | null | party | tinyint(1) | no | | null | org_notes | varchar(255)| no | | null | notes | varchar(255)| no | | null | +-------------+-------------+------+-----+-----------+-------------------+

thank help or guidance can give me! new php , mysql. i've been working on concept 3 days , have read ton of info it, still not able work.

i guess contact id key , identity value increments automatically? in case seek insert statement.

insert database_name (first_name, last_name, email_address, phone_number, org, appquestion, banner, bulletin, giveaway, app, tshirt, promised_tee, print, party, org_notes) values ('$namefirst','$namelast','$emailaddy','$phonenum','$org','$appquestion','$banner','$bulletin','$giveaway','$app','$tshirt','$tshirtp','$print','$party','$orgnotes') on duplicate key update first_name = '$namefirst', last_name = '$namelast', email_address = '$emailaddy', phone_number = '$phonenum', org = '$org', appquestion = '$appquestion', banner = '$banner', bulletin = '$bulletin', giveaway = '$giveaway', app = '$app', tshirt = '$tshirt', promised_tee = '$tshirtp', print = '$print', party = '$party', org_notes = '$orgnotes'" ;

php html mysql database forms

No comments:

Post a Comment