php mysql insert and check.

I have an aray that needs to be inserted into two tables.
 in table 1 it has an ID and a name
This table cannot have duplicates.
So I need to loop through the array to inset the names into table 1.
If the name is in the table don't try to insert it just get the ID.
Then insert the ID and $NCID to table 2

If there is no record in Table 1 insert the record then get its ID
Then insert the ID and $NCID to table 2
The ID in table 1 is auto increment.


Array (
[cast] => Array
        (
            [0] => Sam Worthington
            [1] => Zoe Saldana
            [2] => Sigourney Weaver
            [3] => Stephen Lang
            [4] => Joel Moore
            [5] => Giovanni Ribisi
            [6] => Michelle Rodriguez
          
        )
)

Open in new window

LVL 3
jbrashear72Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kumaranmcaCommented:
Try the below code....resolve your problem...

change the variable name for array and table name...

<?php

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);


if (!mysql_query($sql,$con))
for($i=0;$i<count($arrvalue);$i++){
$sql="INSERT INTO table_name1 (Name) VALUES ('$arrvalue[$i]')";
mysql_query($sql);
$last_insert_id=last_insert_id()
$insert_id=mysql_insert_id() ;
$sql1="INSERT INTO table_name2 (NCID) VALUES ('$insert_id')";
mysql_query($sql1);


}

?>
<?php 

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);


if (!mysql_query($sql,$con))
for($i=0;$i<count($arrvalue);$i++){
$sql="INSERT INTO table_name1 (Name) VALUES ('$arrvalue[$i]')";
mysql_query($sql);
$last_insert_id=last_insert_id()
$insert_id=mysql_insert_id() ;
$sql1="INSERT INTO table_name2 (NCID) VALUES ('$insert_id')";
mysql_query($sql1);


}

?>

Open in new window

0
nasirbestCommented:
what is $NCID here I assume by $NCID you mean array index. here is code
foreach ($cast as $ncid => $person) {
    $id = $mysql_result(mysql_query("SELECT id FROM tbl_one WHERE name='$person'"));
    if (!$id) {
        $result = mysql_query(insert into tbl_one (name) values ('$person'));
        $id = mysql_insert_id($result);
    }
    $result = mysql_query("insert into tbl_two (id, ncid) values ($id, $ncid)");
}

Open in new window

0
kumaranmcaCommented:
Kindly remove the code for my program... its mistake...refer the new code below

$last_insert_id=last_insert_id()

<?php 

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);


if (!mysql_query($sql,$con))
for($i=0;$i<count($arrvalue);$i++){
$sql="INSERT INTO table_name1 (Name) VALUES ('$arrvalue[$i]')";
mysql_query($sql);
$insert_id=mysql_insert_id() ;
$sql1="INSERT INTO table_name2 (NCID) VALUES ('$insert_id')";
mysql_query($sql1);


}

?>

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

jbrashear72Author Commented:
@kumaranmca

$ncid = "2314";

in tabl two there are two items I need to add..
$ncid and the insert ID from table_1.

Also the first query:
for($i=0;$i<count($arrvalue);$i++){
$sql="INSERT INTO table_name1 (Name) VALUES ('$arrvalue[$i]')";
mysql_query($sql);
It is not checking for duplicate names?
0
kumaranmcaCommented:
Try the below code...I correct your needs...
<?php 

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);


if (!mysql_query($sql,$con))
for($i=0;$i<count($arrvalue);$i++){
$check_pre_name_qry="select * from table_name1 where name='".$arrvalue[$i]."'";
$count_result=mysql_query($check_pre_name_qry);
if(mysql_num_rows($count_result)==0){
	$sql="INSERT INTO table_name1 (Name) VALUES ('$arrvalue[$i]')";
	mysql_query($sql);
	$insert_id=mysql_insert_id() ;
	
	$sql1="INSERT INTO table_name2 (NCID,name) VALUES ('$insert_id',$arrvalue[$i])";
	mysql_query($sql1);
}


}

?>

Open in new window

0
kumaranmcaCommented:
i forgot....single quote for second adding table query

