douju1365 2012-08-03 21:47
浏览 4
已采纳

MySQL选择问题

I'm building a website to learn coding. I have a claim.php which is a claim your business page (its just a place that you can search for a business and then click a link to addclaimedbiz.php) and a addclaimedbiz.php page which adds the claimed business to the database.

First of all, heres my sql code for the user where the business id will be added to the row biz:

`id` int(15) NOT NULL AUTO_INCREMENT,
`firstname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
`email` varchar(75) NOT NULL,
`password` varchar(50) NOT NULL,
`biz` int(1) NOT NULL,
`verifiedbiz` int(1) NOT NULL,

and heres my sql code for the businesses:

`id` int(15) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`phone` varchar(14) NOT NULL,
`claimed` int(1) NOT NULL,

Now heres the code for claim.php that sends the company id to addclaimedbiz.php. ( I only posted a little but can post more if you need it!)

 while($row = mysql_fetch_array($result))
            {
            $id=$row['id'];
            $company_name=$row['name'];
            $company_phone=$row['phone'];
            $company_address=$row['address'];
            $address2=$row['address2'];
            $company_city=$row['city'];
            $company_zip=$row['zipcode'];
            $cat1=$row['cat1'];
            $cat2=$row['cat2'];
            $cat3=$row['cat3'];
            $subcat1=$row['subcat1'];
            $subcat2=$row['subcat2'];
            $subcat3=$row['subcat3'];
            $claimed=$row['claimed'];
            //Start While Loop
            echo"
            <div class='listing'>
                                    <li>
                                        <span class='bphone'>$company_phone</span>
                                        <span class='bname'>$company_name</span>
                                        <br/>
                                        <div class='blocation'>$company_address, $company_city, CO $company_zip ";if($address2 != ""){echo"(".$address2.")";}echo"</div>
                                        <br/>
                                        ";if($claimed != 1){echo "<a href='addclaimedbiz.php?id=".$id."'><button>Claim Business</button></a>";}else{echo "Already Claimed";}echo"
                                    </li>

                                <!--/Listing-->

                </div>";

            }

And then heres my code for addclaimedbiz.php:

 <?
 $biz_id = $_REQUEST['id'];

 //This next select from db is to take the id of the company and get the name for the corresponding id

 include("./config.php");
 $result = mysql_query("SELECT * FROM company WHERE id = '$biz_id'") or die(mysql_error());
 while($row = mysql_fetch_array($result))
 {
    $business_name = $row['name'];
 }

 ?>

and then to echo out $business_name i use (this is on addclaimedbiz.php as well just down the page a bit)

<?php if($biz == "0"){

            echo "You are claiming:"$business_name " as your business";

            }
            else{"You are already have a claimed business"}
            ?>

What should be happening is it should echo out You are claiming: $business_name as your business but right now its leaving out $business_name. Why is this?

Major thanks to all help!

Oh and I left out some of the rows in my databases that were unnecessary to the problem I thought, but if you need them I can definitely post!


EDIT


my new code looks like this:

<?
error_reporting(E_ALL);
$auth = $_COOKIE["auth"];
if ($auth != "1"){
header("Location: ./signin.php");
}
$firstname = $_COOKIE['firstname'];
$id = $_COOKIE['id'];
$fname = ucwords($_COOKIE['firstname']);
$lname = ucwords($_COOKIE['lastname']);
$email = $_COOKIE['email'];
$city = ucwords($_COOKIE['city']);
$biz = ucwords($_COOKIE['biz']);

$biz_id = $_REQUEST['id'];

include("./config.php");
$result = mysql_query("SELECT * FROM company WHERE id = '$biz_id'") or die(mysql_error());
if(mysql_num_rows($result)){
    while($row = mysql_fetch_assoc($result)){
        $business_name = $row['name'];
    }
}
echo count($result);
?>
  • 写回答

4条回答 默认 最新

  • douzhi3454 2012-08-03 21:50
    关注

    You're not using proper variable interpolation in your string, put $business_name inside the qutoes:

    echo "You are claiming:$business_name as your business";
    

    Ah, also looks like you want mysql_fetch_assoc, not mysql_fetch_array:

    while($row = mysql_fetch_assoc($result)){
        $business_name = $row['name'];
    }
    

    Docs here.

    Hmmm...I have a hunch, try wrapping your while block with:

    if(mysql_num_rows($result)){
       ...   
    }
    

    I'll let someone else do the lecturing on the mysql_* functions. :)

    Cheers

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程