พบกับบทความเกี่ยวกับคอมพิวเตอร์และ programming ได้ที่ http://articles.modoeye.com
PHP development / ช้อมูลไม่แสดงขึ้นมา
Anan
Anan
Fri 13 Oct 2006 01:05:39

สวัสดีครับ

ผมเขียน code program ดังนี้ครับ

<?
function randomstr ($length)
{
    $possible = 'abcdefghijklmnopqrstuvwxyz0198765432';
    $str=" ";
    while ( strlen ($str) < $length)
    {
        $str .= substr ($possible, (rand() % strlen($possible)), 1);
    }
    return ($str);
}
?>
<h3>New user registration</h3>
<?
$exit=0;
$link=mysql_connect("localhost","root","");

if(!$link)
{
    print("Error");
}else{
    mysql_select_db("member",$link);
    $sql="select email1 from member where email1='$email1'";
    $res=mysql_query($sql, $link);
    $n=mysql_num_rows($res);
    if($n==0){
        $sql="insert into member (username,password,company,cperson,address,country,phone,fax,email1,email2,web,productd,agri,alkohol,nonal,bakery,cannedfood,cereals,candies,sugar,coffee,condiments,foodin,
 fruits,dressings,meat,milk,oil,organic,pasta,ready,seafood,mail,fee,status,dtnow)
 values('$email1','$password','$company','$cperson','$address','$country','$phone','$fax','$email1','$email2','$web','$productd','$agri','$alkohol','$nonal','$bakery','$cannedfood',
 '$cereals','$candies','$sugar','$coffee','$condiments','$foodin','$fruits','$dressings','$meat','$milk','$oil','$organic','$pasta','$ready','$seafood','$mail','$fee','0',CURRENT_TIMESTAMP)";
        $res =mysql_query($sql, $link);
        if($res==1){
            print("Please check your information again:");
            $row=mysql_fetch_array($res);
            ?>
            <p><form method="post" action="update.php">
            Company:<input type="text" name="company" value="<? echo $row["company"];?>"><br>
           Contact person:<input type="text" name="cperson" value="<?=$row[4]?>"><br>
            Address:<input type="text" name="address" value="<?=$row[5]?>"><br>
            <? $password=randomstr(6); ?>
            Country:<select name="country"><option value="Albania"<? echo(($row["albania"]==albania)?"selected":"");?>>Albania</option><option value="Algeria"<? echo(($row["algeria"]==algeria)?"selected":"");?>>Algeria</option><option value="Argentina"<? echo(($row["argentina"]==argentina)?"selected":"");?>>Argentina</option></select>
            Phone:<input type="text" name="phone" value="<?=$row[7]?>"><br>
            Fax:<input type="text" name="fax" value="<?=$row[8]?>"><br>
            Email_1:<input type="text" name="email1" value="<?=$row[9]?>"><br>
            Email_2:<input type="text" name="email2" value="<?=$row[10]?>"><br>
            Web:<input type="text" name="web" value="<?=$row[11]?>"><br>
            Product detail:<input type="text" name="productd" value="<?=$row[12]?>"><br>
            Product group:<br>
            <input type="checkbox" name="agri" value="1"<? echo(($row["agri"]==1)?"checked":"");?>>Agricultural Products<br>
            <input type="checkbox" name="alkohol" value="1"<? echo(($row["alkohol"]==1)?"checked":"");?>>Alkoholic Beverages<br>
            <input type="checkbox" name="bakery"value="1"<? echo(($row["bakery"]==1)?"checked":"");?>>Bakery Products<br>
            Would you like to receive food news?<br>
            <input type="radio" name="mail" value="receive"<? echo (($row["mail"]=="1")?" checked":""); ?>>Receive<br>
            <input type="radio" name="mail" value="nreceive"<? echo (($row["mail"]=="0")?" checked":""); ?>>Not receive
            Member fee
            <input type="radio" name="fee" value="1"<? echo (($row["fee"]=="1")?" checked":""); ?>>USD150/3 months<br>
            <input type="radio" name="fee" value="2"<? echo (($row["fee"]=="2")?" checked":""); ?>>USD200/6 months<br>
            <input type="radio" name="fee" value="3"<? echo (($row["fee"]=="3")?" checked":""); ?>>USD250/3 months<br>
            <p><input type="submit" value="Confirm"></p>
            </form>
            <?
        }else{
            print("Error to register.");
        }
    }else{
        print("This email already exists.");
    }
}
?>
 

ปรากฎว่าเกิด error ดังนี้ครับ

New user registration

Please check your information again:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\appserv\www\main\member\member_regist.php on line 34

Company:
Contact person:
Address:
Country: Phone:
Fax:
Email_1:
Email_2:
Web:
Product detail:
Product group:
Agricultural Products
Alkoholic Beverages
Bakery Products
Would you like to receive food news?
Receive
Not receive Member fee USD150/3 months
USD200/6 months
USD250/3 months

และข้อมูลก็ไม่สามารถดึงขึ้นมาจากฐานข้อมูลได้ครับ เป็นเพียงแต่ช่องว่าง ช่วยแนะนำด้วยครับ

และข้อมูลก็ไม่สามารถดึงขึ้นมาจากฐานข้อมูลได้ครับ เป็นเพียงแต่ช่องว่าง ช่วยแนะนำด้วยครับ

Administrator
Fri 13 Oct 2006 02:09:21
ตัวแปร $res นั้นถูก assign ค่าครั้งสุดท้ายก่อนที่คุณจะนำมาใส่ mysql_fetch_array() นั้นคือถูกใช้ในคำสั่ง insert เนื่องจากคำสั่ง insert นั้นจะเป็น resource ชนิดที่เก็บ affectd row ครับไม่สามารถนำมา fetch ได้ครับ หลังจาก insert แล้วถ้าคุณต้องการนำข้อมูลมาแสดงต้องทำการ SELECT อีกครั้งครับ
Anan
Anan
Fri 13 Oct 2006 06:58:46