$sql1="INSERT INTO table_name2 (NCID,name) VALUES ('$insert_id',$arrvalue[$i])";

New one...
$sql1="INSERT INTO table_name2 (NCID,name) VALUES ('$insert_id','$arrvalue[$i]')";

Note:

Kindly change table name and field name as you using...that...

0
kumaranmcaCommented:
Now its ok...if you have any issue..please tell me.

I will rectify..
0
jbrashear72Author Commented:
@ kumaran I aperciate your help. My kids are here with me so I need to break for a day. I will let you know as soon as I have tried it.
Thank you again.
0
Mehul_PanchalCommented:
<?php

$con = mysql_connect("localhost","peter","abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("my_db", $con);


if (!mysql_query($sql,$con))
for($i=0;$i<count($arrvalue);$i++){
$sql="INSERT INTO table_name1 (Name) VALUES ('$arrvalue[$i]')";
mysql_query($sql);
$last_insert_id=last_insert_id()
$insert_id=mysql_insert_id() ;
$sql1="INSERT INTO table_name2 (NCID) VALUES ('$insert_id')";
mysql_query($sql1);


}

?>
0
jbrashear72Author Commented:
@ Mehul
Does this look right?
<?php

//MySQL connection higher up in the code.

if (!mysql_query($sql,$con))
for($i=0;$i<count($arrvalue);$i++){
$sql="INSERT INTO actor (Name) VALUES ('$arrvalue[$i]')";
mysql_query($sql);
$last_insert_id=last_insert_id()
$insert_id=mysql_insert_id() ;
$sql1="INSERT INTO movie_actor (movie_id,actor_id) VALUES ('$ncid', '$insert_id')";
mysql_query($sql1);


}
?>

Open in new window

0
jbrashear72Author Commented:
@ Mehul_Pancha
How are we checking to see if the record is already inserted into the table1 first? The name of the table is actor:
$sql="INSERT INTO actor (name) VALUES ('$arrvalue[$i]')";

The actor name may already be in the table. If it is we do not want to re insert it.
0
jbrashear72Author Commented:
We need to check to see if the actor's name is already in the table first. if the actor is already in table one we need to get  the actor ID and continue to insert the actors ID and $ncid into table 2.
<?

if (!mysql_query($sql,$con))
for($i=0;$i<count($arrvalue);$i++){
// CHECK HERE for actor. if actor is here just get its ID and do the
// table two insert.
$sql="INSERT INTO actor (name) VALUES ('$arrvalue[$i]')";
mysql_query($sql);
$last_insert_id=last_insert_id()
$insert_id=mysql_insert_id() ;
$sql1="INSERT INTO movie_actor (movie_id,actor_id) VALUES ('$ncid', '$insert_id')";
mysql_query($sql1);


}  
?>

Open in new window

0
kumaranmcaCommented:
Hi jbrashear72:,

Try my below code and change the table name and field name.
<?php 

$arrvalue=array('cast'=>array('Sam Worthington','Zoe Saldana','Sigourney Weaver','Stephen Lang','Joel Moore','Giovanni Ribisi','Michelle Rodriguez','Stephen Lang'));

print '<pre>';
print_r($arrvalue);
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("evakenney", $con);


if (!mysql_query($sql,$con))
for($i=0;$i<count($arrvalue['cast']);$i++){
$check_pre_name_qry="select * from table_name1 where name='".$arrvalue['cast'][$i]."'";
$count_result=mysql_query($check_pre_name_qry);
if(mysql_num_rows($count_result)==0){
        $sql="INSERT INTO table_name1 (Name) VALUES ('".$arrvalue['cast'][$i]."')";
        mysql_query($sql);
        $insert_id=mysql_insert_id() ;
        
		$sql1="INSERT INTO table_name2 (NCID,name) VALUES ('$insert_id','".$arrvalue['cast'][$i]."')";
        mysql_query($sql1);
}else{
	echo 'this user name already exists';
}


}

?>

Open in new window

0
kumaranmcaCommented:
i forgot specify the repeated name on the array...now i added my code...
<?php 

$arrvalue=array('cast'=>array('Sam Worthington','Zoe Saldana','Sigourney Weaver','Stephen Lang','Joel Moore','Giovanni Ribisi','Michelle Rodriguez','Stephen Lang'));

print '<pre>';
print_r($arrvalue);
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("evakenney", $con);


if (!mysql_query($sql,$con))
for($i=0;$i<count($arrvalue['cast']);$i++){
$check_pre_name_qry="select * from table_name1 where name='".$arrvalue['cast'][$i]."'";
$count_result=mysql_query($check_pre_name_qry);
if(mysql_num_rows($count_result)==0){
        $sql="INSERT INTO table_name1 (Name) VALUES ('".$arrvalue['cast'][$i]."')";
        mysql_query($sql);
        $insert_id=mysql_insert_id() ;
        
		$sql1="INSERT INTO table_name2 (NCID,name) VALUES ('$insert_id','".$arrvalue['cast'][$i]."')";
        mysql_query($sql1);
}else{
	echo $arrvalue['cast'][$i].'user name already exists';
}


}

?>

Open in new window

0
jbrashear72Author Commented:
@kumaranmca I will try this right now.
0
kumaranmcaCommented:
ok...kindly change db connection property also....
0
jbrashear72Author Commented:
Warning:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/beta/imdb/test.php on line 19


19:   $count_result=mysql_query($check_pre_name_qry);
<?
$arrvalue=array('cast'=>array('Sam Worthington','Zoe Saldana','Sigourney Weaver','Stephen Lang','Joel Moore','Giovanni Ribisi','Michelle Rodriguez','Stephen Lang'));

print '<pre>';
print_r($arrvalue);
$con = mysql_connect("localhost","root","#######");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $con);


