kaimason1 Posted January 22, 2009 Share Posted January 22, 2009 This is the second of my two php problems. I am creating a post page in php and I am getting the following error message : Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource Here is the script that gives me said error message: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> Populate Database </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> <H1>Add Contact</H1> <FORM METHOD="GET" action="post.php"> <P>First Name</P><input type="text" name="first" size=20 maxlength=50> <P>Last Name</P><input type="text" name="last" size=20 maxlength=50> <P>Cell Phone</P><input type="text" name="cell" size=20 maxlength=50> <P>Email</P><input type="text" name="email" size=20 maxlength=50> <INPUT type="hidden" <P></P><INPUT type="submit" value="ADD CONTACT"> </FORM> </BODY> </HTML> <? //Retrieve Variables// $first=@$_GET['first']; $last=@$_GET['last']; $cell=@$_GET['cell']; $email=@$_GET['email']; //Make sure all variables submitted// if($first==""){ die("Enter First Name... please?"); }else if($last==""){ die("Enter Last Name... please?"); }else if($cell==""){ die("Enter Cell Phone... please?"); }else if($email==""){ die("Enter Email... please?"); }else{ //Connect to database// $username="mekam2_kai"; $password="753159"; $database="mekam2_kai1"; mysql_connect(localhost,$username,$password)or die("I couldnt connect, sir. By the way, am I being paid for acting like a personal butler?"); @mysql_select_db($database)or die("The database you are trying to access is currently unavailable. Curious..."); //Query statement// $query="INSERT INTO TEST1_contacts VALUES(,$first,$last,$cell,$email)"; mysql_query($query); $query="SELECT * FROM `TEST1_contacts` WHERE `First_Name`=$first"; $result=mysql_query($query); $num=mysql_num_rows($result); if($num==0){ die("Could not insert data"); }else if($num>1){ $query="SELECT * FROM TEST1_contacts WHERE Last_Name=$last"; $result=mysql_query($query); $num=mysql_num_rows($result); if($num==0){ die("Could not insert data"); }else if($num>1){ $query="SELECT * FROM TEST1_contacts WHERE Phone_Number=$cell"; $result=mysql_query($query); $num=mysql_num_rows($result); if($num==0){ die("Could not insert data"); }else if($num>1){ $query="SELECT * FROM TEST1_contacts WHERE Email=$email"; $result=mysql_query($query); $num=mysql_num_rows($result); if($num==0){ die("Could not insert data"); } } } } } $i=0; while($i<$num); $first=mysql_result($result,$i,"First_Name"); $last=mysql_result($result,$i,"Last_Name"); $cell=mysql_result($result,$i,"Phone_Number"); $email=mysql_result($result,$i,"Email"); echo "Inserted $first $last Cell: $cell Email: $email"; ?> Quote Link to comment Share on other sites More sharing options...
haku Posted January 22, 2009 Share Posted January 22, 2009 First, as I mentioned in your other thread, get rid of the @ marks. You are using mysql_num_rows in a few spots in that script. Which one is throwing up the error? Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 22, 2009 Author Share Posted January 22, 2009 O.K.... my posting page is error message central now... help? ??? ??? ??? Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'censored'@'localhost' (using password: NO) in ... on line 24 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in ... on line 24 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'censored'@'localhost' (using password: NO) in ... on line 25 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in ... on line 25 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'censored'@'localhost' (using password: NO) in ... on line 26 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in ... on line 26 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'censored'@'localhost' (using password: NO) in ... on line 27 Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: A link to the server could not be established in ... on line 27 Enter First Name... please? <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> Populate Database </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> <H1>Add Contact</H1> <FORM METHOD="GET" action="post.php"> <P>First Name</P><input type="text" name="first" size=20 maxlength=50> <P>Last Name</P><input type="text" name="last" size=20 maxlength=50> <P>Cell Phone</P><input type="text" name="cell" size=20 maxlength=50> <P>Email</P><input type="text" name="email" size=20 maxlength=50> <INPUT type="hidden" <P></P><INPUT type="submit" value="ADD CONTACT"> </FORM> </BODY> </HTML> <? //Retrieve Variables// $first=trim(mysql_real_escape_string($_GET['first'])); $last=trim(mysql_real_escape_string($_GET['last'])); $cell=trim(mysql_real_escape_string($_GET['cell'])); $email=trim(mysql_real_escape_string($_GET['email'])); //Make sure all variables submitted// if($first==""){ die("Enter First Name... please?"); }else if($last==""){ die("Enter Last Name... please?"); }else if($cell==""){ die("Enter Cell Phone... please?"); }else if($email==""){ die("Enter Email... please?"); }else{ //Connect to database// $username=""; $password=""; $database=""; mysql_connect(localhost,$username,$password)or die("I couldnt connect, sir. By the way, am I being paid for acting like a personal butler?"); mysql_select_db($database)or die("The database you are trying to access is currently unavailable. Curious..."); //Query statement// $query="INSERT INTO TEST1_contacts VALUES(,$first,$last,$cell,$email)"; mysql_query($query); $query="SELECT * FROM `TEST1_contacts` WHERE `First_Name`=$first"; $result=mysql_query($query); $num=mysql_num_rows($result); if($num==0){ die("Could not insert data"); }else if($num>1){ $query="SELECT * FROM TEST1_contacts WHERE Last_Name=$last"; $result=mysql_query($query); $num=mysql_num_rows($result); if($num==0){ die("Could not insert data"); }else if($num>1){ $query="SELECT * FROM TEST1_contacts WHERE Phone_Number=$cell"; $result=mysql_query($query); $num=mysql_num_rows($result); if($num==0){ die("Could not insert data"); }else if($num>1){ $query="SELECT * FROM TEST1_contacts WHERE Email=$email"; $result=mysql_query($query); $num=mysql_num_rows($result); if($num==0){ die("Could not insert data"); } } } } } $i=0; while($i<$num); $first=mysql_result($result,$i,"First_Name"); $last=mysql_result($result,$i,"Last_Name"); $cell=mysql_result($result,$i,"Phone_Number"); $email=mysql_result($result,$i,"Email"); echo "Inserted $first $last Cell: $cell Email: $email"; ?> Quote Link to comment Share on other sites More sharing options...
mike12255 Posted January 22, 2009 Share Posted January 22, 2009 i dont think your connected to a database? try setting those variables after your connected Quote Link to comment Share on other sites More sharing options...
haku Posted January 22, 2009 Share Posted January 22, 2009 That is correct. mysql_real_escape_string() takes a database connection as a variable (it takes the default connection if this is left blank), and checks the collocation of the database. If you haven't connected, it cannot work. Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 22, 2009 Author Share Posted January 22, 2009 Back to the basics: I'm getting this simpler error again. Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in ... on line 49 Could not insert data Quote Link to comment Share on other sites More sharing options...
haku Posted January 22, 2009 Share Posted January 22, 2009 which one is line 49? Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 22, 2009 Author Share Posted January 22, 2009 lines 48, 49, and 50 are as follows: (this is the first set of the repitition near the end) $result=mysql_query($query); $num=mysql_num_rows($result); if($num==0){ Quote Link to comment Share on other sites More sharing options...
haku Posted January 22, 2009 Share Posted January 22, 2009 Probably a query error. Try this: $result=mysql_query($query); $num=mysql_num_rows($result) OR die(mysql_error()); if($num==0){ Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 22, 2009 Author Share Posted January 22, 2009 the revised error is: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in ... on line 49 Unknown column '(what i enter for First Name)' in 'where clause' Quote Link to comment Share on other sites More sharing options...
haku Posted January 22, 2009 Share Posted January 22, 2009 $query="SELECT * FROM `TEST1_contacts` WHERE `First_Name`=$first"; Remove the single quotes from around First_Name. Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 22, 2009 Author Share Posted January 22, 2009 did that... problems still there... Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 23, 2009 Author Share Posted January 23, 2009 RRRRGGHHHH.... I'm getting zero... nothing... here's my script... <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> Populate Database </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> <H1>Add Contact</H1> <FORM METHOD="GET" action="post.php"> <P>First Name</P><input type="text" name="first" size=20 maxlength=50> <P>Last Name</P><input type="text" name="last" size=20 maxlength=50> <P>Cell Phone</P><input type="text" name="cell" size=20 maxlength=50> <P>Email</P><input type="text" name="email" size=20 maxlength=50> <INPUT type="hidden" <P></P><INPUT type="submit" value="ADD CONTACT"> </FORM> </BODY> </HTML> <? //Connect to database// $username="mekam2_kai"; $password="753159"; $database="mekam2_kai1"; mysql_connect(localhost,$username,$password)or die("I couldnt connect, sir. By the way, am I being paid for acting like a personal butler?"); mysql_select_db($database)or die("The database you are trying to access is currently unavailable. Curious..."); //Retrieve Variables// $first=trim(mysql_real_escape_string($_GET['first'])); $last=trim(mysql_real_escape_string($_GET['last'])); $cell=trim(mysql_real_escape_string($_GET['cell'])); $email=trim(mysql_real_escape_string($_GET['email'])); //Make sure all variables submitted// if($first == "") { die("Enter First Name... please?"); } else if($last == "") { die("Enter Last Name... please?"); } else if($cell == "") { die("Enter Cell Phone... please?"); } else if($email == "") { die("Enter Email... please?"); }else{ //Query statement// $query="INSERT INTO TEST1_contacts VALUES(,$first,$last,$cell,$email)"; mysql_query($query); $query="SELECT * FROM TEST1_contacts WHERE First_Name LIKE '$first'"; $result=mysql_query($query); $num=mysql_num_rows($result) OR die(mysql_error()); if($num==0) { die("Could not insert data"); } else if($num>1) { $query="SELECT * FROM TEST1_contacts WHERE Last_Name LIKE '$last'"; $result=mysql_query($query); $num=mysql_num_rows($result) OR die(mysql_error()); if($num==0) { die("Could not insert data"); } else if($num>1) { $query="SELECT * FROM TEST1_contacts WHERE Phone_Number LIKE '$cell'"; $result=mysql_query($query); $num=mysql_num_rows($result) OR die(mysql_error()); if($num==0) { die("Could not insert data"); } else if($num>1) { $query="SELECT * FROM TEST1_contacts WHERE Email LIKE '$email'"; $result=mysql_query($query); $num=mysql_num_rows($result) OR die(mysql_error()); if($num==0) { die("Could not insert data"); } } } } } $i=0; while($i<$num){ $firstname=mysql_result($result,$i,"First_Name"); $lastname=mysql_result($result,$i,"Last_Name"); $cellphone=mysql_result($result,$i,"Phone_Number"); $emailaddress=mysql_result($result,$i,"Email"); echo "Inserted $firstname $lastname Cell: $cellphone Email: $emailaddress"; ++$i; } ?> Quote Link to comment Share on other sites More sharing options...
MadTechie Posted January 23, 2009 Share Posted January 23, 2009 try this <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> Populate Database </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> <H1>Add Contact</H1> <FORM METHOD="GET" action="post.php"> <P>First Name</P><input type="text" name="first" size=20 maxlength=50> <P>Last Name</P><input type="text" name="last" size=20 maxlength=50> <P>Cell Phone</P><input type="text" name="cell" size=20 maxlength=50> <P>Email</P><input type="text" name="email" size=20 maxlength=50> <INPUT type="hidden" <P></P><INPUT type="submit" value="ADD CONTACT"> </FORM> </BODY> </HTML> <? //Connect to database// $username="mekam2_kai"; $password="753159"; $database="mekam2_kai1"; mysql_connect(localhost,$username,$password)or die("I couldnt connect, sir. By the way, am I being paid for acting like a personal butler?"); mysql_select_db($database)or die("The database you are trying to access is currently unavailable. Curious..."); //Retrieve Variables// $first=trim(mysql_real_escape_string($_GET['first'])); $last=trim(mysql_real_escape_string($_GET['last'])); $cell=trim(mysql_real_escape_string($_GET['cell'])); $email=trim(mysql_real_escape_string($_GET['email'])); //Make sure all variables submitted// if($first == "") { die("Enter First Name... please?"); } else if($last == "") { die("Enter Last Name... please?"); } else if($cell == "") { die("Enter Cell Phone... please?"); } else if($email == "") { die("Enter Email... please?"); }else{ //Query statement// $query="INSERT INTO TEST1_contacts VALUES('$first','$last','$cell','$email')"; mysql_query($query); $query="SELECT * FROM TEST1_contacts WHERE First_Name = '$first'"; $result=mysql_query($query); $num=mysql_num_rows($result) OR die(mysql_error()); if($num==0) { die("Could not insert data"); } else if($num>1) { $query="SELECT * FROM TEST1_contacts WHERE Last_Name = '$last'"; $result=mysql_query($query); $num=mysql_num_rows($result) OR die(mysql_error()); if($num==0) { die("Could not insert data"); } else if($num>1) { $query="SELECT * FROM TEST1_contacts WHERE Phone_Number = '$cell'"; $result=mysql_query($query); $num=mysql_num_rows($result) OR die(mysql_error()); if($num==0) { die("Could not insert data"); } else if($num>1) { $query="SELECT * FROM TEST1_contacts WHERE Email = '$email'"; $result=mysql_query($query); $num=mysql_num_rows($result) OR die(mysql_error()); if($num==0) { die("Could not insert data"); } } } } } $i=0; while($i<$num){ $firstname=mysql_result($result,$i,"First_Name"); $lastname=mysql_result($result,$i,"Last_Name"); $cellphone=mysql_result($result,$i,"Phone_Number"); $emailaddress=mysql_result($result,$i,"Email"); echo "Inserted $firstname $lastname Cell: $cellphone Email: $emailaddress"; ++$i; } ?> Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 23, 2009 Author Share Posted January 23, 2009 tried it, no result STILL :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ :-\ Quote Link to comment Share on other sites More sharing options...
MadTechie Posted January 23, 2009 Share Posted January 23, 2009 okay update this line $query="INSERT INTO TEST1_contacts VALUES('$first','$last','$cell','$email')"; to $query="INSERT INTO TEST1_contacts (`First_Name`, `Last_Name`, `Phone_Number`, `Email`)VALUES('$first','$last','$cell','$email')"; Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 23, 2009 Author Share Posted January 23, 2009 tried it, didn't work. reverted to earlier version Quote Link to comment Share on other sites More sharing options...
gevans Posted January 23, 2009 Share Posted January 23, 2009 Just a personal note, I think that's the most annoying footer I've ever seen. It's kind of put me off reading the thread. (I might have a browse though) Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 23, 2009 Author Share Posted January 23, 2009 sorry... couldn't think of anything else : Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 23, 2009 Share Posted January 23, 2009 can you post your database please for this project..... im not going to guess your database format, i can tell that others are trying to guess the database names ? Quote Link to comment Share on other sites More sharing options...
gevans Posted January 23, 2009 Share Posted January 23, 2009 Well, I've got your code re-written a bit, if it works happy days, if not it might give you a decent error, try this; <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE> Populate Database </TITLE> <META NAME="Generator" CONTENT="EditPlus"> <META NAME="Author" CONTENT=""> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> </HEAD> <BODY> <H1>Add Contact</H1> <FORM METHOD="GET" action="post.php"> <P>First Name</P><input type="text" name="first" size=20 maxlength=50> <P>Last Name</P><input type="text" name="last" size=20 maxlength=50> <P>Cell Phone</P><input type="text" name="cell" size=20 maxlength=50> <P>Email</P><input type="text" name="email" size=20 maxlength=50> <INPUT type="hidden" <P></P><INPUT type="submit" value="ADD CONTACT"> </FORM> </BODY> </HTML> <?php //Retrieve Variables// $first = $_GET['first']; $last = $_GET['last']; $cell = $_GET['cell']; $email = $_GET['email']; //Make sure all variables submitted// if(empty($first)) { die("Enter First Name... please?"); } else if(empty($last)) { die("Enter Last Name... please?"); } else if(empty($cell)) { die("Enter Cell Phone... please?"); } else if(empty($email)) { die("Enter Email... please?"); } else { //Connect to database// $username = "mekam2_kai"; $password = "753159"; $database = "mekam2_kai1"; mysql_connect('localhost',$username,$password)or die("I couldnt connect, sir. By the way, am I being paid for acting like a personal butler?"); mysql_select_db($database) or die("The database you are trying to access is currently unavailable. Curious..."); //Query statement// $query = "INSERT INTO TEST1_contacts VALUES(NULL,'$first','$last','$cell','$email')"; mysql_query($query) or die(mysql_error()); $query = "SELECT * FROM `TEST1_contacts` WHERE `First_Name`='$first'"; $result = mysql_query($query) or die(mysql_error()); $num = mysql_num_rows($result); if(!$num) { die("Could not insert data"); } else if($num>=1) { $query = "SELECT * FROM TEST1_contacts WHERE Last_Name='$last'"; $result = mysql_query($query) or die(mysql_error()); $num = mysql_num_rows($result); if(!$num) { die("Could not insert data"); } else if($num>=1) { $query = "SELECT * FROM TEST1_contacts WHERE Phone_Number='$cell'"; $result = mysql_query($query) or die(mysql_error()); $num = mysql_num_rows($result); if(!$num) { die("Could not insert data"); } else if($num>=1) { $query = "SELECT * FROM TEST1_contacts WHERE Email='$email'"; $result = mysql_query($query) or die(mysql_error()); $num = mysql_num_rows($result); if(!$num){ die("Could not insert data"); } else { for($i=0;$i<$num;$i++) { $first = mysql_result($result,$i,"First_Name"); $last = mysql_result($result,$i,"Last_Name"); $cell = mysql_result($result,$i,"Phone_Number"); $email = mysql_result($result,$i,"Email"); echo "Inserted $first $last Cell: $cell Email: $email"; } } } } } } ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 23, 2009 Share Posted January 23, 2009 if you post the database info then we can do a proper test otherwise where all guessing should off worked buy now. Quote Link to comment Share on other sites More sharing options...
kaimason1 Posted January 23, 2009 Author Share Posted January 23, 2009 AND THAT SOLVES IT, THANK YOU GEVANS! AND THANK YOU EVERYONE WHO SPENT VALUABLE TIME HELPING ME! Quote Link to comment Share on other sites More sharing options...
redarrow Posted January 23, 2009 Share Posted January 23, 2009 so it was the database connection then 'localhost' and variable names all wrong. Quote Link to comment Share on other sites More sharing options...
gevans Posted January 23, 2009 Share Posted January 23, 2009 There were a few problems, localhost not wrapped in quotes, suppresing the select database error (so could run the script without a database selected), variables not wrapped with quotes in queries, setting id to NULL (I'm guessing it's id), checking the count is greater than one rather that greater than or equal and a very strange syntax for a while loop at the end! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.