Jump to content

[SOLVED] insert / update query help


herghost

Recommended Posts

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

   //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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.