if (!mysql_query($sql,$con))
for($i=0;$i<count($arrvalue['cast']);$i++){
$check_pre_name_qry="select * from actor where name='".$arrvalue['cast'][$i]."'";
$count_result=mysql_query($check_pre_name_qry);
if(mysql_num_rows($count_result)==0){
        $sql="INSERT INTO actor (name) VALUES ('".$arrvalue['cast'][$i]."')";
        mysql_query($sql);
        $insert_id=mysql_insert_id() ;

                $sql1="INSERT INTO movie_actor (NCID,name) VALUES ('$insert_id','".$arrvalue['cast'][$i]."')";
        mysql_query($sql1);
}else{
        echo $arrvalue['cast'][$i].'user name already exists';
}


}


?>

Open in new window

0
kumaranmcaCommented:
Hi

Now i print the query...kindly run and copy the paste  printed query and check in phpmyadmin..the query getting values or not...

please confirm...




<?
$arrvalue=array('cast'=>array('Sam Worthington','Zoe Saldana','Sigourney Weaver','Stephen Lang','Joel Moore','Giovanni Ribisi','Michelle Rodriguez','Stephen Lang'));

print '<pre>';
print_r($arrvalue);
$con = mysql_connect("localhost","root","#######");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $con);


if (!mysql_query($sql,$con))
for($i=0;$i<count($arrvalue['cast']);$i++){
echo $check_pre_name_qry="select * from actor where name='".$arrvalue['cast'][$i]."'";
$count_result=mysql_query($check_pre_name_qry);
if(mysql_num_rows($count_result)==0){
        $sql="INSERT INTO actor (name) VALUES ('".$arrvalue['cast'][$i]."')";
        mysql_query($sql);
        $insert_id=mysql_insert_id() ;

                $sql1="INSERT INTO movie_actor (NCID,name) VALUES ('$insert_id','".$arrvalue['cast'][$i]."')";
        mysql_query($sql1);
}else{
        echo $arrvalue['cast'][$i].'user name already exists';
}


}


?>

Open in new window

0
jbrashear72Author Commented:
select * from actor where name='Sam Worthington'

Warning:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/beta/imdb/test.php on line 19

select * from actor where name='Zoe Saldana'

Warning:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/beta/imdb/test.php on line 19

