Mon 9 Oct 2006 02:18:45
สวัสดีครับ
เขียนมารบกวนอีกแล้วครับ
ผมกำลังเขียนในส่วนโปรแกรมรับเมื่อ member เข้ามา register กรอกข้อมูลต่างๆ เรียบร้อยแล้ว กด enter ข้อมูลต่างๆจะส่งเข้าไปยัง ฐานข้อมูล mysql พร้อมทั้งแสดงข้อมูลขึ้นมาอีกครั้งให้กับ member ตรวจสอบว่าถูกต้องหรือไม่ ถ้าไม่ถูกต้องก็แก้ไขได้เลย หรือถ้าถูกต้องแล้ว member ก็กด confirm เพื่อเข้าไป update ข้อมูลอีกครั้งหนึ่ง
คำถามครับ
1. ผมเขียนโปรแกรมด้านล่างนี้ครับ แล้วเกิด error ดังนี้ครับ
Parse error: parse error, unexpected T_ELSE in c:\appserv\www\main\member\member_regist.php on line 83
พยายามหาก็หาไม่เจอ กรุณาช่วยแนะนำด้วยครับ
2. ตัว password นั้นผมใช้เป็นการ random ครับ แล้วข้อมูลจะถูกส่งไปเก็บยังฐานข้อมูล แต่ยังไม่ได้ส่งไปยัง member นะครับ จะรอจนกว่า ลูกค้าชำระค่า member เรียบร้อยแล้วจึงจะส่งไปทาง email ผมจึงกำหนด status ของ member เป็น 0 ก่อน เมื่อ ชำระเงินเรียบร้อยแล้ว จึงจะเปลี่ยนเป็น 1 ในภายหลัง ซึ่งผมเองก็กำลังถามตัวเองเหมือนกันว่าการที่ผมนำเอาเรื่อง status เข้ามาเกี่ยวข้อง คงจะต้องมีส่วนเกี่ยวข้องกับการเขียนโปรแกรมอย่างไร ซึ่งคงต้องคิดต่อว่า status ที่ผมนำมาเกี่ยวข้องนี้มีประโยชน์หรือไม่ ถ้าไม่มีคงต้องตัดทิ้งไป
3. ตัวแปร country ซึ่งเป็น list menu ชื่อประเทศต่างๆ ไม่ทราบว่าผมเขียนถูกหรือเปล่าครับ คือผมอยากให้ประเทศที่ member เลือกนั้นโชว์ขึ้นมาใน list ก่อน ถ้า member ต้องการเปลี่ยนแปลงก็ click เลือกประเทศอื่นต่อไป
<?
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="<?=$row[3]?>"><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="
<option value="
<option value="
</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.");
}
}
?>
if($res==1) กับ else print("Error to register."); คุณคงจะต้องการให้เป็นคู่เดียวกัน แต่ที่จับคู่กันไม่ได้เนื่องจาก if คุณไม่ใส่วงเล็บปีกกา {} เพื่อบอกขอบเขตครับ
2. ส่วนตัวแล้วผมยังไม่เห็นประโยชน์ของ flag ตัวนี้ครับ ต้องดูโปรเจ็คโดยรวม ใน phase อื่นๆว่าจะมีประโยชน์กับเราหรือไม่ครับ ถ้าไม่มีก็เอาออกดีกว่าครับ
3. <option value="Albania"<? echo(($row["albania"]==albania)?"selected":"");?>>
<option value="Algeria"<? echo(($row["algeria"]==algeria)?"selected":"");?>>
คุณต้องอ้างจากชื่อ field เดียวกันที่เก็บชื่อประเทศไว้ครับ เช่น
<option value="Algeria"<? echo(($row["country"]=="algeria")?"selected":"");?>>
คุณลืมใส่ double-quote เพื่อบ่งบอกว่าเป็น string ด้วยครับ (สีเขียว)
Comment:
1. $sql="insert into member....";
$res =mysql_query($sql, $link);
$row=mysql_fetch_array($res);
ตรงนี้ การใช้คำสั่ง INSERT นั้นจะได้เป็น result resource นั้นจะเป็นจำนวน record ที่มีผลครับ จะไม่สามารถนำมา fetch เพื่อให้ได้ข้อมูลได้ครับ
2. ปัญหาที่ไม่สามารถหาคู่ if...else... ได้นั้นผมขอแนะนำว่า เวลาเปิด if ก็ให้ใส่ {} รอไว้เลยถ้ามี else ก็ให้ใส่ else พร้อมทั้ง {} รอไว้เลยครับ แล้วค่อยมาใส่โค๊ดภายในทีหลังครับ จะช่วยแก้ปัญหาการจับคู่ได้ครับ
ถ้าที่ผมเข้าใจในคำถามแรกถูกต้องผม format code ให้ใหม่แล้วครับ
<?
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="<?=$row[3]?>"><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.");
}
}
?>
Tue 10 Oct 2006 01:10:23
ขอขอบคุณครับ ที่กรุณาแนะนำ
เกิด error ขึ้นดังนี้ครับ
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in c:\appserv\www\main\member\member_regist.php on line 31
เข้าใจว่าต้องเข้าไปเพิ่ม "select email1 from member where email1='$email1'"; แล้วทำการ query
มีปัญหา 2 ข้อเกิดขึ้น
1. form ที่ โชว์ขึ้นมาเป็น form ว่างๆ ไม่มีข้อมูลของ member ที่ดึงจากฐานช้อมูลขึ้นมาดชว์ เพื่อให้ member ตรวจดูว่าจะ confirm หรือ แก้ไข
2. ตัว password ไม่ได้บันทึกเข้าฐานข้อมูล mysql
<? $password=randomstr(6); ?>
ตอนนี้กำลังนั่งหาอยู่เลยครับว่าผิดตรงจุดไหน
ผมคิดว่าจะลองเขียนฟอร์มที่มีข้อมูลน้อยๆ แล้วลองดูใหม่ครับ
อนันต์
Tue 10 Oct 2006 18:43:22
สวัสดีครับ
พยายามแก้แล้วครับ แต่ไม่ออกสักที จึงต้องขอคำแนะนำดังนี้ครับ
โปรแกรมที่ 1 ชื่อว่า member_register.php เป็น form ให้ member เข้ามากรอกข้อมูลต่างๆดังนี้ครับ
<?
session_start();
error_reporting(E_ALL^E_NOTICE);
function passwdgen($len){
$code="abcdefghijkmnpqrstuvwxy123456789";
srand((double)microtime()*1000000);
for($i=0; $i<$len;$i++){
$password .=$code[rand()%strlen($code)];
}
return $password;
}
$_SESSION['ses_code']=passwdgen(5);
?>
<meta http-equiv="Content-Type" content="text/html; charset=TIS-620;">
<script language="javascript" type="text/javascript">
function validate(obj)
{
if(obj.company.value == ""){
alert("Please fill your company");
obj.company.focus();
return false;
}
else if(obj.country.value == "0"){
alert("Please select your country");
obj.country.focus();
return false;
}
else if(obj.email1.value == ""){
alert("Please fill your email in email1");
obj.email1.focus();
return false;
}
else if(!obj.agri.checked && !obj.alkohol.checked && !obj.nonal.checked && !obj.bakery.checked){
alert("Please select product group");
return false;
}
else if(obj.code.value == ""){
alert("Please fill code in box");
obj.code.focus();
return false;
}
return true;
}
</script>
<form name="form1" method="post" action="member_regist.php" onSubmit="return validate(this);">
<table border="0" cellspacing="1" cellpadding="1">
<tr><td><font color="#8B0000" face="verdana" size="1"><b><font color="#000000">*</font>Company Name: </b></font></td>
<td><input type="text" size="30" name="company" style="font-family:verdana; font-size: 10px; color:#000000;"></td></tr>
<tr><td><font color="#8B0000" face="verdana" size="1"><b><font color="#000000">*</font>Country: </b></font></td>
<td><select name="country">
<option value="0" style="font-family:verdana; font-size: 10px; color:#000000;">Please select</option>\n
<option value="
<option value="
<option value="
</select></td></tr>
<tr><td><font color="#8B0000" face="verdana" size="1"><b><font color="#000000">*</font>Email_1: </b></font></td>
<td><input type="text" size="30" name="email1" style="font-family:verdana; font-size: 10px; color:#000000;"></td></tr>
<tr><td><font color="#8B0000" face="verdana" size="1"><b>Product Detail: </b></font></td>
<td><textarea cols="30" row="10" name="productd" style="font-family:verdana; font-size: 10px; color:#000000;"></textarea></td></tr>
<tr><td height="30" valign="bottom"><font color="#8B0000" face="verdana" size="1"><b><font color="#000000">*</font>Product Group: </b></font></td></tr>
<tr>
<td></td>
<td><input type="checkbox" name="agri" value="1"><font color="#8B0000" face="verdana" size="1">Agricultural Products</font></td>
<td><input type="checkbox" name="alkohol" value="1"><font color="#8B0000" face="verdana" size="1">Alkoholic Beverages</font></td></tr>
<tr>
<td></td>
<td><input type="checkbox" name="nonal" value="1"><font color="#8B0000" face="verdana" size="1">Non Alkoholic Beverages</font></td>
<td><input type="checkbox" name="bakery" value="1"><font color="#8B0000" face="verdana" size="1">Bakery Products</font></td></tr>
<tr>
</table>
<table>
<tr>
<td height="30" valign="bottom"><font color="#8B0000" face="verdana" size="1"><b>Would you like to receive food news?<b></td></tr>
<tr><td><input type="radio" name="mail" value="1" checked><font color="#8B0000" face="verdana" size="1">Receive</td>
<td><input type="radio" name="mail" value="0"><font color="#8B0000" face="verdana" size="1">Not receive</td></tr>
</table>
<table border="0" cellspacing="1" cellpadding="1">
<tr>
<td height="30" valign="bottom"><font color="#8B0000" face="verdana" size="1"><b><font color="#000000">*</font>Member fee<b></td></tr>
<tr><td width=206><input type="radio" name="fee" value="1" checked><font color="#8B0000" face="verdana" size="1">USD150/3 months</td>
<td width=206><input type="radio" name="fee" value="2"><font color="#8B0000" face="verdana" size="1">USD200/6 months</td>
<td width=208><input type="radio" name="fee" value="3"><font color="#8B0000" face="verdana" size="1">USD300/year</td></tr>
<tr><td height="30" valign="bottom"><font color="#8B0000" face="verdana" size="1"><b>Please fill code below into this box.</b></td>
<td height="30" valign="bottom"><input type="text" name="code" size=8></td>
<tr><td height="30" valign="bottom"></td><td height="30" valign="bottom"><img src=imagettf.php></td></tr>
<tr><td height="50" valign="bottom"></td><td height="30" valign="bottom"><input type="submit" value="Member Register"></td></tr>
</table>
</form>
โปรแกรมนี้ผมจะมีการเขียนฟังก์ชั่น randomstr ซึ่งผมอ่านในหนังสือบอกว่าจะเป็นการป้องกันพวก robot เข้ามา register.
โปรแกรมที่ 2 ชื่อว่า member_regist.php ซึ่งเป็นโปรแกรมรับค่าจากแบบฟอร์มที่ 1 แล้วส่งข้อมูลเข้าไปเก็บในฐานข้อมูล Mysql ขณะเดียวกันตัวของโปรแกรมนี้ก็เป็นแบบฟอร์มเหมือนกัน โดยนำข้อมูลจากฐานข้อมูลใน Mysql ขึ้นมาให้ member ตรวจอีกครั้ง ซึ่ง member สามารถแก้ไข และกด confirm ได้เลย ข้อมูลที่ได้มาใหม่นี้ก็จะไป update ข้อมูลใน Mysql อีกครั้งหนึ่ง โปรแกรมผมเขียนดังนี้ครับ
<?
function randomstr ($length)
{
$possible = 'abcdefghijklmnopqrstuvwxyz0198765432';
$str=" ";
while ( strlen ($str) < $length)
{
$str .= substr ($possible, (rand() % strlen($possible)), 1);
}
return ($str);
}
?>
<h3>New user registration</h3>
<?
$link=mysql_connect("localhost","root","");
if(!$link)
{
print("Error");
}
else
{
mysql_select_db("test",$link);
$sql="select email1 from test where email1='$email1'";
$res=mysql_query($sql, $link);
$n=mysql_num_rows($res);
if($n==0)
{
$sql="insert into member (username,company,country,email1,productd,agri,alkohol,nonal,mail,fee,dtnow)
values('$email1','$company','$country','$email1','$productd','$agri','$alkohol','$nonal','$mail','$fee',CURRENT_TIMESTAMP)";
$res =mysql_query($sql, $link);
if($res==1)
{
print" Please confirm your information as below:
$sql="select email1 from test where email1='$email1'";
$res=mysql_query($sql, $link);
while($row=mysql_fetch_array($res, MYSQL_ASSOC))
{
?>
<form method="post" action="member_update.php">
Company:<input type="text" name="company" value="<?=.$row["company"].?>"><br>
Country:<select name="country">
<option value="
<option value="
<option value="
Email_1:<input type="text" name="email1" value="<?=.$row["email1"].?>"><br>
<?$password=randomstr(6);?>
Product detail:<input type="text" name="productd" value="<?=.$row['productd'].?>"><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:<br>
<input type="radio" name="fee" value="1"<? echo (($row["fee"]=="1")?" checked":""); ?>>USD150/3 months<br>







