herghost Posted April 19, 2009 Share Posted April 19, 2009 Hey all, I have this : <?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']; $status = clean($_POST['status']); $summary = clean($_POST['summary']); $howformed = clean($_POST['howformed']); $history = clean($_POST['history']); $ach = clean($_POST['ach']); $inf = clean($_POST['inf']); $sql = mysql_query("SELECT * FROM bandmaininfo WHERE userid = '$userid'"); if(mysql_num_rows($sql) == 0) { //Create INSERT query $qry = "INSERT INTO bandmaininfo(userid, status, summary, howformed, history, ach, inf) VALUES('$userid', '$status', '$summary', '$howformed', '$history', '$ach', '$inf')"; $result = @mysql_query($qry); } else { //Create update query $qry = "UPDATE bandmaininfo SET status = '$status', maininfo = '$summary', howformed = '$howformed', history = '$history', ach ='$ach', inf = '$inf' WHERE userid = '$userid'"; } $result = mysql_query($qry) or die(mysql_error()); // here we define the session variable for each field $fields = array('status', 'summary'); foreach($fields as $var) { if(isset($_POST[$var])) $_SESSION[$var] = $_POST[$var]; } //Check whether the query was successful or not if($result) { header("location: ../member_home.php"); exit(); }else { die("Query failed"); } ?> Whic simply inserts or updates a database from a form, however, this issue I have is that if the row is empty then it inserts twice on two different rows? I can not see why? Cheers Link to comment https://forums.phpfreaks.com/topic/154781-solved-insert-update-query-help/ Share on other sites More sharing options...
FaT3oYCG Posted April 19, 2009 Share Posted April 19, 2009 //Create INSERT query $qry = "INSERT INTO bandmaininfo(userid, status, summary, howformed, history, ach, inf) VALUES('$userid', '$status', '$summary', '$howformed', '$history', '$ach', '$inf')"; $result = @mysql_query($qry); that is creating and inserting remove the result line Link to comment https://forums.phpfreaks.com/topic/154781-solved-insert-update-query-help/#findComment-813961 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.