select * from actor where name='Sigourney Weaver'

Warning:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/beta/imdb/test.php on line 19

select * from actor where name='Stephen Lang'

Warning:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/beta/imdb/test.php on line 19

select * from actor where name='Joel Moore'

Warning:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/beta/imdb/test.php on line 19

select * from actor where name='Giovanni Ribisi'

Warning:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/beta/imdb/test.php on line 19

select * from actor where name='Michelle Rodriguez'

Warning:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/beta/imdb/test.php on line 19

select * from actor where name='Stephen Lang'

Warning:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/beta/imdb/test.php on line 19
0
jbrashear72Author Commented:
ouch
its actor_name
0
kumaranmcaCommented:
okay, no problem...change fieldname and execute...
0
kumaranmcaCommented:
You got any error. let me know...
0
jbrashear72Author Commented:
Ok this is what I have.
Also the else needs to do a actor insert into the movie_actor table.
the $ncid is coming as a post:
$ncid =_POST['2343'];


<?
$arrvalue=array('cast'=>array('Sam Worthington','Zoe Saldana','Sigourney Weaver','Stephen Lang','Joel Moore','Giovanni Ribisi','Michelle Rodriguez','Stephen Lang'));

print '<pre>';
print_r($arrvalue);
$con = mysql_connect("localhost","root","########");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $con);


if (!mysql_query($sql,$con))
for($i=0;$i<count($arrvalue['cast']);$i++){
echo $check_pre_name_qry="select * from actor where name='".$arrvalue['cast'][$i]."'";
$count_result=mysql_query($check_pre_name_qry);
if(mysql_num_rows($count_result)==0){
        $sql="INSERT INTO actor (actor_name) VALUES ('".$arrvalue['cast'][$i]."')";
        mysql_query($sql);
        $insert_id=mysql_insert_id() ;

                $sql1="INSERT INTO movie_actor (movie_id,actor_id) VALUES ('$ncid','$insert_id')";
        mysql_query($sql1);
}else{
// If actor exists I need to do the table 2 insert.
        echo $arrvalue['cast'][$i].'user name already exists';
}


}


?>

Open in new window

0
kumaranmcaCommented:
Assign the value in variable then execute...your file...its come $_POST['2343'] same or dynamic...

if dynamic based on post value assign to $ncid...

            $ncid =$_POST['2343'];
<?
$arrvalue=array('cast'=>array('Sam Worthington','Zoe Saldana','Sigourney Weaver','Stephen Lang','Joel Moore','Giovanni Ribisi','Michelle Rodriguez','Stephen Lang'));

print '<pre>';
print_r($arrvalue);
$con = mysql_connect("localhost","root","########");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $con);


if (!mysql_query($sql,$con))
for($i=0;$i<count($arrvalue['cast']);$i++){
echo $check_pre_name_qry="select * from actor where name='".$arrvalue['cast'][$i]."'";
$count_result=mysql_query($check_pre_name_qry);
if(mysql_num_rows($count_result)==0){
        $sql="INSERT INTO actor (actor_name) VALUES ('".$arrvalue['cast'][$i]."')";
        mysql_query($sql);
        $insert_id=mysql_insert_id() ;
		$ncid =$_POST['2343'];

                $sql1="INSERT INTO movie_actor (movie_id,actor_id) VALUES ('$ncid','$insert_id')";
        mysql_query($sql1);
}else{
// If actor exists I need to do the table 2 insert.
        echo $arrvalue['cast'][$i].'user name already exists';
}


}


?>

Open in new window

0
kumaranmcaCommented:
is it ok?
0
jbrashear72Author Commented:
It looks like it is adding the actors even if they are in the database.
If they are in the actor table we don't want to insert them again.
0
jbrashear72Author Commented:
OK I just needed to clear the database.
OK here is what I have.

What I need to do
is after }else{

I need to delete all records from movie_actor where movie_id = $ncid
then add the actors and ncid to that table.
<?
$arrvalue=array('cast'=>array('Sam Worthington','Zoe Saldana','Sigourney Weaver','Stephen Lang','Joel Moore','Giovanni Ribisi','Michelle Rodriguez','Stephen Lang'));

print '<pre>';
print_r($arrvalue);
$con = mysql_connect("localhost","root","modz4700");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $con);


