herghost Posted April 10, 2009 Share Posted April 10, 2009 Hi all, I have this code which inserts information into a database: <?php //Start session session_start(); //Include database connection details require_once('include/database.php'); //Array to store validation errors $errmsg_arr = array(); //Validation error flag $errflag = false; //Function to sanitize values received from the form. Prevents SQL injection function clean($str) { $str = @trim($str); if(get_magic_quotes_gpc()) { $str = stripslashes($str); } return mysql_real_escape_string($str); } //Sanitize the POST values $userid = $_SESSION['SESS_USERID']; $bandname = $_SESSION['SESS_BANDNAME']; $genre = clean($_POST['genre']); $formed = clean($_POST['formed']); //Input Validations if($formed == '') { $errmsg_arr[] = 'Year Formed is Missing'; $errflag = true; } //Create INSERT query $qry = "INSERT INTO banddata(userid, bandname, genre, formed) VALUES('$userid','$bandname','$genre', '$formed' )"; $result = @mysql_query($qry); //Check whether the query was successful or not if($result) { header("location: member_home.php"); exit(); }else { die(mysql_error()); } ?> Which works great, however if information is already stored in the database then I just get an error, how would I change this to update if information exists? I guess I would have to select the row first, then check for date, insert if empty or update if data is present, just not quite sure how I would go about this! Thanks Quote Link to comment https://forums.phpfreaks.com/topic/153466-solved-insert-or-update-help/ Share on other sites More sharing options...
FaT3oYCG Posted April 10, 2009 Share Posted April 10, 2009 http://www.w3schools.com/php/php_mysql_update.asp Quote Link to comment https://forums.phpfreaks.com/topic/153466-solved-insert-or-update-help/#findComment-806293 Share on other sites More sharing options...
DeanWhitehouse Posted April 10, 2009 Share Posted April 10, 2009 change //Create INSERT query $qry = "INSERT INTO banddata(userid, bandname, genre, formed) VALUES('$userid','$bandname','$genre', '$formed' )"; $result = @mysql_query($qry); to $sql = mysql_query("SELECT * FROM bandata WHERE user_id = '$userid'"); if(mysql_num_rows($sql) == 0) { //Create INSERT query $qry = "INSERT INTO banddata(userid, bandname, genre, formed) VALUES('$userid','$bandname','$genre', '$formed' )"; } else { //Create update query $qry = "UPDATE bandata SET bandname = '$bandname', genre = '$genre', formed = '$formed' WHERE userid = '$userid'"; } $result = @mysql_query($qry); Quote Link to comment https://forums.phpfreaks.com/topic/153466-solved-insert-or-update-help/#findComment-806294 Share on other sites More sharing options...
nankoweap Posted April 10, 2009 Share Posted April 10, 2009 consider performing the update first, then... http://us3.php.net/manual/en/function.mysql-affected-rows.php if the number of rows affected is zero, insert. jason Quote Link to comment https://forums.phpfreaks.com/topic/153466-solved-insert-or-update-help/#findComment-806295 Share on other sites More sharing options...
herghost Posted April 10, 2009 Author Share Posted April 10, 2009 Thanks everyone! This did it: $sql = mysql_query("SELECT * FROM banddata WHERE userid = '$userid'"); if(mysql_num_rows($sql) == 0) { //Create INSERT query $qry = "INSERT INTO banddata(userid, bandname, genre, formed) VALUES('$userid','$bandname','$genre', '$formed' )"; } else { //Create update query $qry = "UPDATE banddata SET bandname = '$bandname', genre = '$genre', formed = '$formed' WHERE userid = '$userid'"; } $result = @mysql_query($qry); Quote Link to comment https://forums.phpfreaks.com/topic/153466-solved-insert-or-update-help/#findComment-806297 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.