Using PHP to check for MySQL value and INSERT if not exists

I am working with 2 database tables: SPONSORS and ADINFO

I have a form that will change which adspace a sponsor’s ad goes in. Here’s what I’m trying to do

ON FORM SUBMIT:
Check to see if company_name exists in ADINFO table
If it DOES exist, UPDATE adspace value in ADINFO table
If it DOES NOT exist, copy company_name AND url from SPONSORS table and INSERT into ADINFO table, along with adspace data.

I’ve tried a few things all morning, none of which are working at all.

[php] function procEditSpace(){
global $session, $database, $form;
$compname = $_POST[‘compname’];
$adspace = $_POST[‘adspace’];

$q1 = ("SELECT company_name,url FROM sponsors WHERE company_name='$compname'");
$result = $database -> query($q1);

if($q3 = ("INSERT INTO adinfo ('company_name','url','adspace') VALUES('$compname','".$result['url']."','$adspace')
		WHERE NOT EXISTS company_name='$compname'")){
	$database -> query($q3);}
else{
	$q2 = ("UPDATE adinfo SET adspace='$adspace' WHERE EXISTS company_name='$compname'");
$database -> query($q2);	
}

} //bracket from procEditSpace

  //* Admin submitted Edit Ad Space form 
  if(isset($_POST['subadspace'])){
     procEditSpace();
  }

[/php]

And I tried this, too.
[php] function procEditSpace(){
global $session, $database, $form;
$compname = $_POST[‘compname’];
$adspace = $_POST[‘adspace’];

$q1 = ("SELECT company_name,adspace FROM adinfo WHERE EXISTS company_name=$compname");
$database ->query($q1);
$mysqlcomp  = mysql_result($result,$i,"company_name");

if(!$compname = $mysqlcomp){
	while ($row = mysql_fetch_assoc($compname)){
	$q = ("UPDATE adinfo SET adspace='$adspace' WHERE company_name='$compname' ");
	$database -> query($q);
	echo "SUCCESS!";
	}}
else{
	$q2 = ("SELECT url FROM sponsors WHERE company_name=$compname");
	$result = $database2 -> query($q2);
	$url = mysql_result($result,$i,"url");
	$q = ("INSERT INTO adinfo (company_name,adspace,url) 
     VALUES('$compname','$adspace','$url') ");
	$database -> query($q);
	echo "FAILURE!";
	 } 

} //bracket from procEditSpace

  //* Admin submitted Edit Ad Space form 
  if(isset($_POST['subadspace'])){
     procEditSpace();
  }

[/php]

I’ve been searching online for a couple days and can’t seem to find something. Can anyone get me started?

Thanks in advance!

Hey…
I created this code… Hope it works
[php]

<?php $company_name = $_REQUEST['company_name']; $sql1 = mysql_query("SELECT * FROM ADINFO WHERE company_name = '$company_name'"); if($sql1) { mysql_query("UPDATE ADINFO SET adspace = '$adspace' WHERE company_name = '$company_name'") or die("Update error"); }else{ $result = mysql_query("SELECT * FROM SPONSORS WHERE company_name = '$company_name'"); while ($row=mysql_fetch_array($result)) { $url = $row['url']; } mysql_query("INSERT INTO ADINFO (company_name,url) VALUES ('$company_name','$url')"); } ?>

Cheers
[/php]

Thank you so much for doing this!

I’m getting the following error:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/n/g/s/ngsolitaries/html/admin/ad_process.php on line 123

Referring to the following line:
while ($row = mysql_fetch_array($result)) {

so I changed the previous line to:
$result = mysql_query(“SELECT * FROM sponsors WHERE company_name = ‘$company_name’”) or die(mysql_error());

But it’s still not working. If it doesn’t exist, it just does nothing… As a work around, I am having everything that is inserted into “sponsors” also have the company_name and url inserted in to "adinfo’, then when showing the table for adinfo, I only display those that have an adspace value.

Thanks for trying. If you have any other ideas, I really would prefer to do it this way than the way I’m currently doing it.

have you tried simplifying things count the number of results there if is no results insert otherwise run an update:

[php]
//make sure your connected to your database first!

$compname = mysql_real_escape_string($_POST[‘compname’]);//collect data and make safe for DB

$result = mysql_query(“SELECT * FROM sponsors WHERE company_name = ‘$compname’”) or die(mysql_error());
$number = mysql_num_rows($result);

if($number == 0){
//no results run your insert code
} else {
//there’s a match so run an update
}
[/php]

Hi again…
check if the table name is correct… if it is, then try this:
[php]
mysql_query("SELECT * FROM sponsors WHERE company_name = ‘$company_name’);
[/php]
and continue the while loop…

Sponsor our Newsletter | Privacy Policy | Terms of Service