Page 1 of 1

PDO statement Insert Into with where not

Posted: 17 Oct 2019, 11:49
by JanMolendijk
Chef hope you do fine.... I readed many documents about PDO insert into, mostly with the function where not exists.

I tried also some things into the code under but I did not get a good result.

It is for a request-sending but if someone request for a second-time I would like to detect it with the function "where not".

Code: Select all

$stmt = $DB_con->prepare('INSERT INTO topic_requests(over_all_id,title,id_requester,name_requester,e_mail_requester,ip_adres) VALUES(:over_all_id, :title, :id_requester, :name_requester, :e_mail_requester, :ip_adres)');
Upper-code is from this script

Code: Select all

<?php

	error_reporting( ~E_NOTICE ); // avoid notice
	
	require_once 'dbconfignotice.php';
	
	if(isset($_POST['submit']))
	{
		$over_all_id = $_POST['over_all_id'];// user email
		$title = $_POST['title'];// user email
		$id_requester = $_POST['id_requester'];// user email

		$name_requester = $_POST['name_requester'];// user email

		$e_mail_requester = $_POST['e_mail_requester'];// user email

		$ip_adres = $_POST['ip_adres'];// user email
		
		// if no error occured, continue ....
		if(!isset($errMSG))
		{
			$stmt = $DB_con->prepare('INSERT INTO topic_requests(over_all_id,title,id_requester,name_requester,e_mail_requester,ip_adres) VALUES(:over_all_id, :title, :id_requester, :name_requester, :e_mail_requester, :ip_adres)');

			$stmt->bindParam(':over_all_id',$over_all_id);

			$stmt->bindParam(':title',$title);
			$stmt->bindParam(':id_requester',$id_requester);

			$stmt->bindParam(':name_requester',$name_requester);

			$stmt->bindParam(':e_mail_requester',$e_mail_requester);
			$stmt->bindParam(':ip_adres',$ip_adres);
			
			if($stmt->execute())
			{
				$successMSG = "<script type= 'text/javascript'>alert('New Album Inserted Successfully');</script>";
				header("<script type= 'text/javascript'>alert('New Album Inserted Successfully');</script>"); // redirects image view page after 5 seconds.
			}
			else
			{
				$errMSG = "error while inserting....";
			}
		}
	}
?>

PDO statement Insert Into with where not

Posted: 17 Oct 2019, 12:31
by Admin
The query: "Insert into ... where not exists ..." is used when you want to insert something in database when a specified data not exists in that table.

I didn't work with such sql queries, but try this:

Code: Select all

$stmt = $DB_con->prepare('INSERT INTO topic_requests (over_all_id,title,id_requester,name_requester,e_mail_requester,ip_adres) SELECT :over_all_id, :title, :id_requester, :name_requester, :e_mail_requester, :ip_adres WHERE NOT EXISTS (Select title, name_requester From topic_requests Where title=:title And name_requester=:name_requester) LIMIT 1');
- If it not work, try look on the net for: mysql Insert Into with where not.