Jump to content

Inserting into Database Problems


DigitalChaos85

Recommended Posts

Ok, the point of this code is I want a user to be able to create a campaign, two things need to be unique when creating a campaign, the name and the keyword.

 

The name needs to be unique to the table, and the keyword needs to be unique to the user. I can't seem to figure it out and I'm sure it's a problem with my SQL queries. Any help is appreciated

 

if($user && $name && $pw && $kw && $desc && $reply)
{
	//Check for a UNIQUE campaign Name
	$q = "SELECT user_id FROM campaigns WHERE name='$name'";
	$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));
	$qq = "SELECT name FROM campaigns WHERE (user_id={$_SESSION['user_id']} AND keyword='$kw')";
	$rr = mysqli_query ($dbc, $qq) or trigger_error("Query: $qq\n<br />MySQL Error: " . mysqli_error($dbc));

	if(mysqli_num_rows($r) == 0) // Campaign Name is Unique
	{
		if(mysqli_num_rows($rr) == 0) // Keyword is Unique to user
		{

		//Add campaign to database
		$q = "INSERT INTO campaigns (user_id, name, description, password, keyword , reply, creation_date) VALUES ('$user', '$name', '$desc', '$pw', '$kw', '$reply', NOW() )";
		$r = mysqli_query ($dbc, $q) or trigger_error("Query: $q\n<br />MySQL Error: " . mysqli_error($dbc));

		//If it ran OK
		if(mysqli_affected_rows($dbc) == 1)
		{	

			echo '<p>Campaign Successfully Created</p>';	
			include('footer.html');
			exit();
		}
		else
		{
			echo '<p>Campaign could not be created</p>';
		}
		}
		else
		{
			echo 'Keyword is not unique';
		}
	}
	else //campaign name is not UNIQUE
	{
		echo 'Your campaign name is not unique';
	}
}
	else
	{
		echo 'There was a problem creating your new campaign';
	}

	mysqli_close($dbc);
}

Link to comment
https://forums.phpfreaks.com/topic/232464-inserting-into-database-problems/
Share on other sites

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.