开发者

variable values not passing into function?

i have a php file that sends data to a mysql database. when i try to send the data get this:

ERROR! EMPTY RESULTINSERT INTO email (First, Last, Full, Email, CountryCoude, Phone, Company, Serial) VALUES ('','',' ','benespitz@gmail.com',,,'Webscan, Inc.','TEST mySQL')BenSpitzbenespitz@gmail.com+1303-459-4521Webscan, Inc.TEST mySQL

as you can see from the output, the variables i put into my insert function are not empty but the values being sent in the query are. why is this?

non-confidential part of the php file:

insert($firstname,$lastname,$email,$countrycode,$phonenumber,$companyname,$serialnumber);
$result = mysql_query("SELECT DISTICT * FROM email");
if (empty($result)) {
    echo "ERROR! EMPTY RESULT";
}
else {
    print_r($result);
}
echo "INSERT INTO email (First, Last, Full, Email, CountryCoude, Phone, Company, Serial)
    VALUES ('".$first."','".$last."','".$first." ".$last."','".$email."',".$code.",".$phone.",'".$company."','".$serial."')";
echo $firstname.$lastname.$email.$countrycode.$phonenumber.$companyname.$serialnumber;
?>
</div>
</div>
</body>
<?php
function insert ($first, $last, $email, $code, $phone, $company, $serial) {
    mysql_query("INSERT INTO email (First, 开发者_StackOverflow中文版Last, Full, Email, CountryCoude, Phone, Company, Serial)
    VALUES ('".$first."','".$last."','".$first." ".$last."','".$email."',".$code.",".$phone.",'".$company."','".$serial."')");
}
?>


empty is a PHP feature that checks PHP variables for certain conditions.

It does not have knowledge of MySQL libraries and the fact that your $result is just a resource handle. (Indeed, $result is never "empty"; it's always an object, and it's always a resource handle, even if it's a handle to an empty resultset.)

Instead of this:

if (empty($result)) {
    echo "ERROR! EMPTY RESULT";
}

write:

if (!mysql_num_rows($result)) {
    echo "ERROR! EMPTY RESULT";
}

Similarly, print_r($result) isn't going to do what you think. Look at the documentation for the library you're using, and use its API. There are loads of examples out there about how to iterate a resultset that you retrieved via mysql_query; I am not going to repeat them here.


You also misspelt DISTINCT. Add proper error checking/handling to your code.


Don't be misled by the red herring of your broken debug output:

echo "INSERT INTO email (First, Last, Full, Email, CountryCoude, Phone, Company, Serial)
    VALUES ('".$first."','".$last."','".$first." ".$last."','".$email."',".$code.",".$phone.",'".$company."','".$serial."')";

You used the wrong variable names here. You did not make this mistake in the function that actually performs the insert. (I don't understand why this debug output isn't inside that function, where this sort of mistake would be really obvious and easy to avoid.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