if (!mysql_query($sql,$con))
for($i=0;$i<count($arrvalue['cast']);$i++){
$check_pre_name_qry="select * from actor where actor_name='".$arrvalue['cast'][$i]."'";
$count_result=mysql_query($check_pre_name_qry);
if(mysql_num_rows($count_result)==0){
        $sql="INSERT INTO actor (actor_name) VALUES ('".$arrvalue['cast'][$i]."')";
        mysql_query($sql);
        $insert_id=mysql_insert_id() ;

                $sql1="INSERT INTO movie_actor (movie_id,actor_id) VALUES ('$ncid','$insert_id')";
        mysql_query($sql1);
}else{
echo $arrvalue['cast'][$i].'user name already exists';
echo "<br />";
}


}


?>

Open in new window

0
jbrashear72Author Commented:
Also the first time I run this I get this:
Array
(
    [cast] => Array
        (
            [0] => Sam Worthington
            [1] => Zoe Saldana
            [2] => Sigourney Weaver
            [3] => Stephen Lang
            [4] => Joel Moore
            [5] => Giovanni Ribisi
            [6] => Michelle Rodriguez
            [7] => Stephen Lang
        )

)
Stephen Languser name already exists


Wonder why the last array it is telling me that it is already there?
0
kumaranmcaCommented:
ok, which field name you will check already exist or not...please
0
kumaranmcaCommented:
Hi jbrashear72:,

I have add "jbrashear72:" two times...for checking already exits or not....If you remove on last value of array.
$arrvalue=array('cast'=>array('Sam Worthington','Zoe Saldana','Sigourney Weaver','Stephen Lang','Joel Moore','Giovanni Ribisi','Michelle Rodriguez'));

Open in new window

0
jbrashear72Author Commented:
ok cool.

So the 2nd part of the scritp adding the actor_id and $ncid to the movie_actor table need to run.
So if the actor exists then we still need to add them to the 2nd table "movie_actor".
But we don't want to add it if it is already in this table.
0
jbrashear72Author Commented:
This is what I tried:
I got:

Parse error: syntax error, unexpected '=' in /var/www/html/beta/imdb/test.php on line 29
This was line 29:
sql3= mysql_query("SELECT actor_id FROM actor WHERE actor_name='".$arrvalue['cast'][$i]."'");
or die(mysql_error());
<?
$arrvalue=array('cast'=>array('Sam Worthington','Zoe Saldana','Sigourney Weaver','Stephen Lang','Joel Moore','Giovanni Ribisi','Michelle Rodriguez','Stephen Lang'));
$ncid = "2034";
print '<pre>';
print_r($arrvalue);
$con = mysql_connect("localhost","root","######");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $con);


if (!mysql_query($sql,$con))
for($i=0;$i<count($arrvalue['cast']);$i++){
$check_pre_name_qry="select * from actor where actor_name='".$arrvalue['cast'][$i]."'";
$count_result=mysql_query($check_pre_name_qry);
if(mysql_num_rows($count_result)==0){
        $sql="INSERT INTO actor (actor_name) VALUES ('".$arrvalue['cast'][$i]."')";
        mysql_query($sql);
        $insert_id=mysql_insert_id() ;
// need to clear the movie actors then insert them.
                 mysql_query("DELETE FROM movie_actor WHERE actor_id = $insert_id");
                $sql1="INSERT INTO movie_actor (movie_id,actor_id) VALUES ('$ncid','$insert_id')";
        mysql_query($sql1);
}else{
// If some actors exist then I need to go ahead and add them to the table but check to see if they are there first.
sql3= mysql_query("SELECT actor_id FROM actor WHERE actor_name='".$arrvalue['cast'][$i]."'");
or die(mysql_error());
                                while($row = mysql_fetch_array( $sql3 )) {
                                $actor_id = $row['actor_id'];
                                }
 mysql_query("DELETE FROM movie_actor WHERE actor_id = $actor_id");
                $sql1="INSERT INTO movie_actor (movie_id,actor_id) VALUES ('$ncid','$actor_id')";
        mysql_query($sql1);
} // end else


}


