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 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应