ขอบคุณครับ ผมได้ทำการ select แต่ข้อมูลก็ยังไม่ขึ้นครับ

<?
function randomstr ($length)
{
    $possible = 'abcdefghijklmnopqrstuvwxyz0198765432';
    $str=" ";
    while ( strlen ($str) < $length)
    {
        $str .= substr ($possible, (rand() % strlen($possible)), 1);
    }
    return ($str);
}
?>
 <h3>New user registration</h3>
 <?
 $exit=0;
 $link=mysql_connect("localhost","root","");

 if(!$link)
 {
 print("Error");
 }else{
 mysql_select_db("member",$link);
 $sql="select email1 from member where email1='$email1'";
 $res=mysql_query($sql, $link);
 $n=mysql_num_rows($res);
  if($n==0){
  $sql="insert into member (username,password,company,cperson,address,country,phone,fax,email1,email2,web,productd,agri,alkohol,nonal,bakery,cannedfood,cereals,candies,sugar,coffee,condiments,foodin,
  fruits,dressings,meat,milk,oil,organic,pasta,ready,seafood,mail,fee,status,dtnow)
  values('$email1','$password','$company','$cperson','$address','$country','$phone','$fax','$email1','$email2','$web','$productd','$agri','$alkohol','$nonal','$bakery','$cannedfood',
  '$cereals','$candies','$sugar','$coffee','$condiments','$foodin','$fruits','$dressings','$meat','$milk','$oil','$organic','$pasta','$ready','$seafood','$mail','$fee','0',CURRENT_TIMESTAMP)";
 $res=mysql_query($sql, $link);
 if($res==1){
  print("Please check your information again:");
  
                 $sql="select email1 from member where email1='$email1'";
                 $res=mysql_query($sql, $link);
                 $n=mysql_num_rows($res);
  $row=mysql_fetch_array($res);
?>
<p><form method="post" action="update.php">
Company:<input type="text" name="company" value="<? echo $row["company"];?>"><br>
Contact person:<input type="text" name="cperson" value="<?=$row[4]?>"><br>
Address:<input type="text" name="address" value="<?=$row[5]?>"><br>
<? $password=randomstr(6); ?>
Country:<select name="country"><option value="Albania"<? echo(($row["country"]==albania)?"selected":"");?>>Albania</option><option value="Algeria"<? echo(($row["country"]==algeria)?"selected":"");?>>Algeria</option><option value="Argentina"<? echo(($row["country"]==argentina)?"selected":"");?>>Argentina</option></select>
Phone:<input type="text" name="phone" value="<?=$row[7]?>"><br>
Fax:<input type="text" name="fax" value="<?=$row[8]?>"><br>
Email_1:<input type="text" name="email1" value="<?=$row[9]?>"><br>
Email_2:<input type="text" name="email2" value="<?=$row[10]?>"><br>
Web:<input type="text" name="web" value="<?=$row[11]?>"><br>
Product detail:<input type="text" name="productd" value="<?=$row[12]?>"><br>
Product group:<br>
<input type="checkbox" name="agri" value="1"<? echo(($row["agri"]==1)?"checked":"");?>>Agricultural Products<br>
<input type="checkbox" name="alkohol" value="1"<? echo(($row["alkohol"]==1)?"checked":"");?>>Alkoholic Beverages<br>
<input type="checkbox" name="bakery"value="1"<? echo(($row["bakery"]==1)?"checked":"");?>>Bakery Products<br>
Would you like to receive food news?<br>
<input type="radio" name="mail" value="receive"<? echo (($row["mail"]=="1")?" checked":""); ?>>Receive<br>
<input type="radio" name="mail" value="nreceive"<? echo (($row["mail"]=="0")?" checked":""); ?>>Not receive
Member fee
<input type="radio" name="fee" value="1"<? echo (($row["fee"]=="1")?" checked":""); ?>>USD150/3 months<br>
<input type="radio" name="fee" value="2"<? echo (($row["fee"]=="2")?" checked":""); ?>>USD200/6 months<br>
<input type="radio" name="fee" value="3"<? echo (($row["fee"]=="3")?" checked":""); ?>>USD250/3 months<br>
<p><input type="submit" value="Confirm"></p>
</form>
<?
        }else{
            print("Error to register.");
        }

ผลที่แสดงได้ดังนี้ครับ

New user registration

Please check your information again:

Company:
Contact person:
Address:
Country: Phone:
Fax:
Email_1:
Email_2:
Web:
Product detail:
Product group:
Agricultural Products
Alkoholic Beverages
Bakery Products
Would you like to receive food news?
Receive
Not receive Member fee USD150/3 months
USD200/6 months
USD250/3 months

รบกวนช่วยแนะนำด้วยครับ

อนันต์

รบกวนช่วยแนะนำด้วยครับ

อนันต์

Administrator
Sat 14 Oct 2006 00:44:49
คุณทำการ SELECT เพียง field เดียวนี่ครับ
$sql="select email1 from member where email1='$email1'";
Reply
Name:
E-mail:
Home | Services | Forum | Classified | Directories | Support | Contact
ATOM feed RSS 0.9 feed RSS 1.0 feed RSS 2.0 feed
Copyright © 2005 - 2007 Modoeye.com, All Rights Reserved.
Disclaimer | Privacy policy | Term of Use | Term of Services
Valid XHTML Valid CSS! PHP: Hypertext Preprocessor MySQL database Apache Powered! FreeBSD Power to serve
Modoeye Sitemap Client login