?>

Open in new window

0
kumaranmcaCommented:
Try now,,,,
<?
$arrvalue=array('cast'=>array('Sam Worthington','Zoe Saldana','Sigourney Weaver','Stephen Lang','Joel Moore','Giovanni Ribisi','Michelle Rodriguez','Stephen Lang'));
$ncid = "2034";
print '<pre>';
print_r($arrvalue);
$con = mysql_connect("localhost","root","######");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("test", $con);


if (!mysql_query($sql,$con))
for($i=0;$i<count($arrvalue['cast']);$i++){
$check_pre_name_qry="select * from actor where actor_name='".$arrvalue['cast'][$i]."'";
$count_result=mysql_query($check_pre_name_qry);
if(mysql_num_rows($count_result)==0){
        $sql="INSERT INTO actor (actor_name) VALUES ('".$arrvalue['cast'][$i]."')";
        mysql_query($sql);
        $insert_id=mysql_insert_id() ;
// need to clear the movie actors then insert them.
                 mysql_query("DELETE FROM movie_actor WHERE actor_id = $insert_id");
                $sql1="INSERT INTO movie_actor (movie_id,actor_id) VALUES ('$ncid','$insert_id')";
        mysql_query($sql1);
}else{
// If some actors exist then I need to go ahead and add them to the table but check to see if they are there first.
sql3= mysql_query("SELECT actor_id FROM actor WHERE actor_name='".$arrvalue['cast'][$i]."'");
                                while($row = mysql_fetch_array( $sql3 )) {
                                $actor_id = $row['actor_id'];
                                }
 mysql_query("DELETE FROM movie_actor WHERE actor_id = $actor_id");
                $sql1="INSERT INTO movie_actor (movie_id,actor_id) VALUES ('$ncid','$actor_id')";
        mysql_query($sql1);
} // end else


}


?>

Open in new window

0
jbrashear72Author Commented:
Parse error: syntax error, unexpected '=' in /var/www/html/beta/imdb/test.php on line 29
sql3= mysql_query("SELECT actor_id FROM actor WHERE actor_name='".$arrvalue['cast'][$i]."'");
                                while($row = mysql_fetch_array( $sql3 )) {
                                $actor_id = $row['actor_id'];
                                }
