|
 |
|
เรื่องการเขียนขู้มูลลง mysql คับ คือสมมุติว่าผมมีตัวแปร 2 ตัว มีตารางชื่อ hello มี 2 ฟิล player และ score ผมอยากให้มันเช็คว่าถ้าค่าในตัวแปร |
|
 |
|
|
 |
 |
|
มีรูปแบบการเขียนใน style ของคุณไหมครับถ้ามีขอดูหน่อยจะได้เขียนตอบคุณให้เข้าใจง่ายๆ
|
 |
 |
 |
 |
Date :
28 พ.ย. 2549 09:20:47 |
By :
jankasion |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
<?
$user = $_GET["player"]; //รับค่าชื่อผู้เล่นมาจาก flash
$point = $_GET["Apoint"]; //รับค่าคะแนนที่ได้จาก flash
$host = "localhost";
$user = "root";
$password = "1234";
$dbname = "game";
$tblname = "bomber";
mysql_connect($host, $user, $password) or die("ติดต่อฐานข้อมูลไม่ได้");
mysql_select_db($dbname) or die("เลือกฐานข้อมูลไม่ได้");
$sql = "select * from $tblname where player='$user' ";
$dbquery = mysql_db_query($dbname, $sql);
$num_rows = mysql_num_rows($dbquery);
if($num_rows==1){
$strSQL = "INSERT INTO bomber (' ',' score')";
$strSQL = $strSQL. "Values (' ','$point')";
header("location:home.php");
}else {
$strSQL = "INSERT INTO bomber ('player',' score')";
$strSQL = $strSQL. "Values ('$user','$point')";
header("location:home.php");
}
?>
ผมลองรันดูแล้วมันไม่เขียนลง ตารางครับ
|
 |
 |
 |
 |
Date :
28 พ.ย. 2549 10:19:11 |
By :
Mozart |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
$user ในโค้ดข้างบนนี้ จะได้ค่าเป็น root ไม่ใช่ ค่าที่รับได้จาก form หรือ อะไรที่พ่วงท้าย url มานะครับ
$sql = "select * from $tblname where player='$user' ";
$dbquery = mysql_db_query($dbname, $sql);
$num_rows = mysql_num_rows($dbquery);
3 บรรทัดข้างบนไม่น่าจะมีปัญหา
แต่ เมื่อ $num_rows ,มีค่าเป็น 1 หรืออื่น ๆ ที่ไม่ใช่ 0 นั่นแสดงว่า มีข้อมูลอยู่ในตารางนี้อยู่แล้ว
ตามที่คุณต้องการข้างขน ก็อย่าให้มัน add ข้อมูลลงใน DB
นั่นคือ ผิดที่ if - else ของคุณครับ แค่เงื่อนไขก็ผิดแล้ว คราวนี้
มาลองดูโค้ด ที่เขียนลง DB บ้าง
ถ้าจะให้ได้ตามโจทย์ ต้องทำเมื่อ $num_rows ไม่เท่า 0
เพียงเท่านี้แหล่ะครับ
[email protected]
|
 |
 |
 |
 |
Date :
28 พ.ย. 2549 10:59:18 |
By :
triplea |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 05
|