473,386 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Newbi

I am getting the following error at the line
mysql_free_result($result);
Warning: Supplied argument is not a valid MySQL result resource in
/var/www/html/php/phoneadd.php on line 27

My code is as follows:

<html>

<body>
<?php

if ($submit) {

// process form

$db = mysql_connect("localhost", "", "")
or die("Could not connect : " . mysql_error());

mysql_select_db("test",$db);

$sql = "INSERT INTO phone (fname,lname,street,h_phone) VALUES
('$fname','$lname','$street','$h_phone')";

$result = mysql_query($sql);

echo "Thank you! Information entered.\n";
/* Free resultset */
mysql_free_result($result);
/* Closing connection */
mysql_close($db);
} else{
// display form

?>
<form method="post" action="<?php echo $PHP_SELF?>">
<table>
<tr>
<td>First name: </td><td><input type="Text" name="fname"></td>
</tr>
<tr>
<td>Last name: </td><td> <input type="Text" name="lname"></td>
</tr>
<tr>

<td>Address: </td><td><input type="Text" name="street"></td>
</tr>
<tr>

<td>Home Phone:</td><td> <input type="Text" name="h_phone"></td>
</tr>
<tr>

<td></td><td> <input type="Submit" name="submit" value="Enter information">
</td>
</tr>
</table>
</form>

<?php
} // end if

?>

</body>

</html>

Jul 17 '05 #1
3 2164
mickey wrote:
I am getting the following error at the line
mysql_free_result($result);

Warning: Supplied argument is not a valid MySQL result resource in
/var/www/html/php/phoneadd.php on line 27

My code is as follows:
<snip>
$sql = "INSERT INTO phone (fname,lname,street,h_phone) VALUES
('$fname','$lname','$street','$h_phone')";

$result = mysql_query($sql);
echo "Thank you! Information entered.\n";

/* Free resultset */
mysql_free_result($result);


Quote from http://us2.php.net/mysql_query :

"Only for SELECT,SHOW,EXPLAIN or DESCRIBE statements mysql_query()
returns a resource identifier or FALSE if the query was not executed
correctly. For other type of SQL statements, mysql_query() returns TRUE
on success and FALSE on error. A non-FALSE return value means that the
query was legal and could be executed by the server. It does not
indicate anything about the number of rows affected or returned. It is
perfectly possible for a query to succeed but affect no rows or return
no rows."

You are trying to do an INSERT query, therefore, $result with be
*either* TRUE or FALSE, not a resource identifier as expected by
mysql_free_result...
--
Justin Koivisto - sp**@koivi.com
PHP POSTERS: Please use comp.lang.php for PHP related questions,
alt.php* groups are not recommended.
Jul 17 '05 #2
mickey wrote:
if ($submit) {
Your code won't run if PHP is configured with the default values. This
should be:

if (isset($_POST['submit'])) {

$sql = "INSERT INTO phone (fname,lname,street,h_phone) VALUES
('$fname','$lname','$street','$h_phone')";

$result = mysql_query($sql);

echo "Thank you! Information entered.\n";

/* Free resultset */
mysql_free_result($result);
You don't have a result set here. You did an INSERT, not a SELECT.
<form method="post" action="<?php echo $PHP_SELF?>">


Same as my first comment. This won't work with a default installation of
PHP. Use

<form method="post" action="<?php echo($_SERVER['PHP_SELF']); ?>">
I know that you didn't ask for the additional comments about PHP
defaults. But what will you do if your project has to run on a machine
with PHP defaults? So read the documentation, change your code now and
you won't have problems concerning compatibility between different
installations in the future.
Regards,
Matthias
Jul 17 '05 #3
On Thu, 29 Jul 2004 21:03:32 GMT, mickey <me**********@yahoo.com> wrote:
if ($submit) {
Not defined anywhere, assumes register_globals is on, which is deprecated.
$db = mysql_connect("localhost", "", "")
or die("Could not connect : " . mysql_error());
Here you check for errors.
mysql_select_db("test",$db);
Here you don't.
$sql = "INSERT INTO phone (fname,lname,street,h_phone) VALUES
('$fname','$lname','$street','$h_phone')";
Wide open for SQL injection attacks - you're trusting user input. Never trust
user input, treat it as evil data.
$result = mysql_query($sql);
Here you don't check for errors.
echo "Thank you! Information entered.\n";
That's not necessarily true, you haven't checked whether it was entered.
mysql_free_result($result);


Not needed; insert statements don't return a result set resource identifier
that needs closing in the first place, and even if they did, would fail if
there was an error - you can't close FALSE.

--
Andy Hassall <an**@andyh.co.uk> / Space: disk usage analysis tool
http://www.andyh.co.uk / http://www.andyhsoftware.co.uk/space
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Christoffer T | last post by:
Sorry for bad subjekt. I have just started to learn programing, and python sound like a god choice. But there is something I do not understand. I want to write the code in notepad, and then...
0
by: darren | last post by:
2
by: Amadelle | last post by:
Hi all and thanks in advance, I have written a class library in C# (a set of cs classes) which I would like to install on our production webserver. It is used by some of the asp.net pages that...
4
by: Coffee | last post by:
i want to transfer the content of a text box/message box to a string array. like i have the word "hellO" and want the h to be myvariable(1), the e myvariable(2) the l myvariable(3) and so on... can...
44
by: MadCrazyNewbie | last post by:
Hey Group, I keep getting a error saying "No Value Given For One Or More Parameters". Unfortunatly I don`t know where this error is coming from:(. Can I run through my code line by line, so I...
1
by: 6655326 | last post by:
Newbi needs a very small help, thank you very much. Hello everyone and thank you very much for your time. I Have a small db for invoicing and on my form (with a subform) there is a CANCEL...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.