0
jbrashear72Author Commented:
My tables:
CREATE TABLE IF NOT EXISTS `actor` (
  `actor_id` mediumint(9) unsigned NOT NULL auto_increment,
  `actor_name` varchar(128) NOT NULL,
  PRIMARY KEY  (`actor_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC AUTO_INCREMENT=188 ;

CREATE TABLE IF NOT EXISTS `movie_actor` (
  `movie_id` int(11) unsigned NOT NULL default '0',
  `actor_id` int(11) unsigned NOT NULL default '0',
  PRIMARY KEY  (`movie_id`,`actor_id`),
  KEY `actor_id` (`actor_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ROW_FORMAT=FIXED;

Open in new window

0
kumaranmcaCommented:
i will fix
0
kumaranmcaCommented:
I have fixed the error try...

before
sql3=mysql_query("SELECT actor_id FROM actor WHERE actor_name='".$arrvalue['cast'][$i]."'");
now
$sql3=mysql_query("SELECT actor_id FROM actor WHERE actor_name='".$arrvalue['cast'][$i]."'");

$ missing..
<?php
$arrvalue=array('cast'=>array('Sam Worthington','Zoe Saldana','Sigourney Weaver','Stephen Lang','Joel Moore','Giovanni Ribisi','Michelle Rodriguez','Stephen Lang'));
$ncid = "2034";
print '<pre>';
print_r($arrvalue);
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("user", $con);


if (!mysql_query($sql,$con))
for($i=0;$i<count($arrvalue['cast']);$i++){
$check_pre_name_qry="select * from actor where actor_name='".$arrvalue['cast'][$i]."'";
$count_result=mysql_query($check_pre_name_qry);
if(mysql_num_rows($count_result)==0){
        $sql="INSERT INTO actor (actor_name) VALUES ('".$arrvalue['cast'][$i]."')";
        mysql_query($sql);
        $insert_id=mysql_insert_id() ;
// need to clear the movie actors then insert them.
                 mysql_query("DELETE FROM movie_actor WHERE actor_id = $insert_id");
                $sql1="INSERT INTO movie_actor (movie_id,actor_id) VALUES ('$ncid','$insert_id')";
        mysql_query($sql1);
}else{
// If some actors exist then I need to go ahead and add them to the table but check to see if they are there first.
$sql3=mysql_query("SELECT actor_id FROM actor WHERE actor_name='".$arrvalue['cast'][$i]."'");
                                while($row = mysql_fetch_array( $sql3 )) {
                                $actor_id = $row['actor_id'];
                                }
 mysql_query("DELETE FROM movie_actor WHERE actor_id = $actor_id");
                $sql1="INSERT INTO movie_actor (movie_id,actor_id) VALUES ('$ncid','$actor_id')";
        mysql_query($sql1);
} // end else


}


?>

Open in new window

0
kumaranmcaCommented:
Now, its okay...
0
jbrashear72Author Commented:
OK I got this:


Line 19: if(mysql_num_rows($count_result)==0){
arning:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/beta/imdb/test.php on line 19



Warning:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/beta/imdb/test.php on line 19



Warning:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/beta/imdb/test.php on line 19



Warning:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/beta/imdb/test.php on line 19



Warning:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/beta/imdb/test.php on line 19



Warning:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/beta/imdb/test.php on line 19



Warning:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/beta/imdb/test.php on line 19



Warning:  mysql_num_rows(): supplied argument is not a valid MySQL result resource in /var/www/html/beta/imdb/test.php on line 19


0
kumaranmcaCommented:
Hi,

I have checked my code...its working fine for me...now i try some other method to check the condition....kindly refer my code...below...
CREATE TABLE IF NOT EXISTS `actor` (
  `actor_id` mediumint(9) unsigned NOT NULL auto_increment,
  `actor_name` varchar(128) NOT NULL,
  PRIMARY KEY  (`actor_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC AUTO_INCREMENT=188 ;

CREATE TABLE IF NOT EXISTS `movie_actor` (
  `movie_id` int(11) unsigned NOT NULL default '0',
  `actor_id` int(11) unsigned NOT NULL default '0',
  PRIMARY KEY  (`movie_id`,`actor_id`),
  KEY `actor_id` (`actor_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ROW_FORMAT=FIXED;

<?php
$arrvalue=array('cast'=>array('Sam Worthington','Zoe Saldana','Sigourney Weaver','Stephen Lang','Joel Moore','Giovanni Ribisi','Michelle Rodriguez','Stephen Lang'));
$ncid = "2034";
print '<pre>';
print_r($arrvalue);
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("user", $con);


if (!mysql_query($sql,$con))
for($i=0;$i<count($arrvalue['cast']);$i++){
$check_pre_name_qry="select count(*) from actor where actor_name='".$arrvalue['cast'][$i]."'";
$count_result=mysql_query($check_pre_name_qry);
$result_arr=mysql_fetch_array($count_result);
echo $result_arr[0];
//if(mysql_num_rows($count_result)==0){
if($result_arr[0]==0){

        $sql="INSERT INTO actor (actor_name) VALUES ('".$arrvalue['cast'][$i]."')";
        mysql_query($sql);
        $insert_id=mysql_insert_id() ;
// need to clear the movie actors then insert them.
                 mysql_query("DELETE FROM movie_actor WHERE actor_id = $insert_id");
                $sql1="INSERT INTO movie_actor (movie_id,actor_id) VALUES ('$ncid','$insert_id')";
        mysql_query($sql1);
}else{
// If some actors exist then I need to go ahead and add them to the table but check to see if they are there first.
$sql3=mysql_query("SELECT actor_id FROM actor WHERE actor_name='".$arrvalue['cast'][$i]."'");
                                while($row = mysql_fetch_array( $sql3 )) {
                                $actor_id = $row['actor_id'];
                                }
 mysql_query("DELETE FROM movie_actor WHERE actor_id = $actor_id");
                $sql1="INSERT INTO movie_actor (movie_id,actor_id) VALUES ('$ncid','$actor_id')";
        mysql_query($sql1);
} // end else


}

?>

Open in new window

0
kumaranmcaCommented:
change DB connection property values...
0
kumaranmcaCommented:
its not coding issue...Kindly add the below code in your program..


You can use
    $vana=ini_set('mysql.trace_mode','Off');
    // do your $res1 and $res2 queries.
    ini_set('mysql.trace_mode',$vana);
for temporary disabling.
CREATE TABLE IF NOT EXISTS `actor` (
  `actor_id` mediumint(9) unsigned NOT NULL auto_increment,
  `actor_name` varchar(128) NOT NULL,
  PRIMARY KEY  (`actor_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC AUTO_INCREMENT=188 ;

CREATE TABLE IF NOT EXISTS `movie_actor` (
  `movie_id` int(11) unsigned NOT NULL default '0',
  `actor_id` int(11) unsigned NOT NULL default '0',
  PRIMARY KEY  (`movie_id`,`actor_id`),
  KEY `actor_id` (`actor_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 ROW_FORMAT=FIXED;

<?php


//You can use
    $vana=ini_set('mysql.trace_mode','Off');
    // do your $res1 and $res2 queries.
    ini_set('mysql.trace_mode',$vana);
//for temporary disabling.

$arrvalue=array('cast'=>array('Sam Worthington','Zoe Saldana','Sigourney Weaver','Stephen Lang','Joel Moore','Giovanni Ribisi','Michelle Rodriguez','Stephen Lang'));
$ncid = "2034";
print '<pre>';
print_r($arrvalue);
$con = mysql_connect("localhost","root","");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("user", $con);


if (!mysql_query($sql,$con))
for($i=0;$i<count($arrvalue['cast']);$i++){
$check_pre_name_qry="select count(*) from actor where actor_name='".$arrvalue['cast'][$i]."'";
$count_result=mysql_query($check_pre_name_qry);
$result_arr=mysql_fetch_array($count_result);
echo $result_arr[0];
//if(mysql_num_rows($count_result)==0){
if($result_arr[0]==0){

        $sql="INSERT INTO actor (actor_name) VALUES ('".$arrvalue['cast'][$i]."')";
        mysql_query($sql);
        $insert_id=mysql_insert_id() ;
// need to clear the movie actors then insert them.
                 mysql_query("DELETE FROM movie_actor WHERE actor_id = $insert_id");
                $sql1="INSERT INTO movie_actor (movie_id,actor_id) VALUES ('$ncid','$insert_id')";
        mysql_query($sql1);
}else{
// If some actors exist then I need to go ahead and add them to the table but check to see if they are there first.
$sql3=mysql_query("SELECT actor_id FROM actor WHERE actor_name='".$arrvalue['cast'][$i]."'");
                                while($row = mysql_fetch_array( $sql3 )) {
                                $actor_id = $row['actor_id'];
                                }
 mysql_query("DELETE FROM movie_actor WHERE actor_id = $actor_id");
                $sql1="INSERT INTO movie_actor (movie_id,actor_id) VALUES ('$ncid','$actor_id')";
        mysql_query($sql1);
} // end else


}

?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kumaranmcaCommented:
I think, your issue is resolved. If any issue let me know. I will fix it.
0
jbrashear72Author Commented:
You got it! thank you. This was crazy fun and hard for me.. I learned much,
Thank you.
Were did you learn all of this?
0
kumaranmcaCommented:
Hi jbrashear72,

Thank for your given points...by experience :)

Refer the following tutorial urls. Its might be helpful for you...

http://www.w3schools.com/php/
http://www.tizag.com/phpT/
http://php.net
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.