开发者

Posting data from Database

I am validating a form (checking if field are empty e开发者_JAVA技巧tc and at the end I am using my last validation rule:

//Database Information
//Connect to database

mysql_connect($dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
mysql_select_db($dbname)or die(mysql_error());

$email = mysql_real_escape_string($_POST['email']);
$cust_code = mysql_real_escape_string($_POST['cust_code']);

//validation e.g.
if (empty($email) + empty($cust_code) > 1){
....

//if everything is ok
$sql = "SELECT * FROM clients WHERE ID='$cust_code'";
$result = mysql_query($sql);
if(mysql_num_rows($result) > 0){
$data = mysql_num_rows($result);

//get all fields from db and do something

}else{
//My error that is showing up
echo "<span class=\"difftext\">The customer code you have entered is not valid!
<br />
Please enter a valid Customer Code to procceed!
</span>";

Is anything wrong with that because even if I enter the correct cust_code I am getting my error msg instead of my data...

Thank you

EDIT...(I removed, as it is wrong) AND YOU DID WELL... I JUST REALISE WHAT I DID... SORRY...

I have corrected it above.

Thank you


HOW TO DEBUG

  1. Do not put the query string immediately into the mysql method, echo it first

    $sql = "SELECT * FROM clients WHERE ID='$cust_code'";
    echo $sql;
    $res=mysql_query($sql);

  2. Are you even connected to the DB?

  3. Error messages are written in English (if it is not MS error messages). Why would you ignore them? Put the error message, read it, try to understand what it says.

  4. An advice, if you will write code that way, it is ok for very small application, for big ones, you need to take a different approach completely to code organization. Which is one of the problems/main problem frameworks are trying to solve for you.


Actually, you are wrong, your error is here, in this two lines:

$sql = mysql_query("SELECT * FROM clients WHERE ID='$cust_code'");
$result = mysql_query($sql);

You are running the query twice.
After the first time $sql holds the resource, then you refer to the resource as if it was a query string. To fix it, change it to:

$sql = "SELECT * FROM clients WHERE ID='$cust_code'";
$result = mysql_query($sql);

You might have more underlying errors, but fix this one first.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